简体   繁体   中英

Parsing JSON Data to Chartjs in React

I have been import JSON files from my MongoDB server through axios. I am able to fetch the data successfully but showing it on chart is not possible. I ahve seen other answers and it seems easy to store each key column in a seperate variable and then loading to labels and data objects, but this more optimized and Chartjs also allows us this approach as listed in its documentation but I might be going wrong somewhere. In need for help as I need to implement it for my project

import React, { useState } from 'react';
import axios from 'axios';
import { useEffect } from 'react';
import {Bar, Line} from 'react-chartjs-2';

const URL = process.env.REACT_APP_SERVER_URL || 'http://localhost:5000/';

function ChartRoughPage(props) {
    const [historicalData,setHistoricalData] = useState([]);

    useEffect(()=>{
        axios.get(URL+'stock/BLUEDART')
            .then((response)=>{
                if(response.status===200){   
                    console.log(response.data)
                    setHistoricalData(response.data)
                }
                else{
                    console.log("ERROR: "+response.status+" , "+response.statusText)
                }
            })
            .catch((err) => console.log.err);
    },[]);

    return (
        <div>
            <Line 
                data={{
                    datasets:[{
                        label:'Everyday Chart',
                        data : historicalData,
                        parsing:{
                            xAxisKey:'DATE',
                            yAxisKey:'CLOSE'
                        }
                    }]
                }}
            />
        </div>
    );
}

export default ChartRoughPage;

Output: It just shows a chart with no data

For better understanding here is the link to the documentation: https://www.chartjs.org/docs/latest/general/data-structures.html

Also I have tried following things on my code:

  • Tried writing it to options
...
            <Line 
                data={{
                    datasets:[{
                        data : historicalData
                    }]
                }}
                options={{
                    parsing:{
                        xAxisKey:'Date',
                        yAxisKey:'Close'
                    }
                }}
            />
...
  • Providing a static data like:
historicalData = [{Date : '22-02-2000',Close: 56},{Date : '22-03-2000',Close: 656},{Date : '23-05-2000',Close: 6}]

also the documents that I send as JSON from MongoDB is like this is(all the values are accessible by their keys):

{"_id":{"$oid":"some-object-id"},"Date":"2019-01-03","Symbol":"20MICRONS","Series":"EQ","Prev Close":{"$numberDouble":"44.05"},"Open":{"$numberDouble":"44.05"},"High":{"$numberDouble":"44.1"},"Low":{"$numberDouble":"43.1"},"Last":{"$numberDouble":"43.4"},"Close":{"$numberDouble":"43.45"},"VWAP":{"$numberDouble":"43.48"},"Volume":{"$numberInt":"15741"},"Turnover":{"$numberDouble":"68447485000.0"},"Trades":{"$numberDouble":"368.0"},"Deliverable Volume":{"$numberInt":"9487"},"%Deliverble":{"$numberDouble":"0.6027"}}

Will be grateful for your help!

It seems the issue is in the type of the x-axis, cause if you provide

  datasets: [{
        data: [{Date : '11',Close: 56},{Date : '22',Close: 656},{Date : '23',Close: 6}],
        showLine: true,
        fill: false,
        borderWidth: 1,
        parsing: {
            xAxisKey: 'Date',
            yAxisKey: 'Close'
        },
        backgroundColor: 'rgba(255, 99, 132, 1)'
    }]

it draws just fine

在此处输入图像描述

So you should better find how to show time on the x-axis

I know I am late, but I have to answer this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM