我在VS中创建了一段代码,用于在PowerPoint中新建一个柱形图(chart),但是它的样式总是没有创建,我不知道是什么原因导致了这件事情的发生,这是我的代码:
public static void EditChart(Options options, IChart chart)
{
try
{
if (options.ChartData != null && !string.IsNullOrEmpty(options.ChartData.Title))
{
chart.ChartTitle.AddTextFrameForOverriding(options.ChartData.Title);
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.HasTitle = true;
}
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
int defaultWorksheetIndex = 0;
// 添加Y轴的图例(对应Excel中的第一行)
if (options.ChartData != null && options.ChartData.Legends != null)
{
chart.ChartData.Series.Clear();
for (int i = 0; i < options.ChartData.Legends.Length; i++)
{
string item = options.ChartData.Legends[i];
IChartSeries chartSeries = chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, i + 1, item), chart.Type);
// 如果是圆环图的话,设置圆环的洞的大小
if (chart.Type == ChartType.Doughnut)
{
chartSeries.ParentSeriesGroup.DoughnutHoleSize = 60;
}
}
}
// 设置图例位置:上方或者下方
chart.Legend.Position = !string.IsNullOrEmpty(options.Legend) ? Enum.Parse<LegendPositionType>(options.Legend, true) : LegendPositionType.Bottom;
// 添加X轴的图例(对应Excel中的第一列)
if (options.ChartData != null && options.ChartData.Labels != null)
{
chart.ChartData.Categories.Clear();
for (int i = 0; i < options.ChartData.Labels.Length; i++)
{
string item = options.ChartData.Labels[i];
IChartCategory chartCategory = chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, i + 1, 0, item));
}
}
// 设置区域颜色
if (options.ThemeColor != null && options.ThemeColor.Length > 0)
{
for (int i = 0; i < chart.ChartData.Series.Count; i++)
{
string color = options.ThemeColor.FirstOrDefault();
if (options.ThemeColor.Length > i)
{
color = options.ThemeColor[i];
}
// 设置颜色
IChartSeries chartSeries = chart.ChartData.Series[i];
chartSeries.Format.Fill.FillType = FillType.Solid;
chartSeries.Format.Fill.SolidFillColor.Color = ColorTransform(color);
}
}
// 向图表中添加具体的值
if (options.ChartData != null && chart.ChartData.Series.Count > 0 && options.ChartData.Series != null && options.ChartData.Series.Count > 0)
{
int column = 1;
for (int i = 0; i < chart.ChartData.Series.Count; i++)
{
int row = 1;
IChartSeries series = chart.ChartData.Series[i];
List<double> valueList = options.ChartData.Series[i];
foreach (double value in valueList)
{
IChartDataPoint dataPoint = null;
switch (chart.Type)
{
// 柱状图
case ChartType.ClusteredColumn:
dataPoint = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
break;
// 条形图
case ChartType.ClusteredBar:
dataPoint = series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
break;
// 折线图
case ChartType.Line:
dataPoint = series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
break;
// 面积图
case ChartType.Area:
dataPoint = series.DataPoints.AddDataPointForAreaSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
break;
//// 散点图:实际上是折线图,只是没有连接线(前端已隐藏,此分支不会在运行)
//case ChartType.ScatterWithMarkers:
// dataPoint = series.DataPoints.AddDataPointForLineSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
// break;
// 饼状图
case ChartType.Pie:
dataPoint = series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
dataPoint.Format.Fill.FillType = FillType.Solid;
dataPoint.Format.Fill.SolidFillColor.Color = ColorTransform(options.ThemeColor[row - 1]);
break;
// 圆环图
case ChartType.Doughnut:
dataPoint = series.DataPoints.AddDataPointForDoughnutSeries(fact.GetCell(defaultWorksheetIndex, row, column, value));
dataPoint.Format.Fill.FillType = FillType.Solid;
dataPoint.Format.Fill.SolidFillColor.Color = ColorTransform(options.ThemeColor[row - 1]);
break;
default:
break;
}
dataPoint.Label.DataLabelFormat.ShowValue = true;
row++;
}
column++;
}
}
}
catch (Exception ex)
{
_ = ex.Message;
}
}
在PowerPoint中创建之后的样子是这样的:
屏幕截图 2024-08-21 185307.png (13.3 KB)
如果有了解的请帮助我解决一下,谢谢你们^_^