0

I am new in reactjs. Currently I'm developing an app which shows json COVID-19 api data into visualization using chartjs. I tried this from yesterday but I can't show the visual data.

Here is my code

App Component

import React, { useState, useEffect } from "react";
import axios from "axios";

import Chart from "./Chart";

const App = () => {
  const [state, setState] = useState({});
  const [loading, setLoading] = useState(true);
  const [chart, setChart] = useState({});

  useEffect(() => {
    getData("italy");
    setChart({
      labels: ["Cases", "Deaths", "Recovered"],
      datasets: [
        {
          label: "cases",
          data: [state.cases]
        },
        {
          label: "deaths",
          data: [state.deaths]
        },
        {
          label: "recovered",
          data: [state.recovered]
        }
      ]
    });
  }, []);

  const getData = async country => {
    try {
      const res = await axios.get(
        `https://corona.lmao.ninja/v2/historical/${country}`
      );
      setLoading(false);
      setState(res.data.timeline);
    } catch (error) {
      console.log(error.response);
    }
  };

  return (
    <div>
      {!loading
        ? console.log(
            "cases",
            state.cases,
            "deaths",
            state.deaths,
            "recovered",
            state.recovered
          )
        : null}

      {!loading ? <Chart chart={chart} /> : "loading failed"}
    </div>
  );
};

export default App;

And Here is Chart Component

import React from "react";

import { Line } from "react-chartjs-2";

const Chart = ({chart}) => {

  return (
    <div>
      <Line
        data={chart}
        height={300}
        width={200}
        options={{
          maintainAspectRatio: false,
          title: {
            display: true,
            text: "Covid-19",
            fontSize: 25
          },
          legend: {
            display: true,
            position: "top"
          }
        }}
      />
    </div>
  );
};

export default Chart;


If I open browser and dev tools it look likes this

browser and dev tool

I want to visualize the data like this

data visualization

Here is codeSandBox.io

2

1 Answer 1

1

Looks like data property within dataset takes only array of numbers. I have simplifies your code using class based component. It will help you get started.

https://codesandbox.io/s/react-chartjs-2-example-mzh9o

 setChartData = () => {
    let { data } = this.state;
    let chartData = {
      labels: ["Cases", "Deaths", "Recovered"],
      datasets: [
        {
          label: "cases",
          data: Object.values(data.cases)
        },
        {
          label: "deaths",
          data: Object.values(data.deaths)
        },
        {
          label: "recovered",
          data: Object.values(data.recovered)
        }
      ]
    };
    this.setState({
      chart: chartData
    });
  };

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.