我正在试图绘制一个从Web中获取数据的图表。我可以看到一些数据正在通过,但我仍然无法绘制图表。如果我做错了什么,请告诉我。
import React, { Component } from 'react';
import './main.styles.scss';
import { createChart } from 'lightweight-charts';
async function getData() {
const response = await fetch(`http://localhost:3500/stock/app/RY`);
const data = await response.json();
return data.webApiData;
}
class Main extends Component {
ref = React.createRef();
componentDidMount() {
const chart = createChart(this.ref.current, {
width: 1400,
height: 550,
timeScale: {
timeVisible: true,
secondsVisible: false,
},
});
const candleSeries = chart.addCandlestickSeries();
const chartData = getData().then((data) => {
console.log(data);
candleSeries.setData(data);
});
}
render() {
return (
<div className="main">
<div className="trading">
<div className="box one">1</div>
<div className="box two" ref={this.ref}></div>
</div>
</div>
);
}
}
export default Main;以下是console.log上的数据

这是我正在犯的错误

发布于 2020-07-31 12:56:24
这是因为time属性的格式不正确。它应该是YYYY-MM-DD风格的。
例如,您可以尝试
const chartData = getData().then((data) => {
console.log(data);
candleSeries.setData(data.map((sD) => {
return {time: `${sD.time.year}-${sD.month > 9 ? sD.month : `0${sD.time.month}`}-${sD.day > 9 ? sD.day : `0${sD.day}`}`, ...sD}
}));
});发布于 2020-07-31 12:53:20
这是因为time格式不正确。它应该是一个日期字符串。
从烛台图文档
烛台系列的每一项要么是OHLC,要么是空白项。
因此,time必须采用以下格式。
const ohlcItem = {
time: '2018-06-25',
open: 10,
high: 12,
low: 9,
close: 11,
};或
// note it might be any type of series here
const series = chart.addHistogramSeries();
series.setData([
{ time: '2018-12-01', value: 32.51 },
{ time: '2018-12-02', value: 31.11 },
{ time: '2018-12-03', value: 27.02 },
{ time: '2018-12-04' }, // whitespace
{ time: '2018-12-05' }, // whitespace
{ time: '2018-12-06' }, // whitespace
{ time: '2018-12-07' }, // whitespace
{ time: '2018-12-08', value: 23.92 },
{ time: '2018-12-09', value: 22.68 },
{ time: '2018-12-10', value: 22.67 },
{ time: '2018-12-11', value: 27.57 },
{ time: '2018-12-12', value: 24.11 },
{ time: '2018-12-13', value: 30.74 },
]);https://stackoverflow.com/questions/63191912
复制相似问题