Create a combined bar/line chart with dimplejs and use self-defined colors

我们两清 提交于 2019-12-06 08:53:18

That data structure is not ideal for your data requirement within dimple. The way dimple would like that data is with your index names as dimension values:

var data = [
    { "Index" : "Index 1", "Country" : "UK", "Index Value": 0.2 },
    { "Index" : "Index 1", "Country" : "Spain", "Index Value": 0.7 },
    { "Index" : "Index 2", "Country" : "UK", "Index Value": 0.5 },
    { "Index" : "Index 2", "Country" : "Spain", "Index Value": 0.6 },
    { "Index" : "Index 3", "Country" : "UK", "Index Value": 0.4 },
    { "Index" : "Index 3", "Country" : "Spain", "Index Value": 0.3 },
    ...
];

Then in order to draw index 1 as a bar and the rest as lines you would need to split your data set into 2:

var barData = [],
    lineData = [],
    i,
    keyIndex = "Index 1";

for (i = 0; i < data.length; i += 1) {
    if (data[i]["Index"] === keyIndex) {
        barData.push(data[i]);
    } else {
        lineData.push(data[i]);
    }
}

You could then just define your chart as follows:

var chart = new dimple.chart(svg),
    bars,
    lines;

chart.addMeasureAxis("x", "Index Value");
chart.addCategoryAxis("y", "Country");

bars = chart.addSeries("Index", dimple.plot.bar);
bars.data = barData;

lines = chart.addSeries("Index", dimple.plot.line);
lines.data = lineData;

chart.draw();

I haven't tested this code but it ought to do what you want (minus the formatting etc).

If you want to continue on the road you have started in your code above (which is still possible) you will find composite axes very helpful to avoid hidden secondary axes and potential problems with differing max/min values. See the relevant section in the version 2 release notes for an example of these.

I don't understand why you don't want to tag the indices with something which appears in the tooltip, there must be some difference between them which you can communicate, but if you want to remove the index name from the tooltip you can define custom tooltips as shown here.

Edit: I should add if you just want to change the set of colours which dimple will arbitrarily ascribe to your data points you can override the default colours in the chart object as discussed here.

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