reactjs - 带有 React 和 Boost 的 Highcharts/Highstock 无法正常工作(CPU 使用率非常高)
问题描述
我正在实现这个示例http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/samples/highcharts/boost/line-series-heavy-dynamic/但使用 React。
import React from 'react'
import Highcharts from 'highcharts'
import HighchartsBoost from 'highcharts/modules/boost'
HighchartsBoost(Highcharts)
Highcharts.setOptions({
global: {
useUTC: false
}
});
class Plot extends React.Component {
constructor(props) {
super(props)
this.n = 20
this.s = 600
this.addPoint = this.addPoint.bind(this)
}
componentDidMount() {
var series = getSeries(this.n, this.s)
console.time('line');
this.chart = Highcharts.chart('hc-container', {
boost: {
enabled: true,
seriesThreshold: 1
},
chart: {
animation: false,
zoomType: 'x'
},
title: {
text: 'Highcharts drawing ' + (this.n * this.s) + ' points across ' + this.s + ' series'
},
navigator: {
xAxis: {
ordinal: false//,
},
yAxis: {
},
series: {
color: null
}
},
legend: {
enabled: false
},
xAxis: {
ordinal: false
},
yAxis: {
},
subtitle: {
text: 'Using the Boost module'
},
tooltip: {
valueDecimals: 2,
shared: false
},
series: series
});
console.timeEnd('line');
setInterval(this.addPoint, 1000);
}
addPoint() {
++this.n;
if (!this.chart.series) return
this.chart.series.forEach(se => {
var x = this.n
var y = 2 * Math.sin(x / 100) + Math.random()
//Yeah...
if (se.options.className === "highcharts-navigator-series") {
return;
}
se.addPoint([x, y], false, true, false);
// se.options.data.push([x, y]);
// se.options.data.shift();
// se.isDirty = true;
// se.isDirtyData = true;
});
this.chart.redraw();
}
render() {
return <div id='hc-container' />
}
}
export default Plot
function getData(n) {
var arr = [],
i;
for (i = 0; i < n; i = i + 1) {
arr.push([
i,
2 * Math.sin(i / 100) + Math.random()
]);
}
return arr;
}
function getSeries(n, s) {
var i = 0,
r = [];
for (; i < s; i++) {
r.push({
data: getData(n),
animation: false,
lineWidth: 2,
boostThreshold: 1,
turboThreshold: 1,
showInNavigator: true,
requireSorting: false
});
}
return r;
}
我不认为 boost 模块是这样工作的,因为 CPU 非常非常高。我他们的示例(在 chrome 中运行)CPU 大约为 0.x 但在我的实现中,它大部分时间都达到 120。
解决方案
推荐阅读
- python-xarray - 如何计算 xarray 中的完整外积(协方差)?
- react-native - react native usememo renderitem 为什么不起作用?
- java - Shopee 在 api v2 中返回错误:error_auth - 无权限。请到虾皮卖家中心通过店铺KYC验证;该怎么办?
- javascript - 如何将 ViewBag 数据接收到 js 文件
- javascript - 如何渲染从异步钩子返回的数据?
- logstash - 无法将多行 json 数据解析为 logstash
- javascript - 有条件地将 x 个数组项推送到另一个数组
- python - 查找列中每个值之间的中点(Python)
- powershell - Powershell 比较 2 个对象上的 2 个 csv 文件并上传 3 个文件
- r - 在 if 条件 %in% 操作期间加速 R 的 grep