简体   繁体   English

ChartJS创建图表Ajax

[英]ChartJS create chart Ajax

I have followed the example on the chartJS website and have placed the following code in window.onload: 我在chartJS网站上遵循了示例,并将以下代码放置在window.onload中:

ctx = document.getElementById("graphcanvas").getContext("2d");      
window.myBar = new Chart(ctx).Bar(barChartData, {
    responsive : true
});

... which works perfectly fine as when i click my button to display the popup window, the bar chart with dummy data is shown. ...效果非常好,因为当我单击我的按钮以显示弹出窗口时,将显示带有伪数据的条形图。

In my function i have taken a copy of the dummy data and modified it slightly so a difference can be seen. 在我的函数中,我已复制了虚拟数据并对其进行了少许修改,以便可以看到差异。

In my function i have the following code: 在我的函数中,我有以下代码:

ctx = document.getElementById("graphcanvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartDataDiff, {
    responsive : true
});

... but for some reason when this code is run the popup window which previously contained my bar chart is now empty, the bar chart disappears. ...但是由于某种原因,运行此代码时,以前包含我的条形图的弹出窗口现在为空,条形图消失了。

I have tried adding .destroy() before recreating the chart and using 'myBar' instead of 'window.myBar' but nothing seems to work. 我尝试在重新创建图表之前添加.destroy()并使用“ myBar”而不是“ window.myBar”,但是似乎没有任何效果。

Why don't you create the graph with dummy data (hidden on load) and then use chart js's update method to insert that data into the 'barChartData' ? 为什么不使用虚拟数据(隐藏在负载中)创建图形,然后使用chart js的update方法将数据插入到“ barChartData”中?

  1. create graph, hide it 创建图,将其隐藏
  2. chart.update() with the new data 具有新数据的chart.update()
  3. show the graph 显示图表

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM