【发布时间】:2020-04-15 12:49:40
【问题描述】:
嘿,我正在尝试在堆积条形图中实现 json API 数据值,但我不知道该怎么做,这里是 json api http://www.mocky.io/v2/5e96ff763000006500b6da20,它看起来像
{"topology": [
{
"label": "living_street",
"value": 0.010549953326361517
},
{
"label": "tertiary",
"value": 0.767246375468044
},
{
"label": "primary",
"value": 0.21522791175081937
},
{
"label": "service",
"value": 0.006975759454774865
}
]
}
这是我目前在代码中所做的。
`fetchData() {
fetch(`http://www.mocky.io/v2/5e96ff763000006500b6da20`)
.then(response => response.json())
.then(dat => {
const dat1 = dat.map(c => {
return c.label;
});
const dat2 = dat.map(c => {
return c.value;
});
const newXaxis = dat1;
const newSeries = [];
newSeries.push({
data: dat2,
name: this.state.series.labels
});
this.setState({
series: newSeries,
options: {
...this.state.options,
labels: newXaxis,
xaxis: { ...this.state.options.xaxis, categories: newXaxis }
}
});
});
}
componentDidMount() {
this.fetchData();
}`
...
return (
<Chart
options={this.state.options}
series={newSeries}
type="bar"
height="150px"
/>
}
但它没有显示在堆叠图表中是这样显示的
我希望结果看起来像这样。
【问题讨论】:
标签: javascript reactjs charts apexcharts