Skip to content

Commit 238ebaf

Browse files
authored
Merge pull request #3011 from Ivy-Interactive/plan-01701-Ivy-Framework
[01701] Document BarChart YAxisIndex and dual-axis pattern
2 parents e77e29c + c8a90da commit 238ebaf

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/Ivy.Docs.Shared/Docs/02_Widgets/06_Charts/02_BarChart.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,84 @@ public class TiobeIndexDemo : ViewBase
227227

228228
</Body>
229229
</Details>
230+
231+
## Dual-axis charts
232+
233+
BarChart supports multiple Y-axes through the `YAxisIndex` property on `Bar`. This lets different bar series use different scales — useful when comparing metrics with very different ranges (e.g., revenue in thousands alongside a conversion rate as a percentage).
234+
235+
Y-axes are indexed starting from 0. The first Y-axis defaults to the left side and additional axes can be placed on the right using `.Orientation(YAxis.Orientations.Right)`. Assign each bar to its axis with `.YAxisIndex(index)`.
236+
237+
### Manual BarChart construction
238+
239+
```csharp demo-below
240+
public class DualAxisBarChart : ViewBase
241+
{
242+
public override object? Build()
243+
{
244+
var data = new[]
245+
{
246+
new { Month = "Jan", Revenue = 4200, ConversionRate = 2.1 },
247+
new { Month = "Feb", Revenue = 5800, ConversionRate = 3.4 },
248+
new { Month = "Mar", Revenue = 5100, ConversionRate = 2.8 },
249+
new { Month = "Apr", Revenue = 7400, ConversionRate = 4.2 },
250+
new { Month = "May", Revenue = 6200, ConversionRate = 3.6 },
251+
};
252+
253+
return Layout.Vertical()
254+
| new BarChart(data,
255+
new Bar("Revenue").Fill(Colors.Blue).YAxisIndex(0),
256+
new Bar("ConversionRate").Fill(Colors.Orange).Name("Conversion Rate").Unit("%").YAxisIndex(1))
257+
.XAxis(new XAxis("Month").TickLine(false).AxisLine(false))
258+
.YAxis(new YAxis().Orientation(YAxis.Orientations.Left))
259+
.YAxis(new YAxis().Orientation(YAxis.Orientations.Right))
260+
.CartesianGrid(new CartesianGrid().Horizontal())
261+
.Tooltip()
262+
.Legend();
263+
}
264+
}
265+
```
266+
267+
### Using ToBarChart with polish
268+
269+
When building charts from queryable data with `ToBarChart()`, use the `polish` callback to add extra Y-axes and reassign bars. The default style already creates bars from your measures, so `polish` modifies the scaffolded chart — replacing bars with versions that have `YAxisIndex` set:
270+
271+
```csharp demo-below
272+
public class DualAxisPolishBarChart : ViewBase
273+
{
274+
record SalesData(string Month, int Revenue, double ConversionRate);
275+
276+
public override object? Build()
277+
{
278+
var data = new SalesData[]
279+
{
280+
new("Jan", 4200, 2.1),
281+
new("Feb", 5800, 3.4),
282+
new("Mar", 5100, 2.8),
283+
new("Apr", 7400, 4.2),
284+
new("May", 6200, 3.6),
285+
};
286+
287+
return Layout.Vertical()
288+
| data.ToBarChart(
289+
polish: chart =>
290+
{
291+
chart = chart.YAxis(new YAxis().Orientation(YAxis.Orientations.Right));
292+
return chart with
293+
{
294+
Bars =
295+
[
296+
new Bar("Revenue").YAxisIndex(0).Fill(Colors.Blue),
297+
new Bar("ConversionRate").YAxisIndex(1).Fill(Colors.Orange)
298+
.Name("Conversion Rate").Unit("%")
299+
]
300+
};
301+
}
302+
)
303+
.Dimension("Month", e => e.Month)
304+
.Measure("Revenue", e => e.Sum(f => f.Revenue))
305+
.Measure("ConversionRate", e => e.Average(f => f.ConversionRate));
306+
}
307+
}
308+
```
309+
310+
> **Note:** The `polish` callback receives the fully scaffolded `BarChart` which already includes one Y-axis and bars from the style. The example above adds a second Y-axis and replaces the `Bars` array with versions that specify `YAxisIndex`. Use contrasting colors for bars on different axes so readers can quickly match each bar to its scale.

0 commit comments

Comments
 (0)