Параметры Chartjs до неузнаваемых типов - PullRequest
0 голосов
/ 27 февраля 2019

Я настраиваю свои графики, рисуя горизонтальную линию, и использую плагины

Я объявляю свой плагин

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

    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);

Тогда я использую это так

options: {
    'horizontalLine': [{
      'y': this.objectifs[this.name],
      'style': '#82be00',
      'text': "Objectif"
    }],
    showLines: true,
    ....

На самом деле я получаю эту ошибку

Тип '{' horizontalLine ': {' y ': any;'style': строка;'текст': строка;} [];showLines: правда;легенда: {... 'нельзя назначить типу' ChartOptions '.

Литерал объекта может указывать только известные свойства, а' HorizontalLine '' не существует в типе 'ChartOptions'.

Но приложение работает, и я получаю горизонтальную линию, как я хотел.

...