admin管理员组文章数量:1345725
I have a line chart much like this one: .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.
This was a quick sketch using paint. Anything similar to this is wele. 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!
I have a line chart much like this one: http://www.chartjs/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.
This was a quick sketch using paint. Anything similar to this is wele. 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!
Share Improve this question edited Mar 22, 2018 at 14:26 beaver 17.7k2 gold badges43 silver badges68 bronze badges asked Mar 19, 2018 at 21:43 Francisco FaríasFrancisco Farías 2273 silver badges15 bronze badges 2- Please share your code so someone here on SO could work on it and help you – beaver Commented Mar 20, 2018 at 7:48
- @Farias: have you tried my proposed solution? – beaver Commented Mar 22, 2018 at 9:06
1 Answer
Reset to default 11You can use annotation plugin and in particular Box annotations.
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./ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js"></script>
<canvas id="canvas"></canvas>
本文标签: javascriptChartjsColor specific parts of the background in a line chartStack Overflow
版权声明:本文标题:javascript - Chart.js - Color specific parts of the background in a line chart - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743807823a2542500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论