Blazor Chart Scenarios
Charts that shrink and grow in Bootstrap rows and cards #
.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:
- Add
style="min-width: 0;"to the column or flex item that holds the chart. -
Give the chart's container a height that follows its width:
ContainerStyle="position: relative; width: 100%; aspect-ratio: 2 / 1; min-height: 240px;". Keepwidth: 100%; without it,aspect-ratioturns the minimum height into a minimum width and the chart overflows on small screens. - Set
MaintainAspectRatio = falsein the chart options, so the chart fills that container. - Do not set
WidthorHeighton the chart; a fixed size stops it following the layout.
Days missing from the query result #
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 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 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 #
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 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 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 #
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.
- Install the package used by these demos:
dotnet add package BlazorExpress.ChartJS --version 2.0.0 - Import the component namespace in
_Imports.razor:@using BlazorExpress.ChartJS - 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.