Angular 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.
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:
- <template>
- <DxChart ...
- :data-source="dataSource">
- </DxChart>
- </template>
- <script>
- import DxChart from 'devextreme-vue/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 },
- // ...
- ];
- export default {
- components: {
- DxChart
- },
- data() {
- return {
- dataSource: new DataSource({
- store: new ArrayStore({
- data: population
- }),
- map: (dataItem) => {
- return {
- age: dataItem.age,
- male: dataItem.male,
- female: -dataItem.female // Changing the values' sign
- }
- }
- })
- };
- }
- }
- </script>
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:
- <template>
- <DxChart ... >
- <DxCommonSeriesSettings
- type="stackedbar"
- argument-field="age"
- />
- <DxSeries
- value-field="male"
- name="Male"
- />
- <DxSeries
- value-field="female"
- name="Female"
- />
- </DxChart>
- </template>
- <script>
- import DxChart, {
- DxCommonSeriesSettings,
- DxSeries
- } from 'devextreme-vue/chart';
- export default {
- components: {
- DxChart,
- DxCommonSeriesSettings,
- DxSeries
- }
- }
- </script>
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.
- <template>
- <DxChart ...
- :rotated="true">
- </DxChart>
- </template>
- <script>
- import DxChart from 'devextreme-vue/chart';
- export default {
- components: {
- DxChart
- }
- }
- </script>
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:
- <template>
- <DxChart ... >
- <DxTooltip
- :enabled="true"
- :customize-tooltip="customizeTooltip"
- />
- <DxValueAxis>
- <DxLabel :customize-text="customizeLabel"/>
- </DxValueAxis>
- </DxChart>
- </template>
- <script>
- import DxChart, {
- DxTooltip,
- DxValueAxis,
- DxLabel
- } from 'devextreme-vue/chart';
- export default {
- components: {
- DxChart,
- DxTooltip,
- DxValueAxis,
- DxLabel
- },
- methods: {
- customizeTooltip(pointInfo) {
- return {
- text: Math.abs(pointInfo.originalValue)
- };
- },
- customizeLabel(axisValue) {
- return `${Math.abs(axisValue.value)}%`;
- }
- }
- }
- </script>
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.
If you have technical questions, please create a support ticket in the DevExpress Support Center.