private void AddChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor) { var chart = drawing.CreateChart(anchor); var legend = chart.GetOrCreateLegend(); legend.Position = LegendPosition.Bottom; var data = chart.ChartDataFactory.CreateLineChartData <string, double>(); var bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom); var leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); leftAxis.Crosses = AxisCrosses.AutoZero; var row = sheet.CreateRow(0); for (int i = 0; i < _errorList.Count; i++) { var cell = row.CreateCell(i); cell.SetCellValue(_errorList[i]); } var xAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, _errorList.Count - 1)); FillChartDataCells(sheet, 1, _dichotomyIterations, xAxis, data); FillChartDataCells(sheet, 2, _goldenRatioIterations, xAxis, data); FillChartDataCells(sheet, 3, _fibonacciIterations, xAxis, data); FillChartDataCells(sheet, 4, _parabolaIterations, xAxis, data); FillChartDataCells(sheet, 5, _brentIterations, xAxis, data); chart.Plot(data, bottomAxis, leftAxis); }
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.CreateValueAxis(AxisPosition.Bottom); IChartAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left); IScatterChartData <string, double> scatterChartData = chart.ChartDataFactory.CreateScatterChartData <string, double>(); IChartDataSource <String> xs = DataSources.FromStringCellRange(sheet, CellRangeAddress.ValueOf("A1:J1")); IChartDataSource <double> ys = DataSources.FromNumericCellRange(sheet, CellRangeAddress.ValueOf("A2:J2")); IScatterChartSeries <string, double> series = scatterChartData.AddSeries(xs, ys); Assert.IsNotNull(series); Assert.AreEqual(1, scatterChartData.GetSeries().Count); Assert.IsTrue(scatterChartData.GetSeries().Contains(series)); chart.Plot(scatterChartData, bottomAxis, leftAxis); }
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); }
public void TestStringCellDataSource() { IWorkbook wb = new HSSFWorkbook(); ISheet sheet = new SheetBuilder(wb, stringCells).Build(); CellRangeAddress numCellRange = CellRangeAddress.ValueOf("A2:E2"); IChartDataSource <String> numDataSource = DataSources.FromStringCellRange(sheet, numCellRange); Assert.IsTrue(numDataSource.IsReference); Assert.IsFalse(numDataSource.IsNumeric); Assert.AreEqual(numericCells[0].Length, numDataSource.PointCount); for (int i = 0; i < stringCells[1].Length; ++i) { Assert.AreEqual(stringCells[1][i], numDataSource.GetPointAt(i)); } }
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); }
public void TestMixedCellDataSource() { IWorkbook wb = new HSSFWorkbook(); ISheet sheet = new SheetBuilder(wb, mixedCells).Build(); CellRangeAddress mixedCellRange = CellRangeAddress.ValueOf("A1:F1"); IChartDataSource <String> strDataSource = DataSources.FromStringCellRange(sheet, mixedCellRange); IChartDataSource <double> numDataSource = DataSources.FromNumericCellRange(sheet, mixedCellRange); for (int i = 0; i < mixedCells[0].Length; ++i) { if (i % 2 == 0) { Assert.IsNull(strDataSource.GetPointAt(i)); Assert.AreEqual(((double)mixedCells[0][i]), numDataSource.GetPointAt(i), 0.00001); } else { Assert.IsNaN(numDataSource.GetPointAt(i)); Assert.AreEqual(mixedCells[0][i], strDataSource.GetPointAt(i)); } } }
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); }