搜索/过滤器下拉列表:更改变量的值

时间:2018-09-26 10:55:23

标签: javascript html

我有一个创建d3图形的javascript项目:

document.addEventListener("DOMContentLoaded", function(event) {
  allNameMuseums().forEach(function(item) {
    document.getElementById("myDropdown").innerHTML += '<a href="' + item + '">' + item + '</a>';
  })
});


function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  div = document.getElementById("myDropdown");
  a = div.getElementsByTagName("a");
  for (i = 0; i < a.length; i++) {
    if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
    } else {
      a[i].style.display = "none";
    }
  }
}

function allNameMuseums() {
  return ["ACQUARIO", "Museo2", "Museo3"];
}


async function allMuseums() {
  let nomeFile = "dati_musei.csv";
  let data = await d3.dsv(";", nomeFile, function(d) {
    return {
      Museo: d.Museo,
      Ingresso: d.Ingresso,
      Anno: d.Anno,
      Mese: d.Mese,
      Visitatori: d.Visitatori
    };
  });
  return filtraggio(data);
};



async function specificoMuseo(nomeMuseo) {
  var nomeFile = "dati_musei2.csv";

  let data2 = await d3.dsv(";", nomeFile, function(d) {
    return {
      Museo: d.Museo,
      Ingresso: d.Ingresso,
      Anno: d.Anno,
      Mese: d.Mese,
      Visitatori: d.Visitatori
    };
  });
  return filtraggioMuseo(data2, nomeMuseo);
};



specificoMuseo("ACQUARIO").then(proof2 => {
  allMuseums().then(proof => {

    var svg = d3.select("svg"),
      margin = {
        top: 20,
        right: 20,
        bottom: 110,
        left: 40
      },
      margin2 = {
        top: 430,
        right: 20,
        bottom: 30,
        left: 40
      },
      width = +svg.attr("width") - margin.left - margin.right - 150, // sommo 150 perchè la width prima era 1200, mentre ora è stata settata a 1350, in modo da far vedere l'etichetta dei valori correnti, quindi sommo 150 per tenere invece la width (inteso come valore) uguale a prima
      height = +svg.attr("height") - margin.top - margin.bottom, // l'altezza del piano grafico
      height2 = +svg.attr("height") - margin2.top - margin2.bottom;

    var parseDate = d3.timeParse("%b %Y")
    bisectDate = d3.bisector(function(d) {
      return d.date;
    }).left;;



    var data = proof;


    var datax = proof2;


    data.forEach(function(d) {
      d.date = parseDate(d.date);
      d.price = d.price;
    });

    datax.forEach(function(d) {
      d.date = parseDate(d.date);
      d.price = d.price;
    });



    x.domain(d3.extent(data, function(d) {
      return d.date;
    }));
    y.domain([0, Math.max(d3.max(data, function(d) {
      return d.price;
    }), d3.max(datax, function(d) {
      return d.price;
    }))]);

    x2.domain(x.domain());
    y2.domain(y.domain());

    g.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area);

    g.append("path")
      .datum(datax)
      .attr("class", "areax")
      .attr("d", area);

    g.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

    g.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis);


    var focus = g.append("g")
      .attr("class", "focus")
      .style("display", "none");

    focus.append("line")
      .attr("class", "x-hover-line hover-line")
      .attr("y1", 0) // punto di inizio della linea
      .attr("y2", height); // punto di fine della linea, dicendomi quanto deve essere lunga

    focus.append("line")
      .attr("class", "y-hover-line hover-line")
      .attr("x1", width)
      .attr("x2", width);

    focus.append("circle")
      .attr("r", 0);

    focus.append("text")
      .attr("x", 15) // mi dice la distanza dall'asse verticale
      .attr("dy", ".31em");

    context.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area2);

    context.append("path")
      .datum(datax)
      .attr("class", "areax")
      .attr("d", area2);

    context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

    context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

    svg.append("rect")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .attr("class", "overlay")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .call(zoom)
      // .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .on("mouseover", function() {
        focus.style("display", null);
      })
      .on("mouseout", function() {
        focus.style("display", "none");
      })
      .on("mousemove", mousemove);


  });
});

但是我刚刚添加了搜索/过滤器下拉列表:

<h2>Search/Filter Dropdown</h2>
<p>Click on the button to open the dropdown menu, and use the input field to search for a specific dropdown link.</p>

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
  </div>
</div>

此搜索/过滤器下拉列表将allNameMuseums()方法的数组作为值,即[“ ACQUARIUM”,“ Museo2”,“ Museo3”]。 变量“数据” var data = proof将allMuseums()的结果作为输入。 我希望当我单击ACQUARIO变量'data'时,将specificMuseo(“ ACQUARIO”)的结果作为输入。

0 个答案:

没有答案