组合图表中至少含有两种不同类型的图表样式,常见的组合图表如柱状图和折线图的组合,能将不同类型的数据分别显示到主轴和次轴。本文将展示如何使用Spire.Presentation在PPT中创建柱状加折线的组合图表。
C#
//创建Presentation对象
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("一月", 200, 0);
dataTable.Rows.Add("二月", 250, 0.25);
dataTable.Rows.Add("三月", 300, 0.20);
dataTable.Rows.Add("四月", 150, -0.5);
dataTable.Rows.Add("五月", 200, 0.33);
dataTable.Rows.Add("六月", 400, 1);
//将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"];
//将系列二的图表类型设置为了折线图
chart.Series[1].Type = ChartType.LineMarkers;
//将系列二显示到第二个轴
chart.Series[1].UseSecondAxis = true;
//设置第二个轴的数字格式为百分数
chart.SecondaryValueAxis.NumberFormat = "0%";
//隐藏第二个轴的主网格线
chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;
//设置系列重叠
chart.OverLap = -50;
//设置分类间距
chart.GapWidth = 200;
//保存文档
presentation.SaveToFile("output.pptx", FileFormat.Pptx2010);
VB.NET
'创建Presentation对象
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("一月", 200, 0)
dataTable.Rows.Add("二月", 250, 0.25)
dataTable.Rows.Add("三月", 300, 0.2)
dataTable.Rows.Add("四月", 150, -0.5)
dataTable.Rows.Add("五月", 200, 0.33)
dataTable.Rows.Add("六月", 400, 1)
'将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")
'将系列二的图表类型设置为了折线图
chart.Series(1).Type = ChartType.LineMarkers
'将系列二显示到第二个轴
chart.Series(1).UseSecondAxis = True
'设置第二个轴的数字格式为百分数
chart.SecondaryValueAxis.NumberFormat = "0%"
'隐藏第二个轴的主网格线
chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None
'设置系列重叠
chart.OverLap = -50
'设置分类间距
chart.GapWidth = 200
'保存文档
presentation.SaveToFile("output.pptx", FileFormat.Pptx2010)