Data Visualization for React Developers

React and Radial Chart Exercise & Solution

Shirley Wu

Shirley Wu

Data Sketches
Data Visualization for React Developers

Check out a free preview of the full Data Visualization for React Developers course

The "React and Radial Chart Exercise & Solution" Lesson is part of the full, Data Visualization for React Developers course featured in this preview video. Here's what you'd learn in this lesson:

Shirley live codes how to implement a radial chart within a React project. A different way of implementing the SVG in the React project using componentDidUpdate()/componentDidMount() is also introduced.


Transcript from the "React and Radial Chart Exercise & Solution" Lesson

>> Shirley Wu: Let's do the radial chart together, because it's the same concept. So, let's just really quickly grab the data for the radial chart.
>> Shirley Wu: Then, in this case, I have to go to the starter code.
>> Shirley Wu: And then I already grabbed the data for the radial chart, just copy and paste that in and then instead of returning let me call that slices is equal to that.

Return slices as state, make sure that slices diff is defined in the initial state as an empty array, and then,
>> Shirley Wu: Loop through those slices, this.state.slices and for each one of them create a path element, and use d.path as the d attribute, and use fill to color them in.

And then,
>> Shirley Wu: Yay, so it gets drawn right here. So the bars are getting drawn right here and you might be like why is it getting drawn right there? And that's because when we did the arcs generator, the arcs generator assumes the center to be at 0,0. So what we need to do is do an SVG transformation.

So wrap that around.
>> Shirley Wu: In a group element. And so group elements are the only SVG elements that can have children. So with HTML, you can have children in pretty much almost every element, right? But with SVG, the only elements that can have children nested inside of it are group elements.

And then you want to put SVG elements within the group elements, so that you can transform it to get it all together. So, in this case, I want to say that everything within this group element, please move it.
>> Shirley Wu: So please move it, and moving an SVG is called translating, so move it by half of the width.

And actually, if you wanna see. So that moves it to the center and then please move it by half of the height, and that will move it down to where it should be. And that's all it takes to draw the slices in React. And so this is actually probably the easiest part of this whole workshop.

I want to talk really briefly about in this exercise, we did all of the data calculations and get derived state from props. But I did mention that there is other options and so this was one of the suggestions I've gotten of putting the calculations in both componentDidMount, and componentDidUpdate.

And then so in this particular example, we have the data calculation in a separate function, and after we calculate all of the data, we set that as state. And then in componentDidUpdate, we check to make sure that something has changed, so we use some sort of a prop to make sure that something has changed.

Only if something has changed with the data do we recalculate the data. In this one, just to make sure that the component is already rendered before we calculate the data. So this is another option that we can do. We just didn't do it in the example cuz it just takes a few more lines.

But I think this is a really good suggestion also. As long as there's some easy way for you to figure out if the data has updated or not, so this is the main part. So this is the architecture for React rendering.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now