简体   繁体   English

每次从下拉列表中选择项目时,如何使图表动态化?

[英]How can I make my charts dynamic every time I choose an item from my dropdown?

after I click on one of the items in my dropdown you can see that it loads dynamically.单击下拉列表中的一项后,您可以看到它是动态加载的。 However, after it has first loaded it does not load anymore.但是,在它首次加载后,它不再加载。 My goal is to make it completely dynamic here.我的目标是让它在这里完全动态化。 Do you have an idea how I can make it happen?你知道我怎样才能让它发生吗?

const ctx = document.getElementById('jobChart').getContext('2d')
const chart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9+"],
    datasets: []
  },
});

const jobDatasets = {
  backend: [{
    label: "10th Percentile",
    borderColor: "#c4c1ff",
    backgroundColor: "#c4c1ff",
    data: [0, 10, 5, 2, 20, 30, 45, 33, 67]
  }, {
    label: "25th Percentile",
    borderColor: "#645bff",
    backgroundColor: "#645bff",
    data: [5, 12, 14, 15, 19, 31, 55, 61, 62]
  }],
  frontend: [{
    label: "10th Percentile",
    borderColor: "#c4c1ff",
    backgroundColor: "#c4c1ff",
    data: [35, 11, 49, 45, 55, 47, 5, 62, 1]
  }, {
    label: "25th Percentile",
    borderColor: "#645bff",
    backgroundColor: "#645bff",
    data: [10, 31, 48, 49, 59, 65, 67, 76, 12]
  }],
  fullstack: [{
    label: "10th Percentile",
    borderColor: "#c4c1ff",
    backgroundColor: "#c4c1ff",
    data: [11, 37, 10, 27, 62, 52, 8, 19, 24]
  }, {
    label: "25th Percentile",
    borderColor: "#645bff",
    backgroundColor: "#645bff",
    data: [8, 25, 28, 30, 38, 45, 58, 62, 74]
  }]
}

document.getElementById('job-role').addEventListener('change', function() {
  chart.data.datasets = jobDatasets[this.value]
  chart.update()
});

Your code looks fine.你的代码看起来不错。 I just added an empty option that reflects the initial state of the chart where no data is displayed yet.我刚刚添加了一个空选项,它反映了尚未显示数据的图表的初始 state。 Accordingly I changed the event listener on the select element as follows:因此,我更改了 select 元素上的事件侦听器,如下所示:

document.getElementById('job-role').addEventListener('change', function() {
  chart.data.datasets = this.value == 'none' ? [] : jobDatasets[this.value];
  chart.update();
});

Please take a look at your amended code and see how it works.请查看您修改后的代码,看看它是如何工作的。

 const chart = new Chart('jobChart', { type: 'line', data: { labels: ["1", "2", "3", "4", "5", "6", "7", "8", "9+"], datasets: [] }, }); const jobDatasets = { backend: [{ label: "10th Percentile", borderColor: "#c4c1ff", backgroundColor: "#c4c1ff", data: [0, 10, 5, 2, 20, 30, 45, 33, 67] }, { label: "25th Percentile", borderColor: "#645bff", backgroundColor: "#645bff", data: [5, 12, 14, 15, 19, 31, 55, 61, 62] }], frontend: [{ label: "10th Percentile", borderColor: "#c4c1ff", backgroundColor: "#c4c1ff", data: [35, 11, 49, 45, 55, 47, 5, 62, 1] }, { label: "25th Percentile", borderColor: "#645bff", backgroundColor: "#645bff", data: [10, 31, 48, 49, 59, 65, 67, 76, 12] }], fullstack: [{ label: "10th Percentile", borderColor: "#c4c1ff", backgroundColor: "#c4c1ff", data: [11, 37, 10, 27, 62, 52, 8, 19, 24] }, { label: "25th Percentile", borderColor: "#645bff", backgroundColor: "#645bff", data: [8, 25, 28, 30, 38, 45, 58, 62, 74] }] }; document.getElementById('job-role').addEventListener('change', function() { chart.data.datasets = this.value == 'none'? []: jobDatasets[this.value]; chart.update(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script> <b>Chart Type</b> <select id="job-role"> <option value="none"></option> <option value="backend">Backend Engineer</option> <option value="frontend">Frontend Engineer</option> <option value="fullstack">Fullstack Engineer</option> </select> <div style="position: relative; width:85vw"> <canvas id="jobChart" height="110"></canvas> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保每次从下拉列表中选择图表时都会加载图表? - How can I ensure that my charts diagram load every time I choose it from my dropdown? 我可以做些什么来确保每次从下拉列表中选择图表时重新加载图表? - What can I do to ensure that my charts diagram reload every time I choose it from my dropdown? 每次我的 JavaScript 文件从其中提取代码时,如何使变量“刷新”自身? - How can I make a variable “refresh” itself every time my JavaScript file has code pulled from it? 如何使阵列每次运行都能选择不同的选择? - How can I make my array choose different choices each time it runs? 如何汇总从数据库中检索到的下拉列表中选择的商品的价格 - How can I sum the price of the item that I selected from my dropdown that retrieved in the database 如何添加下拉菜单以将项目添加到我的ckeditor - How can I add a dropdown to add item to my ckeditor 如何使用 item.datasetIndex.== 1 &amp;&amp; item;datasetIndex?== 4. 的替代方法从图例中删除两个标签? (图表.js) - How can I remove two labels from my legend with an alternative to item.datasetIndex !== 1 && item.datasetIndex !== 4; ? (Charts.js) 每次用户被定向到视图/页面时,如何让我的 Javascript 不弹出 - How can I make my Javascript not pop up every time a user gets directed to the a View/Page "如何显示我的 localStorage 项目中的每个元素?" - How do I display every element from my localStorage item? 如何让我的下拉菜单通过动画展开? - How can I make my dropdown expanding with an animation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM