NPOI 中繪製 Chart

 

NPOI 在新版本中增長了對圖表的有限支持(僅  xlsx 文件)git

項目地址: https://github.com/tonyqus/npoigithub

支持折線圖和散點圖blog

來看 demo :string

     const int NUM_OF_ROWS = 3;
           const int NUM_OF_COLUMNS = 10;

          static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2)
        {
            var chart = drawing.CreateChart(anchor) as XSSFChart;   
           //生成圖例
            var legend = chart.GetOrCreateLegend();
       //圖例位置
            legend.Position = LegendPosition.TopRight;
            
           //圖表
            var data = chart.ChartDataFactory.CreateLineChartData<double, double>(); //折線圖
       //var data = chart.ChartDataFactory.CreateScatterChartData<double, double>(); //散點圖

            // X軸.
            var bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);            
            bottomAxis.IsVisible = false; //默認爲true 不顯示  設置爲fase 顯示座標軸(BUG?)
            
            //Y軸
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);
            leftAxis.Crosses = (AxisCrosses.AutoZero);
            leftAxis.IsVisible = false; //設置顯示座標軸

            //數據源
            IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
            
            IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
            IChartDataSource<double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
            
            //數據系列
            var s1 = data.AddSeries(xs, ys1);
            s1.SetTitle(serie1);
            var s2 = data.AddSeries(xs, ys2);
            s2.SetTitle(serie2);


            chart.Plot(data, bottomAxis, leftAxis);          
            
        }

        static void Main(string[] args)
        {
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("linechart");

            // Create a row and put some cells in it. Rows are 0 based.
            IRow row;
            ICell cell;
            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++)
            {
                row = sheet.CreateRow((short)rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++)
                {
                    cell = row.CreateCell((short)colIndex);
                    cell.SetCellValue(colIndex * (rowIndex + 1));
                }
            }

            IDrawing drawing = sheet.CreateDrawingPatriarch();
            //錨點
            IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);
            CreateChart(drawing, sheet, anchor1, "title1","title2");
            IClientAnchor anchor2 = drawing.CreateAnchor(0, 0, 0, 0, 0, 20, 10, 35);
            CreateChart(drawing, sheet, anchor2, "s1", "s2");
            using (FileStream fs =File.Create("test.xlsx"))
            {
                wb.Write(fs);
            }

        }

  

 

 

不少特性都還不支持,這是比較難受的.將就用吧it

相關文章
相關標籤/搜索