gpt4 book ai didi

javascript - 在动态生成的图表中为最大值添加颜色?

转载 作者:行者123 更新时间:2023-11-30 10:16:41 24 4
gpt4 key购买 nike

jsfiddle 链接在这里 http://jsfiddle.net/MzQE8/110/

我觉得这里的问题出在我的 JavaScript 中。

我将值从数组输入到 HighChart 中的系列对象。在数组上,我试图找到索引和最大元素的值,然后通过此修改将最大数组元素保存回来

                        yArr[index] = {
y: value,
color: '#aaff99'
};

因此它显示为与动态图上其余点不同的颜色。这是它的滑动部分。

这是我的代码

$(function () {
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});

var chart;

$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {

// set up the updating of the chart each second
var series = this.series[0];
//As this graph is generated due to random values. I am creating an Array with random values.
var yArr = [];
yArr[0] = Math.random();

yArr[1] = Math.random();

yArr[2] = Math.random();

yArr[3] = Math.random();



setInterval(function () {
console.log(yArr.length);
var x = (new Date()).getTime(), // current time
y = Math.random();
var index = findIndexOfGreatest(yArr);
var value = yArr[index];
yArr[index] = {
y: value,
color: '#aaff99'
};
series.addPoint([x, yArr.shift()], true, true);
yArr.push(Math.random());

}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 450
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
plotOptions: {

series: {
lineWidth: 1
}
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random Data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;

for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: yArr.shift()
});
}
return data;
})(),
color: 'red'
}]
});
});

function findIndexOfGreatest(array) {
var greatest;
var indexOfGreatest;
for (var i = 0; i < array.length; i++) {
if (!greatest || array[i] > greatest) {
greatest = array[i];
indexOfGreatest = i;
}
}
return indexOfGreatest;
}

});

我觉得我的想法是正确的,但我的实现有很大的漏洞。我猜。

谢谢

最佳答案

查看演示:http://jsfiddle.net/MzQE8/350/

所有 y 值都存储在 series.yData 中,因此您不必为此创建另一个数组。现在只需更新最高点,并添加新点。类似于上面的演示,代码:

            events: {
load: function () {

// set up the updating of the chart each second
var series = this.series[0],
index = series.yData.indexOf(series.yData.max());

// mark first max points
this.series[0].prevMax = this.series[0].data[index];
this.series[0].prevMax.update({
color: '#aaff99'
});

setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random(),
color = null,
index, max;
if (series.prevMax && series.prevMax.update) {
// remove previously colored point
if (y > series.prevMax.y) {
series.prevMax.update({
color: null
}, false);
color = '#aaff99';

// store max, which is last point
series.prevMax = series.data[series.yData.length];
}
} else {
max = series.yData.max();
index = series.yData.indexOf(max);

if(y > max) {
color = '#aaff99';
series.prevMax = series.data[series.yData.length];
} else {
series.prevMax = series.data[index];
series.prevMax.update({
color: '#aaff99'
}, false)
}
}

// add new point
series.addPoint({
x: x,
y: y,
color: color
}, true, true);

}, 1000);
}
}

关于javascript - 在动态生成的图表中为最大值添加颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326036/

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