更改dimple.js中的图表类型以自动生成图表

时间:2014-10-01 16:23:01

标签: dimple.js

我希望能够使用变量使用dimple.js更改图表的图表类型。我想要例如从条形切换到线条。我试过没有成功,但看起来很简单!有什么想法吗?

以下是我的代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test</title>
        <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
        <script type="text/javascript" src="http://dimplejs.org/dist/dimple.v2.1.0.min.js"></script>
    </head>
<body>
<div id="chartContainer">
  <script type="text/javascript">
  var chartType = "line";
  var chartDimple = "dimple.plot." + chartType;
    var svg = dimple.newSvg("#chartContainer", 590, 400);
    d3.csv("data/test.csv", function (data) { //d3.tsv("data/example_data.tsv", function (data) {
      var myChart = new dimple.chart(svg, data);
      myChart.setBounds(60, 30, 510, 305)
      var x = myChart.addCategoryAxis("x", "Month");
      x.addOrderRule("Date");
      myChart.addMeasureAxis("y", "Unit Sales");
      // myChart.addSeries(null, dimple.plot.bar);
      myChart.addSeries(null, chartDimple);
      myChart.draw();
    });
  </script>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您正在将字符串传递给myChart.addSeries而不是dimple.plot对象。要使其动态化,您需要在dimple.plot对象上引用您正在寻找的静态对象:

var chartType = "line";
var chartDimple = dimple.plot[chartType];
myChart.addSeries(null, chartDimple);

https://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.plot#static-objects