【问题标题】:Draw horizontal lines in Chart.js 2.0在 Chart.js 2.0 中绘制水平线
【发布时间】:2017-11-29 03:28:17
【问题描述】:

你能帮我如何扩展 Chart.js v2.0。我需要在图表中绘制一些水平线,类似于:http://jsfiddle.net/vsh6tcfd/3/

var originalLineDraw = Chart.controllers.bar.prototype.draw;

Chart.helpers.extend(Chart.controllers.bar.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.left);
      ctx.strokeStyle = '#ff0000';
      ctx.lineTo(xaxis.getPixelForValue(undefined, index), yaxis.right);
      ctx.stroke();
      ctx.restore();
    }
  }
});

var config = {
    type: type,
    data:  jQuery.extend(true, {}, data),
    options: this.chartdata.options,
    lineAtIndex: 2
};

new Chart(ctx, config);  

【问题讨论】:

  • 在提供的小提琴中,您实际上确实画了水平线,不是吗?你到底想改变什么?
  • 提供的小提琴使用 Chart.js v1,但我使用的是 v2。该代码不适用于 v2。
  • 对不起,没注意到...

标签: chart.js chart.js2


【解决方案1】:

选项

使用 chart.js,您有 2 个选项。

  1. 您可以创建混合图表类型 (Example Here)。这将允许您添加折线图来创建您的线条。
  2. 您可以创建一个插件(参见下面的示例)。

我推荐选项 2,因为它可以让您更好地控制线条的外观。

修复

demo of the plugin

Chart.js 现在supports plugins。这允许您向图表添加任何您想要的功能!

要创建插件,您需要在事件发生后运行代码并根据需要修改图表/画布。 下面的代码应该给你一个很好的起点:

var horizonalLinePlugin = {
  afterDraw: function(chartInstance) {
    var yValue;
    var yScale = chartInstance.scales["y-axis-0"];
    var canvas = chartInstance.chart;
    var ctx = canvas.ctx;
    var index;
    var line;
    var style;

    if (chartInstance.options.horizontalLine) {
      for (index = 0; index < chartInstance.options.horizontalLine.length; index++) {
        line = chartInstance.options.horizontalLine[index];

        if (!line.style) {
          style = "rgba(169,169,169, .6)";
        } else {
          style = line.style;
        }

        if (line.y) {
          yValue = yScale.getPixelForValue(line.y);
        } else {
          yValue = 0;
        }

        ctx.lineWidth = 3;

        if (yValue) {
          ctx.beginPath();
          ctx.moveTo(0, yValue);
          ctx.lineTo(canvas.width, yValue);
          ctx.strokeStyle = style;
          ctx.stroke();
        }

        if (line.text) {
          ctx.fillStyle = style;
          ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
        }
      }
      return;
    }
  }
};
Chart.pluginService.register(horizonalLinePlugin);

【讨论】:

  • 如果您删除 ctx.font = "20px Georgia";,看起来 chart.js 将使用与图表相同的字体和字体大小。我认为整体看起来更好,建议删除它。
  • 感谢代码! 在 ECMA6 中添加 var yValue; 后工作得很好 ;-) 让我在控制台中出现 yValue is undefined 错误时感到慌张。
  • 好消息@StanSmulders。我用更正更新了我的答案。
  • 您可能需要考虑将代码更改为 ctx.moveTo(chartInstance.chartArea.left, yValue);ctx.lineTo(chartInstance.chartArea.right, yValue);,这将仅在图表上而不是整个画布上绘制线条。
  • 是的。使用 ctx.setLineDash([3, 5]);/*破折号为 3px,空格为 5px*/。 Demo use. Change added on line 33
猜你喜欢
  • 2015-09-14
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多