gpt4 book ai didi

javascript - Chart.js - 在折线图中为背景的特定部分着色

转载 作者:行者123 更新时间:2023-12-05 00:34:46 25 4
gpt4 key购买 nike

我有一个很像这样的折线图:http://www.chartjs.org/samples/latest/charts/line/basic.html

我想为这些区域着色 -100 < y < -4040 < y < 100轻微的红色表示落在该区域的点处于危险区域。

enter image description here
这是一个使用油漆的速写。欢迎任何类似的事情。
我怎样才能做到这一点?我尝试查看文档但一无所获。

我目前有一个使用堆叠在水平条形图上的折线图的解决方法,但它远非理想。

提前致谢!

最佳答案

您可以使用annotation plugin尤其是 Box annotations .

下面是散点图的示例:

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>

关于javascript - Chart.js - 在折线图中为背景的特定部分着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49372827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com