React Chart - Bi-Directional Bar Chart

In a bi-directional bar chart, two sets of bars "grow" from the axis in the center in two opposite directions. This chart can be used to compare two values side by side. This article describes the main steps to implement a bi-directional bar chart using the Chart UI component.

View Demo

Prepare Data

To prepare data for the bi-directional bar chart, convert one of the two sets of bars' values from positive to negative. You can change the sign in each value manually or use the DevExtreme DataSource's map function:

App.js
  • import React from 'react';
  • import Chart from 'devextreme-react/chart';
  • import DataSource from 'devextreme/data/data_source';
  • import ArrayStore from 'devextreme/data/array_store';
  •  
  • const population = [
  • { age: '0-4', male: 3.1, female: 2.9 },
  • { age: '5-9', male: 3.1, female: 3.0 },
  • // ...
  • ];
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  •  
  • this.dataSource = new DataSource({
  • store: new ArrayStore({
  • data: population
  • }),
  • map: (dataItem) => {
  • return {
  • age: dataItem.age,
  • male: dataItem.male,
  • female: -dataItem.female // Changing the values' sign
  • }
  • }
  • });
  • }
  •  
  • render() {
  • return (
  • <Chart ...
  • dataSource={this.dataSource}>
  • </Chart>
  • );
  • }
  • }

Configure the Series

You need two Stacked Bar series for the bi-directional bar chart. The following code declares and binds them to the data source from the previous topic:

App.js
  • import React from 'react';
  • import Chart, {
  • CommonSeriesSettings,
  • Series
  • } from 'devextreme-react/chart';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Chart ... >
  • <CommonSeriesSettings
  • type="stackedbar"
  • argumentField="age"
  • />
  • <Series
  • valueField="male"
  • name="Male"
  • />
  • <Series
  • valueField="female"
  • name="Female"
  • />
  • </Chart>
  • );
  • }
  • }
  •  
  • export default App;
See Also

Rotate the Chart

Bars in the Stacked Bar series are vertical by default. Change them to horizontal by setting the rotated property to true.

App.js
  • import React from 'react';
  • import Chart from 'devextreme-react/chart';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Chart ...
  • rotated={true}>
  • </Chart>
  • );
  • }
  • }
See Also

Customize the Appearance

UI elements like tooltips and axis labels display incorrect data when you convert the chart's values from positive to negative. You can fix this by customizing these elements' text:

App.js
  • import React from 'react';
  • import Chart, {
  • Tooltip,
  • ValueAxis,
  • Label
  • } from 'devextreme-react/chart';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Chart ... >
  • <Tooltip
  • enabled={true}
  • customizeTooltip={customizeTooltip}
  • />
  • <ValueAxis>
  • <Label customizeText={customizeLabel} />
  • </ValueAxis>
  • </Chart>
  • );
  • }
  • }
  •  
  • function customizeTooltip(pointInfo) {
  • return {
  • text: Math.abs(pointInfo.originalValue)
  • };
  • }
  •  
  • function customizeLabel(axisValue) {
  • return `${Math.abs(axisValue.value)}%`;
  • }

You can also adjust the bar's width. See Specify the Bar Width for details.

This article outlined the steps to implement a bi-directional bar chart and provided code examples for each step. Refer to the Bi-Directional Bar Chart demo for the full code.

View Demo