【发布时间】:2021-04-28 03:11:43
【问题描述】:
您好,我有这个 reacr/flask 应用程序,当您输入关键字时,它会将其发送到烧瓶检查一些数据,然后将其发送回以做出以下 json 响应:
我希望能够使用“highcharts-react-official”包绘制这些值。
到目前为止,我可以在控制台上看到我的回复,但图表上什么也没有。我将日期更改为该时间戳,但不确定是否更好或者我应该使用日期时间,如有必要,我可以将其更改回来。
我的代码如下:
import React, { Component } from 'react';
import axios from 'axios';
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
class Keyword extends Component {
state = {
kws :[]
}
keyword = e => {
e.preventDefault();
axios.post("/trends",{search_keyword: document.getElementById("keywords").value})
.then((res) => {
const data = res.data
const keyword = data.data
console.log("first index", keyword[1])
this.setState({kws: keyword})
}
)}
render() {
const {kws =[]} = this.state
const options = {
title: {
text: 'My chart'
},
xAxis: {
type: 'datetime'
},
series: [{
data: kws
}]
}
return (
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
);
}
}
我想画这样的东西:
更新:我已将 JSON 响应更新为如下:
这会是一个更好的绘图结构吗?如果是这样,我该怎么做才能像上图一样绘制每个关键字及其值?
【问题讨论】:
-
我会说该系列的数据不是图表可以理解的正确格式...快速浏览一下您正在为数据提供对象数组。您的数据也有时间戳作为键,您可能希望对其进行操作。你看过 HighCharts 文档吗? github.com/highcharts/highcharts-react#optimal-way-to-update
-
正确的方法是什么?我可以转置 JSON 响应,所以它是另一种方式。这样会更好吗?
-
嗯,随着时间的推移,您有价格行为......并且需要将系列作为数字系列的数组:[ { data: [1, 2, 3] } ],
-
这提供了一个很好的例子来说明数据如何寻找系列......而你的不匹配这个......stackblitz.com/edit/react-4ded5d?file=index.js
-
我明白了。那么拥有这样的数据会起作用吗? imgur.com/a/3dP09Du??
标签: json reactjs highcharts axios