SUSI Skill CMS has a dashboard option available at the /dashboard route which displays several data for the logged in user as the skills created by the user and the ratings the user has provided to all the skills, since we have a skill usage section available on all skill pages which depicts the skill usage count for the past week in a line chart. Skill creators didn’t have a functionality to see the skill usage distribution on their skills which can provide some useful insight like how some of the skills they created are performing in comparison to the others so I developed a ‘My Analytics’ section in the dashboard page and displayed the skill usage distribution in the form of pie chart among the skills created by the logged in users.
About the API
An API is developed at the server so from the client we call this API to fetch data from the server and plug this data into the chart we wish to render.
Email ID which is taken from the cookies since it is stored there once the user logs in.
Sample API call :
Fetching the data for the component
We first create a separate My Analytics component and require it in the dashboard and make an AJAX call to the appropriate endpoint inside a loadSkillsUsage function which is called inside the componentDidMount hook after which the server returns raw data in the form of JSON. We then pass the response into a saveUsageData function to parse the data for our use and save it to the application state.
Set the application state with the received data which the pie chart component will use as it’s data source.
Implementing the UI
We create a separate ‘My Analytics’ component which is imported into the dashboard component to make the code cleaner and manageable. So inside the My analytics component, we fetch the data from the server as depicted above and after that, we render the pie chart component after importing from the recharts library.
Importing the pie chart components from the recharts library.
Rendering the pie chart component while supplying appropriate props most important of which is the data prop which will be used in the chart and that data is available in the application state as saved earlier. We also have other styling props and a function which is triggered when hovering over cells of the pie chart to represent the data of the hovered cell. We also supply the appropriate nameKey and dataKey props as per the data format available in the state.
Configuring color for each Cell in the pie so it looks more interactive and we have distinguished colors for all devices.
Rendering the Pie only when data is available in props so we don’t end up rendering a blank chart which obviously won’t look good.
- Swizec Teller, Rendering a pie chart using react and d3, https://dzone.com/articles/how-to-make-a-piechart-using-react-and-d3
- Pie chart example from recharts, http://recharts.org/#/en-US/examples/CustomActiveShapePieChart