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

Exploring React Chart Properties and Dynamic Options

Exploring React Chart Properties and Dynamic Options

In this tutorial, we focus on creating reusable React components by extracting hardcoded data and passing it as properties using props. Additionally, we explore how to make components dynamic with Chart.js options.


Key Concepts Covered

  1. Using Props for Data Management:

    • Always strive to extract hardcoded data from components and pass it via props.
    • Use state only for data that changes during the component's lifecycle.
  2. Chart.js Options:

    • The type property allows switching between chart types like bar, horizontalBar, or line with a single change.
    • The responsive property ensures the chart adapts to its container size dynamically.
    • Setting responsive: false allows precise control over the chart's width and height.
  3. Dynamic Properties:

    • Pass width and height values as props for flexible rendering.
    • Use JavaScript to define defaults if properties are not set, ensuring adaptability.
  4. Configuring Canvas Attributes:

    • Use canvasProps objects to centralize configurations for width, height, and other dynamic settings.
    • React intelligently ignores undefined properties, simplifying the logic.

Step-by-Step Implementation

1. Extract Data to Props

Instead of hardcoding chart data, pass it dynamically using props. This approach enhances reusability:

<ChartComponent 
    type="bar" 
    data={data} 
    options={options} 
    width={800} 
    height={400} 
/>

2. Enable Dynamic Chart Types

Switch between chart types easily by modifying the type prop:

const options = {
    type: 'horizontalBar', // Change to 'line', 'bar', etc.
    responsive: true,
};

3. Control Responsiveness

  • To enable responsiveness, set responsive: true.
  • To use fixed dimensions, set responsive: false and define width and height.
const options = {
    responsive: false,
    width: 400,
    height: 400,
};

4. Dynamic Width and Height with Default Values

Implement a fallback mechanism for props:

const canvasProps = {
    width: props.width || 400,
    height: props.height || 400,
};

Pass these properties dynamically to the <canvas> element:

<canvas {...canvasProps}></canvas>

Conclusion

This tutorial has equipped you with strategies for building reusable React components and dynamically customizing Chart.js visualizations. By using props and options effectively, your components will be more modular and easier to integrate.

Video Transcript

