Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Histogram

Since 0.1.0-alpha.23
Counts numbers or dates in equal bins, to show how they spread.

Use a histogram for a first look at one measure, such as delivery times. It shows where values bunch, whether they lean to one side, and whether there are two peaks. The bin width changes the picture, so try a few. To compare more than three groups, use box plots.

A histogram

ChartHistogramSeries cuts each item's Value into bins and draws a bar for how many fall in each. The chart takes no Argument: the bins are its axis, here a day's page load times.

Static

Preview

Product page load time, March 9
Arrow keys move between values; Escape hides the tooltip.Good: 2.5 s: 2.5

Code

Usage.razor
@* How long the main content of a store's product pages took to show (Largest Contentful Paint)
   over a day's 600 visits: most within 2.5 seconds, Google's "good" mark, with a long tail. *@
<Chart Items="_visits" Title="Product page load time, March 9">
    <ChartArgumentAxis Title="Largest Contentful Paint (seconds)" />
    <ChartValueAxis Title="Visits" />
    <ChartHistogramSeries Value="v => v.Seconds" Name="Visits" />
    <ChartReferenceLine Argument="@(2.5)" Label="Good: 2.5 s" />
</Chart>

@code {
    private record PageVisit(double Seconds);

    // Sample data: a skewed spread around 1.6 seconds, as page timings are.
    private readonly PageVisit[] _visits = Visits(600, seed: 9);

    private static PageVisit[] Visits(int count, int seed)
    {
        var random = new Random(seed);
        return [.. Enumerable.Range(0, count).Select(_ =>
        {
            var normal = Math.Sqrt(-2 * Math.Log(1 - random.NextDouble())) * Math.Cos(2 * Math.PI * random.NextDouble());
            return new PageVisit(Math.Round(1.6 * Math.Exp(0.45 * normal), 2));
        })];
    }
}

Add this example to a page with the izzy CLI:

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

By default the bins are about as many as Sturges' rule gives for the number of values, at a round width on round edges. Bins sets them: ChartBins.Count(20) for at most 20, or ChartBins.Width(0.25) for a quarter second each. The width changes the picture, so try a few.

A ChartReferenceLine with an Argument marks a value on the scale, here Google's 2.5-second mark for a good load.

The last bin takes in the largest value, and an empty bin counts zero. Dates bin by a TimeSpan, such as ChartBins.Width(TimeSpan.FromDays(7)).

Split by a group

SeriesBy splits the counts by a group, here phones and desktops, on one set of bins. A lower Opacity lets each group's bars show through the other's.

Static

Preview

Product page load time by device, March 9
  • Desktop
  • Mobile
Arrow keys move between values; Escape hides the tooltip.

Code

HistogramSplit.razor
@* The same page's load times on phones and on desktops: one set of bins, each device's bars
   see-through so the two spreads show where they overlap. *@
<Chart Items="_visits" Title="Product page load time by device, March 9">
    <ChartArgumentAxis Title="Largest Contentful Paint (seconds)" />
    <ChartValueAxis Title="Visits" />
    <ChartHistogramSeries Value="v => v.Seconds" SeriesBy="v => v.Device" Bins="ChartBins.Width(0.5)" Opacity="0.55" />
</Chart>

@code {
    private record PageVisit(string Device, double Seconds);

    // Sample data: desktops load in about 1.3 seconds, phones in about 2.2, both skewed.
    private readonly PageVisit[] _visits = [.. Visits("Desktop", 1.3, 320, seed: 4), .. Visits("Mobile", 2.2, 280, seed: 5)];

    private static IEnumerable<PageVisit> Visits(string device, double median, int count, int seed)
    {
        var random = new Random(seed);
        return Enumerable.Range(0, count).Select(_ =>
        {
            var normal = Math.Sqrt(-2 * Math.Log(1 - random.NextDouble())) * Math.Cos(2 * Math.PI * random.NextDouble());
            return new PageVisit(device, Math.Round(median * Math.Exp(0.4 * normal), 2));
        }).ToList();
    }
}

With the chart's Stacking="ChartStacking.Normal", the groups stack instead, so each bar's height is the bin's total.

A histogram's chart holds only histogram series, upright, and doesn't zoom.

Details

The bars touch, the axis along the bottom is the values' own scale with the bins' edges marked, and the value axis counts from zero. Bins sets how they're cut. The chart takes no Argument. SeriesBy splits the counts by a group, on one set of bins: stacked with the chart's Stacking, or overlaid with a lower Opacity. A histogram's chart holds only histogram series; mark a value on its scale with a ChartReferenceLine's Argument, such as a target time.

Mode support

What works in each mode, Histogram
FeatureStaticInteractive
Values cut into round bins: by Sturges' rule, a most count, or a widthYesYes
Bars that touch, on an axis that marks the bins' edgesYesYes
Numbers or dates, the last bin taking in the largest valueYesYes
Split by a group on one set of bins, overlaid or stackedYesYes
Reference lines at a value on the scale, such as a targetYesYes
Each bin read "2 – 2.5: 96" in tooltips and the tableYesYes
A table of the counts in a "Show data" disclosureYesYes
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

ChartHistogramSeries parameters
PropTypeDefault
ValueThe value each item is counted by, such as o => o.DeliveryDays: a number or a date, or null to leave the item out.Expression<Func<TItem, object?>>Required
BinsHow the values are cut into bins: Auto (the default), Count or Width.ChartBins?
OpacityHow solid the bars are, from 0 to 1.double?computed
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…