Dimple.js multi series bar not stacked

浪子不回头ぞ 提交于 2019-12-06 01:00:42

That data format is not ideal for dimple. The current chart is not stacked, the bars are actually overlapping each other. To work simply in Dimple you need your data in the following format:

data = [
    { "ID" : "1", "Measure" : "Value 1", "Value" : 100000 },
    { "ID" : "2", "Measure" : "Value 1", "Value" : 90000 },
    { "ID" : "3", "Measure" : "Value 1", "Value" : 140000 }
    { "ID" : "1", "Measure" : "Value 2", "Value" : 110000 },
    { "ID" : "2", "Measure" : "Value 2", "Value" : 145000 },
    { "ID" : "3", "Measure" : "Value 2", "Value" : 60000 }
];

Then to do a properly stacked bar you can do the following:

chart = new dimple.chart(svg, data);
chart.addCategoryAxis("x", "ID");
chart.addMeasureAxis("y", "Value");
chart.addSeries("Measure", dimple.plot.bar);
chart.draw();

To do this as a grouped bar you could do it like this:

chart = new dimple.chart(svg, data);
chart.addCategoryAxis("x", ["Measure", "ID"]);
chart.addMeasureAxis("y", "Value");
chart.addSeries("Measure", dimple.plot.bar);
chart.draw();

In your case you are actually drawing the measures on different y axes. I'm afraid that doing grouped bars on separate axes is tricky, however there is a slightly hacky way to do it shown here: http://jsbin.com/jawig/1/edit?js,output

The simplest way is to just use another function to "explode" the data returned from the server, assuming this is performant for you. Since the chart data are just raw Javascript objects, this is rather easy and painless.

For instance:

  var explodeData = function (oldData) {
    var newData = [];
    foreach(row in oldData) {
      newData.push({ID: row["ID"], MeasureType: "Value 1", Value: row["Value 1"]});
      newData.push({ID: row["ID"], MeasureType: "Value 2", Value: row["Value 2"]});
    }
    return newData;
  };

  d3.csv("/data", function (rawData) {
    var data = explodeData(rawData);
    var myChart = new dimple.chart(svg, data);
    ...
  });

Hope that helps! That's what I did, anyway

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!