02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Preparing the Chart Data Object

Preparing the Chart Data Object in React for Chart.js

In this lesson, we focus on how to prepare data for visualizations in React using Chart.js. Preparing data correctly ensures reusability, separation of concerns, and scalable design. Here’s how to create and configure your data objects for a bar chart.


Why Externalize Data?

Embedding hardcoded data into React components limits their reusability. By externalizing the data, we keep the components flexible, allowing them to be reused with different datasets across your application.


Steps to Configure Chart Data

  1. Define the Data Object:

    • Chart.js requires a type, data, and options object.
    • Example:
      const chartConfig = {
          type: 'bar',
          data: {
              labels: ['Ben', 'James', 'Mary', 'Sam', 'Gloria'],
              datasets: [
                  {
                      label: 'Fun Level',
                      data: [90, 50, 20, 80, 95],
                      backgroundColor: 'rgba(255, 99, 132, 0.2)',
                      borderColor: 'rgba(255, 99, 132, 1)',
                      borderWidth: 1
                  },
                  {
                      label: 'Smart Level',
                      data: [85, 30, 95, 50, 100],
                      backgroundColor: 'rgba(54, 162, 235, 0.2)',
                      borderColor: 'rgba(54, 162, 235, 1)',
                      borderWidth: 2
                  }
              ]
          },
          options: {}
      };
      
  2. Labels:
    Define labels to name your chart categories (e.g., "Ben," "James"). This will represent the x-axis for bar charts.

  3. Datasets:
    Each dataset represents a series of values. For example, “Fun Level” is one dataset, and “Smart Level” is another. You can customize:

    • label for naming the dataset.
    • data for the corresponding values.
    • backgroundColor, borderColor, and borderWidth for visual styles.

Why Use const for Data?

Using const ensures that your data remains immutable unless explicitly updated. It’s a good practice for maintaining predictable application behavior and catching unintended changes during development.


Passing Data to the Chart Component

When sending data to the Chart component, use JSX’s spread operator for clean syntax:

<Chart {...chartConfig} />

This approach breaks down the type, data, and options into separate properties, making the Chart component easier to manage and test.


Conclusion

By externalizing and configuring data for Chart.js, you set up a flexible and reusable structure for visualizing datasets. In the next lesson, you’ll learn how to connect this data to a functional Chart.js component and render it dynamically in React.

Video Transcript

