Chart.js 2.0 - 垂直线

时间:2016-03-31 09:38:15

标签: chart.js

任何人都可以告诉我如何扩展Chart.js v2.0。我需要在折线图中使用垂直线,我想实现与http://jsfiddle.net/dbyze2ga/类似的东西。

Chart.types.Line.extend({
name: "LineWithLine",
draw: function () {
    Chart.types.Line.prototype.draw.apply(this, arguments);

    var point = this.datasets[0].points[this.options.lineAtIndex]
    var scale = this.scale

    // draw line
    this.chart.ctx.beginPath();
    this.chart.ctx.moveTo(point.x, scale.startPoint + 24);
    this.chart.ctx.strokeStyle = '#ff0000';
    this.chart.ctx.lineTo(point.x, scale.endPoint);
    this.chart.ctx.stroke();

    // write TODAY
    this.chart.ctx.textAlign = 'center';
    this.chart.ctx.fillText("TODAY", point.x, scale.startPoint + 12);
}
});

new Chart(ctx).LineWithLine(data, {
                            datasetFill : false,
                            lineAtIndex: 2
 });

3 个答案:

答案 0 :(得分:23)

更新:有关使用图表注释插件的更简单,更强大的解决方案,请参阅https://stackoverflow.com/a/45092928/360067

您可以扩展line类型以添加​​对绘制线条的支持

预览

enter image description here

<强>脚本

var originalLineDraw = Chart.controllers.line.prototype.draw;
Chart.helpers.extend(Chart.controllers.line.prototype, {
  draw: function() {
    originalLineDraw.apply(this, arguments);

    var chart = this.chart;
    var ctx = chart.chart.ctx;

    var index = chart.config.data.lineAtIndex;
    if (index) {
      var xaxis = chart.scales['x-axis-0'];
      var yaxis = chart.scales['y-axis-0'];

      ctx.save();
      ctx.beginPath();
      ctx.moveTo(xaxis.getPixelForValue(undefined, index), yaxis.top);
      ctx.strokeStyle = '#ff0000';
      ctx.lineTo(xaxis.getPixelForValue(undefined, index), yaxis.bottom);
      ctx.stroke();
      ctx.restore();
    }
  }
});

然后

var config = {
  type: 'line',
  data: {
    labels: ...
    datasets: [
        ...
    ],
    lineAtIndex: 2
  }
};

小提琴 - http://jsfiddle.net/mn8x6fso/

答案 1 :(得分:9)

对于v2.0,最好的方法是使用Chart Annotations插件(https://github.com/chartjs/chartjs-plugin-annotation

小提琴 - https://codepen.io/anon/pen/ZywgKr

<强>脚本

var ctx = document.getElementById("canvas").getContext("2d");
new Chart(ctx, {
    type: "line",
    data: {
      labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
      datasets: [
        {
          data: [12, 3, 2, 1, 8, 8, 2, 2, 3, 5, 7, 1]
        }
      ]
    },
    options: {
      annotation: {
        annotations: [
          {
            type: "line",
            mode: "vertical",
            scaleID: "x-axis-0",
            value: "MAR",
            borderColor: "red",
            label: {
              content: "TODAY",
              enabled: true,
              position: "top"
            }
          }
        ]
      }
    }
  }
);

https://github.com/chartjs/Chart.js/issues/4495#issuecomment-315238365

发布

答案 2 :(得分:1)

对于寻找水平线的人来说,这是我到目前为止所得到的:

  ctx.save();
  ctx.beginPath();
  ctx.moveTo(xaxis.left, limits[i].value);
  ctx.strokeStyle = limits[i].color;
  ctx.lineTo(xaxis.right, limits[i].value);
  ctx.stroke();
  ctx.restore();

jsFiddle