View | Details | Raw Unified | Return to bug 55768
Collapse All | Expand All

(-)src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java (+77 lines)
Line 0 Link Here
1
package org.apache.poi.xssf.usermodel.examples;
2
3
import java.io.FileOutputStream;
4
5
import org.apache.poi.ss.usermodel.Cell;
6
import org.apache.poi.ss.usermodel.Chart;
7
import org.apache.poi.ss.usermodel.ClientAnchor;
8
import org.apache.poi.ss.usermodel.Drawing;
9
import org.apache.poi.ss.usermodel.Row;
10
import org.apache.poi.ss.usermodel.Sheet;
11
import org.apache.poi.ss.usermodel.Workbook;
12
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
13
import org.apache.poi.ss.usermodel.charts.AxisPosition;
14
import org.apache.poi.ss.usermodel.charts.ChartAxis;
15
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
16
import org.apache.poi.ss.usermodel.charts.ChartLegend;
17
import org.apache.poi.ss.usermodel.charts.DataSources;
18
import org.apache.poi.ss.usermodel.charts.LegendPosition;
19
import org.apache.poi.ss.usermodel.charts.LineChartData;
20
import org.apache.poi.ss.usermodel.charts.ValueAxis;
21
import org.apache.poi.ss.util.CellRangeAddress;
22
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
23
24
/**
25
 * Line chart example.
26
 *
27
 * @author Martin Andersson
28
 */
29
public class LineChart {
30
31
    public static void main(String[] args) throws Exception {
32
        Workbook wb = new XSSFWorkbook();
33
        Sheet sheet = wb.createSheet("linechart");
34
        final int NUM_OF_ROWS = 3;
35
        final int NUM_OF_COLUMNS = 10;
36
37
        // Create a row and put some cells in it. Rows are 0 based.
38
        Row row;
39
        Cell cell;
40
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
41
            row = sheet.createRow((short) rowIndex);
42
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
43
                cell = row.createCell((short) colIndex);
44
                cell.setCellValue(colIndex * (rowIndex + 1));
45
            }
46
        }
47
48
        Drawing drawing = sheet.createDrawingPatriarch();
49
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
50
51
        Chart chart = drawing.createChart(anchor);
52
        ChartLegend legend = chart.getOrCreateLegend();
53
        legend.setPosition(LegendPosition.TOP_RIGHT);
54
55
        LineChartData data = chart.getChartDataFactory().createLineChartData();
56
57
        // Use a category axis for the bottom axis.
58
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
59
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
60
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
61
62
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
63
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
64
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
65
66
67
        data.addSerie(xs, ys1);
68
        data.addSerie(xs, ys2);
69
70
        chart.plot(data, bottomAxis, leftAxis);
71
72
        // Write the output to a file
73
        FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");
74
        wb.write(fileOut);
75
        fileOut.close();
76
    }
77
}
(-)src/java/org/apache/poi/ss/usermodel/charts/ChartDataFactory.java (-2 / +7 lines)
Lines 22-35 import org.apache.poi.util.Beta; Link Here
22
/**
22
/**
23
 * A factory for different charts data types.
23
 * A factory for different charts data types.
24
 *
24
 *
25
 * @author Roman Kashitsyn
25
 * @author Roman Kashitsyn, Martin Andersson
26
 */
26
 */
