使用dygraph绘制堆积图表

时间:2016-12-07 06:29:17

标签: javascript dygraphs

我想使用dygraphs JavaScript库绘制堆积图表,我在下面做了。但是我希望每个条形线都有不同的颜色。两种颜色,如深色和底部的条形顶部,颜色相同,如下所示:

enter image description here

请建议我如何为dygraph传递不同的颜色。
这是我到目前为止所做的:

function barChartPlotter(e) {
  var ctx = e.drawingContext;
  var points = e.points;
  var y_bottom = e.dygraph.toDomYCoord(0); // see <a href="http://dygraphs.com/jsdoc/symbols/Dygraph.html#toDomYCoord">jsdoc</a>

  // This should really be based on the minimum gap
  var bar_width = 2 / 3 * (points[1].canvasx - points[0].canvasx);
  ctx.fillStyle = e.color; // a lighter shade might be more aesthetically pleasing

  // Do the actual plotting.
  for (var i = 0; i < points.length; i++) {
    var p = points[i];
    var center_x = p.canvasx; // center of the bar

    ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
    ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
  }
}

g = new Dygraph(document.getElementById("graph"),
  "X,Y\n" +
  "1,2\n" +
  "2,4\n" +
  "3,6\n" +
  "4,8\n" +
  "5,10\n" +
  "6,12\n" +
  "7,14\n" +
  "8,16\n", {
    // options go here. See http://dygraphs.com/options.html
    legend: 'always',
    animatedZooms: true,
    plotter: barChartPlotter,
    dateWindow: [0, 9]
  });
/*
Relevant CSS classes include:

Labels on the X & Y axes:
.dygraph-axis-label
.dygraph-axis-label-x
.dygraph-axis-label-y
.dygraph-axis-label-y2

Chart labels, see http://dygraphs.com/tests/styled-chart-labels.html
.dygraph-title
.dygraph-xlabel
.dygraph-ylabel
.dygraph-y2label

The legend, see http://dygraphs.com/tests/series-highlight.html
.dygraph-legend
*/

.dygraph-title {
  color: gray;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/mootools/1.4.5/mootools-core-full-compat.min.js"></script>
<script src="//dygraphs.com/dygraph-dev.js"></script>
<!-- You may set style: "width: ...; height: ..." to size the chart -->
<div id="graph"></div>

View on JSFiddle

2 个答案:

答案 0 :(得分:1)

以下是添加不同颜色的代码:

<div id="graph"></div>

function barChartPlotter(e) {
var myColor = ["#ECD078", "#D95B43", "#C02942", "#542437", "#53777A", "#42d7f4", "red", "green"];
var ctx = e.drawingContext;
var points = e.points;
var y_bottom = e.dygraph.toDomYCoord(0); 

// This should really be based on the minimum gap
var bar_width = 2 / 3 * (points[1].canvasx - points[0].canvasx);
// a lighter shade might be more aesthetically pleasing

// Do the actual plotting.
for (var i = 0; i < points.length; i++) {
    var p = points[i];
    var center_x = p.canvasx; // center of the bar
    ctx.fillStyle = myColor[i];
    ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
    ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, y_bottom - p.canvasy);
}}
g = new Dygraph(document.getElementById("graph"),
"X,Y\n" +
"1,2\n" +
"2,4\n" +
"3,6\n" +
"4,8\n" +
"5,10\n" +
"6,12\n" +
"7,14\n" +
"8,16\n", {
    // options go here. See http://dygraphs.com/options.html
    legend: 'always',
    animatedZooms: true,
    plotter: barChartPlotter,
    dateWindow: [0, 9]
});

答案 1 :(得分:1)

以下是为同一个栏添加两种不同颜色的代码:

for loop
相关问题