예제 #1
0
        public void TestAddChartsToNewWorkbook()
        {
            XSSFWorkbook     wb = new XSSFWorkbook();
            XSSFSheet        s1 = (XSSFSheet)wb.CreateSheet();
            XSSFDrawing      d1 = (XSSFDrawing)s1.CreateDrawingPatriarch();
            XSSFClientAnchor a1 = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 10, 30);
            XSSFChart        c1 = (XSSFChart)d1.CreateChart(a1);

            Assert.AreEqual(1, d1.GetCharts().Count);

            Assert.IsNotNull(c1.GetGraphicFrame());
            Assert.IsNotNull(c1.GetOrCreateLegend());

            XSSFClientAnchor a2 = new XSSFClientAnchor(0, 0, 0, 0, 1, 11, 10, 60);
            XSSFChart        c2 = (XSSFChart)d1.CreateChart(a2);

            Assert.IsNotNull(c2);
            Assert.AreEqual(2, d1.GetCharts().Count);
            Assert.IsNotNull(XSSFTestDataSamples.WriteOutAndReadBack(wb));
        }
예제 #2
0
        public void TestAccessMethods()
        {
            XSSFWorkbook     wb      = new XSSFWorkbook();
            XSSFSheet        sheet   = wb.CreateSheet() as XSSFSheet;
            XSSFDrawing      Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing;
            XSSFClientAnchor anchor  = Drawing.CreateAnchor(0, 0, 0, 0, 1, 1, 10, 30) as XSSFClientAnchor;
            XSSFChart        chart   = Drawing.CreateChart(anchor) as XSSFChart;
            XSSFCategoryAxis axis    = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom) as XSSFCategoryAxis;

            axis.Crosses = (AxisCrosses.AutoZero);
            Assert.AreEqual(axis.Crosses, AxisCrosses.AutoZero);

            Assert.AreEqual(chart.GetAxis().Count, 1);
        }
        static void CreateChart(ISheet sheet)
        {
            XSSFDrawing      drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFClientAnchor anchor  = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 3, 0, 10, 15);

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

            legend.Position = LegendPosition.Bottom;

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

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

            bottomAxis.MajorTickMark = AxisTickMark.None;
            IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);

            leftAxis.Crosses = AxisCrosses.AutoZero;
            leftAxis.SetCrossBetween(AxisCrossBetween.Between);

            IChartDataSource <string> categories = DataSources.FromStringCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 0, 0));

            // Abono
            IChartDataSource <double> xAbono = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 1, 1));
            // Isento
            IChartDataSource <double> xIsento = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 2, 2));
            // Violação
            IChartDataSource <double> xViolacao = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, GetPracas().Count() - 1, 3, 3));

            var s1 = data.AddSeries(categories, xAbono);

            s1.SetTitle("Abono");

            var s2 = data.AddSeries(categories, xIsento);

            s2.SetTitle("Isento");

            var s3 = data.AddSeries(categories, xViolacao);

            s3.SetTitle("Violação");

            chart.Plot(data, bottomAxis, leftAxis);
        }
        private XSSFWorkbook CreateBarchart()
        {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet    sheet    = (XSSFSheet)workbook.CreateSheet("WorkForceAnalytics");

            XSSFCellStyle styleHeader = (XSSFCellStyle)workbook.CreateCellStyle();

            styleHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            //styleHeader.SetFont(getNewXSSFFont(workbook, styleHeader));

            XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();

            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;

            XSSFRow row1 = (XSSFRow)sheet.CreateRow(0);

            row1.CreateCell(0).SetCellValue("");

            List <string> lstdatase = new List <string>(4)
            {
                "A1",
                "B1",
                "C1",
                "D1",
                "E1"
            };

            for (int i = 1; i < 5; i++)
            {
                row1.CreateCell(i).SetCellValue(lstdatase[i - 1].ToString());
                row1.GetCell(i).CellStyle = styleHeader;
            }

            int           rowvalue = 1;
            List <string> lstdata  = new List <string>(8)
            {
                "A",
                "B",
                "C",
                "D",
                "E",
                "F",
                "G",
                "H"
            };

            int d = 10;

            XSSFDrawing      drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
            XSSFClientAnchor anchor  = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 6, 1, 15, 18);

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

            legend.Position = (LegendPosition.Bottom);

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

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

            leftAxis.Crosses = AxisCrosses.AutoZero;
            leftAxis.SetCrossBetween(AxisCrossBetween.Between);

            IChartDataSource <string> xs = DataSources.FromStringCellRange(sheet, new NPOI.SS.Util.CellRangeAddress(0, 0, 1, 5 - 1));

            for (int ii = 0; ii < 8; ii++)
            {
                XSSFRow rownew = (XSSFRow)sheet.CreateRow(rowvalue);
                rownew.CreateCell(0).SetCellValue(lstdata[ii].ToString());

                for (int i = 1; i < 5; i++)
                {
                    rownew.CreateCell(i).SetCellValue(d * 0.1);
                    d++;
                    rownew.GetCell(i).CellStyle = style;
                }
                rowvalue++;


                IChartDataSource <double> ys = DataSources.FromNumericCellRange(sheet, new NPOI.SS.Util.CellRangeAddress(ii + 1, ii + 1, 1, 5 - 1));
                data.AddSeries(xs, ys).SetTitle(lstdata[ii].ToString());
            }

            chart.Plot(data, bottomAxis, leftAxis);

            sheet.ForceFormulaRecalculation = true;
            return(workbook);
        }