c# Aspose.Words插入餅圖PieChart


        private static void Main(string[] args)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.Writeln("普通餅圖:");
            AddPieChart(builder);
            builder.Writeln();
            builder.Writeln("3D餅圖:");
            AddPie3dChart(builder);
            doc.Save("abc.docx");
        }

        /// <summary>
        /// 添加餅圖
        /// </summary>
        private static void AddPieChart(DocumentBuilder builder)
        {
            AddPieChart(builder,ChartType.Pie,"普通餅圖");
        }
        /// <summary>
        /// 添加3D餅圖
        /// </summary>
        private static void AddPie3dChart(DocumentBuilder builder)
        {
            AddPieChart(builder,ChartType.Pie3D,"3D餅圖");
        }

        private static void AddPieChart(DocumentBuilder builder, ChartType chatType, string title)
        {
            var shape = builder.InsertChart(chatType, 200, 200);
            Chart chart = shape.Chart;

            ChartSeriesCollection seriesCollection = chart.Series;
            seriesCollection.Clear();//清除默認
            //類別
            var categories = new string[]
            {
                "1", "2", "3", "4"
            };
            //
            var values = new double[]
            {
                20, 20, 30, 40
            };
            
            chart.Legend.Position= LegendPosition.Right;//顯示在右邊
            //添加數據
            var chatSeries = seriesCollection.Add(title,categories , values);
            //設置數據顯示
            SetChartSeriesDataLabel(chatSeries,categories.Count());
        }

        private static void SetChartSeriesDataLabel(ChartSeries chatSeries,int count)
        {
             ChartDataLabelCollection dataLabelCollection = chatSeries.DataLabels;
            for (int i = 0; i < count; i++)
            {
                ChartDataLabel chartDataLabel = dataLabelCollection.Add(i);
                chartDataLabel.ShowLegendKey = true;
                chartDataLabel.ShowLeaderLines = true;
                chartDataLabel.ShowCategoryName = true;
                chartDataLabel.ShowPercentage = true;
                chartDataLabel.ShowSeriesName = false;
                chartDataLabel.ShowValue = true;
                chartDataLabel.Separator = "  ";
            }
        }
說明:目前還有一個問題,就是通過aspose.words插入的圖表目前還不支持右鍵編輯數據功能,這個查看了官方的回復,說現階段還不支持,具體的可以參考一下地址說明
https://forum.aspose.com/t/unable-to-edit-chart-data-manually-in-word-chart-using-apose-word-for-net-15-6-0/44166
https://forum.aspose.com/t/how-to-enable-data-editing-of-chart-in-ms-word/24575

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM