[英]How do I get a chart.js chart to display data in a Node/React web application?
I'm trying to display financial data on a web app using the Chart.js library. 我正在尝试使用Chart.js库在Web应用程序上显示财务数据。
In my Node.js app, a user uploads a csv file. 在我的Node.js应用程序中,用户上传了一个csv文件。 This file gets processed into a pandas dataframe by a flask rest api.
该文件由flask rest api处理成pandas数据帧。 The dataframe is saved on the flask server and is accessible via a get request.
数据帧保存在flask服务器上,可以通过获取请求进行访问。 The get request returns a JSON object with fields Open, High, Low, Close, and Volume.
get请求返回一个JSON对象,其字段为Open,High,Low,Close和Volume。 These fields hold arrays.
这些字段保存数组。
I have a web socket (socket.io) established between my express server and a react component. 我在快递服务器和react组件之间建立了一个Web套接字(socket.io)。 My express server pings my flask api looking for the chart data.
我的快递服务器ping我的烧瓶api,以查找图表数据。 When the chart data is available (after uploading a file) the socket sends this data to the component.
当图表数据可用时(在上传文件之后),套接字会将这些数据发送到组件。 The client socket triggers a component method that creates the chart.
客户端套接字触发创建图表的组件方法。 A chart appears but has no data points displayed.
出现一个图表,但没有显示数据点。
The component: 组件:
class Canvas extends Component {
constructor(props){
super(props);
this.state = {
data: false,
endpoint: 'http://localhost:4000'
}
this.establishSocket = this.establishSocket.bind(this);
this.makeChart = this.makeChart.bind(this);
}
componentDidMount() {
this.establishSocket();
}
establishSocket() {
const { endpoint } = this.state;
const socket = socketIOClient(endpoint);
socket.on("ohlcv_data", data => this.makeChart(data.Close));
}
makeChart(datapoints) {
this.setState({ data: datapoints })
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: datapoints,
options: {
responsive: true,
maintainAspectRatio: false
}
});
console.log(myChart.data);
}
render() {
return (
<div id="chartContainer">
<canvas id="myChart"></canvas>
</div>
)
}
}
The charting code has been adapted from the Chart.js docs here: 图表代码已从此处的Chart.js文档改编而成:
https://www.chartjs.org/docs/latest/getting-started/usage.html https://www.chartjs.org/docs/latest/getting-started/usage.html
https://www.chartjs.org/docs/latest/charts/line.html https://www.chartjs.org/docs/latest/charts/line.html
This is my app with the empty chart: 这是我的应用,带有空图表:
The console log at the bottom of the makeChart function displays the expected array. makeChart函数底部的控制台日志显示预期的数组。 This is the output of that log:
这是该日志的输出:
My question is, what am I missing in this implementation in order to get the data to appear? 我的问题是,为了使数据显示出来,我在该实现中缺少什么?
I could be wrong, but it looks like the input data for your linechart is in an incorrect format. 我可能是错的,但看来您的折线图的输入数据格式不正确。 Looking here at chartJs' documentation, you either have to specify the x and y of each point individually using an array of objects, or in your case, when passing an array of numbers, specify a labels array.
查看 chartJs的文档,您要么必须使用对象数组分别指定每个点的x和y,要么在传递数字数组时指定标签数组。
When the data array is an array of numbers, the x axis is generally a category.
当数据数组是数字数组时,x轴通常是一个类别。 The points are placed onto the axis using their position in the array.
使用它们在阵列中的位置将这些点放置在轴上。 When a line chart is created with a category axis, the labels property of the data object must be specified.
使用类别轴创建折线图时, 必须指定数据对象的标签属性。
You can see how to do that here , so your chart data param should look like this: 您可以在此处查看操作方法 ,因此图表数据参数应如下所示:
let chart = new Chart(ctx, { type: ... data: { labels: ['January', 'February', 'March', 'April', 'May', 'June'], datasets: ... }, });
Thanks to cdm for putting me in the right direction. 感谢cdm为我提供了正确的指导。 I remembered that one of my github repositories has chart.js implementations that my teammates made which replaced the google charts that I was using originally.
我记得我的一个github仓库中有我的队友进行的chart.js实现,该实现取代了我最初使用的google图表。
They are implemented like so: 它们的实现方式如下:
makeChart(datapoints) {
this.setState({ data: datapoints })
var ctx = document.getElementById("myChart");
var datapoints = datapoints.slice(0,100)
var config = {
type: 'line',
data: {
labels: this.linspace(0,datapoints[0],datapoints.length),
datasets: [{
data: datapoints,
label: "Price:",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
title: {
display: true,
text: 'WTI Closing Price'
},
legend: {
display: false
}
}
}
var myChart = new Chart(ctx, config );
}
This configuration format works as expected producing the chart below: 此配置格式按预期工作,生成下表:
For completeness, for those who may use this exact code, the linspace
function is implemented as follows: 为了完整起见,对于那些可能使用此确切代码的人,
linspace
函数的实现如下:
precisionRound(number, precision) {
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
linspace (a, b, n) {
if (typeof n === 'undefined') n = Math.max(Math.round(b - a) + 1, 1)
if (n < 2) {
return n === 1 ? [a] : []
}
var i,ret = Array(n)
n--
for (i = n;i >= 0;i--) {
ret[i] = this.precisionRound((i * b + (n - i) * a) / n, 2)
}
return ret
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.