.NetCore下使用NPOI绘制统计图表()

 

 

        const int NUM_OF_ROWS = 3;
        const int NUM_OF_COLUMNS = 10;
        public static void CreateExcel()
        {
            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();

           //图表的位置锚点

//dx1:起始单元格的x偏移量;
//dy1:起始单元格的y偏移量;
//dx2:终止单元格的x偏移量;
//dy2:终止单元格的y偏移量;
//col1:起始单元格列序号;
//row1:起始单元格行序号;
//col2:终止单元格列序号;
//row2:终止单元格行序号;
            IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);
            CreateChart(drawing, sheet, anchor1, "title1", "title2");

           //图表的位置锚点

           //dx1:起始单元格的x偏移量;
//dy1:起始单元格的y偏移量;
//dx2:终止单元格的x偏移量;
//dy2:终止单元格的y偏移量;
//col1:起始单元格列序号;
//row1:起始单元格行序号;
//col2:终止单元格列序号;
//row2:终止单元格行序号;


            IClientAnchor anchor2 = drawing.CreateAnchor(0, 0, 0, 0, 0, 20, 10, 35);

            
            CreateChart(drawing, sheet, anchor2, "s1", "s2");
            using (FileStream fs = File.Create("test5.xlsx"))
            {
                wb.Write(fs);

            }

        }

   //serie1 图表统计项标题  serie2 图表统计项标题
            static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2)
        {
            IChart chart = drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();
            //标题位置
            legend.Position = LegendPosition.Top;

            IBarChartData<double, double> data = chart.ChartDataFactory.CreateBarChartData<double, double>();

            // Use a category axis for the bottom axis.
            //横坐标
            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
            //纵坐标
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);


            leftAxis.Crosses = AxisCrosses.AutoZero;//AxisCrosses.Max 则纵坐标在右侧

            //X 轴名称
            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);
        }
 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值