00:00As we mentioned a few times before, your goal when
00:03You're creating React components is
00:05to extract all the data from the component
00:08and send it in through properties and only use state
00:11whenever it's a property that is intended
00:13to change in the lifecycle of that component.
00:16So we're gonna take a deeper look into this props
00:19and with that, lean all of our data on
00:22External sources And remove all the hardcoded data
00:26that needs to be updateable
00:28Out of The component
00:30By the process. We're creating a
00:32Reusable component.
00:34Alright, let's continue visualizing our data
00:36and we're, we really have a good familiarity
00:40with our component,
00:41although there's one thing we haven't really played with,
00:44with is, which is the options.
00:46So to do that, I'm gonna go into my client
00:48and I wanna show you a few different options.
00:50You know what, even before that, for the type of data
00:52that we prepared using our A to chart,
00:56we could use a bar chart, which is
00:58what we've been doing so far.
00:59But we could have also used if we wanted to a horizontal,
01:06a horizontal bar.
01:07And literally all we have
01:08to do now is just change it to horizontal bar.
01:10And if we go back into our coat, we'll see
01:12that now our bars are horizontal.
01:15Moreover than that, we could also also, let me just go ahead
01:18and just save this so you guys can look at it later.
01:21You could oto place it to be aligned.
01:23Now there's a lot of different types of
01:25that you could put there, but the type depends on the
01:27data formats that are needed.
01:29So we're gonna take a look at different types
01:31a little bit later, but I just want to show you
01:34that those three, so you have your three different types
01:36that you could use very, very easily.
01:39Now I want to go back and let me just get rid of line
01:42and let's just put it back to our bar
01:43'cause it seemed the cleanest for our purposes right now.
01:47And I want to go into the options
01:48and show you what our options and how to work with them.
01:51So I'm not gonna show you every single option.
01:53There is a lot of them.
01:54And you'll find in our documentation a link to a list
01:57of all the options that you could use and I or encourage you
02:01and implore you to just scan through them so you're familiar
02:04with the different options that you could control your react
02:08chart JS application component.
02:11We've just, that we've just created right now.
02:13Okay, so response responsive. True.
02:17When responsive is true,
02:18what it actually means is the default, which basically means
02:22that it's gonna try to get the component to be the largest
02:25that it cut can inside of the current bounds that is in.
02:29So for example, when we were talking previously in our chart
02:33and we've created these hard set values of 802 50
02:37to our diviv, if I've got, if I would get rid of those
02:41and I would just set it
02:42as a canvas without even setting a width
02:45or a height, what will happen, it would automatically try
02:48to capture as much of the screen as it can.
02:51Basically taking over a really large portion,
02:54well the whole screen, it's responsive.
02:57Now what we did is we made it responsive.
03:00You could say within the bounds of this box that we told it
03:03that it could live within.
03:05Now this box is hardcoded,
03:07which really makes it not really responsive.
03:09But in reality, if we just said it this way
03:12and let's say we didn't want it to be responsive,
03:15if I just go ahead and say this, I'm just gonna go ahead
03:17and set here a width of 400
03:21and I'm gonna set a height of 400.
03:25So this is gonna be my height that I and width of my canvas.
03:28Now if I go into my application
03:29and take a look at it, it's actually
03:31gonna ignore me completely.
03:32That is because we are responsive
03:35and once I set responsive to be false,
03:38it will respect the size
03:39and bounds of the component size
03:42that it is when I set it in.
03:44So it's gonna smash it in
03:45and set it to whatever size I set my component to be.
03:48And if that's the size that you want
03:49and you want it hard set, then you could just go ahead
03:52and set the actual width
03:53and height of your canvas element in advance
03:57and then go inside into your component
03:59and set it up thereafter.
04:01And by the way, one more thing that we could do here is oto.
04:06If we wanted to set our chart,
04:07if we're really gonna set this width
04:09and height, we could also set our width
04:11and height as a property and just set it to be 400
04:15and we'll set our height to be 400.
04:17But if we do that, the one thing that we're gonna have
04:20to do is when we go inside of our chart itself,
04:23one thing is, if you recall, we took out the type data
04:26and options, it doesn't really matter
04:28that there's more properties in there,
04:29it's not grabbing them.
04:32But inside of our canvas itself here, we could just go ahead
04:35and just set here a, a value instead
04:37of putting it to be hard coded.
04:38I could just say this, do props with,
04:44let me just word wrap here so we could see better.
04:47And same applies.
04:48I could do just this, do that pla that height
04:51and just set here the height.
04:54And now if I go ahead
04:55and let me just put here a different value.
04:56And let's say we set the width of 800 and the height of 300.
05:01If we go ahead and save this
05:03and run it, it should pass through and and react.
05:06So that way our component is gonna be more dynamic
05:09as it should be because in the end of the day, we don't want
05:12to have here actually any size
05:15that is configured in advance.
05:17Now one more thing that we could set if we wanted to
05:20but we don't is to put here maybe not
05:23to set a width if a value is not sent.
05:25And put here a little bit more extra logic
05:28that would define if it's there or not.
05:30But how about we do that? 'cause it's a,
05:31it would be a really great exercise to see
05:33how do you do things in JSX
05:35and how do you do things in ES six.
05:37So for this matter, let's say that there is a property
05:40of width and height, maybe there isn't.
05:42And if there isn't, then we don't want to put anything.
05:45So in this case,
05:46what we'd wanna do is we could set here basically a if,
05:49and in our if, and by the way, let me just set here a let,
05:52and I'm gonna set this, let to be um, canvas props.
05:56And for our canvas props,
05:58I'm just gonna set it to be an object.
06:00And I'm, I could literally,
06:02what I could do is just set my width to be that object
06:06that's gonna be that value.
06:08And for the height, I'm gonna set the, this height.
06:11So I'm literally just creating an object that has a width
06:15and a height properties
06:17and those are gonna be my canvas properties.
06:19Now that I have canvas properties, I don't have
06:21to literally put them directly inside,
06:23but instead of that, I'll just put here 1, 2, 3
06:26and I'm gonna put here the canvas prompts literally
06:29sending in the properties.
06:30Now the beauty of this is that if the value is undefined,
06:33react is smart enough to basically react to it.
06:37So if I go ahead and save this
06:38and I look back into my application, first of all,
06:42if the values of the are there
06:43and we look into our inspector
06:45and we look to find our canvas element, we'll see
06:48that it has a height and it has a width.
06:50But if I go ahead and I change it,
06:52and let's say I don't send a height, I only send a width.
06:56Let me say this and let's see what happens.
06:58And we take a look, we'll see that.
07:00Now our canvas element will only have a width,
07:03but it won't have a height pre-configured,
07:06although it will have a style that is pre,
07:09that is configured from the platform, the, from the
07:13library itself, making sure
07:15that there is at least some width something going on there.
07:18But that's it. That is basically
07:19how you would create it in a way
07:21that would be completely dynamic
07:22where you could control if you're putting a, a width
07:25and height, and you could even decide, for example,
07:27if the valley, if there is no width,
07:30then let's say if there's a width
07:32or we could put inside of here,
07:34literally we could put here a width
07:36or we could put the value of 400.
07:41And in that situation, I could set here,
07:43and this is really, if you wanna make your app like really
07:45smart and really responsive, you could do that very easily.
07:49But there is also default properties that you could put in,
07:51which we'll talk about in a future lecture
07:53where you could actually set default properties if the
07:56properties are not filled out.
07:58Although in our case we kinda like the idea
08:00of if it's not set, then it's not set,
08:02which is fine, maybe we don't want it set.
08:04And that's basically how you would configure
08:07and set up your chart.
08:08So we saw how to use one option,
08:11but there's more options obviously.
08:12So I'm, we're gonna explore more of them in the next lecture
08:15and get really familiar with the different response
08:18with the different options that are available
08:20to charts in general.
08:23In this lecture, we created a reusable component
08:26by extracting all the data out
08:28and using props instead in the next lecture,
08:31we'll continue down our path
08:33as we explore some final options in 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!