Skip to content

Commit 6706280

Browse files
authored
docs(chart): Add Pie Chart dimensions documentation and example (#2341)
1 parent 1197ca5 commit 6706280

File tree

1 file changed

+113
-7
lines changed

1 file changed

+113
-7
lines changed

components/chart/types/pie.md

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,11 @@ Pie series
7373
}
7474
````
7575

76-
## Pie Chart Specific Appearance Settings
77-
78-
### Rotation
76+
## Rotation
7977

8078
By default, the first segment starts at the top. You can change that by using the `StartAngle` property of the series.
8179

82-
### Color Field
80+
## Color Field
8381

8482
You can control the color of the individual segments of the pie chart by providing a string with the desired color in the model, and setting the `ColorField` of the series to it. You can pass a valid CSS color (for example, `#abcdef`, `#f00`, or `blue`).
8583

@@ -131,7 +129,7 @@ Set color to the pie chart items
131129
}
132130
````
133131

134-
### Exploded Segment
132+
## Exploded Segment
135133

136134
You can have some of the segments of the pie separated from the rest of the circle with a small margin. This helps bring attention to them as outliers or as important bits that the viewer should focus on.
137135

@@ -185,7 +183,7 @@ Separate items from the main body of the chart
185183
}
186184
````
187185

188-
### Visible In Legend
186+
## Visible In Legend
189187

190188
You can hide certain segments from the legend (for example, if their contribution is insignificantly small). To do this, add a boolean field to the model and set its name to the `VisibleInLegendField` property of the pie series. The flags in this field will denote whether the particular item will be rendered in the legend.
191189

@@ -242,10 +240,118 @@ Show only some items in the legend
242240
}
243241
````
244242

243+
## Width and Height
244+
245+
The main part of the Pie Chart is a circle. Thus, you may need to set both the `Width` and `Height` parameters to achieve the desired layout and dimensions. By default, the Chart container `<div>` expands horizontally to 100% and the height is set to `"400px"` in the CSS theme.
246+
247+
>caption Setting Pie Chart Width and Height
248+
249+
````CSHTML
250+
Chart Width
251+
<TelerikDropDownList Data="@ChartDimensions"
252+
Value="@ChartWidth"
253+
ValueChanged="@( async (string newValue) => {
254+
ChartWidth = newValue;
255+
await RefreshChart();
256+
} )"
257+
DefaultText="100%"
258+
Width="120px" />
259+
Chart Height
260+
<TelerikDropDownList Data="@ChartDimensions"
261+
Value="@ChartHeight"
262+
ValueChanged="@( async (string newValue) => {
263+
ChartHeight = newValue;
264+
await RefreshChart();
265+
} )"
266+
Width="120px" />
267+
268+
Chart Legend Position
269+
<TelerikDropDownList Data="@ChartLegendPositions"
270+
@bind-Value="@ChartLegendPosition"
271+
Width="120px" />
272+
273+
<TelerikChart @ref="ChartRef"
274+
Width="@ChartWidth"
275+
Height="@ChartHeight"
276+
Class="chart-border">
277+
<ChartSeriesItems>
278+
<ChartSeries Type="ChartSeriesType.Pie"
279+
Data="@PieData"
280+
Field="@nameof(PieChartModel.Value)"
281+
CategoryField="@nameof(PieChartModel.Category)">
282+
</ChartSeries>
283+
</ChartSeriesItems>
284+
285+
<ChartTitle Text="Revenue per product"></ChartTitle>
286+
287+
<ChartLegend Position="@ChartLegendPosition">
288+
</ChartLegend>
289+
</TelerikChart>
290+
291+
<style>
292+
/* Make the Chart dimensions more evident. */
293+
.chart-border {
294+
border: 1px solid #ccc;
295+
}
296+
</style>
297+
298+
@code {
299+
private TelerikChart? ChartRef { get; set; }
300+
301+
private List<string> ChartDimensions { get; set; } = new List<string>() { "200px", "400px", "600px", "800px" };
302+
303+
private List<ChartLegendPosition> ChartLegendPositions { get; set; } = new List<ChartLegendPosition>() {
304+
ChartLegendPosition.Top,
305+
ChartLegendPosition.Bottom,
306+
ChartLegendPosition.Left,
307+
ChartLegendPosition.Right
308+
};
309+
310+
private string? ChartWidth { get; set; }
311+
private string ChartHeight { get; set; } = "400px";
312+
private ChartLegendPosition ChartLegendPosition { get; set; } = ChartLegendPosition.Right;
313+
314+
private async Task RefreshChart()
315+
{
316+
// The Chart renders in the browser and must receive its new dimension parameters first.
317+
// The Task.Delay() triggers parameter update before the Chart actually re-renders.
318+
await Task.Delay(1);
319+
320+
ChartRef?.Refresh();
321+
}
322+
323+
private List<PieChartModel> PieData = new List<PieChartModel>() {
324+
new PieChartModel
325+
{
326+
Category = "Product 1",
327+
Value = 2
328+
},
329+
new PieChartModel
330+
{
331+
Category = "Product 2",
332+
Value = 3
333+
},
334+
new PieChartModel
335+
{
336+
Category = "Product 3",
337+
Value = 4
338+
}
339+
};
340+
341+
public class PieChartModel
342+
{
343+
public string Category { get; set; } = string.Empty;
344+
public double Value { get; set; }
345+
}
346+
}
347+
````
348+
349+
## Other Settings
350+
245351
@[template](/_contentTemplates/chart/link-to-basics.md#configurable-nested-chart-settings)
246352

247353
@[template](/_contentTemplates/chart/link-to-basics.md#configurable-nested-chart-settings-axis-free)
248354

249355
## See Also
250356

251-
* [Live Demo: Pie Chart](https://demos.telerik.com/blazor-ui/chart/pie-chart)
357+
* [Live Demo: Pie Chart](https://demos.telerik.com/blazor-ui/chart/pie-chart)

0 commit comments

Comments
 (0)