【问题标题】:Draw stacked chart using dygraph使用 dygraph 绘制堆积图
【发布时间】:2016-12-08 15:29:22
【问题描述】:

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

请建议我如何为 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

【问题讨论】:

    标签: javascript dygraphs


    【解决方案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]
    });
    

    【讨论】:

    • 嗨 Anil Samal - 谢谢,这看起来很棒;我们可以在一个栏中有两种颜色吗?就像在第一个栏中一样,您添加了黄色;但我实际上正在寻找的是一半是深黄色,一半是浅黄色,如果有什么方法可以做到这一点,这对我有很大帮助。谢谢
    • 是的,我们可以在一个栏中有两种颜色。让我发布代码。
    • 嗨,Paresh,请找到下面的代码,它会为同一个栏提供两种不同的颜色。
    • 嗨 Anil - 抱歉没有找到代码。你能再发一次吗
    • 你需要选择你想要使用的颜色
    【解决方案2】:

    这是为同一个 Bar 添加两种不同颜色的代码:

    <div id="graph"></div>
        function barChartPlotter(e) {
        var myColor = ["#ECD078", "#D95B43", "#C02942", "#542437", "#53777A", "#42d7f4", "red", "green"];
        var myColor1 = ["black", "red", "green", "#ECD078", "#D95B43", "#C02942", "#542437", "#53777A"];
        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);
        }
        for (var i = 0; i < points.length; i++) {
            var p = points[i];
            console.log(points[1]);
            var center_x = p.canvasx; // center of the bar
            ctx.fillStyle = myColor1[i];
            ctx.fillRect(center_x - bar_width / 2, p.canvasy, bar_width, (y_bottom - p.canvasy) / 2);
            ctx.strokeRect(center_x - bar_width / 2, p.canvasy, bar_width, (y_bottom - p.canvasy) / 2);
        }
    }
    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]
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 2021-12-27
      • 2021-01-01
      • 2021-06-18
      • 2016-05-24
      • 2017-03-22
      相关资源
      最近更新 更多