React Chart - Drill-Down Chart
A drill-down chart visualizes data on several hierarchical levels. Data is usually generalized on the first level, but it becomes more detailed with each level. This article describes the main steps you need to take to implement a drill-down chart using the Chart UI component.
Provide Data
Although a drill-down chart visualizes hierarchical data, the data source should be an array of plain objects, for example:
- var population = [
- { arg: "North America", val: 493603615, parentID: "" },
- { arg: "South America", val: 331126555, parentID: "" },
- { arg: "United States", val: 325310275, parentID: "North America" },
- { arg: "Mexico", val: 121005815, parentID: "North America" },
- { arg: "Canada", val: 36048521, parentID: "North America" },
- { arg: "Cuba", val: 11239004, parentID: "North America" },
- { arg: "Brazil", val: 205737996, parentID: "South America" },
- { arg: "Colombia", val: 48400388, parentID: "South America" },
- { arg: "Venezuela", val: 30761000, parentID: "South America" },
- { arg: "Peru", val: 28220764, parentID: "South America" }
- ];
The main idea is to filter the data source by the parentID for different drill-down views. You can create a function that does the filtering:
- import React, { useState } from 'react';
- import Chart, {
- Series
- } from 'devextreme-react/chart';
- type DataItem = {
- arg: string;
- val: number;
- parentID: string;
- };
- const population: DataItem[] = [
- // ...
- ];
- function filterData(name: string): DataItem[] {
- return population.filter((item) => item.parentID === name);
- }
- function App() {
- const [dataSource] = useState<DataItem[]>(() => filterData(''));
- return (
- <Chart ...
- dataSource={dataSource}>
- <Series
- argumentField="arg"
- valueField="val"
- type="bar"
- />
- </Chart>
- );
- }
- export default App;
... or employ the DevExtreme DataSource object that provides an API for filtering:
- import React from 'react';
- import Chart, {
- Series
- } from 'devextreme-react/chart';
- import DataSource from 'devextreme/data/data_source';
- type DataItem = {
- arg: string;
- val: number;
- parentID: string;
- };
- const population: DataItem[] = [
- // ...
- ];
- const dxDataSource = new DataSource<DataItem>({
- store: {
- type: 'array',
- data: population
- },
- filter: ['parentID', '=', '']
- });
- function App() {
- return (
- <Chart ...
- dataSource={dxDataSource}>
- <Series
- argumentField="arg"
- valueField="val"
- type="bar"
- />
- </Chart>
- );
- }
- export default App;
Implement View Navigation
To navigate from the first to the second view, filter data by a different parentID in the Chart's onPointClick event handler. To navigate back, add the Button UI component and reset the filter in the onClick event handler. Distinguish between levels using the isFirstLevel flag.
- import React, { useCallback, useState } from 'react';
- import Chart, {
- Series,
- type ChartTypes
- } from 'devextreme-react/chart';
- import Button from 'devextreme-react/button';
- import service from './data';
- function App() {
- const [isFirstLevel, setIsFirstLevel] = useState(true);
- const [dataSource, setDataSource] = useState(() => service.filterData(''));
- const onPointClick = useCallback(({ target }: ChartTypes.PointClickEvent) => {
- if (isFirstLevel) {
- setIsFirstLevel(false);
- setDataSource(service.filterData(target.originalArgument as string));
- }
- }, [isFirstLevel]);
- const onButtonClick = useCallback(() => {
- if (!isFirstLevel) {
- setIsFirstLevel(true);
- setDataSource(service.filterData(''));
- }
- }, [isFirstLevel]);
- return (
- <React.Fragment>
- <Chart ...
- dataSource={dataSource}
- onPointClick={onPointClick}>
- <Series
- argumentField="arg"
- valueField="val"
- type="bar"
- />
- </Chart>
- <Button className="button-container"
- text="Back"
- icon="chevronleft"
- visible={!isFirstLevel}
- onClick={onButtonClick}
- />
- </React.Fragment>
- );
- }
- export default App;
- .button-container {
- text-align: center;
- height: 40px;
- position: absolute;
- top: 7px;
- left: 0px;
- }
The following code shows how to implement navigation when using the DevExtreme DataSource:
- import React, { useCallback, useState } from 'react';
- import Chart, {
- Series,
- type ChartTypes
- } from 'devextreme-react/chart';
- import Button from 'devextreme-react/button';
- import DataSource from 'devextreme/data/data_source';
- type DataItem = {
- arg: string;
- val: number;
- parentID: string;
- };
- const population: DataItem[] = [
- // ...
- ];
- const dxDataSource = new DataSource<DataItem>({
- store: {
- type: 'array',
- data: population
- },
- filter: ['parentID', '=', '']
- });
- function App() {
- const [isFirstLevel, setIsFirstLevel] = useState(true);
- const onPointClick = useCallback(({ target }: ChartTypes.PointClickEvent) => {
- if (isFirstLevel) {
- setIsFirstLevel(false);
- dxDataSource.filter(['parentID', '=', target.originalArgument]);
- dxDataSource.load();
- }
- }, [isFirstLevel]);
- const onButtonClick = useCallback(() => {
- if (!isFirstLevel) {
- setIsFirstLevel(true);
- dxDataSource.filter(['parentID', '=', '']);
- dxDataSource.load();
- }
- }, [isFirstLevel]);
- return (
- <React.Fragment>
- <Chart ...
- dataSource={dxDataSource}
- onPointClick={onPointClick}>
- <Series
- argumentField="arg"
- valueField="val"
- type="bar"
- />
- </Chart>
- <Button className="button-container"
- text="Back"
- icon="chevronleft"
- visible={!isFirstLevel}
- onClick={onButtonClick}
- />
- </React.Fragment>
- );
- }
- export default App;
- .button-container {
- text-align: center;
- height: 40px;
- position: absolute;
- top: 7px;
- left: 0px;
- }
Customize the Appearance
The Chart provides the customizePoint and customizeLabel functions specifically for changing individual point and label properties. Any other UI component properties can be changed in the Chart's onPointClick event handler, but remember to change them back in the Button's onClick event handler.
- import React, { useCallback, useState } from 'react';
- import Chart, { type ChartTypes } from 'devextreme-react/chart';
- import Button from 'devextreme-react/button';
- function App() {
- const [isFirstLevel, setIsFirstLevel] = useState(true);
- const [currentTitle, setCurrentTitle] = useState('The Most Populated Countries by Continents');
- const onPointClick = useCallback(({ target }: ChartTypes.PointClickEvent) => {
- if (isFirstLevel) {
- // ...
- setCurrentTitle(`The Most Populated Countries in ${target.originalArgument}`);
- }
- }, [isFirstLevel]);
- const onButtonClick = useCallback(() => {
- if (!isFirstLevel) {
- // ...
- setCurrentTitle('The Most Populated Countries by Continents');
- }
- }, [isFirstLevel]);
- return (
- <React.Fragment>
- <Chart ...
- title={currentTitle}
- onPointClick={onPointClick}>
- </Chart>
- <Button ...
- onClick={onButtonClick}
- />
- </React.Fragment>
- );
- }
- export default App;
This article outlined the steps to implement a drill-down chart and provided code examples for each step. For the full code, refer to the Drill-Down Chart demo.