00:00Before we could start working with our chart js,
00:04which we'll be doing really soon, we need
00:06to prepare data for it.
00:07And for that we have this lecture
00:09where we'll create our data.
00:12Now data generally speaking,
00:14should never live within a react component.
00:16There's a lot of logic to it.
00:18And the biggest one is
00:19that react components should be built for reusability.
00:23And if you bake inside of them actual text,
00:26then they're not really reusable
00:28and that's really the big biggest main
00:30reason why you shouldn't.
00:31So let's jump right into it
00:32and let's see where should you put your data
00:34and what data we need for our component?
00:38Okay, before we start charting,
00:40the next thing we gotta do is we need to prepare the data
00:43that our chart is gonna need.
00:45So to do that, I am definitely not gonna put the data inside
00:48of our chart component.
00:50And I ask you why. Pause the video, think about it. Good.
00:54You thought about it. The answer is, is
00:56because we don't want our components to have hardcoded data
01:00inside of them because I'm creating a component
01:02that is generic, it's called chart.
01:05Now if I put data inside of it, that's it.
01:07Now it's bounded to whatever data that is in it.
01:10So I really don't want to put inside
01:11of react component hardcoded data.
01:14Instead of that, I'm gonna go to our client area
01:17and there I'm gonna create the data itself
01:20and then send it into our chart.
01:22So to do that, I'm gonna go ahead
01:23and what I'm gonna do, I'm gonna create a new let.
01:25And you know what? Let's learn about something new.
01:28Just like there is a let, there is also a cons
01:30and cons assume that assumes
01:32that the content doesn't change.
01:35With that said, a lot of developers, they just choose
01:38to set everything to be a constant.
01:40That way they'll get errors whenever something changes
01:43and only if they really wanted it to change,
01:45then they'll go ahead and move it from a cons to a let.
01:49That way they get, you get a lot more detailed feedback
01:53whenever something is not exactly the way you need it to be.
01:57Now generally speaking, our charts
01:59or our chart js component is going to need three
02:04main data points.
02:05The first one is gonna be a type that is gonna tell it.
02:08What type of chart is it gonna be?
02:10In our case, our first chart we're ever gonna create is
02:13gonna be a bar chart.
02:16The next item that it's going to expect is going to be a
02:21data object, which will hold all the information
02:24that will be inside of that component.
02:26The next thing will be options that will give it our uh,
02:29some customizations that we could do to the library itself.
02:33For now, we're gonna keep the the options empty,
02:36but I'm gonna start off with our data.
02:38And in the data object, the first item is going
02:41to be labels.
02:42Now the labels themselves are gonna be the array
02:45of the data sources that we're gonna have.
02:48For example, in this case, I'm gonna go ahead
02:51and just copy paste an array
02:53that I've pre-configured and pre-created.
02:55And this array is just a list of names, Ben, James, Mary,
02:59Sam, and Gloria.
03:00Those are our subjects that we're going to be using inside
03:04of our bar chart.
03:07The next item that we're gonna have is a dataset.
03:10Now data sets work in a very similar way to
03:14how you would expect them.
03:15They basically list out information.
03:18Now because charts have dimensions, each dataset is going
03:22to be in an object and each dataset will basically contain
03:26only one variable.
03:28So for example, we're gonna set you a label
03:30and let's say that label is gonna be fun.
03:33And for the fun variable, we need to set the data
03:36that corresponds to the items that were previously.
03:39So let's say, uh, Ben is fun.
03:42From between zero to a hundred, Ben is really fun. James.
03:46Nah, not that fun. Mary is really not fun. Sam is a blast.
03:51And Gloria, not that fun.
03:53Although this seems a little bit sexist
03:55because the girls are not fun and the boys are really fun.
03:57So how about we make Sam completely not fun?
04:01And Gloria is a blast.
04:03Okay, so we have here a first data state set
04:06and we could create as many data states
04:08as we really need each data set.
04:10In this case, because we're creating a bar chart would
04:13represent one bar associated to this, to these items.
04:17So if I go ahead and I create another data set there,
04:20and let's say this time around instead of fun,
04:22we'll set smart.
04:24And let's say that I'm really smart.
04:27And although it's a myth, James is a complete fool.
04:31Mary is a genius, Sam is a genius,
04:34and Gloria is beyond genius.
04:38Okay, so these are our data sources
04:40and we could have even more than that.
04:41It's really up to you.
04:43Um, last but not least, really, let's talk about visuals.
04:47So we could go ahead
04:48and also as properties of our chart go ahead
04:51and add for example, a background color.
04:54Now this background color could be an array,
04:56but I don't encourage you to do so
04:58because we really want fun to be in one color.
05:01So I'm gonna go ahead and I'll just set you a value,
05:03which could be any value that is acceptable in CS S3.
05:07In this case, I'm just gonna set it to be red
05:09with a 20% opacity.
05:15Excuse me. Excuse me.
05:21Okay, so I just set it to be red with a 20% capacity
05:25and I'm gonna go to my border width.
05:28And again, these are just properties
05:29that you could find in the description of charge s of things
05:33that it expects.
05:34And lemme just go ahead here and just duplicate this.
05:36And for our smart variable, I'm just gonna go ahead
05:39and set here the color to be, uh, let's say blue.
05:44So it's gonna be blue at 20%.
05:47And again, border width,
05:48I'm fine with a border width of one.
05:49Or how about let's set a border width of two.
05:51And by the way, I could set a border color as well.
05:54So for border color, I could
05:56Just go ahead here and say, okay,
05:59your border color will be basically blue as well,
06:02but instead of that it's gonna be um, 40%.
06:06Okay. So with that said,
06:07we're not really ready yet to test things out.
06:09We don't have it, uh, anything in our options.
06:11But I do want to send this information into our chart.
06:14Now, the easiest way to send data,
06:17especially if I wanna break it apart, what if I wanted
06:19to send the type the data and the options?
06:23Historically we saw that you could just type data.
06:25Oh, let me just break a few lines here
06:27so you guys can see better.
06:29And then I would basically do an equal open
06:32and close around a curly bracket.
06:34And then I could just go
06:35and say, Hey, I wanna send the data,
06:37but what if I wanted to send the option?
06:38So I would have to say it. Data options and data options.
06:43A big pain in old style JavaScript.
06:45This is not even old style. It's like
06:47getting new type of JavaScript.
06:49Now in JSX, the way you do it,
06:51you could literally just send all of that data object
06:54and what's actually gonna happen inside
06:56of the charge component,
06:57it's actually gonna retrieve the type the data
07:01and the options as separate items that belong
07:04to the props command.
07:05So it won't be this do props.data.
07:08Instead of that you would have this props type
07:12da data options.
07:14So you don't, you basically have access
07:16to all them much more easily without needing
07:18to type data within the chart.
07:19Now we're not ready yet 'cause we have
07:21to actually implement the chart on the other side,
07:23which is exactly what we're gonna do in the next lecture.
07:28In this lecture we saw how to configure our data
07:31and where we should place it.
07:33And the next one we'll continue down the path
07:35of actually connecting it to a real chart using chart js.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!