问题
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>
回答1:
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
来源:https://stackoverflow.com/questions/26145982/change-chart-type-in-dimple-js-to-automate-chart-production