2014-11-13 82 views
0

我正在与dimple js合作,我有一个折线图代码,但这里是我设置事件的问题,只要点击一个按钮,图表再次绘制,但chart.draw()没有工作。问题出在哪里帮助将不胜感激 HTML:更新折线图

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
</head> 
<body> 
<button id="test">Click Me</button> 
<div id="chartContainer" class="chart_style"></div> 
</body> 
<script src="js/d3.js" charset="utf-8"></script> 
<script src="js/dimple.js"></script> 
<script type="text/javascript"> 
    var svg = dimple.newSvg("#chartContainer", 600, 400), 
      data = [ 
       { "Value" : 10, "Year" : 2009 }, 
       { "Value" : 5, "Year" : 2010 }, 
       { "Value" : 4, "Year" : 2011 }, 
       { "Value" : 7, "Year" : 2012 }, 
       { "Value" : 10, "Year" : 2010 }, 
       { "Value" : 50, "Year" : 2020 }, 
       { "Value" : 40, "Year" : 2015 }, 
       { "Value" : 100, "Year" : 2014 }, 
       { "Value" : 200, "Year" : 2014 } 
      ]; 
    var chart = new dimple.chart(svg, data); 
    var x = chart.addCategoryAxis("x", "Year"); 
    x.addOrderRule("Year"); 
    var y = chart.addMeasureAxis("y", "Value"); 
    chart.addColorAxis("Value", ["green", "yellow", "red"]); 
    var lines = chart.addSeries(null, dimple.plot.line); 
    lines.lineWeight = 4; 
    lines.lineMarkers = true; 
    chart.ease = "bounce"; 
    chart.staggerDraw = true; 
    chart.draw(2000); 
    d3.select("#test").on("click", function() { 
    ///here is the problem 
     data = [ 
      { "Value" : 10, "Year" : 2009 }, 
      { "Value" : 5, "Year" : 2010 }, 
      { "Value" : 4, "Year" : 2011 }, 
      { "Value" : 50, "Year" : 2020 }, 
      { "Value" : 40, "Year" : 2015 }, 
      { "Value" : 120, "Year" : 2014 }, 
      { "Value" : 150, "Year" : 2011 }, 
      { "Value" : 500, "Year" : 2018 }, 
      { "Value" : 250, "Year" : 2015 }, 
      { "Value" : 200, "Year" : 2014 } 
     ]; 
     chart.draw(1000); 
    }); 
</script> 
</html> 

回答

1

您需要使用更新chart.data attribute更改与图表相关的数据:

chart.data = [ ... ]; 

在代码中,你重新分配data变量,没有链接到酒窝图。完整演示here