图表数据标签
Contents
[
Hide
]
图表上的数据标签显示有关图表数据系列或单个数据点的详细信息。它们使读者能够快速识别数据系列,并且还使图表更易于理解。
设置图表数据标签中的数据精度
以下 C# 代码演示了如何在图表数据标签中设置数据精度:
using (Presentation pres = new Presentation())
{
IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Line, 50, 50, 450, 300);
chart.HasDataTable = true;
chart.ChartData.Series[0].NumberFormatOfValues = "#,##0.00";
pres.Save("PrecisionOfDatalabels_out.pptx", SaveFormat.Pptx);
}
显示百分比作为标签
Aspose.Slides for .NET 允许您在显示的图表上设置百分比标签。以下 C# 代码演示了该操作:
// 创建 Presentation 类的实例
Presentation presentation = new Presentation();
ISlide slide = presentation.Slides[0];
IChart chart = slide.Shapes.AddChart(ChartType.StackedColumn, 20, 20, 400, 400);
IChartSeries series = chart.ChartData.Series[0];
IChartCategory cat;
double[] total_for_Cat = new double[chart.ChartData.Categories.Count];
for (int k = 0; k < chart.ChartData.Categories.Count; k++)
{
cat = chart.ChartData.Categories[k];
for (int i = 0; i < chart.ChartData.Series.Count; i++)
{
total_for_Cat[k] = total_for_Cat[k] + Convert.ToDouble(chart.ChartData.Series[i].DataPoints[k].Value.Data);
}
}
double dataPontPercent = 0f;
for (int x = 0; x < chart.ChartData.Series.Count; x++)
{
series = chart.ChartData.Series[x];
series.Labels.DefaultDataLabelFormat.ShowLegendKey = false;
for (int j = 0; j < series.DataPoints.Count; j++)
{
IDataLabel lbl = series.DataPoints[j].Label;
dataPontPercent = (Convert.ToDouble(series.DataPoints[j].Value.Data) / total_for_Cat[j]) * 100;
IPortion port = new Portion();
port.Text = String.Format("{0:F2} %", dataPontPercent);
port.PortionFormat.FontHeight = 8f;
lbl.TextFrameForOverriding.Text = "";
IParagraph para = lbl.TextFrameForOverriding.Paragraphs[0];
para.Portions.Add(port);
lbl.DataLabelFormat.ShowSeriesName = false;
lbl.DataLabelFormat.ShowPercentage = false;
lbl.DataLabelFormat.ShowLegendKey = false;
lbl.DataLabelFormat.ShowCategoryName = false;
lbl.DataLabelFormat.ShowBubbleSize = false;
}
}
// 保存包含图表的演示文稿
presentation.Save("DisplayPercentageAsLabels_out.pptx", SaveFormat.Pptx);
为图表数据标签设置百分号
以下 C# 代码演示了如何为图表数据标签设置百分号:
// 创建 Presentation 类的实例
Presentation presentation = new Presentation();
// 通过索引获取幻灯片的引用
ISlide slide = presentation.Slides[0];
// 在幻灯片上创建 PercentsStackedColumn 图表
IChart chart = slide.Shapes.AddChart(ChartType.PercentsStackedColumn, 20, 20, 500, 400);
// 将 NumberFormatLinkedToSource 设置为 false
chart.Axes.VerticalAxis.IsNumberFormatLinkedToSource = false;
chart.Axes.VerticalAxis.NumberFormat = "0.00%";
chart.ChartData.Series.Clear();
int defaultWorksheetIndex = 0;
// 获取图表数据工作表
IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
// 添加新系列
IChartSeries series = chart.ChartData.Series.Add(workbook.GetCell(defaultWorksheetIndex, 0, 1, "Reds"), chart.Type);
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 1, 1, 0.30));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 2, 1, 0.50));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 3, 1, 0.80));
series.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 4, 1, 0.65));
// 设置系列的填充颜色
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red;
// 设置 LabelFormat 属性
series.Labels.DefaultDataLabelFormat.ShowValue = true;
series.Labels.DefaultDataLabelFormat.IsNumberFormatLinkedToSource = false;
series.Labels.DefaultDataLabelFormat.NumberFormat = "0.0%";
series.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FontHeight = 10;
series.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
series.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.White;
series.Labels.DefaultDataLabelFormat.ShowValue = true;
// 添加新系列
IChartSeries series2 = chart.ChartData.Series.Add(workbook.GetCell(defaultWorksheetIndex, 0, 2, "Blues"), chart.Type);
series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 1, 2, 0.70));
series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 2, 2, 0.50));
series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 3, 2, 0.20));
series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(defaultWorksheetIndex, 4, 2, 0.35));
// 设置填充类型和颜色
series2.Format.Fill.FillType = FillType.Solid;
series2.Format.Fill.SolidFillColor.Color = Color.Blue;
series2.Labels.DefaultDataLabelFormat.ShowValue = true;
series2.Labels.DefaultDataLabelFormat.IsNumberFormatLinkedToSource = false;
series2.Labels.DefaultDataLabelFormat.NumberFormat = "0.0%";
series2.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FontHeight = 10;
series2.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
series2.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.White;
// 将演示文稿写入磁盘
presentation.Save("SetDataLabelsPercentageSign_out.pptx", SaveFormat.Pptx);
设置距离轴的标签
以下 C# 代码演示了如何在处理来自轴的图表时设置标签距离类别轴的距离:
// 创建 Presentation 类的实例
Presentation presentation = new Presentation();
// 获取幻灯片的引用
ISlide sld = presentation.Slides[0];
// 在幻灯片上创建图表
IChart ch = sld.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
// 设置与轴的标签距离
ch.Axes.HorizontalAxis.LabelOffset = 500;
// 将演示文稿写入磁盘
presentation.Save("SetCategoryAxisLabelDistance_out.pptx", SaveFormat.Pptx);
调整标签位置
当您创建一个不依赖于任何轴的图表(例如饼图)时,图表的数据标签可能会靠得太近。在这种情况下,您必须调整数据标签的位置,以确保引导线能够清晰显示。
以下 C# 代码演示了如何在饼图上调整标签位置:
using (Presentation pres = new Presentation())
{
IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Pie, 50, 50, 200, 200);
IChartSeriesCollection series = chart.ChartData.Series;
IDataLabel label = series[0].Labels[0];
label.DataLabelFormat.ShowValue = true;
label.DataLabelFormat.Position = LegendDataLabelPosition.OutsideEnd;
label.X = 0.71f;
label.Y = 0.04f;
pres.Save("pres.pptx", SaveFormat.Pptx);
}