Showcase

Blazor Chart Scenarios

Fixes for problems people hit when they put charts into real business screens: charts that do not grow back in Bootstrap layouts, days missing from query results, pie charts with too many slices, colours that move between categories, and mixed bar and line charts: two units on one chart, a Pareto chart, and stacked bars with reference lines.
Each example below starts from a problem people hit when they put charts into real business screens, reported in the Blazor Bootstrap issues or common to Chart.js reports. For each chart type and option on its own, see the BlazorExpress.ChartJS samples.

Charts that shrink and grow in Bootstrap rows and cards #

The problem: a chart in a Bootstrap column or a flex card gets smaller when the window narrows but does not grow back, or pushes past the edge of its card when a side panel opens (#1089, #797). Two things cause it. A Bootstrap .col is a flex item, and a flex item never gets narrower than its content, so the canvas holds the column open. And Chart.js never makes a chart taller than its container already is; a container without its own height is only as tall as the canvas, so the chart can shrink but never grow back.
The fix:
  1. Add style="min-width: 0;" to the column or flex item that holds the chart.
  2. Give the chart's container a height that follows its width: ContainerStyle="position: relative; width: 100%; aspect-ratio: 2 / 1; min-height: 240px;". Keep width: 100%; without it, aspect-ratio turns the minimum height into a minimum width and the chart overflows on small screens.
  3. Set MaintainAspectRatio = false in the chart options, so the chart fills that container.
  4. Do not set Width or Height on the chart; a fixed size stops it following the layout.

Days missing from the query result #

The problem: a GROUP BY date query returns only the days that had data, so a chart of orders per day silently skips closed days and outages, and the bars look evenly spaced when they are not (#643).
The fix: build the full list of days for the range in C# and fill the days the query did not return. Use 0 when a missing day means nothing happened, and null when it means no data was reported, so the chart shows a gap.

Too many slices: top N plus Other #

The problem: a pie or doughnut chart of every customer, country or product turns into thin slices nobody can read, and once there are more slices than palette colours, colours repeat.
The fix: sort the rows, keep the largest few as their own slices, add the rest together as one Other slice in a neutral colour, and list what Other contains next to the chart.

Keep each category's colour when the data changes #

The problem: colours given by list position move to another category when the data is sorted differently or a category has no rows, so Billing is teal in one quarter and orange in the next, and readers compare the wrong slices (#411).
The fix: give every category a colour once, from the full category list, and look the colour up by name each time you build the data. Use the same lookup for every chart on the page.

Mixed bar and line charts #

A BarChart can hold LineChartDatasets next to its BarChartDatasets. Bars and a line together answer two questions on one chart, such as how much and how good, but they bring their own traps: units that do not share a scale, lines drawn behind bars, and lines that get stacked by accident. Set Order on each dataset to choose what is drawn on top; a lower order is drawn later, in front.

Revenue and margin: two units on one chart #

The problem: revenue in dollars and gross margin in percent share one y-axis, so a 34% margin next to $538,000 of revenue is a flat line along the bottom and its ups and downs disappear.
The fix: add a second y-axis on the right with Scales.AdditionalAxes["y1"], set YAxisID = "y1" on the line, give each axis its own number format and title, and turn off the second axis's grid lines with DrawOnChartArea = false.

Pareto chart: find the few causes behind most problems #

The problem: a quality, support or returns team needs to know which few causes account for most of the problems. Chart.js has no Pareto chart type, and a plain bar chart of unsorted export data does not show where the 80% line falls.
The fix: sort the causes from most to fewest, add a running total as a share of the whole, and draw it as a line on a second axis from 0 to 100%. A dashed line at 80% and faded bars after it show which causes to fix first.

Stacked bars with reference lines #

The problem: with Scales.Y.Stacked = true, every dataset that has no Stack joins the default stack of its type. Two reference lines, such as team capacity and the same week last year, are then added together, and the second line is drawn far too high. Turning stacking off fixes the lines but breaks the bars.
The fix: keep the axis stacked, put the bars in one named stack, and give each reference line a Stack of its own.

Chart Components — Dedicated Library

Chart components have been moved to the dedicated BlazorExpress.ChartJS library to support independent feature additions and easier maintenance. Complete the following steps in the Blazor project that renders the charts.

  1. Install the package used by these demos:
    dotnet add package BlazorExpress.ChartJS --version 2.0.0
  2. Import the component namespace in _Imports.razor:
    @using BlazorExpress.ChartJS
  3. Load the browser scripts in this order from the host page, after the Blazor framework script. BlazorExpress.ChartJS 2.0.0 is built for Chart.js 4.6.1. The data-labels plugin is needed only when a chart uses data labels, and time axes need no extra date-adapter script.
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.6.1/chart.umd.js" integrity="sha512-Vj/VnWZpOof68AJOIBxA90+7KQccA7eISngKbl18q5MQO3KnvO1a0z76Q+7hj8+iDIwETpKUBPN+cLy7PxvTEQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.2.0/chartjs-plugin-datalabels.min.js" integrity="sha512-JPcRR8yFa8mmCsfrw4TNte1ZvF1e3+1SdGMslZvmrzDYxS69J7J49vkFL8u6u8PlPJK+H3voElBtUCzaXj+6ig==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="_content/BlazorExpress.ChartJS/blazorexpress.chartjs.js"></script>

Upgrading from 1.x? Version 2.0.0 changes some defaults and behaviour. Read the 2.0.0 breaking changes before you update.

See the BlazorExpress.ChartJS website and GitHub repository for current package documentation. We also recommend checking the original BlazorExpress.ChartJS demos when adapting advanced examples.

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please reload the page.