Aspose.Words 操作 Word 画 EChart 图( 二 )

【Aspose.Words 操作 Word 画 EChart 图】2、单双曲线图,饼图,柱状图
曲线图需要用到 InsertChart 方法指定图表类型 Line,这三种图区别不大方法一样,只需要指定图表类型
ChartSeriesCollection seriesCollection = chart.Series; 提供了向图表插入数据的操作,有点类似 List 如果你只有一条曲线(柱状图),写一个 Add 就行,如果是多条曲线(柱状图),则添加多个 Add 数据即可展示多条曲线(柱状图)

Aspose.Words 操作 Word 画 EChart 图

文章插图
Aspose.Words 操作 Word 画 EChart 图

文章插图
1 #region 曲线 2builder.MoveToBookmark("双曲线"); 3var shape = builder.InsertChart(ChartType.Line, 430, 210);//插入线形图 4Chart chart = shape.Chart; 5ChartSeriesCollection seriesCollection = chart.Series; 6seriesCollection.Clear();//清除默认 7chart.Title.Text = "双曲线"; 8chart.AxisX.TickLabelPosition = AxisTickLabelPosition.Low; 910dt = DBHelper.ExecuteDataTable("select top 5 TNAME,sal,sal+100 as SAL1 from TEACHER");1112var categories = new string[dt.Rows.Count];13var values = new double[dt.Rows.Count];14var values1 = new double[dt.Rows.Count];1516for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)17{18categories[rowIndex] = dt.Rows[rowIndex][0].ToString();19values[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][1]);20values1[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][2]);21}2223chart.Legend.Position = LegendPosition.Top;2425seriesCollection.Add("线条一", categories, values);//添加数据26seriesCollection.Add("线条二", categories, values1);//添加数据27#endregion2829#region 饼状图30dt = DBHelper.ExecuteDataTable("select top 1 sal,sal+100 as SAL1,sal+200 as SAL2 from TEACHER");3132List<CountModel> list1 = new List<CountModel>();33for (int i = 0; i < dt.Rows.Count; i++)34{35list1.Add(new CountModel { mark = "一", num = Convert.ToDouble(dt.Rows[i][0]) });36list1.Add(new CountModel { mark = "二", num = Convert.ToDouble(dt.Rows[i][1]) });37list1.Add(new CountModel { mark = "三", num = Convert.ToDouble(dt.Rows[i][2]) });38}3940builder.MoveToBookmark("饼状图");41var shape1 = builder.InsertChart(ChartType.Pie, 430, 210);//插入线形图42Chart chart1 = shape1.Chart;43ChartSeriesCollection seriesCollection1 = chart1.Series;44seriesCollection1.Clear();//清除默认45chart1.Title.Text = "饼状图";4647var categories2 = new string[list1.Count];48var values2 = new double[list1.Count];4950for (int rowIndex = 0; rowIndex < list1.Count; rowIndex++)51{52categories2[rowIndex] = list1[rowIndex].mark.ToString();53values2[rowIndex] = Convert.ToDouble(list1[rowIndex].num);54}5556chart1.Legend.Position = LegendPosition.Top;5758seriesCollection1.Add("饼状图", categories2, values2);//添加数据59#endregion6061#region 柱状图62dt = DBHelper.ExecuteDataTable("select top 5 tname,sal,sal+100 as SAL1,sal+200 as SAL2 from TEACHER");6364builder.MoveToBookmark("柱状图");65var shape2 = builder.InsertChart(ChartType.Column, 430, 210);//插入线形图66Chart chart2 = shape2.Chart;67ChartSeriesCollection seriesCollection3 = chart2.Series;68seriesCollection3.Clear();//清除默认69chart2.Title.Text = "柱状图";70chart2.AxisX.TickLabelPosition = AxisTickLabelPosition.Low;7172var categories3 = new string[dt.Rows.Count];73var values11 = new double[dt.Rows.Count];74var values22 = new double[dt.Rows.Count];75var values3 = new double[dt.Rows.Count];7677for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)78{79categories3[rowIndex] = dt.Rows[rowIndex][0].ToString();80values11[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][1]);81values22[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][2]);82values3[rowIndex] = Convert.ToDouble(dt.Rows[rowIndex][3]);83}8485chart2.Legend.Position = LegendPosition.Top;8687seriesCollection3.Add("一", categories3, values11);//添加数据88seriesCollection3.Add("二", categories3, values22);//添加数据89seriesCollection3.Add("三", categories3, values3);//添加数据90#endregion3、散点图
散点图跟其他图略有区别,散点图是 XY 的坐标点形式处理数据
Aspose.Words 操作 Word 画 EChart 图

文章插图
Aspose.Words 操作 Word 画 EChart 图

文章插图
1 #region 散点图 2var result = new List<SD>(); 3result.Add(new SD() { wd = 15, wy = 16 }); 4result.Add(new SD() { wd = 14, wy = 23 }); 5result.Add(new SD() { wd = 34, wy = 23 }); 6result.Add(new SD() { wd = 12, wy = 23 }); 7result.Add(new SD() { wd = 34, wy = 45 }); 8result.Add(new SD() { wd = 32, wy = 23 }); 9result.Add(new SD() { wd = 45, wy = 56 });10result.Add(new SD() { wd = 34, wy = 55 });1112builder.MoveToBookmark("散点图");13var shape3 = builder.InsertChart(ChartType.Scatter, 430, 210);//插入线形图14Chart chart3 = shape3.Chart;15ChartSeriesCollection seriesCollection4 = chart3.Series;16seriesCollection4.Clear();//清除默认17chart3.Title.Text = "散点图";18chart3.AxisX.TickLabelPosition = AxisTickLabelPosition.Low;1920var categories4 = new double[result.Count];21var values4 = new double[result.Count];2223for (int i = 0; i < result.Count; i++)24{25categories4[i] = Convert.ToDouble(result[i].wd);26values4[i] = Convert.ToDouble(result[i].wy);27}2829chart3.Legend.Position = LegendPosition.Top;30seriesCollection4.Add("散点图", categories4, values4);//添加数据31#endregion

推荐阅读