TL;DR: Dual-axis charts help compare two datasets with different value ranges on the same chart, making data relationships clearer. Learn when to use them and how to create one using our React Charts component, featuring Texas weather data for a practical example.
Welcome to the “Weekly Data Visualization” blog series!
Dual-axis charts are powerful visualization tools that allow you to compare two datasets with different value ranges on the same chart, providing deeper insights into data relationships.
In the world of data visualization, conveying complex relationships effectively is crucial. Dual-axis charts provide a powerful way to compare two data sets with a common category but different value ranges. In this blog, we’ll explore when and how to use dual-axis charts effectively, including practical use cases, best practices, and implementation using the Syncfusion React Charts.
Here, we’ll create a dual-axis chart using Texas weather data, featuring rainfall and temperature.
A dual-axis chart, also known as a two-axis chart or a combo chart, allows users to plot two different sets of data on the same chart using two vertical axes. Typically, one data series is displayed on the primary Y-axis, while the other is mapped to a secondary Y-axis, making analyzing correlations between different metrics easier.
Dual-axis charts are useful when:
While dual-axis charts are powerful, they should be used cautiously to avoid misleading interpretations due to scale differences.
Follow these steps to create a dual-axis chart using our React Charts:
To begin with, we need to structure our data properly. Here, we’ll use the data from the World Climate web page for Texas’s average monthly rainfall (in mm) and temperature (in °C).
export let weatherData: Object[] = [ { month: 'Jan', rainfall: 86.7, temperature: 14.6 }, { month: 'Feb', rainfall: 67.9, temperature: 15.8 }, { month: 'Mar', rainfall: 56.3, temperature: 19.2 }, { month: 'Apr', rainfall: 64.7, temperature: 23.0 }, { month: 'May', rainfall: 89.1, temperature: 26.5 }, { month: 'Jun', rainfall: 109.3, temperature: 29.4 }, { month: 'Jul', rainfall: 94.5, temperature: 30.7 }, { month: 'Aug', rainfall: 114.4, temperature: 30.9 }, { month: 'Sep', rainfall: 136.5, temperature: 29.1 }, { month: 'Oct', rainfall: 74.3, temperature: 25.2 }, { month: 'Nov', rainfall: 86.3, temperature: 20.7 }, { month: 'Dec', rainfall: 87.7, temperature: 16.6 }, ];
Let’s configure the React Charts component to create a stunning dual-axis chart. It provides multiple customization options, enabling us to fine-tune every aspect of the chart.
Here, the primary X-axis represents the common category (Months), and the primary Y-axis is assigned to one of the data series. Here, we configure the primary Y-axis for rainfall measurements.
import * as React from 'react'; import { useRef } from 'react'; import { ChartComponent, Legend, SeriesCollectionDirective, AxesDirective, AxisDirective, SeriesDirective, Inject, ColumnSeries, Category, Tooltip, SplineSeries, } from '@syncfusion/ej2-react-charts'; <ChartComponent id="charts" style={{ textAlign: 'center' }} primaryXAxis={{ valueType: 'Category', title: 'Months', }} primaryYAxis={{ lineStyle: { width: 0 }, maximum: 180, title: 'Rainfall (mm)', labelFormat: '{value} mm', }} > <Inject services={[ColumnSeries, Category, Tooltip, Legend, SplineSeries]} /> </ChartComponent>
To visualize temperature data, we introduce a secondary Y-axis using the <AxesDirective> component. This secondary Y-axis will be positioned on the right side of the chart.
When adding a secondary axis to a chart, it does not automatically associate with any data series. To link a series to the secondary axis, we should explicitly set the YAxisName property in the series configuration. The value assigned to YAxisName must match the name property of the secondary axis to establish the connection.
Refer to the following code example.
<AxesDirective> <AxisDirective name="yAxis" opposedPosition={true} // Moves axis to the right side title="Temperature (°C)" labelFormat="{value}°C" /> </AxesDirective>
Now, we’ll bind the rainfall and temperature data to separate chart series, associating them with their respective Y-axes.
The Primary Y-axis is automatically assigned based on the xName and yName properties in the series.
<SeriesCollectionDirective> <SeriesDirective dataSource={weatherData} xName="month" yName="rainfall" type="Column" name="Rainfall" /> </SeriesCollectionDirective>
Refer to the following image.
When we need a secondary axis, we must manually map it to a specific series using the YAxisName property.
Refer to the following code example.
<SeriesCollectionDirective> <SeriesDirective dataSource={weatherData} xName="month" yName="temperature" type="Spline" name="Temperature" yAxisName="yAxis" marker={{ visible: true, width: 6, height: 6 }} /> </SeriesCollectionDirective>
Refer to the following image.
To improve the appearance of the dual-axis chart, we can:
Refer to the following code example to add a title and subtitle to the React dual-axis chart.
<ChartComponent title="Average Monthly Weather Data for Texas" subTitle="Source: WorldClimate (www.worldclimate.com)" titleStyle={{ textAlignment: 'Near', }} />
Refer to the following image.
For more details, refer to the React dual-axis chart Stackblitz demo.
Thanks for reading! Dual-axis charts are a valuable visualization tool when comparing two related datasets with different units or scales. By using Syncfusion React Charts, we can create an effective and engaging dual-axis chart to analyze trends and patterns efficiently.
The new version of Essential Studio® is available for current customers from the License and Downloads page. If you are not a Syncfusion customer, try our 30-day free trial to check out our newest features.
For questions, you can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you. Happy coding!