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

推薦閱讀