27
@Beta
27
@Beta
28
public interface ChartDataFactory {
28
public interface ChartDataFactory {
29
	
29
30
	/**
30
	/**
31
	 * @return an appropriate ScatterChartData instance
31
	 * @return an appropriate ScatterChartData instance
32
	 */
32
	 */
33
	ScatterChartData createScatterChartData();
33
	ScatterChartData createScatterChartData();
34
34
35
	/**
36
	 * @return a LineChartData instance
37
	 */
38
	LineChartData createLineChartData();
39
35
}
40
}
(-)src/java/org/apache/poi/ss/usermodel/charts/LineChartData.java (+42 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.usermodel.charts;
19
20
import java.util.List;
21
22
import org.apache.poi.util.Beta;
23
24
/**
25
 *
26
 * @author Martin Andersson
27
 */
28
@Beta
29
public interface LineChartData extends ChartData {
30
31
	/**
32
	 * @param categories data source for categories.
33
	 * @param values data source for values.
34
	 * @return a new line chart serie.
35
	 */
36
	LineChartSerie addSerie(ChartDataSource<?> categories, ChartDataSource<? extends Number> values);
37
38
	/**
39
	 * @return list of all series.
40
	 */
41
	List<? extends LineChartSerie> getSeries();
42
}
(-)src/java/org/apache/poi/ss/usermodel/charts/LineChartSerie.java (+41 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.usermodel.charts;
19
20
import org.apache.poi.util.Beta;
21
22
/**
23
 *
24
 * Represents a line chart serie.
25
 *
26
 * @author Martin Andersson
27
 */
28
@Beta
29
public interface LineChartSerie {
30
31
	/**
32
	 * @return data source used for category axis data.
33
	 */
34
	ChartDataSource<?> getCategoryAxisData();
35
36
	/**
37
	 * @return data source used for value axis.
38
	 */
39
	ChartDataSource<? extends Number> getValues();
40
41
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFChartDataFactory.java (+7 lines)
Lines 40-45 public class XSSFChartDataFactory implements ChartDataFactory { Link Here
40
	}
40
	}
41
41
42
	/**
42
	/**
43
	 * @return new line charts data instance
44
	 */
45
	public XSSFLineChartData createLineChartData() {
46
		return new XSSFLineChartData();
47
	}
48
49
	/**
43
	 * @return factory instance
50
	 * @return factory instance
44
	 */
51
	 */
45
	public static XSSFChartDataFactory getInstance() {
52
	public static XSSFChartDataFactory getInstance() {
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFLineChartData.java (+123 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
   http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
   ==================================================================== */
17
18
package org.apache.poi.xssf.usermodel.charts;
19
20
import java.util.ArrayList;
21
import java.util.List;
22
23
import org.apache.poi.ss.usermodel.Chart;
24
import org.apache.poi.ss.usermodel.charts.ChartAxis;
25
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
26
import org.apache.poi.ss.usermodel.charts.LineChartData;
27
import org.apache.poi.ss.usermodel.charts.LineChartSerie;
28
import org.apache.poi.util.Beta;
29
import org.apache.poi.xssf.usermodel.XSSFChart;
30
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
31
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart;
32
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
33
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
34
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
35
import org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle;
36
37
/**
38
 *
39
 * @author Martin Andersson
40
 */
41
@Beta
42
public class XSSFLineChartData implements LineChartData {
43
44
    /**
45
     * List of all data series.
46
     */
47
    private List<Serie> series;
48
49
	public XSSFLineChartData() {
50
		series = new ArrayList<Serie>();
51
	}
52
53
	static class Serie implements LineChartSerie {
54
        private int id;
55
        private int order;
56
        private ChartDataSource<?> categories;
57
        private ChartDataSource<? extends Number> values;
58
59
        protected Serie(int id, int order,
60
                        ChartDataSource<?> categories,
61
                        ChartDataSource<? extends Number> values) {
62
            this.id = id;
63
            this.order = order;
64
            this.categories = categories;
65
            this.values = values;
66
        }
67
68
		public ChartDataSource<?> getCategoryAxisData() {
69
			return categories;
70
		}
71
72
		public ChartDataSource<? extends Number> getValues() {
73
			return values;
74
		}
75
76
		protected void addToChart(CTLineChart ctLineChart) {
77
			CTLineSer ctLineSer = ctLineChart.addNewSer();
78
			ctLineSer.addNewIdx().setVal(id);
79
			ctLineSer.addNewOrder().setVal(order);
80
81
			// No marker symbol on the chart line.
82
			ctLineSer.addNewMarker().addNewSymbol().setVal(STMarkerStyle.NONE);
83
84
			CTAxDataSource catDS = ctLineSer.addNewCat();
85
			XSSFChartUtil.buildAxDataSource(catDS, categories);
86
			CTNumDataSource valueDS = ctLineSer.addNewVal();
87
			XSSFChartUtil.buildNumDataSource(valueDS, values);
88
		}
89
	}
90
91
	public LineChartSerie addSerie(ChartDataSource<?> categoryAxisData, ChartDataSource<? extends Number> values) {
92
        if (!values.isNumeric()) {
93
            throw new IllegalArgumentException("Value data source must be numeric.");
94
        }
95
        int numOfSeries = series.size();
96
        Serie newSerie = new Serie(numOfSeries, numOfSeries, categoryAxisData, values);
97
        series.add(newSerie);
98
        return newSerie;
99
	}
100
101
	public List<? extends LineChartSerie> getSeries() {
102
		return series;
103
	}
104
105
	public void fillChart(Chart chart, ChartAxis... axis) {
106
        if (!(chart instanceof XSSFChart)) {
107
            throw new IllegalArgumentException("Chart must be instance of XSSFChart");
108
        }
109
110
        XSSFChart xssfChart = (XSSFChart) chart;
111
        CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
112
        CTLineChart lineChart = plotArea.addNewLineChart();
113
        lineChart.addNewVaryColors().setVal(false);
114
115
        for (Serie s : series) {
116
            s.addToChart(lineChart);
117
        }
118
119
        for (ChartAxis ax : axis) {
120
            lineChart.addNewAxId().setVal(ax.getId());
121
        }
122
	}
123
}
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/charts/TestXSSFLineChartData.java (+54 lines)
Line 0 Link Here
1
package org.apache.poi.xssf.usermodel.charts;
2
3
import junit.framework.TestCase;
4
5
import org.apache.poi.ss.usermodel.Chart;
6
import org.apache.poi.ss.usermodel.ClientAnchor;
7
import org.apache.poi.ss.usermodel.Drawing;
8
import org.apache.poi.ss.usermodel.Sheet;
9
import org.apache.poi.ss.usermodel.Workbook;
10
import org.apache.poi.ss.usermodel.charts.AxisPosition;
11
import org.apache.poi.ss.usermodel.charts.ChartAxis;
12
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
13
import org.apache.poi.ss.usermodel.charts.DataSources;
14
import org.apache.poi.ss.usermodel.charts.LineChartData;
15
import org.apache.poi.ss.usermodel.charts.LineChartSerie;
16
import org.apache.poi.ss.util.CellRangeAddress;
17
import org.apache.poi.ss.util.SheetBuilder;
18
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
19
20
/**
21
 *
22
 * @author Martin Andersson
23
 */
24
public class TestXSSFLineChartData extends TestCase {
25
26
    private static final Object[][] plotData = {
27
            {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"},
28
            {  1,    2,   3,    4,    5,   6,    7,   8,    9,  10}
29
    };
30
31
    public void testOneSeriePlot() throws Exception {
32
        Workbook wb = new XSSFWorkbook();
33
        Sheet sheet = new SheetBuilder(wb, plotData).build();
34
        Drawing drawing = sheet.createDrawingPatriarch();
35
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 10, 30);
36
        Chart chart = drawing.createChart(anchor);
37
38
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
39
        ChartAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
40
41
        LineChartData lineChartData =
42
                chart.getChartDataFactory().createLineChartData();
43
44
        ChartDataSource<String> xs = DataSources.fromStringCellRange(sheet, CellRangeAddress.valueOf("A1:J1"));
45
        ChartDataSource<Number> ys = DataSources.fromNumericCellRange(sheet, CellRangeAddress.valueOf("A2:J2"));
46
        LineChartSerie serie = lineChartData.addSerie(xs, ys);
47
48
        assertNotNull(serie);
49
        assertEquals(1, lineChartData.getSeries().size());
50
        assertTrue(lineChartData.getSeries().contains(serie));
51
52
        chart.plot(lineChartData, bottomAxis, leftAxis);
53
    }
54
}

Return to bug 55768