Reputation: 63
I'm fetching price change data from an API endpoint, and I am getting this response from axios
0: (2) [1639382650242, 48759.49757943229]
1: (2) [1639386250855, 49131.24712464529]
2: (2) [1639389730718, 48952.65930253478]
3: (2) [1639393442318, 48821.67463307133]
4: (2) [1639396908986, 48835.214009032316]
5: (2) [1639400493105, 48618.25814914513]
6: (2) [1639404186695, 48572.06354948456]
7: (2) [1639407821756, 47974.48879991822]
8: (2) [1639411648037, 47105.42054562465]
9: (2) [1639414998352, 47176.56337927206]
I tried using this example code from the docs to render a simple line chart but it says that i must have atleast 2 columns of data:
<Chart
width={'600px'}
height={'400px'}
chartType="Line"
loader={<div>Loading Chart</div>}
data={
historicData.prices
}
/>
Is there a way to get it to work with google-react-charts?
Upvotes: 1
Views: 1470
Reputation: 13235
Add time
and price
as labels to the first entry. You can convert timestamps to actual date objects.
export default function App() {
const data = [
[1639382650242, 48759.49757943229],
[1639386250855, 49131.24712464529],
[1639389730718, 48952.65930253478],
[1639393442318, 48821.67463307133],
[1639396908986, 48835.214009032316],
[1639400493105, 48618.25814914513],
[1639404186695, 48572.06354948456],
[1639407821756, 47974.48879991822],
[1639411648037, 47105.42054562465],
[1639414998352, 47176.56337927206]
];
const processedData = [[{ type: "date", label: "time" }, "price"]].concat(
data.map(([timestamp, price]) => {
return [new Date(timestamp), price];
})
);
return (
<div className="App">
<Chart
width={"600px"}
height={"400px"}
chartType="Line"
loader={<div>Loading Chart</div>}
data={processedData}
/>
</div>
);
}
Code sandbox => https://codesandbox.io/s/busy-johnson-1xc0w?file=/src/App.js
Upvotes: 1
Reputation: 191
Please check Have you missed this labels in data section as per above image?
Upvotes: 0