【发布时间】:2019-10-14 11:58:51
【问题描述】:
我正在尝试从https://api.data.gov.sg/v1/environment/air-temperature 获取城市名称及其各自的温度,然后将其显示给 Graphs。那么,如何获取所有城市名称并将其设置为图形代码中的标签?下面是我的代码。我是 reactjs 的新手。因此,任何形式的帮助都将得到高度利用。
import React, { Component } from "react";
import axios from "axios";
import { Line } from "react-chartjs-2";
class Chart extends Component {
constructor(props) {
super(props);
this.state = {
chartData: {},
areas: [],
temperature: [],
all: {}
};
}
componentDidMount() {
axios
.get("https://api.data.gov.sg/v1/environment/air-temperature")
.then(res =>
this.setState({
all: res.data,
areas: res.data.metadata.stations,
temperature: res.data.items[0].readings
})
)
.catch(err => console.log(err));
this.getChartData();
}
getChartData() {
console.log("length", this.state.areas);
// Ajax calls here
this.setState({
chartData: {
labels: [
"Boston",
"Worcester",
"Springfield",
"Lowell",
"Cambridge",
"New Bedford"
],
datasets: [
{
label: "Population",
data: [617594, 181045, 153060, 106519, 105162, 95072],
backgroundColor: [
"rgba(255, 99, 132, 0.6)",
"rgba(54, 162, 235, 0.6)",
"rgba(255, 206, 86, 0.6)",
"rgba(75, 192, 192, 0.6)",
"rgba(153, 102, 255, 0.6)",
"rgba(255, 159, 64, 0.6)",
"rgba(255, 99, 132, 0.6)"
]
}
]
}
});
}
render() {
//console.log(this.state.areas);
return (
<div style={{ position: "relative", width: 500, height: 500 }}>
<h1>Chart</h1>
<Line data={this.state.chartData} />
</div>
);
}
}
export default Chart;
【问题讨论】:
-
从
areas获取“名称”是什么意思?您还可以附上areas的样例吗? -
基本上我想从此 API 获取所有“名称”,然后在 chartDate 函数下将其设置为“标签”。主要目的是显示带有城市名称及其温度的图表。
标签: javascript reactjs