DwsChartDoughnut
Shows the parts of one whole as segments of a ring, with the total in the hole. The ring scales with its container and stays round, its colors are theme colors, and hovering or focusing a segment or its legend entry shows the segment's value and share in the center. A click on a legend entry hides the segment from the ring and the total.
API ReferenceBasic
Only ChartData is needed: one segment per part. Each segment shows its share on the ring, the hole shows the total, and the legend below names the colors with each value and share.
- Engineering420,000 · 42%
- Sales260,000 · 26%
- Marketing180,000 · 18%
- Support95,000 · 9.5%
- Administration45,000 · 4.5%
Show Code
DoughnutChart.razor
<DwsChartDoughnut Name="BudgetChart" ChartData="@_budget" Title="Budget by department" />DoughnutChart.razor.cs
private readonly DwsChartDoughnutData _budget = new DwsChartDoughnutData()
.Add("Engineering", 420_000)
.Add("Sales", 260_000)
.Add("Marketing", 180_000)
.Add("Support", 95_000)
.Add("Administration", 45_000);Shares on the ring
ShowSegmentLabels writes the shares on the ring where a segment is wide enough; a thin or small ring leaves them to the legend and the center.
- Engineering420,000 · 42%
- Sales260,000 · 26%
- Marketing180,000 · 18%
- Support95,000 · 9.5%
- Administration45,000 · 4.5%
Show Code
DoughnutChart.razor
<div class="mb-3"><DwsSwitch Name="ShowSegmentLabels" @bind-Value="_showSegmentLabels" Label="ShowSegmentLabels" /></div>
<DwsChartDoughnut Name="SegmentLabelsChart" ChartData="@_budget" ShowSegmentLabels="@_showSegmentLabels" LegendPosition="DwsChartLegendPosition.Right" />DoughnutChart.razor.cs
private bool _showSegmentLabels = true;
private readonly DwsChartDoughnutData _budget = new DwsChartDoughnutData()
.Add("Engineering", 420_000)
.Add("Sales", 260_000)
.Add("Marketing", 180_000)
.Add("Support", 95_000)
.Add("Administration", 45_000);Center label
ShowCenterLabel shows the total in the hole, captioned by CenterTitle (default: the localized "Total"), and the active segment's label, value and share instead while one is hovered or focused. ValueFormat formats the values in the current culture.
Show Code
DoughnutChart.razor
<div class="mb-3"><DwsSwitch Name="ShowCenterLabel" @bind-Value="_showCenterLabel" Label="ShowCenterLabel" /></div>
<DwsChartDoughnut Name="RevenueChart" ChartData="@_revenue" ShowCenterLabel="@_showCenterLabel"
CenterTitle="Revenue" ValueFormat="#,0 €" WithLegend="false" />DoughnutChart.razor.cs
private bool _showCenterLabel = true;
// The Tag carries the owner of each region, shown by the center template.
private readonly DwsChartDoughnutData _revenue = new DwsChartDoughnutData()
.Add("North", 312_000, tag: "A. Weber")
.Add("South", 268_000, tag: "M. Rossi")
.Add("East", 154_000, tag: "K. Nowak")
.Add("West", 121_000, tag: "J. Martin");Center template
CenterTemplate replaces the content of the hole: it gets the active segment (or null) and the total.
Show Code
DoughnutChart.razor
<DwsChartDoughnut Name="RevenueTemplateChart" ChartData="@_revenue" WithLegend="false">
<CenterTemplate Context="center">
@if (center.Segment is { } segment)
{
<strong>@segment.Label</strong>
<span>@($"{segment.Value / 1000:0}k €")</span>
<small>Owner: @segment.Data.Tag</small>
}
else
{
<small>4 regions</small>
<strong>@($"{center.Total / 1000:0}k €")</strong>
}
</CenterTemplate>
</DwsChartDoughnut>DoughnutChart.razor.cs
// The Tag carries the owner of each region, shown by the center template.
private readonly DwsChartDoughnutData _revenue = new DwsChartDoughnutData()
.Add("North", 312_000, tag: "A. Weber")
.Add("South", 268_000, tag: "M. Rossi")
.Add("East", 154_000, tag: "K. Nowak")
.Add("West", 121_000, tag: "J. Martin");Legend and its values
WithLegend shows or hides the legend; ShowLegendValues adds each value and share to its entries.
- Organic search4,820 · 45.1%
- Direct2,310 · 21.6%
- Social1,640 · 15.4%
- Referral910 · 8.5%
- Email620 · 5.8%
- Paid380 · 3.6%
Show Code
DoughnutChart.razor
<div class="d-flex flex-wrap align-items-center gap-3 mb-2">
<DwsSwitch Name="WithLegend" @bind-Value="_withLegend" Label="WithLegend" />
<DwsSwitch Name="ShowLegendValues" @bind-Value="_showLegendValues" Label="ShowLegendValues" />
</div>
<DwsChartDoughnut Name="LegendValuesChart" ChartData="@_traffic" WithLegend="@_withLegend" ShowLegendValues="@_showLegendValues" />DoughnutChart.razor.cs
private bool _withLegend = true;
private bool _showLegendValues = true;
// A segment's own Color wins over the palette: Paid stays Secondary.
private readonly DwsChartDoughnutData _traffic = new DwsChartDoughnutData()
.Add("Organic search", 4820)
.Add("Direct", 2310)
.Add("Social", 1640)
.Add("Referral", 910)
.Add("Email", 620)
.Add("Paid", 380, DwsColor.Secondary);Dashboard cards: legend Auto, caption, loading and empty state
Two cards of a dashboard grid: side by side on a wide screen, one column on a phone. The chart measures its own width, so in a card narrower than 576px LegendPosition Auto puts the legend below the ring and the shares stay readable (text keeps its screen size). A tap on a segment shows its value and share in the centre until the next tap elsewhere or Escape. Caption writes the takeaway under the chart (the ring's aria-describedby points at it), IsLoading shows the loading state while the data is fetched, and EmptyContent replaces the empty-state text when there is nothing to draw.
Visits by source, this week
- Organic search4,820 · 45.1%
- Direct2,310 · 21.6%
- Social1,640 · 15.4%
- Referral910 · 8.5%
- Email620 · 5.8%
- Paid380 · 3.6%
Organic search brings almost half of all visits.
Returns by reason
No returns this week.
Show Code
DoughnutChart.razor
<DwsSwitch Name="PhoneLoading" @bind-Value="_phoneLoading" Label="IsLoading" Class="mb-3" />
<div class="row g-3">
<div class="col-12 col-lg-6">
<div class="border rounded p-3 h-100">
<DwsTypography Name="TrafficCardTitle" Typography="DwsTypographyType.Paragraph" Class="fw-semibold mb-2">Visits by source, this week</DwsTypography>
<DwsChartDoughnut Name="TrafficCardChart" ChartData="@_traffic" LegendPosition="DwsChartLegendPosition.Auto"
Caption="Organic search brings almost half of all visits." IsLoading="@_phoneLoading" />
</div>
</div>
<div class="col-12 col-lg-6">
<div class="border rounded p-3 h-100">
<DwsTypography Name="ReturnsCardTitle" Typography="DwsTypographyType.Paragraph" Class="fw-semibold mb-2">Returns by reason</DwsTypography>
<DwsChartDoughnut Name="ReturnsCardChart" ChartData="@_returns" IsLoading="@_phoneLoading">
<EmptyContent>
<DwsTypography Name="ReturnsEmptyText" Typography="DwsTypographyType.Paragraph" Class="mb-0">No returns this week.</DwsTypography>
</EmptyContent>
</DwsChartDoughnut>
</div>
</div>
</div>DoughnutChart.razor.cs
private bool _phoneLoading;
// Nothing recorded yet: the chart shows its EmptyContent.
private readonly DwsChartDoughnutData _returns = new();
// A segment's own Color wins over the palette: Paid stays Secondary.
private readonly DwsChartDoughnutData _traffic = new DwsChartDoughnutData()
.Add("Organic search", 4820)
.Add("Direct", 2310)
.Add("Social", 1640)
.Add("Referral", 910)
.Add("Email", 620)
.Add("Paid", 380, DwsColor.Secondary);Size limits
Width, Height, MinWidth, MaxWidth, MinHeight and MaxHeight size the chart in pixels, caption and legend included; the ring takes the largest circle that fits in the rest. Without a width or height the ring is at most 18rem across. In a chart narrower than 576px the legend goes below and a fixed Height no longer applies, so the ring is not squeezed on a phone. Drag the corner of the box: the chart is 320px high and keeps between 200px and 640px wide. The second chart has a fixed Width of 200px, whatever room its container has.
- Organic search4,820 · 45.1%
- Direct2,310 · 21.6%
- Social1,640 · 15.4%
- Referral910 · 8.5%
- Email620 · 5.8%
- Paid380 · 3.6%
- Organic search
- Direct
- Social
- Referral
- Paid
Show Code
DoughnutChart.razor
<div class="doughnut-demo-resizable">
<DwsChartDoughnut Name="SizedChart" ChartData="@_traffic" Height="320" MinWidth="200" MaxWidth="640" LegendPosition="DwsChartLegendPosition.Right" />
</div>
<DwsChartDoughnut Name="FixedWidthChart" ChartData="@_traffic" Width="200" ShowLegendValues="false" Class="mt-3" />
<style>
.doughnut-demo-resizable {
resize: horizontal;
overflow: auto;
width: 100%;
min-width: 0;
max-width: 100%;
padding: 0.5rem;
border: 1px dashed var(--secondary);
}
</style>DoughnutChart.razor.cs
// A segment's own Color wins over the palette: Paid stays Secondary.
private readonly DwsChartDoughnutData _traffic = new DwsChartDoughnutData()
.Add("Organic search", 4820)
.Add("Direct", 2310)
.Add("Social", 1640)
.Add("Referral", 910)
.Add("Email", 620)
.Add("Paid", 380, DwsColor.Secondary);Interface-Description
| Name | Typ | Default | Description |
|---|---|---|---|
| ExpandedSegment | DwsChartDoughnutDataSegment? | null | The opened segment whose children the outer ring shows; null at the top level. |
| ExpandedPath | IReadOnlyList<DwsChartDoughnutDataSegment> | [] | The opened segments from the top level down; empty at the top level. |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| GetDataAsCsv | string | CultureInfo? culture | The data the chart was given as CSV text (RFC 4180, CRLF, a header line): one row per mark in data order, the values as passed in. Null writes numbers the invariant way with commas between the cells; a culture writes them its way with its list separator. Text a spreadsheet would run as a formula gets an apostrophe in front. |
| ExportDataAsync | Task | string? fileName, CultureInfo? culture | Downloads GetDataAsCsv() as a .csv file (UTF-8 with a byte order mark). Null names the file after the chart's title, else its Name. |
| ExportSvgAsync | Task<string?> | A picture of the chart as it is on screen, as SVG markup: the drawing with the theme's colours and the fonts written into it, the title, the legend and the HTML texts drawn in where they stand, on a rectangle in the colour of the surface the chart lies on. Its size is the chart's size on screen in CSS pixels. Null when there is no picture (not interactive, no data, hidden). | |
| DownloadSvgAsync | Task | string? fileName | Downloads the picture of ExportSvgAsync() as an .svg file. Null names the file after the chart's title, else its Name. |
| DownloadPngAsync | Task | string? fileName, double scale | Downloads the picture as a .png file, scale times the chart's size on screen (2 is sharp on a high-density screen and in print; at most 8). Null names the file after the chart's title, else its Name. Text is drawn in the fonts installed on the device: a web font of the page is not available to the picture. |
| DrillIntoAsync(DwsChartDoughnutDataSegment segment) | Task<bool> | Opens a segment of the inner or the outer ring that has children. False when it is not shown or has none. Leaves the keyboard focus where it is. | |
| DrillUpAsync | Task<bool> | Goes one level up. False when no segment is open. | |
| DrillToRootAsync | Task | Closes every opened segment. |
| Name | Typ | Default | Description |
|---|---|---|---|
| ChartData | DwsChartDoughnutData? | null | The segments, clockwise: Segments (each with a Label, a Value, an optional Color and Tag). Add(label, value, color, tag) chains segments. A negative value, NaN or infinity is drawn as 0 with a warning. Null, or no segment, shows NoDataText. |
| Title | string? | null | Caption above the chart, also its accessible name (with the total). |
| NoDataText | string? | null | Text shown when there is nothing to draw. Null uses the localized "No data to display". |
| Width | int? | null | Width in px, caption and legend included. Never wider than the container unless MinWidth asks for more. Without a width (or MinWidth) and a height the ring is at most 18rem across. |
| Height | int? | null | Height in px, caption and legend included; the ring takes the largest circle that fits in the rest. |
| MinWidth | int? | null | Smallest width in px; the only size that may make the chart wider than its container. |
| MaxWidth | int? | null | Largest width in px, and never more than the container. |
| MinHeight | int? | null | Smallest height in px; the ring grows to fill it. |
| MaxHeight | int? | null | Largest height in px; the ring shrinks to fit it. |
| HoleSize | double | 0.6 | Diameter of the hole as a share of the ring's diameter, from 0.3 to 0.9. A value outside is clamped with a warning. |
| StartAngle | double | 0 | Where the first segment starts, in degrees clockwise from 12 o'clock. |
| SegmentGap | double | 1 | Gap between two segments in degrees, from 0 to 10 (clamped with a warning). A single segment has none, and the gaps never take more than half of the ring. |
| Colors | IReadOnlyList<DwsColor>? | null | Theme colors of the segments, in order, repeated when there are more segments; a segment's own Color wins. Null or empty uses Primary, Info, Success, Warning, Accent and Error. Undefined entries are skipped. |
| ShowSegmentLabels | bool | true | Writes each segment's share on the ring, in black or white, whichever reads better on the segment. Left out for segments under 18°, thin rings (HoleSize 0.85 and more) and rings under 180px. |
| ShowCenterLabel | bool | true | Shows the total in the hole, and the label, value and share of the hovered or focused segment (or legend entry) instead while there is one. |
| CenterTitle | string? | null | Caption of the total in the hole. Null uses the localized "Total". |
| CenterTemplate | RenderFragment<DwsChartDoughnutCenter>? | null | Content of the hole. It gets a DwsChartDoughnutCenter with the active Segment (or null) and the Total of the shown segments. |
| ValueFormat | string | "#,0.##" | .NET number format for the values and the total, in the current UI culture. An invalid format falls back to the default with a warning. |
| WithLegend | bool | true | Shows a legend with each segment's color and label. Hovering or focusing an entry highlights its segment and shows its detail in the center. |
| LegendPosition | DwsChartLegendPosition | DwsChartLegendPosition.Bottom | Bottom, Top, Right or Left of the ring. Right and Left move below it on screens narrower than 576px. |
| EmptyContent | RenderFragment? | null | Shown instead of the empty-state text when there is no segment to draw (not while loading). |
| IsLoading | bool | false | Shows the loading state instead of the ring, e.g. while the page fetches the data. |
| Caption | string? | null | A sentence under the chart (<p class="dws-chart-caption">) - the takeaway or the source; the ring's aria-describedby points at it. |
| ShowLegendValues | bool | true | Adds each segment's value and share to its legend entry. |
| ToggleSegmentsOnLegendClick | bool | true | A click on a legend entry hides its segment from the ring, the shares and the total, and a second click shows it again. The last shown segment cannot be hidden; new ChartData shows all segments again. |
| HighlightOnHover | bool | true | Fades the other segments while one is hovered or focused, or while the pointer or focus is on its legend entry. |
| DrillDown | bool | true | Lets the user open a segment that has children (DwsChartDoughnutDataSegment.Children, AddChild): a click, a tap, Enter or Space shows them as a second ring around the chart; the button in the centre, Backspace, Escape or the open segment itself go one level up. A segment with children stands for them: its value is the sum of theirs. Data without children is not affected; false draws the top level only. |
| Name | Typ | Description |
|---|---|---|
| OnSegmentClick | EventCallback<DwsChartDoughnutSegment> | Raised when a segment is clicked, tapped or activated with Enter/Space. With a handler the segments become buttons in the tab order. Not raised while disabled or in permission select mode (a click then selects the chart). |
| OnSegmentHover | EventCallback<DwsChartDoughnutSegment?> | Raised when the pointer or focus enters a segment, and with null when neither is on one any more: also when the chart stops reacting (disabled, permission select mode) or new data no longer has the segment. |
| OnLegendItemClick | EventCallback<DwsChartDoughnutSegment> | Raised when a legend entry is clicked, after its segment was hidden or shown (see IsHidden). With a handler the entries are buttons even without ToggleSegmentsOnLegendClick. |
| OnDrillChanged | EventCallback<DwsChartDoughnutDrillEventArgs> | Raised when a segment was opened or closed, by the user or a method: Segment (the data segment whose children the outer ring shows, null at the top level) and Path (the opened segments from the top down). |
| Name | Typ | Default | Description |
|---|---|---|---|
| Element | ElementReference | The associated web component. May be null if accessed before the component is rendered. | |
| ComponentGuid | Guid | Unique identifier for the component. | |
| TypeString | string | The name of the component type, obtained from reflection. | |
| Authorized_RegisterEnabled | bool | false | Indicates whether this component is enabled through a permission registry. |
| Authorized_RegisterSelected | bool | false | Indicates whether this component is selected in permission selection mode. |
| GetInitializedState | bool | false | Returns whether the component is initialized. |
| GetRenderModeState | DwsRenderMode | Returns the current render mode. | |
| GetComponentState | DwsComponentStateType | Returns the current component state. | |
| GetIsInteractiveState | bool | false | Returns whether the component is interactive. |
| GetIsPrerenderingState | bool | false | Returns whether the component is prerendering. |
| GetComponentLoadingState | bool | false | Returns whether the component is loading. |
| IsFormBusy | bool | false | Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState). |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DebugInfo | void | Logs debug information about the component. | |
| UpdateState | void | Updates the component's state and triggers StateHasChanged. | |
| UpdateStateAsync | Task | Asynchronously updates the component's state and triggers StateHasChanged. | |
| Dispose | void | Disposes the component and releases resources. | |
| FocusAsync | ValueTask | bool containedInScrollableList | Sets focus to the component's element. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Name | string | Required unique identifier for the component within its container. | |
| Visible | bool | true | Determines if the component is visible in the UI. |
| Disabled | bool | false | Disables the form control, ensuring it doesn't participate in form submission. |
| ComponentNameSpace | string | The namespace for permission-based access control. | |
| PermissionKey | string? | null | A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins. |
| Class | string | Optional CSS class names. | |
| Style | string | Optional in-line styles. | |
| Tag | object | An optional user-defined object to store any additional data with the component. | |
| BackReference | DwsReference | A reference to the enclosing component. | |
| ID | string | The HTML ID attribute for the component. | |
| AdditionalAttributes | Dictionary<string, object> | Additional HTML attributes for the component's root element. | |
| IgnorePermissionChecks | bool | false | Bypasses all permission checks for this component. |
| IgnorePermissionSelection | bool | false | Ignore permission selection mode for this component. |
| IgnoreFormBusy | bool | false | Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it. |
| Name | Typ | Description |
|---|---|---|
| OnInitializedCallbackStarted | EventCallback | Callback when OnInitialized starts. |
| OnInitializedCallback | EventCallback | Callback during OnInitialized. |
| OnInitializedCallbackCompleted | EventCallback | Callback when OnInitialized completes. |
| OnInitializedAsyncCallbackStarted | EventCallback | Callback when OnInitializedAsync starts. |
| OnInitializedAsyncCallback | EventCallback | Callback during OnInitializedAsync. |
| OnInitializedAsyncCallbackCompleted | EventCallback | Callback when OnInitializedAsync completes. |
| OnIsComponentLoadingChanged | EventCallback<bool> | Callback when IsComponentLoading changes. |
| OnCurrentRenderModeChanged | EventCallback<DwsRenderMode> | Callback when CurrentRenderMode changes. |
| OnComponentStateChanged | EventCallback<DwsComponentStateType> | Callback when ComponentState changes. |
