Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I want to draw a single bar over vertical bars(Green Bar). I am using Chart JS V2 with Angular 4.

I found some code to draw lines but its not working in Angular 4.

I used also tried using annotation but its not working. Command to add annotation: npm install chartjs-plugin-annotation --save

Below is my code, works fine to draw vertical bars only. Can anyone help me to draw horizontal line over it.

Answer:

Install npm install chartjs-plugin-annotation --save

Then import 'chartjs-plugin-annotation';

this.ctx = document.getElementById("myChart");     this.myChart = new Chart(this.ctx, {         type: 'bar',         data: {             labels: this.barData.getLabels(),             datasets: [{                 label: this.barData.actualLegendLabel,                 data: this.barData.getLineData(),                 backgroundColor: this.backgroundColorBarOne,                 borderColor: [                     'rgba(81,117, 194,1)',                 ]}]         },         options: {             scales: {                 responsive: true,                 scaleBeginAtZero: false,                 barBeginAtOrigin: true,                 yAxes: [{                     ticks: {                         beginAtZero: true                     },                     gridLines: {                         display: false                     }                 }],                 xAxes: [{                     ticks: {                         beginAtZero: true                     },                     gridLines: {                         display: false                     }                 }]             },             legend: {                 cursor: "line",                 position: 'top',                 labels: {                     fontSize: 10,                                        }             },             layout: {                 padding: {                     left: 3,                     right: 3,                     top: 5,                     bottom: 5                 }             }, annotation: {                 annotations: [{                     drawTime: 'afterDraw', // overrides annotation.drawTime if set                     id: 'a-line-1', // optional                     type: 'line',                     mode: 'horizontal',                     scaleID: 'y-axis-0',                     value: '25',                     borderColor: 'red',                     borderWidth: 2,                                // Fires when the user clicks this annotation on the chart                     // (be sure to enable the event in the events array below).                     onClick: function(e) {                         // `this` is bound to the annotation element                     }                 }]             }         }     });       

回答1:

You can add a plugin to your chart which can draw anything you'd like on the chart, for example a green line. You can read about plugins in the documentation for ChartJS. Since you want the green line to appear above the vertical bars, you should use the afterDraw method.

Once you've set the plugin up, the steps to accomplish this would be:

  1. Calculate the mean of all the bars in your chart (sum them up and divide by the number of bars)
  2. From the previous calculation: Decide the Y position of the line and based on that, draw the green line on the canvas.

Check out CanvasRenderingContext2D if you are new to how the browser canvas works.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!