예제 #1
0
        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.TopRight;

            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>();

            // Use a category axis for the bottom axis.
            IChartAxis bottomAxis = chart.GetChartAxisFactory().CreateCategoryAxis(AxisPosition.Bottom);
            IValueAxis leftAxis   = chart.GetChartAxisFactory().CreateValueAxis(AxisPosition.Left);

            leftAxis.SetCrosses(AxisCrosses.AutoZero);

            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);
        }
예제 #2
0
        static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2, bool enableMajorGridline = false)
        {
            XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);

            chart.SetTitle("Test 1");
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.TopRight;

            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <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;

            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);
            //add major gridline, available since NPOI 2.5.5
            var plotArea = chart.GetCTChart().plotArea;

            plotArea.catAx[0].AddNewMajorGridlines();
            plotArea.valAx[0].AddNewMajorGridlines();
        }
예제 #3
0
        public void GenaretionChart()
        {
            FileStream RfileStream = new FileStream("D:\\test.xlsx", FileMode.Open, FileAccess.Read);
            //建立讀取資料的FileStream
            XSSFWorkbook wb = new XSSFWorkbook(RfileStream);
            //讀取檔案內的Workbook物件
            ISheet Wsheet = wb.GetSheetAt(1);
            //選擇圖表存放的sheet
            ISheet Rsheet = wb.GetSheetAt(0);
            //選擇資料來源的sheet
            IDrawing drawing = Wsheet.CreateDrawingPatriarch();
            //sheet產生drawing物件
            IClientAnchor clientAnchor = drawing.CreateAnchor(0, 0, 0, 0, 0, 0, 5, 10);
            //設定圖表位置
            IChart chart = drawing.CreateChart(clientAnchor);
            //產生chart物件
            IChartLegend legend = chart.GetOrCreateLegend();

            //還沒研究出這行在做甚麼
            legend.Position = LegendPosition.TopRight;
            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <double, double>();
            //產生存放資料的物件(資料型態為double)
            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
            //設定X軸
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            //設定Y軸
            bottomAxis.Crosses = AxisCrosses.AutoZero;
            //設定X軸數值開始為0
            leftAxis.Crosses = AxisCrosses.AutoZero;
            //設定Y軸數值開始為0
            IChartDataSource <double> xs = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 0, 0));
            //取得要讀取sheet的資料位置(CellRangeAddress(first_row,end_row, first_column, end_column))
            //x軸資料
            IChartDataSource <double> ys1 = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 1, 1));
            //第一條y軸資料
            IChartDataSource <double> ys2 = DataSources.FromNumericCellRange(Rsheet, new CellRangeAddress(0, 4, 2, 2));

            //第二條y軸資料
            data.AddSeries(xs, ys1);
            data.AddSeries(xs, ys2);
            //加入到data
            chart.Plot(data, bottomAxis, leftAxis);
            //加入到chart
            FileStream WfileStream = new FileStream("D:\\test.xlsx", FileMode.Create, FileAccess.Write);

            //建立寫入資料的FileStream
            wb.Write(WfileStream);
            //將workbook寫入資料
            RfileStream.Close();
            //關閉FileStream
            WfileStream.Close();
            //關閉FileStream
        }
예제 #4
0
        private IWorkbook CreateWorkbookWithChart()
        {
            IWorkbook wb             = new XSSFWorkbook();
            ISheet    sheet          = wb.CreateSheet("linechart");
            int       NUM_OF_ROWS    = 3;
            int       NUM_OF_COLUMNS = 10;

            // 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 anchor  = Drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            IChart       chart  = Drawing.CreateChart(anchor);
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = (/*setter*/ LegendPosition.TopRight);

            ILineChartData <double, double> data = chart.ChartDataFactory.CreateLineChartData <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 = (/*setter*/ AxisCrosses.AutoZero);

            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));

            data.AddSeries(xs, ys1);
            data.AddSeries(xs, ys2);

            chart.Plot(data, bottomAxis, leftAxis);

            return(wb);
        }
예제 #5
0
        private static void FillChartDataCells(ISheet sheet, int rowNum, IReadOnlyList <double> iterationsList, IChartDataSource <string> xAxis, ILineChartData <string, double> data)
        {
            var row = sheet.CreateRow(rowNum);

            for (int i = 0; i < iterationsList.Count; i++)
            {
                row.CreateCell(i).SetCellValue(iterationsList[i]);
            }

            var yAxis = DataSources.FromNumericCellRange(sheet,
                                                         new CellRangeAddress(rowNum, rowNum, 0, iterationsList.Count - 1));

            data.AddSeries(xAxis, yAxis).SetTitle(((Methods)(rowNum - 1)).ToString());
        }
예제 #6
0
        public void TestOneSeriePlot()
        {
            IWorkbook     wb      = new XSSFWorkbook();
            ISheet        sheet   = new SheetBuilder(wb, plotData).Build();
            IDrawing      Drawing = sheet.CreateDrawingPatriarch();
            IClientAnchor anchor  = Drawing.CreateAnchor(0, 0, 0, 0, 1, 1, 10, 30);
            IChart        chart   = Drawing.CreateChart(anchor);

            IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);
            IChartAxis leftAxis   = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            ILineChartData <string, double> lineChartData =
                chart.ChartDataFactory.CreateLineChartData <string, double>();

            IChartDataSource <String>         xs     = DataSources.FromStringCellRange(sheet, CellRangeAddress.ValueOf("A1:J1"));
            IChartDataSource <double>         ys     = DataSources.FromNumericCellRange(sheet, CellRangeAddress.ValueOf("A2:J2"));
            ILineChartSeries <string, double> series = lineChartData.AddSeries(xs, ys);

            Assert.IsNotNull(series);
            Assert.AreEqual(1, lineChartData.GetSeries().Count);
            Assert.IsTrue(lineChartData.GetSeries().Contains(series));

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