javascript - 如何在 dimple.js 中用不同的颜色为不同的数据集着色

标签 javascript d3.js charts dimple.js

我正在使用另一个帖子的数据和 dimple.js 脚本:

Multi-series in dimplejs

如果您在该帖子中使用未经破解的原始数据,您如何对利润和收入进行不同的着色,即如何为 y1 和 y3 使用两种不同的颜色?从纯粹的哲学 Angular 来看,dimple 似乎不支持这一点。

最佳答案

由于引入了复合轴,较新版本的 dimple 不需要像答案中那样重构数据。现在可以这样实现:

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

如您所见,我还根据您的问题为系列分配了一些颜色。第一个参数是系列颜色标识符(在本例中是应用于每个系列的标签),第二个参数是填充,第三个是描边,第四个是不透明度。

这里唯一的问题是用于线条的标签(以及因此为系列着色的键)本身不能是数据中的维度名称(否则 dimple 将尝试按其值分解)。如有必要,尾随空格用于区分它们。

编辑:我放错了链接

关于javascript - 如何在 dimple.js 中用不同的颜色为不同的数据集着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30154513/

相关文章:

javascript - 是否元刷新重定向 "Secure"

text - 在 SSRS 图表上放置附加文本

javascript - ForceX 使用 DOM 元素作为坐标引用

javascript - D3JS 的小 map

iphone - 请求成员 'constantCoordinateValue' 不是核心情节中的结构或联合错误

javascript - 显示 am4charts.XYChart 中的所有工具提示

javascript - 如何控制单选按钮组?

javascript - Node 需要路径引用问题

javascript - 连接到 websocket 使 webworker 在 Microsoft edge 中没有响应

javascript - 如何添加点击事件以在 NVD3 饼图中显示工具提示?