Exemple #1
0
        private static void CreateChart(ISheet sheet, IDrawing drawing, IClientAnchor anchor, string serieTitle, int startDataRow, int endDataRow, int columnIndex)
        {
            XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);

            IBarChartData <string, double> barChartData = chart.ChartDataFactory.CreateBarChartData <string, double>();
            IChartLegend legend = chart.GetOrCreateLegend();

            legend.Position = LegendPosition.Bottom;

            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> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0));
            IChartDataSource <double> valueAxis    = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnIndex, columnIndex));
            var serie = barChartData.AddSeries(categoryAxis, valueAxis);

            serie.SetTitle(serieTitle);

            chart.Plot(barChartData, bottomAxis, leftAxis);
        }
        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);
        }