简体   繁体   English

Chart.js-折线图中背景的特定部分的颜色

[英]Chart.js - Color specific parts of the background in a line chart

I have a line chart much like this one: http://www.chartjs.org/samples/latest/charts/line/basic.html 我有一个非常像这样的折线图: http : //www.chartjs.org/samples/latest/charts/line/basic.html

I would like to color the areas -100 < y < -40 and 40 < y < 100 a slight tint of red to indicate that point that fall in that area are in a danger zone. 我想为区域-100 < y < -4040 < y < 100涂上一点红色,以表明落入该区域的点处于危险区域。

在此处输入图片说明 This was a quick sketch using paint. 这是使用油漆的快速草图。 Anything similar to this is welcome. 任何与此相似的东西都值得欢迎。 How can I do this? 我怎样才能做到这一点? I tried looking into the documentation but found nothing. 我尝试查看文档,但一无所获。

I currently have a workaround using a line chart stacked over a horizontal bar chart but it is far from ideal. 我目前有一种解决方法,即使用折线图叠加在水平条形图上,但这远非理想。

Thanks in advance! 提前致谢!

You can use annotation plugin and in particular Box annotations . 您可以使用注释插件 ,尤其是Box注释

Here below there is an example with Scatter chart: 下面是一个散点图示例:

 var randomScalingFactor = function() { return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100); }; var randomColor = function(opacity) { return 'rgba(' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + (opacity || '.3') + ')'; }; var data = { datasets: [{ label: "My First dataset", data: [{ x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }] }, { label: "My Second dataset", data: [{ x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }, { x: randomScalingFactor(), y: randomScalingFactor(), }] }] }; data.datasets.forEach(function(dataset) { dataset.borderColor = randomColor(0.4); dataset.backgroundColor = randomColor(0.1); dataset.pointBorderColor = randomColor(0.7); dataset.pointBackgroundColor = randomColor(0.5); dataset.pointBorderWidth = 1; }); var ctx = document.getElementById("canvas").getContext("2d"); window.myScatter = new Chart(ctx, { type: 'scatter', data: data, options: { scales: { xAxes: [{ position: 'bottom', gridLines: { zeroLineColor: "rgba(0,255,0,1)" }, scaleLabel: { display: true, labelString: 'x axis' }, }], yAxes: [{ position: 'left', gridLines: { zeroLineColor: "rgba(0,255,0,1)" }, scaleLabel: { display: true, labelString: 'y axis' }, ticks: { min: -100, max: 100 } }] }, annotation: { drawTime: "afterDraw", events: ['dblclick'], annotations: [{ id: 'low-box', type: 'box', xScaleID: 'x-axis-1', yScaleID: 'y-axis-1', xMin: -100, xMax: 100, yMin: -100, yMax: -40, backgroundColor: 'rgba(255, 0, 0, 0.3)', //borderColor: 'rgb(255, 0, 0)', borderWidth: 1 },{ id: 'hi-box', type: 'box', xScaleID: 'x-axis-1', yScaleID: 'y-axis-1', xMin: -100, xMax: 100, yMin: 100, yMax: 40, backgroundColor: 'rgba(255, 0, 0, 0.3)', //borderColor: 'rgb(255, 0, 0)', borderWidth: 1 }] } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js"></script> <canvas id="canvas"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM