Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Violin plot

Since 0.1.0-alpha.23
Compares the shape of each group's values, such as two peaks a box plot hides.

Use violins when the shape of each group's values matters, such as a second peak a box plot would hide. They need many values per group. With only a few, the outline shows a shape that isn't really there, so use a box plot.

A violin plot

ChartViolinSeries makes a violin from the Values of the items that share an Argument. Here weekend orders that wait for Monday make a second peak.

Static

Preview

Delivery time by carrier, March
Arrow keys move between values; Escape hides the tooltip.

Code

Usage.razor
@* Days from order to delivery for a month's parcels with three carriers. Two of them don't pick
   up at weekends, so orders placed Friday to Sunday wait a day or two longer: a second peak a box
   plot would hide inside its box. *@
<Chart Items="_parcels" Argument="p => p.Carrier" Title="Delivery time by carrier, March">
    <ChartValueAxis Title="Days from order to delivery" />
    <ChartViolinSeries Value="p => p.Days" Name="Delivery time" />
</Chart>

@code {
    private record Parcel(string Carrier, double Days);

    // Sample data: each carrier's weekday time, and how much longer a weekend order waits (0: it
    // picks up every day).
    private readonly Parcel[] _parcels =
    [
        .. Parcels("Northwind Post", 2.2, 2.0, 220, seed: 1),
        .. Parcels("Swift Freight", 1.8, 0, 200, seed: 2),
        .. Parcels("Coastline Express", 3.0, 1.6, 180, seed: 3),
    ];

    private static IEnumerable<Parcel> Parcels(string carrier, double median, double weekendWait, int count, int seed)
    {
        var random = new Random(seed);
        return Enumerable.Range(0, count).Select(k =>
        {
            // Three days in seven are Friday to Sunday.
            var weekend = random.Next(7) >= 4;
            var normal = Math.Sqrt(-2 * Math.Log(1 - random.NextDouble())) * Math.Cos(2 * Math.PI * random.NextDouble());
            var days = median * Math.Exp(0.2 * normal) + (weekend ? weekendWait : 0);
            return new Parcel(carrier, Math.Round(days, 1));
        }).ToList();
    }
}

Add this example to a page with the izzy CLI:

bash
izzy add component ChartViolinSeries --at <file>:<line>

The density is a Gaussian kernel with Silverman's bandwidth, trimmed to each group's smallest and largest value, so it never reaches a value no item has. Every violin is as wide at its densest.

The box inside marks the quartiles, the median and Tukey's whiskers, as on a box plot. ShowMean adds the mean.

SeriesBy puts a violin per group side by side, and the chart's Orientation="Orientation.Horizontal" lays them across. A violin's chart holds only box plot and violin series.

Details

Inside, a slim box from the first quartile to the third, a line at the median, and a line to Tukey's whiskers; outliers past them are circles. The density is a Gaussian kernel with Silverman's bandwidth (as Plotly's), trimmed to the values' range, and every violin is as wide at its densest. SeriesBy puts a violin per group side by side; the chart's Orientation="Orientation.Horizontal" lays them across. A violin's chart holds only box plot and violin series.

Mode support

What works in each mode, Violin plot
FeatureStaticInteractive
Each group's density, a Gaussian kernel with Silverman's bandwidth, trimmed to its valuesYesYes
A slim box, median and whiskers inside, with outliers as circlesYesYes
Every violin as wide at its densest, so shapes compareYesYes
Violins split by a group, side by side, upright or acrossYesYes
Each violin's median and middle half in its tooltip, every number in the tableYesYes
A table of the values behind a "Show data" buttonYesYes
Tooltips on hover and from the keyboardYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Without JavaScript

Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off.

  • Tooltips on hover and from the keyboard: waits for the script

Parameters

ChartViolinSeries parameters
PropTypeDefault
ValueEach item's value, such as d => d.Days: a number, or null to leave the item out.Expression<Func<TItem, object?>>Required
ShowMeanMarks each violin's mean with a diamond, and says it in the tooltip and data table.boolfalse
AggregateWith ChartDrillLevel<T>s, how each level adds up its items' values: DataAggregate.Sum (the default), DataAggregate.Avg, DataAggregate.Min, DataAggregate.Max or DataAggregate.Count (the items with a value).DataAggregateDataAggregate.Sum
NameThe series' name, in the legend, the data table and each value's description.string?computed
SeriesBySplits the series into one per value of this, such as p => p.Device: each draws only its own items' values, is named by its value, and takes the next color and marker, in the order the values first appear.Expression<Func<TItem, object?>>?
ColorA CSS color, such as var(--ib-color-danger) or #0f766e.string?computed
LabelsWrites each value beside its bar or point.ChartLabelsChartLabels.None
LabelFormatA .NET format string for this series' labels, such as C0.string?computed
LabelTextWhat each label says in place of its value, such as r => r.Region to name each bubble or point.Expression<Func<TItem, object?>>?
LabelOverlapWhat labels do when they'd overlap others: left out at those widths (ChartLabelOverlap.Hide, the default), or shown anyway (ChartLabelOverlap.Show).ChartLabelOverlapChartLabelOverlap.Hide
TrendDraws a trend through the values: a dashed line in the series' color, named "Revenue trend" (a moving average "Revenue, 7-day average") in the legend, tooltips and data table.ChartTrendChartTrend.None
TrendOrderA polynomial trend's order, 2 (a parabola) to 6.int2
TrendPeriodHow many values a moving average takes in, 2 or more.int3
TrendColorThe trend's CSS color, such as var(--ib-color-danger), used as given.string?computed
ValueAxisThe Name of the value axis the series is read off, for a measure in other units on a second axis at the end side, such as "margin".string?computed
HiddenStarts with the series hidden: the chart is drawn as if it weren't there, and it keeps its place in the legend, to show it again, and in the data table.boolfalse

Connection lost

Rejoining the server…