0

I am making a SPA in React that displays data in a chart.js graph from the Carbon Intensity API which can be found here: https://carbon-intensity.github.io/api-definitions/?shell#get-intensity-from-to .

I have been able to get the data from the API using axios and can see this when I inspect the console, however it just parses in NaN for all the data when you inspect it in the console:enter image description here enter image description here

I will post all relevant code below, my desired output is for the data from the API to be displayed on the line chart.

Chart3.js

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

const Chart3 = () => {
  const [chartData, setChartData] = useState({});
  const [Forecast, setForecast] = useState([]);
  const [Actual, setActual] = useState([]);

  const chart = () => {
    let Fore = [];
    let Act = [];


    axios
      .get('https://api.carbonintensity.org.uk/intensity/2020-08-25T15:30Z/2020-09-10T17:00Z')
      .then(res => {
        console.log(res);
        for (const dataObj of res.data.data) {
          Fore.push(parseInt(dataObj.forecast));
          Act.push(parseInt(dataObj.actual));
        }
        setChartData({
          labels: Fore,
          datasets: [
            {
              label: "Forestreet",
              data: Act,
              backgroundColor: ["rgba(75, 192, 192, 0.6)"],
              borderWidth: 4
            }
          ]
        });
      })
      .catch(err => {
        console.log(err);
      });
    console.log(Fore, Act);
  };

  useEffect(() => {
    chart();
  }, []);
  return (
    <div className="App">
      <h1>Dankmemes</h1>
      <div style={{height:"500px", width:"500px"}}>
        <Line
          data={chartData}
          options={{
            responsive: true,
            title: { text: "ForeStreet", display: true },
            scales: {
              yAxes: [
                {
                  ticks: {
                    autoSkip: true,
                    maxTicksLimit: 10,
                    beginAtZero: true
                  },
                  gridLines: {
                    display: false
                  }
                }
              ],
              xAxes: [
                {
                  gridLines: {
                    display: false
                  }
                }
              ]
            }
          }}
        />
      </div>
    </div>
  );
};

export default Chart3;

App.js

import React from 'react';
import { BrowserRouter as Router} from 'react-router-dom';
import './App.css';
import Chart from './Components/Chart/Chart';
import Chart1 from './Components/Chart/Chart1';
import Chart2 from './Components/Chart/Chart2';
import Chart3 from './Components/Chart/Chart3';
import Footer from './Components/Footer/Footer';
import Header from './Components/Header/Header';
import Container from './Components/Hero/Hero';
import Hero from './Components/Hero/Hero';

function App() {
  return (
    <Router>
        <Header />
        <Chart3 />
        <Footer />
    </Router>
      
    
  );
}

export default App;

This is what my chart currently looks like it with the API data: enter image description here

1 Answer 1

1

Your dataObject in axios res is not correct, you forgot intensity. your Chart3.js have to look like:

import axios from "axios";
import React, {useState, useEffect} from "react";
import { Line } from "react-chartjs-2";
const Chart3 = () => {
  const [chartData, setChartData] = useState({});
  const [Forecast, setForecast] = useState([]);
  const [Actual, setActual] = useState([]);

  const chart = () => {
    let Fore = [];
    let Act = [];


    axios
      .get('https://api.carbonintensity.org.uk/intensity/2020-08-25T15:30Z/2020-09-10T17:00Z')
      .then(res => {
        console.log(res);
        for (const dataObj of res.data.data) {
          Fore.push(parseInt(dataObj.intensity.forecast));
          Act.push(parseInt(dataObj.intensity.actual));
        }
        setChartData({
          labels: Fore,
          datasets: [
            {
              label: "Forestreet",
              data: Act,
              backgroundColor: ["rgba(75, 192, 192, 0.6)"],
              borderWidth: 4
            }
          ]
        });
      })
      .catch(err => {
        console.log(err);
      });
    console.log(Fore, Act);
  };

  useEffect(() => {
    chart();
  }, []);
  return (
    <div className="App">
      <h1>Dankmemes</h1>
      <div style={{height:"500px", width:"500px"}}>
        <Line
          data={chartData}
          options={{
            responsive: true,
            title: { text: "ForeStreet", display: true },
            scales: {
              yAxes: [
                {
                  ticks: {
                    autoSkip: true,
                    maxTicksLimit: 10,
                    beginAtZero: true
                  },
                  gridLines: {
                    display: false
                  }
                }
              ],
              xAxes: [
                {
                  gridLines: {
                    display: false
                  }
                }
              ]
            }
          }}
        />
      </div>
    </div>
  );
};

export default Chart3;

the only difference is dataObject : dataObj.intensity.forecast instead of dataObj.forecast sandBox

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

1 Comment

Cheers mate thats solved a problem thats really got on my nerves!!! Appreciate that

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.