List of usage examples for org.apache.poi.ss.usermodel Drawing createAnchor
ClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2);
From source file:packtest.LineChart.java
License:Apache License
public static void main(String[] args) throws Exception { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("linechart"); final int NUM_OF_ROWS = 3; final int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. Row row;//from w w w . j a v a 2 s . co m Cell 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)); } } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); LineChartData data = chart.getChartDataFactory().createLineChartData(); // Use a category axis for the bottom axis. ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> 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); // Write the output to a file FileOutputStream fileOut = new FileOutputStream(Utils.getPath("ooxml-line-chart.xlsx")); wb.write(fileOut); fileOut.close(); }
From source file:packtest.ScatterChart.java
License:Apache License
public static void main(String[] args) throws Exception { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet 1"); final int NUM_OF_ROWS = 3; final int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. Row row;// w w w . ja v a2 s.c o m Cell 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)); } } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); data.addSerie(xs, ys1); data.addSerie(xs, ys2); chart.plot(data, bottomAxis, leftAxis); // Write the output to a file FileOutputStream fileOut = new FileOutputStream(Utils.getPath("ooxml-scatter-chart.xlsx")); wb.write(fileOut); fileOut.close(); }
From source file:panel.AnalysisPanel.java
void excel(Workbook wb, String name, double xData[], double yData[]) { // Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet(name); //? ? final int NUM_OF_ROWS = 2; final int NUM_OF_COLUMNS = yData.length; // Create a row and put some cells in it. Rows are 0 based. Row row;//from w w w . ja v a 2 s .c o m Cell 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); if (rowIndex == 0) { cell.setCellValue(xData[colIndex]); } if (rowIndex == 1) { cell.setCellValue(yData[colIndex]); } } } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 25, 35); org.apache.poi.ss.usermodel.Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); LineChartData data = chart.getChartDataFactory().createLineChartData(); // Use a category axis for the bottom axis. ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); data.addSeries(xs, ys1); chart.plot(data, bottomAxis, leftAxis); }
From source file:poi.xssf.usermodel.examples.LineChart.java
License:Apache License
public static void main(String[] args) throws Exception { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("linechart"); final int NUM_OF_ROWS = 3; final int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. Row row;//from ww w . j av a 2s .co m Cell 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)); } } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); LineChartData data = chart.getChartDataFactory().createLineChartData(); // Use a category axis for the bottom axis. ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); data.addSerie(xs, ys1); data.addSerie(xs, ys2); chart.plot(data, bottomAxis, leftAxis); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx"); wb.write(fileOut); fileOut.close(); }
From source file:poi.xssf.usermodel.examples.ScatterChart.java
License:Apache License
public static void main(String[] args) throws Exception { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet 1"); final int NUM_OF_ROWS = 3; final int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. Row row;/*from w w w. j a va 2 s.com*/ Cell 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)); } } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); data.addSerie(xs, ys1); data.addSerie(xs, ys2); chart.plot(data, bottomAxis, leftAxis); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx"); wb.write(fileOut); fileOut.close(); }
From source file:searchPatternClassifierPackage.ScatterChart.java
License:Apache License
public static void exampeScatterChart() { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet 1"); final int NUM_OF_ROWS = 3; final int NUM_OF_COLUMNS = 10; // Create a row and put some cells in it. Rows are 0 based. Row row;/* w w w . j a v a 2s .co m*/ Cell 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)); } } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); data.addSerie(xs, ys1); data.addSerie(xs, ys2); chart.plot(data, bottomAxis, leftAxis); try { // Write the output to a file FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx"); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:sistemas.Utils.java
public static void chartExcel(ArrayList<Object> actuales, ArrayList<Object> futuros, String path) throws Exception { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("linechart"); final int NUM_OF_ROWS = 30; final int NUM_OF_COLUMNS = 5; // Create a row and put some cells in it. Rows are 0 based. Row row;/*from w w w.ja v a2 s . co m*/ Cell cell; for (int i = 0; i < 15; i++) { row = sheet.createRow((short) i); cell = row.createCell((short) 0); cell.setCellValue(i + 1); cell = row.createCell((short) 1); if (actuales.get(i) instanceof Integer) cell.setCellValue((Integer) actuales.get(i)); else cell.setCellValue((Double) actuales.get(i)); cell = row.createCell((short) 3); cell.setCellValue(i + 1); cell = row.createCell((short) 4); cell.setCellValue((Double) futuros.get(i)); } for (int i = 15; i < 30; i++) { row = sheet.createRow((short) i); cell = row.createCell(3); cell.setCellValue(i + 1); cell = row.createCell(4); cell.setCellValue((Double) futuros.get(i)); } Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 35, 10, 45); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); LineChartData data = chart.getChartDataFactory().createLineChartData(); // Use a category axis for the bottom axis. ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 29, 3, 3)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 29, 1, 1)); ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 29, 4, 4)); data.addSeries(xs, ys1); data.addSeries(xs, ys2); chart.plot(data, bottomAxis, leftAxis); // Write the output to a file FileOutputStream fileOut = new FileOutputStream(path); wb.write(fileOut); fileOut.close(); }
From source file:tasklist.model.ExcelWriterChart.java
public ExcelWriterChart(List<Task> taskData, File file) throws IOException { //add Excel table Header row = sheet.createRow(0);/*w w w .ja v a 2 s .c o m*/ cell = row.createCell(0); cell.setCellValue("Name"); cell = row.createCell(1); cell.setCellValue("PID"); cell = row.createCell(2); cell.setCellValue("Memory"); // Create a row and put some cells in it. Rows are 0 based. int rowIndex = 1; for (Task taskD : taskData) { row = sheet.createRow((short) rowIndex); cell = row.createCell(0); cell.setCellValue(taskD.getName()); cell = row.createCell(1); cell.setCellValue(taskD.getPID()); cell = row.createCell(2); cell.setCellValue(taskD.getMemory()); rowIndex++; } //auto fit "Nmae" column sheet.autoSizeColumn(0); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 15, 25); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); LineChartData data = chart.getChartDataFactory().createLineChartData(); // Use a category axis for the bottom axis. ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, taskData.size(), 0, 0)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, taskData.size(), 2, 2)); data.addSeries(xs, ys1); chart.plot(data, bottomAxis, leftAxis); // Write the output to a file FileOutputStream fileOut; try { fileOut = new FileOutputStream(file.getPath()); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException ex) { Logger.getLogger(ExcelWriterChart.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:y.graphs.XLSHelper.java
License:Open Source License
public static boolean saveElfData(String filename, ElfDb db, double sensibilita, boolean save_grafico) { final DateTime[] times = db.getPeriods(); final ElfValue[][] dayvalues = db.getSampledData(); final int[] mediane = db.getOpValues(); final int[] maxs = db.getOpMaxDay(); final int[] counts = db.getOpValueCount(); final int maxi = db.getMaxidx(); Workbook wb = null;/*from w ww. jav a 2 s . com*/ try { if (Utils.abortOnExistingAndDontOverwrite(filename)) return false; wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet(Config.getResource("TitleStats")); int rown = 0; Row row = sheet.createRow(rown++); Cell cell = row.createCell(0); cell.setCellValue(Config.getResource("TitleDate")); cell = row.createCell(1); cell.setCellValue(Config.getResource("TitleMediana")); cell = row.createCell(2); cell.setCellValue(Config.getResource("TitleMaxM")); cell = row.createCell(3); cell.setCellValue(Config.getResource("TitleNumberOfData")); CellStyle dateStyle1 = wb.createCellStyle(); dateStyle1.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yy")); CellStyle doubleFormat1 = wb.createCellStyle(); DataFormat format1 = wb.createDataFormat(); doubleFormat1.setDataFormat(format1.getFormat("0.00")); for (int i = 0; i < mediane.length; i++) { row = sheet.createRow(rown++); cell = row.createCell(0); cell.setCellStyle(dateStyle1); cell.setCellValue(Utils.toDateString(dayvalues[i][0].getTime())); cell = row.createCell(1); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(mediane[i])); cell = row.createCell(2); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(maxs[i])); cell = row.createCell(3); cell.setCellValue(counts[i]); } // line with DataFunction max row = sheet.createRow(rown++); row = sheet.createRow(rown++); cell = row.createCell(0); cell.setCellValue(Config.getResource("MsgMax") + "(" + db.getOperationPerformed().getName() + ") - " + Utils.toDateString(times[maxi])); cell = row.createCell(1); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(mediane[maxi])); cell = row.createCell(2); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(maxs[maxi])); cell = row.createCell(3); cell.setCellValue(counts[maxi]); // line with max final ElfValue maxvalue = db.getSelectedElfValue(new Comparator<ElfValue>() { @Override public int compare(ElfValue o1, ElfValue o2) { return o1.getValue() - o2.getValue(); } }); row = sheet.createRow(rown++); cell = row.createCell(0); cell.setCellValue(Config.getResource("MsgMax") + "(" + Utils.toDateString(maxvalue.getTime()) + ")"); cell = row.createCell(1); cell.setCellStyle(doubleFormat1); cell.setCellValue(MeasurementValue.valueIntToDouble(maxvalue.getValue())); cell = row.createCell(2); cell.setCellStyle(doubleFormat1); cell.setCellValue(MeasurementValue.valueIntToDouble(maxvalue.getMax())); cell = row.createCell(3); cell.setCellValue(counts[maxi]); // sheet containing all raw data Sheet sheetdata = wb.createSheet(Config.getResource("TitleSheetDatas")); CellStyle dateTimeStyle2 = wb.createCellStyle(); dateTimeStyle2.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm")); CellStyle doubleFormat2 = wb.createCellStyle(); DataFormat format2 = wb.createDataFormat(); doubleFormat2.setDataFormat(format2.getFormat("0.00")); rown = 0; row = sheetdata.createRow(rown++); cell = row.createCell(0); cell.setCellValue(Config.getResource("TitleDate")); cell = row.createCell(1); cell.setCellValue(Config.getResource("TitleValue")); cell = row.createCell(2); cell.setCellValue(Config.getResource("TitlePeak")); cell = row.createCell(3); cell.setCellValue(Config.getResource("TitleMediana")); cell = row.createCell(4); cell.setCellValue(Config.getResource("TitleDayMax")); cell = row.createCell(5); cell.setCellValue(Config.getResource("TitleMedianaMax")); cell = row.createCell(6); cell.setCellValue(Config.getResource("TitleSens")); cell = row.createCell(7); cell.setCellValue(Config.getResource("TitleQualityTarget")); cell = row.createCell(8); cell.setCellValue(Config.getResource("TitleAttentionValue")); for (int i = 0; i < dayvalues.length; i++) { final ElfValue[] day = dayvalues[i]; for (int k = 0; k < day.length; k++) { final ElfValue value = day[k]; final DateTime time = value.getTime(); row = sheetdata.createRow(rown++); cell = row.createCell(0); cell.setCellStyle(dateTimeStyle2); cell.setCellValue(Utils.toDateString(time)); cell = row.createCell(1); cell.setCellStyle(doubleFormat2); if (value.isValid()) cell.setCellValue(ElfValue.valueIntToDouble(value.getValue())); else cell.setCellValue(""); cell = row.createCell(2); cell.setCellStyle(doubleFormat2); if (value.isValid()) cell.setCellValue(ElfValue.valueIntToDouble(value.getMax())); else cell.setCellValue(""); cell = row.createCell(3); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(mediane[i])); cell = row.createCell(4); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(maxs[i])); cell = row.createCell(5); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(mediane[maxi])); cell = row.createCell(6); cell.setCellStyle(doubleFormat2); cell.setCellValue(sensibilita); cell = row.createCell(7); cell.setCellStyle(doubleFormat2); cell.setCellValue(3); cell = row.createCell(8); cell.setCellStyle(doubleFormat2); cell.setCellValue(10); } } if (save_grafico) { final int maxline = rown - 1; sheet = wb.createSheet(Config.getResource("TitleChart")); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 18, 25); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); // LineChartData data = chart.getChartDataFactory().createLineChartData(); ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setMinimum(0.0); leftAxis.setMaximum(10.0); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<String> xs = DataSources.fromStringCellRange(sheetdata, new CellRangeAddress(1, maxline, 0, 0)); ChartDataSource<Number> ys_val = DataSources.fromNumericCellRange(sheetdata, new CellRangeAddress(1, maxline, 1, 1)); ChartDataSource<Number> ys_sens = DataSources.fromNumericCellRange(sheetdata, new CellRangeAddress(1, maxline, 6, 6)); ChartDataSource<Number> ys_qual = DataSources.fromNumericCellRange(sheetdata, new CellRangeAddress(1, maxline, 7, 7)); ChartDataSource<Number> ys_att = DataSources.fromNumericCellRange(sheetdata, new CellRangeAddress(1, maxline, 8, 8)); ScatterChartSeries data_val = data.addSerie(xs, ys_val); data_val.setTitle(Config.getResource("TitleMeasuredValues")); ScatterChartSeries data_sens = data.addSerie(xs, ys_sens); data_sens.setTitle(Config.getResource("TitleInstrumentSens")); ScatterChartSeries data_qual = data.addSerie(xs, ys_qual); data_qual.setTitle(Config.getResource("TitleQualityTarget")); ScatterChartSeries data_att = data.addSerie(xs, ys_att); data_att.setTitle(Config.getResource("TitleAttentionValue")); chart.plot(data, bottomAxis, leftAxis); } FileOutputStream fileOut = new FileOutputStream(filename); wb.write(fileOut); fileOut.close(); return true; } catch (Exception e) { Utils.MessageBox(Config.getResource("MsgErrorXlsx") + "\n" + e.toString(), Config.getResource("TitleError")); return false; } finally { if (wb != null) try { wb.close(); } catch (IOException e) { } } }
From source file:y.graphs.XLSHelper.java
License:Open Source License
public static boolean saveCurrentsData(String filename, CurrentDb db, boolean save_grafico) { final DateTime[] times = db.getPeriods(); final CurrentValue[][] dayvalues = db.getSampledData(); final int[] mediane = db.getOpValues(); final int[] maxs = db.getOpMaxDay(); final int[] counts = db.getOpValueCount(); final int maxi = db.getMaxidx(); Workbook wb = null;/*from w w w .j av a2 s . co m*/ try { if (Utils.abortOnExistingAndDontOverwrite(filename)) return false; wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet(Config.getResource("TitleStats")); int rown = 0; Row row = sheet.createRow(rown++); Cell cell = row.createCell(0); cell.setCellValue(Config.getResource("TitleDate")); cell = row.createCell(1); cell.setCellValue(Config.getResource("TitleMediana")); cell = row.createCell(2); cell.setCellValue(Config.getResource("TitleMaxM")); cell = row.createCell(3); cell.setCellValue(Config.getResource("TitleNumberOfData")); CellStyle dateStyle1 = wb.createCellStyle(); dateStyle1.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yy")); CellStyle doubleFormat1 = wb.createCellStyle(); DataFormat format1 = wb.createDataFormat(); doubleFormat1.setDataFormat(format1.getFormat("0.00")); for (int i = 0; i < mediane.length; i++) { row = sheet.createRow(rown++); cell = row.createCell(0); cell.setCellStyle(dateStyle1); cell.setCellValue(Utils.toDateString(dayvalues[i][0].getTime())); cell = row.createCell(1); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(mediane[i])); cell = row.createCell(2); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(maxs[i])); cell = row.createCell(3); cell.setCellValue(counts[i]); } // line with DataFunction max row = sheet.createRow(rown++); row = sheet.createRow(rown++); cell = row.createCell(0); cell.setCellValue(Config.getResource("MsgMax") + "(" + db.getOperationPerformed().getName() + ") - " + Utils.toDateString(times[maxi])); cell = row.createCell(1); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(mediane[maxi])); cell = row.createCell(2); cell.setCellStyle(doubleFormat1); cell.setCellValue(ElfValue.valueIntToDouble(maxs[maxi])); cell = row.createCell(3); cell.setCellValue(counts[maxi]); // line with max final CurrentValue maxvalue = db.getSelectedCurrentValue(new Comparator<CurrentValue>() { @Override public int compare(CurrentValue o1, CurrentValue o2) { return o1.getValue() - o2.getValue(); } }); row = sheet.createRow(rown++); cell = row.createCell(0); cell.setCellValue(Config.getResource("MsgMax") + "(" + Utils.toDateString(maxvalue.getTime()) + ")"); cell = row.createCell(1); cell.setCellStyle(doubleFormat1); cell.setCellValue(MeasurementValue.valueIntToDouble(maxvalue.getValue())); cell = row.createCell(2); cell.setCellStyle(doubleFormat1); cell.setCellValue(""); cell = row.createCell(3); cell.setCellValue(counts[maxi]); // sheet containing all raw data Sheet sheetdata = wb.createSheet(Config.getResource("TitleSheetDatas")); CellStyle dateTimeStyle2 = wb.createCellStyle(); dateTimeStyle2.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm")); CellStyle doubleFormat2 = wb.createCellStyle(); DataFormat format2 = wb.createDataFormat(); doubleFormat2.setDataFormat(format2.getFormat("0.00")); rown = 0; row = sheetdata.createRow(rown++); cell = row.createCell(0); cell.setCellValue(Config.getResource("TitleDate")); cell = row.createCell(1); cell.setCellValue(Config.getResource("TitleValue")); cell = row.createCell(2); cell.setCellValue(Config.getResource("TitlePeak")); cell = row.createCell(3); cell.setCellValue(Config.getResource("TitleMediana")); cell = row.createCell(4); cell.setCellValue(Config.getResource("TitleDayMax")); cell = row.createCell(5); cell.setCellValue(Config.getResource("TitleMedianaMax")); for (int i = 0; i < dayvalues.length; i++) { final CurrentValue[] day = dayvalues[i]; for (int k = 0; k < day.length; k++) { final CurrentValue value = day[k]; final DateTime time = value.getTime(); row = sheetdata.createRow(rown++); cell = row.createCell(0); cell.setCellStyle(dateTimeStyle2); cell.setCellValue(Utils.toDateString(time)); cell = row.createCell(1); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(value.getValue())); cell = row.createCell(2); // cell.setCellStyle(doubleFormat2); cell.setCellValue(""); cell = row.createCell(3); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(mediane[i])); cell = row.createCell(4); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(maxs[i])); cell = row.createCell(5); cell.setCellStyle(doubleFormat2); cell.setCellValue(ElfValue.valueIntToDouble(mediane[maxi])); } } if (save_grafico) { final int maxline = rown - 1; sheet = wb.createSheet(Config.getResource("TitleChart")); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 18, 25); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); // LineChartData data = chart.getChartDataFactory().createLineChartData(); ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setMinimum(0.0); leftAxis.setMaximum(10.0); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<String> xs = DataSources.fromStringCellRange(sheetdata, new CellRangeAddress(1, maxline, 0, 0)); ChartDataSource<Number> ys_val = DataSources.fromNumericCellRange(sheetdata, new CellRangeAddress(1, maxline, 1, 1)); ChartDataSource<Number> ys_sens = DataSources.fromNumericCellRange(sheetdata, new CellRangeAddress(1, maxline, 6, 6)); ScatterChartSeries data_val = data.addSerie(xs, ys_val); data_val.setTitle(Config.getResource("TitleMeasuredValues")); ScatterChartSeries data_sens = data.addSerie(xs, ys_sens); data_sens.setTitle(Config.getResource("TitleInstrumentSens")); chart.plot(data, bottomAxis, leftAxis); } FileOutputStream fileOut = new FileOutputStream(filename); wb.write(fileOut); fileOut.close(); return true; } catch (Exception e) { Utils.MessageBox(Config.getResource("MsgErrorXlsx") + "\n" + e.toString(), Config.getResource("TitleError")); return false; } finally { if (wb != null) try { wb.close(); } catch (IOException e) { } } }