본문 바로가기
프로그램ing/Chart.js

[Chart.js] y축 vertical line 생성하기

by 철밥통 2020. 12. 21.
반응형

[Chart.js] y축 vertical line 생성하기

 

[방법 1] 단일 y축 vertical line 생성

1-1. HTML (Canvas 생성)

1
<canvas id="LineWithLine" width="600" height="400"></canvas>
cs

 

1-2. Script (Chart.js 생성)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var data = {
  labels: ["JAN""FEB""MAR""APR""MAY""JUN""JUL""AUG""SEP""OCT""NOV""DEC"],
  datasets: [{
    data: [1232188223571]
  }]
};
 
var ctx = document.getElementById("LineWithLine").getContext("2d");
 
Chart.types.Line.extend({
  name"LineWithLine",
  draw: function() {
    Chart.types.Line.prototype.draw.apply(thisarguments);
 
    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
});
 
cs

 

[방법 1] 적용결과


[방법 2] 다중 y축 vertical line 생성

2-1. HTML (Canvas 생성)

1
<canvas id="LineWithLine" width="600" height="400"></canvas>
cs

 

2-2. Script (Chart.js 생성)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  monthList = ["JAN""FEB""MAR""APR""MAY""JUN""JUL""AUG""SEP""OCT""NOV""DEC"]
  monthValue = [1232188223571]
 
  var data = {
      labels: monthList,
      datasets: [{
          label: '가용가액 [단위:백만원]',
          backgroundColor: 'rgba(255, 94, 0, 0)',
          borderColor: 'rgba(47, 157, 39, 1)',
          data: monthValue
      }]
  };
 
  const verticalLinePlugin = {
    getLinePosition: function (chart, pointIndex) {
        const meta = chart.getDatasetMeta(0); // first dataset is used to discover X coordinate of a point
        const data = meta.data;
        return data[pointIndex]._model.x;
    },
    renderVerticalLine: function (chartInstance, pointIndex) {
        const lineLeftOffset = this.getLinePosition(chartInstance, pointIndex);
        const scale = chartInstance.scales['y-axis-0'];
        const context = chartInstance.chart.ctx;
 
 
        // render vertical line
        context.beginPath();
        context.strokeStyle = '#ff0000';
        context.moveTo(lineLeftOffset, scale.top + 20);
        context.lineTo(lineLeftOffset, scale.bottom);
        context.stroke();
 
        // write label
        context.fillStyle = "#ff0000";
        context.textAlign = 'center';
        context.fillText('CHECK POINT', lineLeftOffset, scale.top + 10);
    },
    afterDatasetsDraw: function (chart, easing) {
        if (chart.config.lineAtIndex) {
            chart.config.lineAtIndex.forEach(pointIndex => this.renderVerticalLine(chart, pointIndex));
        }
      }
  };
  
  Chart.plugins.register(verticalLinePlugin);
 
  new Chart('LineWithLine' , {
     type: 'line',
     data: data,
     label: 'Progress',
     options: options,
     lineAtIndex: [1,5,8],
 })
cs

 

[방법2] 적용결과

 

반응형

댓글