温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# 创建PPT图表

发布时间:2020-07-03 14:11:21 来源:网络 阅读:807 作者:E_iceblue 栏目:编程语言

图表能够很直观的表现数据在某个时间段的变化趋势,或者呈现数据的整体和局部之间的相互关系,相较于大篇幅的文本数据,图表更增加了我们分析数据时选择的多样性,是我们挖掘数据背后潜在价值的一种更为有效地方式。在做数据汇报时,常用到PPT幻灯片来辅助工作,下面的示例中将演示如何通过C#编程在PPT幻灯片中创建图表。示例中主要介绍了三种图表的创建方法,如下:

  1. 创建柱形图表
  2. 创建饼状图表
  3. 创建混合型图表(柱形图、折线图)

使用工具:Spire.Presentation for .NET
PS:下载安装后,注意添加引用Spire.Presentation.dll到程序,dll文件可在安装路径下的Bin文件夹中获取。
C# 创建PPT图表

【示例 1 】创建柱形图表

using Spire.Presentation;
using Spire.Presentation.Charts;
using System;
using System.Drawing;

namespace ColumnChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个PowerPoint文档
            Presentation presentation = new Presentation();

            //插入柱形图
            RectangleF rect = new RectangleF(40, 50, 680, 500);
            IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.Column3DClustered, rect);

            //添加图表名
            chart.ChartTitle.TextProperties.Text = "2018年上半年销量";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            //定义一个sting[,]数组
            string[,] data = new string[,]
             {
              {"产品大类","1月","2月","3月","4月","5月","6月" },
              {"DW10","1542","1057","1223","1302","1145","1336"},
              {"ZQ13","4587","3658","2515","3154","2984","3890" },
              {"YI73","558","458","369","576","334","482" },
              {"TR11","2011","2485" ,"3010" ,"2785" ,"2225" ,"2476" }
             };

            //将数据写入图表后台数据表
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    //将数字类型的字符串转换为整数
                    int number;
                    bool result = Int32.TryParse(data[i, j], out number);
                    if (result)
                    {
                        chart.ChartData[i, j].Value = number;
                    }
                    else
                    {
                        chart.ChartData[i, j].Value = data[i, j];
                    }
                }
            }

            //设置系列标签
            chart.Series.SeriesLabel = chart.ChartData["B1", "G1"];

            //设置类别标签
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];

            //为各个系列赋值
            chart.Series[0].Values = chart.ChartData["B2", "B5"];
            chart.Series[1].Values = chart.ChartData["C2", "C5"];
            chart.Series[2].Values = chart.ChartData["D2", "D5"];
            chart.Series[3].Values = chart.ChartData["E2", "E5"];
            chart.Series[4].Values = chart.ChartData["F2", "F5"];
            chart.Series[5].Values = chart.ChartData["G2", "G5"];

            //应用内置图标样式
            chart.ChartStyle = ChartStyle.Style12;

            //设置系列重叠
            chart.OverLap = -50;

            //设置类别间距
            chart.GapWidth = 200;

            //保存并打开文档
            presentation.SaveToFile("柱形图.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("柱形图.pptx");
        }
    }
}

调试运行程序后,生成图表,如下图:
C# 创建PPT图表

【示例 2 】创建环形图表

using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace DoughnutChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个PowerPoint文件
            Presentation presentation = new Presentation();

            //插入圆环图
            RectangleF rect = new RectangleF(40, 100, 550, 320);
            IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.Doughnut, rect, false);

            //设置图表名
            chart.ChartTitle.TextProperties.Text = "市场份额";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            //定义数据
            string[] countries = new string[] { "古巴", "墨西哥", "法国", "德国" };
            int[] sales = new int[] { 1800, 3000, 5100, 6200 };

            //将数据写入图表后台数据表
            chart.ChartData[0, 0].Text = "国家";
            chart.ChartData[0, 1].Text = "销售额";
            for (int i = 0; i < countries.Length; ++i)
            {
                chart.ChartData[i + 1, 0].Value = countries[i];
                chart.ChartData[i + 1, 1].Value = sales[i];
            }

            //设置系列标签
            chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];

            //设置分类标签
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];

            //为系列赋值
            chart.Series[0].Values = chart.ChartData["B2", "B5"];

            //添加点到系列
            for (int i = 0; i < chart.Series[0].Values.Count; i++)
            {
                ChartDataPoint cdp = new ChartDataPoint(chart.Series[0]);
                cdp.Index = i;
                chart.Series[0].DataPoints.Add(cdp);
            }

            //为系列里的个点添加背景颜色
            chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.LightBlue;
            chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.MediumPurple;
            chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.DarkGray;
            chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.DarkOrange;

            //设置标签显示数值
            chart.Series[0].DataLabels.LabelValueVisible = true;

            //设置标签显示百分比
            chart.Series[0].DataLabels.PercentValueVisible = true;

            //设置圆环内径大小
            chart.Series[0].DoughnutHoleSize = 60;

            //保存文档
            presentation.SaveToFile("环形图.pptx", FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("环形图.pptx");
        }
    }
}

环形图创建效果:
C# 创建PPT图表

【示例 3 】创建混合型图表

using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System;
using System.Data;
using System.Drawing;

namespace 混合图表
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个PowerPoint文档
            Presentation presentation = new Presentation();

            //插入柱形图
            RectangleF rect = new RectangleF(40, 100, 650, 320);
            IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);

            //添加表名
            chart.ChartTitle.TextProperties.Text = "2017季度销售情况";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            //创建一个DataTable
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("季度", Type.GetType("System.String")));
            dataTable.Columns.Add(new DataColumn("销售额", Type.GetType("System.Int32")));
            dataTable.Columns.Add(new DataColumn("同比增长率", Type.GetType("System.Decimal")));
            dataTable.Rows.Add("1季度", 200, 0.6);
            dataTable.Rows.Add("2季度", 250, 0.8);
            dataTable.Rows.Add("3季度", 300, 0.6);
            dataTable.Rows.Add("4季度", 150, 0.2);            

            //将DataTable数据导入图表后台数据表
            for (int c = 0; c < dataTable.Columns.Count; c++)
            {
                chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
            }
            for (int r = 0; r < dataTable.Rows.Count; r++)
            {
                object[] datas = dataTable.Rows[r].ItemArray;
                for (int c = 0; c < datas.Length; c++)
                {
                    chart.ChartData[r + 1, c].Value = datas[c];

                }
            }

            //设置系列标签
            chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];

            //设置类别标签      
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];

            //为系列赋值
            chart.Series[0].Values = chart.ChartData["B2", "B5"];
            chart.Series[1].Values = chart.ChartData["C2", "C5"];

            //将系列2的图表类型改为折线图
            chart.Series[1].Type = ChartType.LineMarkers;

            //将系列2显示到第二根轴
            chart.Series[1].UseSecondAxis = true;

            //显示百分比数据
            chart.SecondaryValueAxis.NumberFormat = "0%";

            //不显示第二根轴的网格线
            chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;

            //设置系列重叠
            chart.OverLap = -50;

            //设置类别间距
            chart.GapWidth = 200;

            //保存打开文档
            presentation.SaveToFile("混合图表.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("混合图表.pptx");
        }
    }
}

图表创建结果:
C# 创建PPT图表
注:Spire.Presentation 支持创建多大73种不同的图表样式,如下图
C# 创建PPT图表
以上是本次关于“C# 创建PPT图表”的全部内容。
如需转载,请注明出处。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI