Aspose.Slides 创建柱形图,怎么应用样式?

我在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)

如果有了解的请帮助我解决一下,谢谢你们^_^

如果是在PowerPoint中点击创建图表的话,它是这样的
屏幕截图 2024-08-21 190212.png (39.2 KB)

@suicheng,
感谢您联系支持团队。我正在处理这个问题,很快会回复您。

@suicheng,
感谢您的耐心等待。请尝试使用以下代码示例:

using (Presentation presentation = new Presentation())
{
    ISlide firstSlide = presentation.Slides.First();

    IChart chart = firstSlide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
    chart.ChartData.Series.Clear();
    chart.ChartData.Categories.Clear();

    IChartDataWorkbook workbook = chart.ChartData.ChartDataWorkbook;
    int worksheetIndex = 0;

    chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 1, 0, "Category 1"));
    chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 2, 0, "Category 2"));
    chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 3, 0, "Category 3"));
    chart.ChartData.Categories.Add(workbook.GetCell(worksheetIndex, 4, 0, "Category 4"));

    IChartSeries series1 = chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 0, 1, "Series 1"), chart.Type);

    series1.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 1, 4.3));
    series1.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 1, 2.5));
    series1.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 1, 3.5));
    series1.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 4, 1, 4.5));

    IChartSeries series2 = chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 0, 2, "Series 2"), chart.Type);

    series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 2, 2.3));
    series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 2, 4.3));
    series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 2, 1.8));
    series2.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 4, 2, 2.8));

    IChartSeries series3 = chart.ChartData.Series.Add(workbook.GetCell(worksheetIndex, 0, 3, "Series 3"), chart.Type);

    series3.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 1, 3, 2));
    series3.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 2, 3, 2));
    series3.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 3, 3, 3));
    series3.DataPoints.AddDataPointForBarSeries(workbook.GetCell(worksheetIndex, 4, 3, 5));

    // Chart title
    chart.HasTitle = true;
    chart.ChartTitle.Overlay = false;
    ITextFrame chartTitle = chart.ChartTitle.AddTextFrameForOverriding("Chart Title");
    IPortionFormat titleFormat = chartTitle.Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
    titleFormat.FontHeight = 18;
    titleFormat.FontBold = NullableBool.False;
    titleFormat.FillFormat.FillType = FillType.Solid;
    titleFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(89, 89, 89);

    // Chart legend
    chart.Legend.Position = LegendPositionType.Bottom;

    // Colors of the series 
    series1.Format.Fill.FillType = FillType.Solid;
    series1.Format.Fill.SolidFillColor.Color = Color.FromArgb(72, 116, 203);

    series2.Format.Fill.FillType = FillType.Solid;
    series2.Format.Fill.SolidFillColor.Color = Color.FromArgb(238, 130, 47);

    series3.Format.Fill.FillType = FillType.Solid;
    series3.Format.Fill.SolidFillColor.Color = Color.FromArgb(242, 186, 2);

    // Gap between columns
    series1.ParentSeriesGroup.Overlap = -27;
    series1.ParentSeriesGroup.GapWidth = 219;

    // Horizontal axis line
    chart.Axes.HorizontalAxis.Format.Line.FillFormat.FillType = FillType.Solid;
    chart.Axes.HorizontalAxis.Format.Line.FillFormat.SolidFillColor.Color = Color.FromArgb(217, 217, 217);
    chart.Axes.HorizontalAxis.MajorTickMark = TickMarkType.None;

    // Horizontal axis values
    chart.Axes.HorizontalAxis.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
    chart.Axes.HorizontalAxis.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(89, 89, 89);
    chart.Axes.HorizontalAxis.TextFormat.PortionFormat.FontHeight = 15;

    // Vertical axis line
    chart.Axes.VerticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;

    // Vertical axis values
    chart.Axes.VerticalAxis.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
    chart.Axes.VerticalAxis.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(89, 89, 89);
    chart.Axes.VerticalAxis.TextFormat.PortionFormat.FontHeight = 15;

    // Vertical axis grid lines
    chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.Solid;
    chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.FillFormat.SolidFillColor.Color = Color.FromArgb(217, 217, 217);

    // Series styles
    chart.Legend.TextFormat.PortionFormat.FontHeight = 15;
    chart.Legend.TextFormat.PortionFormat.FillFormat.FillType = FillType.Solid;
    chart.Legend.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(89, 89, 89);

    presentation.Save("output.pptx", SaveFormat.Pptx);
}

我的结果:output.png (72.8 KB)

更多示例: PowerPoint Charts

1 Like

@andrey.potapov

感谢您提供的代码,很遗憾我现在才看懂如何去使用,以及它达成的效果。它确实是起作用的。
重点在于这几句代码:

// 对水平坐标轴进行一些设置
if (chart.Axes.HorizontalAxis != null)
{
    // 设置水平坐标轴的线条为“无线条”
    chart.Axes.HorizontalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;

    // 设置水平网格线的主要网格线线条为“无线条”
    chart.Axes.HorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;

    // 设置水平网格线的次要网格线线条为“无线条”
    chart.Axes.HorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
}

// 对垂直坐标轴进行一些设置
if (chart.Axes.VerticalAxis != null)
{
    // 设置垂直坐标轴的线条为“无线条”
    chart.Axes.VerticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;

    // 设置垂直网格线的主要网格线线条为“实线”
    chart.Axes.VerticalAxis.MajorGridLinesFormat.Line.DashStyle = LineDashStyle.Solid;

    // 设置垂直网格线的次要网格线线条为“无线条”
    chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
}

再次感谢您的帮助🙏

@suicheng,
我们很高兴您的问题已经解决。感谢您使用Aspose.Slides。