Search in sources :

Example 6 with ChartLegend

use of org.apache.poi.ss.usermodel.charts.ChartLegend in project poi by apache.

the class ScatterChart method main.

public static void main(String[] args) throws IOException {
    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;
    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();
    wb.close();
}
Also used : ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) ScatterChartData(org.apache.poi.ss.usermodel.charts.ScatterChartData) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Chart(org.apache.poi.ss.usermodel.Chart)

Example 7 with ChartLegend

use of org.apache.poi.ss.usermodel.charts.ChartLegend in project poi by apache.

the class TestXSSFManualLayout method createEmptyLayout.

@Before
public void createEmptyLayout() {
    wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    layout = legend.getManualLayout();
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) Sheet(org.apache.poi.ss.usermodel.Sheet) Chart(org.apache.poi.ss.usermodel.Chart) Before(org.junit.Before)

Example 8 with ChartLegend

use of org.apache.poi.ss.usermodel.charts.ChartLegend in project poi by apache.

the class TestXSSFChartLegend method test_setOverlay_defaultChartLegend_expectOverlayInitialValueSetToFalse.

@Test
public void test_setOverlay_defaultChartLegend_expectOverlayInitialValueSetToFalse() throws IOException {
    // Arrange
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    // Act
    // Assert
    assertFalse(legend.isOverlay());
    wb.close();
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Chart(org.apache.poi.ss.usermodel.Chart) Test(org.junit.Test)

Example 9 with ChartLegend

use of org.apache.poi.ss.usermodel.charts.ChartLegend in project ocvn by devgateway.

the class ExcelChartSheetDefault method createChartAndLegend.

/**
     * Creates a chart and also attaches a legend to it.
     */
@Override
public Chart createChartAndLegend() {
    final Drawing drawing = excelSheet.createDrawingPatriarch();
    final ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 15, 25);
    final Chart chart = drawing.createChart(anchor);
    final ChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.BOTTOM);
    return chart;
}
Also used : Drawing(org.apache.poi.ss.usermodel.Drawing) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) Chart(org.apache.poi.ss.usermodel.Chart)

Aggregations

Chart (org.apache.poi.ss.usermodel.Chart)9 ChartLegend (org.apache.poi.ss.usermodel.charts.ChartLegend)9 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)8 Sheet (org.apache.poi.ss.usermodel.Sheet)8 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)7 Workbook (org.apache.poi.ss.usermodel.Workbook)6 Row (org.apache.poi.ss.usermodel.Row)4 ValueAxis (org.apache.poi.ss.usermodel.charts.ValueAxis)4 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)4 Cell (org.apache.poi.ss.usermodel.Cell)3 ChartAxis (org.apache.poi.ss.usermodel.charts.ChartAxis)3 LineChartData (org.apache.poi.ss.usermodel.charts.LineChartData)3 Test (org.junit.Test)3 FileOutputStream (java.io.FileOutputStream)2 Drawing (org.apache.poi.ss.usermodel.Drawing)2 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)2 AddTrace (com.google.firebase.perf.metrics.AddTrace)1 SpreadsheetUtils.getCellRangeAddress (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getCellRangeAddress)1 SpreadsheetUtils.getMetricForChart (com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getMetricForChart)1 Metric (com.supercilex.robotscouter.data.model.Metric)1