Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
ReactJS populating NVD3Chart with data from an API call

I cannot get my data to populate my line chart. The data from the API call comes back fine but when I try to pass that data into the chart it is of length 0. So I assume there is some issue with the response got being called at the right time? But I have tried so many times to sort this and nothing ever works?

import React from 'react';
import NVD3Chart from 'react-nvd3';
import SamplePage from './SamplePage';

function getData(){
    const alpha = require('account')({ key: 'xxxxxxxxxxxx' });

    var array = [];

    alpha.data.intraday(`msft`).then((data) => {
        const polished = alpha.util.polish(data);
        {Object.keys(polished.data).map((key) => (
            array.push(polished.data[key].open)
            ))}
      });

    return array;
}

function getDatum() {    
    
    let dataArray = getData();

    let newArray = [];

      for (let index = 0; index < dataArray.length; index++) {
          const element = dataArray[index];
          newArray.push({
            'x': index,
            'y': parseFloat(element)
          })
      }
      
     return [
         {
             data: newArray,
             key: 'OpenPrice',
             color: '#A389D4'
         }
     ];
     
}



class LineChart extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            DataisLoaded: false,
            data:[]
        };

    }


    componentDidMount() {
        this.state.data = getDatum();
        this.setState(
           {
            DataisLoaded: true,
            data: this.state.data
            }
        )
        
    }

    render() {

        const { DataisLoaded } = this.state;
        if (!DataisLoaded) return <div>
        <h1> Please wait.... </h1> </div>;
            
        return (
            <div>
                {
                    React.createElement(NVD3Chart, {
                        xAxis: {
                            tickFormat: function(d){ return d; },
                            axisLabel: 'Time (ms)'
                        },
                        yAxis: {
                            axisLabel: 'Voltage (v)',
                            tickFormat: function(d) {return parseFloat(d).toFixed(2); }
                        },
                        type:'lineChart',
                        datum: this.state.data,
                        x: 'x',
                        y: 'y',
                        height: 300,
                        renderEnd: function(){
                            console.log('renderEnd');
                        }
                    })
                }
            </div>
        )
    }
}

export default LineChart;
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!