图表是指将数据用图形的形式表示出来,常见的图表类型有柱形图、饼状图、条形图、折线图和组合图等。Spire.Presentation支持多达70种不同样式的图表类型,本文将以柱形图、圆环图(饼状图)和组合图为例,介绍如何在幻灯片上创建图表。
创建柱形图
C#
//创建一个PowerPoint文档
Presentation presentation = new Presentation();
//插入柱形图
RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);
//添加表名
chart.ChartTitle.TextProperties.Text = "部门信息";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;
//定义一个sting[,]数组
string[,] data = new string[,]
{
{"部门名称","男性员工","女性员工" },
{"开发部","25","15"},
{"测试部","5","10" },
{"销售部","7","3" },
{"技术支持部","20","5" }
};
//将数据写入图表后台数据表
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", "C1"];
//设置类别标签
chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];
//为各个系列赋值
chart.Series[0].Values = chart.ChartData["B2", "B5"];
chart.Series[1].Values = chart.ChartData["C2", "C5"];
//应用内置图标样式
chart.ChartStyle = ChartStyle.Style11;
//设置系列重叠
chart.OverLap = -50;
//设置类别间距
chart.GapWidth = 200;
//保存文档
presentation.SaveToFile("柱形图.pptx", FileFormat.Pptx2010);
VB.NET
'创建一个PowerPoint文档
Dim presentation As New Presentation()
'插入柱形图
Dim rect As New RectangleF(40, 100, 550, 320)
Dim chart As IChart = presentation.Slides(0).Shapes.AppendChart(ChartType.ColumnClustered, rect)
'添加表名
chart.ChartTitle.TextProperties.Text = "部门信息"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 30
chart.HasTitle = True
'定义一个sting[,]数组
Dim data As String(,) = New String(,) {{"部门名称", "男性员工", "女性员工"}, {"开发部", "25", "15"}, {"测试部", "5", "10"}, {"销售部", "7", "3"}, {"技术支持部", "20", "5"}}
'将数据写入图表后台数据表
For i As Integer = 0 To data.GetLength(0) - 1
For j As Integer = 0 To data.GetLength(1) - 1
'将数字类型的字符串转换为整数
Dim number As Integer
Dim result As Boolean = Int32.TryParse(data(i, j), number)
If result Then
chart.ChartData(i, j).Value = number
Else
chart.ChartData(i, j).Value = data(i, j)
End If
Next
Next
'设置系列标签
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")
'应用内置图标样式
chart.ChartStyle = ChartStyle.Style11
'设置系列重叠
chart.OverLap = -50
'设置类别间距
chart.GapWidth = 200
'保存文档
presentation.SaveToFile("柱形图.pptx", FileFormat.Pptx2010)
创建圆环图
C#
//创建一个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);
VB.NET
'创建一个PowerPoint文件
Dim presentation As New Presentation()
'插入圆环图
Dim rect As New RectangleF(40, 100, 550, 320)
Dim chart As IChart = 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
'定义数据
Dim countries As String() = New String() {"古巴", "墨西哥", "法国", "德国"}
Dim sales As Integer() = New Integer() {1800, 3000, 5100, 6200}
'将数据写入图表后台数据表
chart.ChartData(0, 0).Text = "国家"
chart.ChartData(0, 1).Text = "销售额"
For i As Integer = 0 To countries.Length - 1
chart.ChartData(i + 1, 0).Value = countries(i)
chart.ChartData(i + 1, 1).Value = sales(i)
Next
'设置系列标签
chart.Series.SeriesLabel = chart.ChartData("B1", "B1")
'设置分类标签
chart.Categories.CategoryLabels = chart.ChartData("A2", "A5")
'为系列赋值
chart.Series(0).Values = chart.ChartData("B2", "B5")
'添加点到系列
For i As Integer = 0 To chart.Series(0).Values.Count - 1
Dim cdp As New ChartDataPoint(chart.Series(0))
cdp.Index = i
chart.Series(0).DataPoints.Add(cdp)
Next
'为系列里的个点添加背景颜色
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)
创建混合图表
C#
//新建一个PowerPoint文档
Presentation presentation = new Presentation();
//插入柱形图
RectangleF rect = new RectangleF(40, 100, 550, 320);
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ColumnClustered, rect);
//添加表名
chart.ChartTitle.TextProperties.Text = "月销售报表";
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.Rows.Add("5月", 200, 0.5);
dataTable.Rows.Add("6月", 400, 0.9);
//将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", "A7"];
//为系列赋值
chart.Series[0].Values = chart.ChartData["B2","B7"];
chart.Series[1].Values = chart.ChartData["C2","C7"];
//将系列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);
VB.NET
'新建一个PowerPoint文档
Dim presentation As New Presentation()
'插入柱形图
Dim rect As New RectangleF(40, 100, 550, 320)
Dim chart As IChart = presentation.Slides(0).Shapes.AppendChart(ChartType.ColumnClustered, rect)
'添加表名
chart.ChartTitle.TextProperties.Text = "月销售报表"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 30
chart.HasTitle = True
'创建一个DataTable
Dim dataTable As 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.Rows.Add("5月", 200, 0.5)
dataTable.Rows.Add("6月", 400, 0.9)
'将DataTable数据导入图表后台数据表
For c As Integer = 0 To dataTable.Columns.Count - 1
chart.ChartData(0, c).Text = dataTable.Columns(c).Caption
Next
For r As Integer = 0 To dataTable.Rows.Count - 1
Dim datas As Object() = dataTable.Rows(r).ItemArray
For c As Integer = 0 To datas.Length - 1
chart.ChartData(r + 1, c).Value = datas(c)
Next
Next
'设置系列标签
chart.Series.SeriesLabel = chart.ChartData("B1", "C1")
'设置类别标签
chart.Categories.CategoryLabels = chart.ChartData("A2", "A7")
'为系列赋值
chart.Series(0).Values = chart.ChartData("B2", "B7")
chart.Series(1).Values = chart.ChartData("C2", "C7")
'将系列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)