Spravovat popisky dat v grafech v prezentacích v .NET
Úvod
Data labels display information about chart series and individual data points, helping readers identify values and understand the chart. This article explains how to format values, display percentages, read label text, control labels beyond the axis maximum, adjust category axis label spacing, and position pie chart labels.
Nastavení přesnosti dat v popiscích grafu
Use NumberFormatOfValues to format series values. This example creates a line chart with default data, displays its data table, and enables value labels for the first series. The format #,##0.00 displays a thousands separator and two decimal places without changing the underlying values.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Line, 50, 50, 450, 300);
chart.HasDataTable = true;
var series = chart.ChartData.Series[0];
series.NumberFormatOfValues = "#,##0.00";
series.Labels.DefaultDataLabelFormat.ShowValue = true;
presentation.Save("PrecisionOfDatalabels_out.pptx", SaveFormat.Pptx);
Zobrazení procent jako popisků
For a stacked column chart, calculate each value as a percentage of its category total and assign the text to TextFrameForOverriding. This example uses the default chart data and displays percentages with two decimal places in an 8-point font. Categories with a total of zero are skipped to avoid division by zero. Recalculate the custom label text if the chart data changes.
using System;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.StackedColumn, 20, 20, 400, 400);
var categoryTotals = new double[chart.ChartData.Categories.Count];
for (int k = 0; k < chart.ChartData.Categories.Count; k++)
{
for (int i = 0; i < chart.ChartData.Series.Count; i++)
{
var series = chart.ChartData.Series[i];
var pointValue = Convert.ToDouble(series.DataPoints[k].Value.Data);
categoryTotals[k] += pointValue;
}
}
for (int x = 0; x < chart.ChartData.Series.Count; x++)
{
var series = chart.ChartData.Series[x];
series.Labels.DefaultDataLabelFormat.ShowLegendKey = false;
for (int j = 0; j < series.DataPoints.Count; j++)
{
var label = series.DataPoints[j].Label;
if (categoryTotals[j] == 0)
{
continue;
}
var pointValue = Convert.ToDouble(series.DataPoints[j].Value.Data);
var dataPointPercent = (pointValue / categoryTotals[j]) * 100;
var portion = new Portion();
portion.Text = string.Format("{0:F2} %", dataPointPercent);
portion.PortionFormat.FontHeight = 8f;
label.TextFrameForOverriding.Text = "";
var paragraph = label.TextFrameForOverriding.Paragraphs[0];
paragraph.Portions.Add(portion);
label.DataLabelFormat.ShowValue = true;
label.DataLabelFormat.ShowSeriesName = false;
label.DataLabelFormat.ShowPercentage = false;
label.DataLabelFormat.ShowLegendKey = false;
label.DataLabelFormat.ShowCategoryName = false;
label.DataLabelFormat.ShowBubbleSize = false;
}
}
presentation.Save("DisplayPercentageAsLabels_out.pptx", SaveFormat.Pptx);
Nastavení procentního znaménka v popiscích grafu
When values are stored as fractions, use NumberFormat to display percentages. Set IsNumberFormatLinkedToSource to false to apply the label format independently of the source cells.
This example creates a 100% stacked column chart with red and blue series across four categories. Each pair of values adds up to 1. The label format 0.0% displays 0.30 as 30.0%, while the vertical axis uses two decimal places. Both series use white, 10-point label text.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.PercentsStackedColumn, 20, 20, 500, 400);
chart.Axes.VerticalAxis.IsNumberFormatLinkedToSource = false;
chart.Axes.VerticalAxis.NumberFormat = "0.00%";
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
int worksheetIndex = 0;
for (int i = 0; i < 4; i++)
{
var categoryCell = workbook.GetCell(worksheetIndex, i + 1, 0, $"Category {i + 1}");
chart.ChartData.Categories.Add(categoryCell);
}
string[] seriesNames = { "Reds", "Blues" };
Color[] seriesColors = { Color.Red, Color.Blue };
double[,] values = { { 0.30, 0.50, 0.80, 0.65 }, { 0.70, 0.50, 0.20, 0.35 } };
for (int i = 0; i < seriesNames.Length; i++)
{
var seriesCell = workbook.GetCell(worksheetIndex, 0, i + 1, seriesNames[i]);
var series = chart.ChartData.Series.Add(seriesCell, chart.Type);
for (int j = 0; j < 4; j++)
{
var valueCell = workbook.GetCell(worksheetIndex, j + 1, i + 1, values[i, j]);
series.DataPoints.AddDataPointForBarSeries(valueCell);
}
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = seriesColors[i];
var labelFormat = series.Labels.DefaultDataLabelFormat;
labelFormat.ShowValue = true;
labelFormat.IsNumberFormatLinkedToSource = false;
labelFormat.NumberFormat = "0.0%";
labelFormat.TextFormat.PortionFormat.FontHeight = 10;
labelFormat.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
labelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.White;
}
presentation.Save("SetDataLabelsPercentageSign_out.pptx", SaveFormat.Pptx);
Čtení skutečného textu popisků dat
Use GetActualLabelText to retrieve the text produced by a data label’s settings. This is useful when extracting labels for reports, searching presentation content, or validating generated charts. In the example below, the default data label format combines each category name, series name, and value. One point formats its value as a percentage, and another uses custom text from TextFrameForOverriding.
using System;
using Aspose.Slides;
using Aspose.Slides.Charts;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
chart.ChartData.Categories.Add(workbook.GetCell(0, 1, 0, "Q1"));
chart.ChartData.Categories.Add(workbook.GetCell(0, 2, 0, "Q2"));
var north = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 1, "North"), chart.Type);
north.DataPoints.AddDataPointForBarSeries(workbook.GetCell(0, 1, 1, 0.25));
north.DataPoints.AddDataPointForBarSeries(workbook.GetCell(0, 2, 1, 0.75));
var south = chart.ChartData.Series.Add(workbook.GetCell(0, 0, 2, "South"), chart.Type);
south.DataPoints.AddDataPointForBarSeries(workbook.GetCell(0, 1, 2, 0.40));
south.DataPoints.AddDataPointForBarSeries(workbook.GetCell(0, 2, 2, 0.60));
foreach (var series in chart.ChartData.Series)
{
var format = series.Labels.DefaultDataLabelFormat;
format.ShowCategoryName = true;
format.ShowSeriesName = true;
format.ShowValue = true;
}
north.Labels[1].DataLabelFormat.IsNumberFormatLinkedToSource = false;
north.Labels[1].DataLabelFormat.NumberFormat = "0%";
south.Labels[0].TextFrameForOverriding.Text = "Reviewed";
foreach (var series in chart.ChartData.Series)
{
foreach (var point in series.DataPoints)
{
var label = point.Label;
if (!label.IsVisible)
{
continue;
}
Console.WriteLine($"Value: {point.Value.Data}; label: {label.GetActualLabelText()}");
}
}
The number stored in a data point remains 0.75, even when its label shows 75% along with the category and series names. Custom text replaces the generated label text. GetActualLabelText returns the resulting label string in either case. Check IsVisible separately, as shown above, when you want to extract only visible labels.
Ovládání popisků dat nad maximem osy
When you limit an axis range manually, some data points may exceed its maximum. Use ShowDataLabelsOverMaximum to control whether their data labels are shown. This setting changes label visibility; it does not change the axis range or the underlying data values.
The example below creates a 2D clustered column chart with values of 60 and 120. It sets IsAutomaticMaxValue to false and MaxValue to 100 on the vertical axis. The first slide allows labels beyond the maximum; a copy of that slide disables them. Both slides are saved in DataLabelsOverMaximum.pptx.
Enable value labels with ShowValue. The chart-level setting does not enable value display by itself or override an individual label’s disabled value display. This example enables values for the entire series and uses Position to place labels at the outside end of each column.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.HasLegend = false;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var firstCategory = workbook.GetCell(0, 1, 0, "Within range");
var secondCategory = workbook.GetCell(0, 2, 0, "Above maximum");
chart.ChartData.Categories.Add(firstCategory);
chart.ChartData.Categories.Add(secondCategory);
var seriesName = workbook.GetCell(0, 0, 1, "Values");
var series = chart.ChartData.Series.Add(seriesName, chart.Type);
var firstValue = workbook.GetCell(0, 1, 1, 60);
var secondValue = workbook.GetCell(0, 2, 1, 120);
series.DataPoints.AddDataPointForBarSeries(firstValue);
series.DataPoints.AddDataPointForBarSeries(secondValue);
series.Labels.DefaultDataLabelFormat.ShowValue = true;
series.Labels.DefaultDataLabelFormat.Position = LegendDataLabelPosition.OutsideEnd;
chart.Axes.VerticalAxis.IsAutomaticMaxValue = false;
chart.Axes.VerticalAxis.MaxValue = 100;
chart.ShowDataLabelsOverMaximum = true;
var secondSlide = presentation.Slides.AddClone(slide);
var secondChart = (IChart)secondSlide.Shapes[0];
secondChart.ShowDataLabelsOverMaximum = false;
presentation.Save("DataLabelsOverMaximum.pptx", SaveFormat.Pptx);
The following images show the saved slides rendered by Microsoft PowerPoint. With true, the label 120 is visible at the upper boundary; with false, it is hidden. The label 60 remains visible, the axis maximum stays at 100, and the second data point remains 120 in both cases.
| ShowDataLabelsOverMaximum = true | ShowDataLabelsOverMaximum = false |
|---|---|
![]() |
![]() |
Chart Type
This example uses a 2D column chart with a value axis. Charts without a value axis, such as pie and doughnut charts, do not have an axis maximum to limit in this way.Nastavení vzdálenosti popisku od osy
Use LabelOffset to control the distance between category axis labels and the axis. The value is a percentage of the maximum font size of the axis labels. This example creates a clustered column chart and sets the horizontal axis label offset to 500. This setting affects category axis labels rather than labels attached to individual data points.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.Axes.HorizontalAxis.LabelOffset = 500;
presentation.Save("SetCategoryAxisLabelDistance_out.pptx", SaveFormat.Pptx);
Úprava umístění popisků
On a pie chart, adjust data label positions to improve spacing and make room for leader lines.
This example displays the value of the first data point, places its label outside the slice, and adjusts its X and Y offsets. These offsets are relative to the chart width and height, respectively.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Pie, 50, 50, 200, 200);
var series = chart.ChartData.Series;
var label = series[0].Labels[0];
label.DataLabelFormat.ShowValue = true;
label.DataLabelFormat.Position = LegendDataLabelPosition.OutsideEnd;
label.X = 0.71f;
label.Y = 0.04f;
presentation.Save("presentation.pptx", SaveFormat.Pptx);

Často kladené otázky
Jak mohu zabránit překrývání popisků dat v hustých grafech?
Combine automatic label placement, leader lines, and reduced font size; if necessary, hide some fields (for example, the category) or show labels only for extreme values or key points.
Jak mohu zakázat popisky pouze pro nulové, záporné nebo prázdné hodnoty?
Filter data points before enabling labels and turn off display for values of 0, negative values, or missing values according to a defined rule.
Jak zajistit konzistentní styl popisků při exportu do PDF/obrázků?
Explicitly set the font family and size and verify that the font is available in the rendering environment to avoid fallback.

