Dual Y-Axis
Plot series with different units on independent left and right y-axes in LineChart, AreaChart, and BarChart.
Use a secondary y-axis when series have different units or scales, such as revenue in euros and conversion in percent. It works on LineChart, AreaChart, and vertical BarChart.
Basic Usage
- Leave series on the left axis as they are. They use the primary axis, which has id
0. - Give each series on the right axis a
yAxisid. - Describe that axis in the
yAxesprop, under the same id.
Series that share an id share a scale.
<script lang="ts" setup>
import type { BulletLegendItemInterface, YAxisConfig } from 'nuxt-charts/types'
const data = [
{ time: '06:00', indoor: 19.5, outdoor: 11.2, humidity: 68 },
{ time: '09:00', indoor: 20.8, outdoor: 14.6, humidity: 61 },
{ time: '12:00', indoor: 22.4, outdoor: 19.3, humidity: 49 },
{ time: '15:00', indoor: 23.1, outdoor: 21.7, humidity: 44 },
{ time: '18:00', indoor: 22.2, outdoor: 18.4, humidity: 52 },
{ time: '21:00', indoor: 20.9, outdoor: 14.1, humidity: 63 }
]
const categories: Record<string, BulletLegendItemInterface> = {
indoor: { name: 'Indoor' },
outdoor: { name: 'Outdoor' },
humidity: { name: 'Humidity', yAxis: 'humidity' }
}
const yAxes: Record<string, YAxisConfig> = {
0: { label: 'Temperature', domain: [0, 25], numTicks: 6, formatter: (value: number) => `${value}°C` },
humidity: {
orientation: 'right',
label: 'Humidity',
domain: [0, 100],
numTicks: 6,
formatter: (value: number) => `${value}%`
}
}
</script>
<template>
<LineChart
:data="data"
:categories="categories"
:y-axes="yAxes"
:height="300"
:y-grid-line="true"
x-axis="time"
/>
</template>
Bars with a Line
In a BarChart, set type: 'line' on a category to draw it as a line over the bars. Use this instead of the v2 DualChart component, which isn't available in v3.
type: 'line' is a preview API and may change in a minor release. It requires nuxt-charts 3.2.0 or later.<script lang="ts" setup>
import type { BulletLegendItemInterface, YAxisConfig } from 'nuxt-charts/types'
const data = [
{ month: 'Jan', revenue: 42000, conversion: 2.1 },
{ month: 'Feb', revenue: 51000, conversion: 2.6 },
{ month: 'Mar', revenue: 47000, conversion: 3.4 },
{ month: 'Apr', revenue: 63000, conversion: 2.9 },
{ month: 'May', revenue: 58000, conversion: 3.8 },
{ month: 'Jun', revenue: 71000, conversion: 4.2 }
]
const categories: Record<string, BulletLegendItemInterface> = {
revenue: { name: 'Revenue' },
conversion: { name: 'Conversion', yAxis: 'pct', type: 'line' }
}
const yAxes: Record<string, YAxisConfig> = {
0: { domain: [0, 80000], numTicks: 5, formatter: (value: number) => `€${value / 1000}k` },
pct: { orientation: 'right', domain: [0, 8], numTicks: 5, formatter: (value: number) => `${value}%` }
}
</script>
<template>
<BarChart
:data="data"
:categories="categories"
:y-axes="yAxes"
:y-axis="['revenue', 'conversion']"
:height="300"
:y-grid-line="true"
x-axis="month"
/>
</template>
- List line series in the
yAxisprop too. That prop picks the data keys; the category'syAxisfield picks the axis. - Lines are unstacked, with
monotonecurves and no dots. - Bar options such as
variant,stacked,valueLabel, andradiusdon't apply to lines. - On a horizontal
BarChart, the category stays a bar.
Axis Options
Each yAxes entry is a YAxisConfig. It accepts these options, plus the AxisConfig tick options:
| Option | Type | Default | Description |
|---|---|---|---|
orientation | 'left' | 'right' | 'left' | Side of the plot area. Set 'right' for a secondary axis. |
label | string | undefined | Axis label. |
domain | [number | undefined, number | undefined] | Inherited | Fixed [min, max] for this axis. |
numTicks | number | Inherited | Desired number of ticks. |
formatter | axisFormatter | Inherited | Formats ticks and the tooltip values of this axis' series. |
hide | boolean | false | Hides the axis but keeps its scale and series. |
Inherited Values
Options marked Inherited fall back to the top-level yDomain, yNumTicks, and yFormatter props. A right axis without its own values picks up the left axis' domain and formatter, so:
- Configure both axes inside
yAxes, and use the0key for the primary axis. - Don't combine top-level
yDomainoryFormatterwith a secondary axis, unless that axis sets its owndomainandformatter.
Tooltips only use formatter from yAxes. A top-level yFormatter formats ticks, not tooltip values.
Axis Config
tickValues, tickLine, minMaxTicksOnly, tickTextColor, tickTextFontSize, and tickTextAlign work per axis, as they do in yAxisConfig.
Grid Lines and Reference Lines
Grid lines and referenceLines follow the primary axis, so a reference line's y is a value on the left scale.
To line up right-axis ticks with the grid, give both axes a fixed domain and the same numTicks, as in the examples above.
Stacking
stacked is applied per axis on BarChart and AreaChart. Series on different axes are never summed together.
Limitations
- Horizontal bars: there is a single value axis, so
yAxesis ignored. hideYAxis: hides every axis. Series keep their independent scales. To hide one axis, usehideon itsyAxesentry.- Missing
orientation: ayAxisid without anorientation: 'right'entry inyAxesgets a second axis on the left.