2

I am using another post's data and dimple.js script:

Multi-series in dimplejs

If you use the original unhacked data in that post, how do you color profit and revenue differently, that is, how can you use two different colors for y1 and y3? It seems like dimple does not support this, by sheer philosophy.

Community
  • 1
  • 1
Ferenc
  • 884
  • 9
  • 19
  • When I run the code from the accepted answer to the linked question (see JsFiddle: http://jsfiddle.net/mycL02ve/) it looks like profit and revenue are already different colors. How do you want the chart to be different? – seaotternerd May 10 '15 at 18:02
  • Please note that the question is about the original data with its original data structure and not about the data structure in the answer you put in jsfiddle. I don't want to manually restructure the data b/c it is not only four records usually and if possible I'd not restructure it other ways. – Ferenc May 10 '15 at 18:10

2 Answers2

5

Restructuring the data as in that answer is not required with newer versions of dimple because of the introduction of composite axes. It could now be achieved like this:

var svg = dimple.newSvg("body", 800, 400);

var data = [
  {"Month":"01/2013", "Revenue":2000, "Profit":2000, "Units":4},
  {"Month":"02/2013", "Revenue":3201, "Profit":2000, "Units":3},
  {"Month":"03/2013", "Revenue":1940, "Profit":14000, "Units":5},
  {"Month":"04/2013", "Revenue":2500, "Profit":3200, "Units":1},
  {"Month":"05/2013", "Revenue":800, "Profit":1200, "Units":4}
]

var chart = new dimple.chart(svg, data);

chart.setBounds(60,20,680,330);

var x = chart.addCategoryAxis("x", "Month");
var y1 = chart.addMeasureAxis("y", "Revenue");
var y2 = chart.addMeasureAxis("y", "Units");
var y3 = chart.addMeasureAxis(y1, "Profit");
chart.addSeries("Sales Units", dimple.plot.bar, [x,y2]);
chart.addSeries("Sales Revenue", dimple.plot.line, [x,y1]);
chart.addSeries("Gross Profit", dimple.plot.bubble, [x,y3]);

chart.assignColor("Sales Units", "white", "red", 0.5);
chart.assignColor("Sales Revenue", "#FF00FF");
chart.assignColor("Gross Profit", "green");

x.dateParseFormat = "%m/%Y";
x.addOrderRule("Date");

chart.draw();

http://jsbin.com/mafegu/2/edit?js,output

As you can see I've also assigned some colours to the series as per your question. The first parameter is the series colour identifier (in this case the label applied to each series), the second is fill, third is stroke and fourth is opacity.

The only gotcha here is that the labels used for the lines (and therefore the keys to colour the series) can't themselves be dimension names in the data (otherwise dimple will try to disaggregate by their values). A trailing space works to differentiate them if necessary.

EDIT: I put the wrong link in

John Kiernander
  • 4,904
  • 1
  • 15
  • 29
  • It does the trick. Thanks. Now I am having trouble with the automatic aggregation of the data for the same month, or when dimple disaggregates, as you mention it, it also gives different colors to the bubbles across a data series, see my [follow-up question](http://stackoverflow.com/questions/30173692/how-to-use-series-stack-false-in-dimple-js-to-suppress-aggregation-different). – Ferenc May 11 '15 at 17:25
2

This behavior can be achieved through a slight abuse of dimple's colorAxis feature. A color axis is supposed to be a third axis that you can add to a series to have its color change within a given range based on some value. However, you can force a colorAxis to be a constant color by assigning the start and end point of its ranges to be the same color. If you create a single-color colorAxis in this way for each series, you can assign each a different color:

enter image description here

Important note: This works just fine for bubble chart series. However, for reasons that remain unclear to me, if you attempt to assign different colorAxis objects to different line chart series, they will all be given the color of the last one assigned.

Here's the code:

var svg = dimple.newSvg("body", 800, 400);

var data = [
    {"Month":"01/2013", "Revenue":2000, "Profit":2000, "Units":4},
    {"Month":"02/2013", "Revenue":3201, "Profit":2000, "Units":3},
    {"Month":"03/2013", "Revenue":1940, "Profit":14000, "Units":5},
    {"Month":"04/2013", "Revenue":2500, "Profit":3200, "Units":1},
    {"Month":"05/2013", "Revenue":800, "Profit":1200, "Units":4}
]

var chart = new dimple.chart(svg, data);

chart.setBounds(60,20,680,330);

var x = chart.addCategoryAxis("x", "Month");

var prof = chart.addColorAxis("Profit", ["green", "green"])
var rev = chart.addColorAxis("Profit", ["black", "black"])
var y2 = chart.addMeasureAxis("y", "Units");
chart.addSeries("null", dimple.plot.bar, [x,y2]);
var y1 = chart.addMeasureAxis("y", "Revenue");
chart.addSeries("null", dimple.plot.bubble, [x,y1, prof]);
var y3 = chart.addMeasureAxis("y", "Profit");
chart.addSeries("null", dimple.plot.bubble, [x,y3, rev]);

x.dateParseFormat = "%m/%Y";
x.addOrderRule("Date");

chart.draw();

(jsFiddle here)

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
  • Typically I have about a hundred records (hundred months, or dates in general) and one to five different categories I want to put in one plot (using the above example, these could be Revenue, Profit, Earnings, Bottom line, etc.). I want each category to have a different color. The plot is actually a bubble chart instead of a line chart and I'd even try to use different symbols for each category if it is possible in dimple. js. I am also digging deep in the structure of dimple to find out how to change the color on a category and haven't found the solution yet but I am pretty new dimple or d3. – Ferenc May 10 '15 at 19:36
  • I have no idea why this is the case, but the technique I described actually works just fine for bubble charts. You can create as many new colorAxis objects as you want. Not sure why line charts break it. I don't think you can change the symbols for bubble plots. – seaotternerd May 11 '15 at 05:44
  • Introducing a color axis looks like a really shrewd trick and really works. Thanks. Interestingly, this coloring does not seem to give much overhead to the rendering unlike data crunching on the front end. – Ferenc May 11 '15 at 17:15