[英]Graph streaming real-time data with react and chartjs
I want to stream data in real-time with this plugin .我想用这个插件实时获取 stream 数据。 But currently, my chart displays the same dataset and remains static.
但目前,我的图表显示相同的数据集,并且仍然是 static。 Even though I am following the react example of this website Below is the code I used:
即使我正在关注本网站的反应示例下面是我使用的代码:
import React from 'react'; import {Line} from 'react-chartjs-2'; import 'chartjs-plugin-streaming'; var createReactClass = require('create-react-class'); const data = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [ { label: 'Price', fill: false, lineTension: 0.1, backgroundColor: 'rgba(75,192,192,0.4)', borderColor: 'rgba(75,192,192,1)', borderCapStyle: 'butt', borderDash: [], borderDashOffset: 0.0, borderJoinStyle: 'miter', pointBorderColor: 'rgba(75,192,192,1)', pointBackgroundColor: '#fff', pointBorderWidth: 1, pointHoverRadius: 5, pointHoverBackgroundColor: 'rgba(75,192,192,1)', pointHoverBorderColor: 'rgba(220,220,220,1)', pointHoverBorderWidth: 2, pointRadius: 1, pointHitRadius: 10, data: [65, 59, 80, 81, 56, 55, 40] } ] }; export default createReactClass({ displayName: 'LineExample', render() { return ( <div> <Line data={data} options={{ scales: { xAxes: [{ realtime: { onRefresh: function(chart) { data.dataset.data.push({ x: Date.now(), y: Math.random() }); }, delay: 2000 } }] } }} /> </div> ); } });
What could be the problem?可能是什么问题呢? Thanks!
谢谢!
without a snippet to play with I'd guess that the problem is here没有片段可以玩我猜问题就在这里
onRefresh: function(chart) {
data.dataset.data.push({
x: Date.now(),
y: Math.random()
});
}
you've pushed the new data to the initial dataset, but at a guess you'll need to push it to the chart itself.您已将新数据推送到初始数据集,但猜测您需要将其推送到图表本身。 looking at the example at:
查看以下示例:
https://nagix.github.io/chartjs-plugin-streaming/samples/bar-horizontal.html https://nagix.github.io/chartjs-plugin-streaming/samples/bar-horizontal.html
seems like you should be passing the data directly to the chart似乎您应该将数据直接传递给图表
chart.config.data.datasets.forEach(function(dataset) {
dataset.data.push({
x: Date.now(),
y: randomScalingFactor()
});
});
or maybe, (again I'm just guessing where the new data might go without a mvp example to play with, console.log out the chart variable in the onRefresh callback to find the right location), If you setup a mvp on somewhere like stackblitz I/someone can update this answer.或者也许,(我只是在猜测新数据可能在哪里 go 没有 mvp 示例可玩,console.log out the chart variable in the onRefresh callback to find the right location),如果你在某个地方设置一个 mvp stackblitz 我/有人可以更新这个答案。
onRefresh: function(chart) {
chart.dataset.data.push({
x: Date.now(),
y: Math.random()
});
}
The data format you provided is wrong, you are trying to您提供的数据格式错误,您正在尝试
data.dataset.data.push
but the data you provided only have X value. data.dataset.data.push
但您提供的数据只有 X 值。 you need to reformat it, the data should have the format like this: {x:$(value), y:${value}}
你需要重新格式化它,数据应该有这样的格式:
{x:$(value), y:${value}}
Open the code snippet below in CodeSandbox在CodeSandbox中打开下面的代码片段
import React from "react";
import { Line } from "react-chartjs-2";
import "chartjs-plugin-streaming";
var createReactClass = require("create-react-class");
const data = {
datasets: [
{
label: "Dataset 1",
borderColor: "rgb(255, 99, 132)",
backgroundColor: "rgba(255, 99, 132, 0.5)",
lineTension: 0,
borderDash: [8, 4],
data: []
}
]
};
const options = {
scales: {
xAxes: [
{
type: "realtime",
realtime: {
onRefresh: function() {
data.datasets[0].data.push({
x: Date.now(),
y: Math.random() * 100
});
},
delay: 2000
}
}
]
}
};
export default createReactClass({
displayName: "LineExample",
render() {
return (
<div>
<Line data={data} options={options} />
</div>
);
}
});
chart
is undefined because it was never defined in React. chart
是未定义的,因为它从未在 React 中定义。
Here is an example with React:这是一个使用 React 的示例:
onRefresh: function(chart) {
chart.dataset.data.push({
x: Date.now(),
y: Math.random()
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.