Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

change chart type in dimple.js to automate chart production

I would like to be able to change the chart type of charts using dimple.js by using a variable. I want for instance to switch from bars to lines. I've tried with no success, it seems simple however! Any idea?

Below is my code :

<!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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are passing a string to myChart.addSeries instead of a dimple.plot object. To make it dynamic you would need to reference the static object you're looking for on the dimple.plot object :

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...