JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  • Analysis
  • Quick Aside
  • One Last Thing
  • Make It Look Nice!
  • Challenge
  1. Part 8: Apps
  2. 8.0 - Bitcoin Api Application

8.4 - Bitcoin API Fetching Data

Hard-coded graphs are cool, but let's now add the bitcoin data to see it populate in the chart.

We first need to bring in our props. In our component, we need to add our constructor:

  constructor(props) {
    super(props);
  }

We are now including our props from our Bitcoin.js file. This will bring the data from our fetch() that we will use for our graph.

In a componentDidMount() lifecycle method, we need to bring in our data array from the API. Inside your method, add const unsortedData = this.props.data;. This pulls the data from the state we set in Bitcoin.js, hence the this.props, which is pointing to the the data from it's parent component.

Under that, we need to declare two variables, dates and payout and set both to empty arrays. Next, build a for in loop with let item in unsortedData within the parentheses. Inside the loop, push item to your dates array, and push unsortedData[item] to your payout array.

Lastly, under your for in loop, set the state of dates and payout to dates and payout, respectively.

To make this work, we need to declare the state inside our constructor. Add this.state ={ dates: [], payout: [] } under super(props);.

Your code so far should look like this:

import React, { Component } from 'react';
import Chart from 'chart.js';

export default class LineChart extends Component {
  constructor(props) {
    super(props);
    this.state ={
      dates: [],
      payout: []
    }
  }

  componentDidMount() {
    const unsortedData = this.props.data;
    let dates = [];
    let payout = [];
    for (let item in unsortedData) {
      dates.push(item)
      payout.push(unsortedData[item])
    }
    this.setState({
      dates: dates,
      payout: payout
    })
  }

  componentDidMount() {
    var chartContext = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(chartContext, {
      type: 'line',
      data: {
        labels: ['first','second','third','fourth'],
        datasets: [{
          data: [1,1,5,0],
          backgroundColor: 'rgba(54, 162, 235, 0.2)', 
          borderColor: 'rgba(54, 162, 235, 1)',
          borderWidth: 1
        }]
      },
      options: {
        scales: {
          yAxis: [{
            ticks: {
              beginAtZero: true
            }
          }]
        }
      }
    });
  }

  render() {
    return (
      <canvas id='myChart'></canvas>
    );
  }
};

Analysis

Our state has two values, dates and payout. Both are empty arrays. However, why do you also set variables with the same name and datatype inside our componentDidMount()? You NEVER want to push data directly to your state! You only want to change your state with setState().

So, when we declare the two variables and then run a for in loop, we push that data into those local arrays in which we then set the state with these newly filled variables into the arrays in our state.

Now, we need to add it to our graph so we can see the data. Replace the array of labels from ['first','second','third','fourth'] to this.state.dates. Next, replace the array of data from [1,1,5,0] to this.state.payout.

Quick Aside

What do we notice with both labels and dates? They are both arrays! This is why, declared the variables dates and payout, we set them as empty arrays. We want to bring in the correct data type!

One Last Thing

This should work, right? Nope. Sorry. There is one last thing we need to do in order to see this data. In your Bitcoin.js file, inside your JSX, you should see a ternary with the <LineChart/> tag. We need to add our data to that tag. Inside that tag, add this: data={ this.state.data }. This pulls the data from our state into this tag and then populates our chart! We should then see this:

Make It Look Nice!

There are a couple of things that look a little off that we should now fix!

First, the dates look bad! Let's fix that. We are going to use something called Moment to adjust the dates so they look like this 'Mar 20' instead of our ugly '2018-03-20'.

In your terminal where you are running npm start, hit control-c to stop running the app and then type npm install --save moment. Next, import it into your LineChart.js file: import moment from 'moment';.

Now we need to actually use it! In your for in loop, we are going add let bitcoinDates = moment(item).format('MMM DD'); above dates.push(item), and then replace dates.push(item) with dates.push(bitcoinDates) Your for in loop should now look like this:

for (let item in unsortedData) {
  let bitcoinDates = moment(item).format('MMM DD');
  dates.push(bitcoinDates)
  payout.push(unsortedData[item])
}

Your chart should now look like this (but how do we get rid of that dumb legend on the top?):

Chart.js has a number features built in that we can manipulate. Under the actual chart, you have an object beginning with options. Adjust the object to look like this:

options: {
  legend: {
    display: false
  },
  scales: {
    yAxis: [{
      ticks: {
        beginAtZero: true
      }
    }]
  }
}

Your legend should now be gone.

Lastly, let's change the line chart itself. It is currently blue, but this is Eleven Fifty, so it should be red! We'll use one of the "official" colors of Eleven Fifty. Let's use that red. Copy the hex color #d9514e and replace the two rgba colors of the chart so that both are that red color. If you run it, you should see a solid red graph, not a line graph with a pleasant light red underneath. So, for backgroundColor, add 80 at the end of the hex (it should now have 8 digits, instead of 6). The 80 is the hex color's code for 50% opacity.

And, finally, let's change the borderWidth from 1 to 2. You should now see this:

Challenge

The dates look fine now, but what do we do about the payout? The numbers are not formatted to handled currency. The tricky part is that data array of Chart.js only takes an array of numbers; many conversions to currency builds it out as a string. Can you fix it to handled the values but display currency rather than simply numbers?

Previous8.3 - Bitcoin API Line ChartNext8.5 - Bitcoin API Info Box

Last updated 6 years ago

picture
picture
picture