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

(-)a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java (+93 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.examples;
19
20
import java.io.FileOutputStream;
21
import java.util.Date;
22
23
import org.apache.poi.ss.usermodel.*;
24
import org.apache.poi.ss.util.*;
25
import org.apache.poi.xssf.usermodel.*;
26
import org.apache.poi.ss.usermodel.charts.*;
27
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
28
29
/**
30
 * Illustrates how to create cell and set values of different types.
31
 */
32
public class ScatterChart {
33
34
35
	public static void main(String[]args) throws Exception {
36
        XSSFWorkbook wb = new XSSFWorkbook();
37
        CreationHelper creationHelper = wb.getCreationHelper();
38
        XSSFSheet sheet = wb.createSheet("new sheet");
39
		final int NUM_OF_ROWS = 3;
40
		final int NUM_OF_COLUMNS = 10;
41
42
        // Create a row and put some cells in it. Rows are 0 based.
43
        Row row;
44
		Cell cell;
45
		for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
46
			row = sheet.createRow((short)rowIndex);
47
			for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
48
				cell = row.createCell((short)colIndex);
49
				cell.setCellValue(colIndex * (rowIndex + 1));
50
			}
51
		}
52
53
		row = sheet.createRow((short)1);
54
		for (int i = 0; i < 10; i++) {
55
			cell = row.createCell((short)i);
56
			cell.setCellValue(i * 2);
57
		}
58
59
		row = sheet.createRow((short)2);
60
		for (int i = 0; i < 10; i++) {
61
			cell = row.createCell((short)i);
62
			cell.setCellValue(i * 3);
63
		}
64
65
		XSSFClientAnchor anchor = new XSSFClientAnchor(0,0,0,0,0,NUM_OF_COLUMNS + 1,NUM_OF_COLUMNS,NUM_OF_ROWS + 10);
66
67
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
68
69
        XSSFChart chart = drawing.createChart(anchor);
70
        XSSFChartLegend legend = chart.getOrCreateLegend();
71
        legend.setPosition(XSSFChartLegend.Position.RIGHT);
72
73
        ScatterChartData data = chart.getChartDataFactory().getScatterChartData();
74
75
        ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
76
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
77
78
        ScatterChartSerie firstSerie = data.addSerie();
79
        firstSerie.setXValues(sheet, new CellRangeAddress(0, 0, NUM_OF_COLUMNS + 1, NUM_OF_COLUMNS + 1));
80
        firstSerie.setXValues(sheet, new CellRangeAddress(1, 1, NUM_OF_COLUMNS + 1, NUM_OF_COLUMNS + 1));
81
82
        ScatterChartSerie secondSerie = data.addSerie();
83
        secondSerie.setXValues(sheet, new CellRangeAddress(0, 0, NUM_OF_COLUMNS + 1, NUM_OF_COLUMNS + 1));
84
        secondSerie.setYValues(sheet, new CellRangeAddress(2, 2, NUM_OF_COLUMNS + 1, NUM_OF_COLUMNS + 1));
85
86
        chart.plot(data, bottomAxis, leftAxis);
87
88
		// Write the output to a file
89
        FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx");
90
        wb.write(fileOut);
91
        fileOut.close();
92
	}
93
}
(-)a/src/java/org/apache/poi/ss/usermodel/Chart.java (+55 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;
19
20
import java.util.List;
21
22
import org.apache.poi.ss.usermodel.charts.ChartData;
23
import org.apache.poi.ss.usermodel.charts.ChartAxis;
24
import org.apache.poi.ss.usermodel.charts.ChartDataFactory;
25
import org.apache.poi.ss.usermodel.charts.ChartAxisFactory;
26
27
/**
28
 * High level representation of a chart.
29
 *
30
 * @author Roman Kashitsyn
31
 */
32
public interface Chart {
33
	
34
	/**
35
	 * @return an appropriate ChartDataFactory implementation
36
	 */
37
	ChartDataFactory getChartDataFactory();
38
39
	/**
40
	 * @return an appropriate ChartAxisFactory implementation
41
	 */
42
	ChartAxisFactory getChartAxisFactory();
43
44
	/**
45
	 * @return list of all chart axis
46
	 */
47
	List<? extends ChartAxis> getAxis();
48
49
	/**
50
	 * Plots specified data on the chart.
51
	 *
52
	 * @param data a data to plot
53
	 */
54
	void plot(ChartData data, ChartAxis... axis);
55
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/AxisCrossBetween.java (+36 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
/**
21
 *  Specifies the possible crossing states of an axis.
22
 *
23
 * @author Roman Kashitsyn
24
 */
25
public enum AxisCrossBetween {
26
	/**
27
	 * Specifies the value axis shall cross the category axis
28
	 * between data markers.
29
	 */
30
	BETWEEN,
31
	/**
32
	 * Specifies the value axis shall cross the category axis at
33
	 * the midpoint of a category.
34
	 */
35
	MIDPOINT_CATEGORY
36
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/AxisCrosses.java (+40 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
/**
21
 * Specifies the possible crossing points for an axis.
22
 *
23
 * @author Roman Kashitsyn
24
 */
25
public enum AxisCrosses {
26
	/**
27
	 * The category axis crosses at the zero point of the value axis (if
28
	 * possible), or the minimum value (if the minimum is greater than zero) or
29
	 * the maximum (if the maximum is less than zero).
30
	 */
31
	AUTO_ZERO,
32
	/**
33
	 * The axis crosses at the maximum value.
34
	 */
35
	MIN,
36
	/**
37
	 * Axis crosses at the minimum value of the chart.
38
	 */
39
	MAX
40
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/AxisOrientation.java (+36 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
/**
21
 * Specifies the possible ways to place a picture on a data point, series, wall, or floor.
22
 *
23
 * @author Roman Kashitsyn
24
 */
25
public enum AxisOrientation {
26
	/**
27
	 * Specifies that the values on the axis shall be reversed
28
	 * so they go from maximum to minimum.
29
	 */
30
	MAX_MIN,
31
	/**
32
	 * Specifies that the axis values shall be in the usual
33
	 * order, minimum to maximum.
34
	 */
35
	MIN_MAX
36
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/AxisPosition.java (+30 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
/**
21
 * Enumeration of all possible axis positions.
22
 *
23
 * @author Roman Kashitsyn
24
 */
25
public enum AxisPosition {
26
	BOTTOM,
27
	LEFT,
28
	RIGHT,
29
	TOP
30
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ChartAxis.java (+107 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
/**
21
 * High level representation of chart axis.
22
 *
23
 * @author Roman Kashitsyn
24
 */
25
public interface ChartAxis {
26
	
27
	/**
28
	 * @return axis id
29
	 */
30
	long getId();
31
32
	/**
33
	 * @return axis position
34
	 */
35
	AxisPosition getPosition();
36
37
	/**
38
	 * @param new axis position
39
	 */
40
	void setPosition(AxisPosition position);
41
42
	/**
43
	 * @return axis number format
44
	 */
45
	String getNumberFormat();
46
47
	/**
48
	 * @param format axis number format
49
	 */
50
	void setNumberFormat(String format);
51
52
	/**
53
	 * @param logBase a number between 2 and 1000 (inclusive)
54
	 */
55
	void setLogBase(Double logBase);
56
57
	/**
58
	 * @return axis log base or null if not set
59
	 */
60
	Double getLogBase();
61
62
	/**
63
	 * @return axis minimum or null if not set
64
	 */
65
	Double getMinimum();
66
67
	/**
68
	 * @param min axis minimum
69
	 */
70
	void setMinimum(Double min);
71
72
	/**
73
	 * @return axis maximum or null if not set
74
	 */
75
	Double getMaximum();
76
77
	/**
78
	 * @param max axis maximum
79
	 */
80
	void setMaximum(Double max);
81
82
	/**
83
	 * @return axis orientation
84
	 */
85
	AxisOrientation getOrientation();
86
87
	/**
88
	 * @param axis orientation
89
	 */
90
	void setOrientation(AxisOrientation orientation);
91
92
	/**
93
	 * @param crosses axis cross type
94
	 */
95
	void setCrosses(AxisCrosses crosses);
96
97
	/**
98
	 * @return axis cross type
99
	 */
100
	AxisCrosses getCrosses();
101
102
	/**
103
	 * Declare this axis cross another axis.
104
	 * @param axis that this axis should cross
105
	 */
106
	void crossAxis(ChartAxis axis);
107
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ChartAxisFactory.java (+34 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.ss.usermodel.Chart;
21
22
/**
23
 * A factory for different chart axis.
24
 *
25
 * @author Roman Kashitsyn
26
 */
27
public interface ChartAxisFactory {
28
	
29
	/**
30
	 * @return new value axis
31
	 */
32
	ValueAxis createValueAxis(AxisPosition pos);
33
34
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ChartData.java (+36 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.ss.usermodel.Chart;
21
22
/**
23
 * A base for all chart data types.
24
 *
25
 * @author Roman Kashitsyn
26
 */
27
public interface ChartData {
28
29
	/**
30
	 * Fills a chart with data specified by implementation.
31
	 *
32
	 * @param chart a chart to fill in
33
	 * @param axis chart axis to use
34
	 */
35
	void fillChart(Chart chart, ChartAxis... axis);
36
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ChartDataFactory.java (+32 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
/**
21
 * A factory for different chart data types.
22
 *
23
 * @author Roman Kashitsyn
24
 */
25
public interface ChartDataFactory {
26
	
27
	/**
28
	 * @return an appropriate ScatterChartData instance
29
	 */
30
	ScatterChartData getScatterChartData();
31
32
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ScatterChartData.java (+35 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
/**
23
 * @author Roman Kashitsyn
24
 */
25
public interface ScatterChartData extends ChartData {
26
	/**
27
	 * @return a new scatter chart serie
28
	 */
29
	ScatterChartSerie addSerie();
30
31
	/**
32
	 * @return list of all series
33
	 */
34
	List<? extends ScatterChartSerie> getSeries();
35
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ScatterChartSerie.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.ss.usermodel.Sheet;
21
import org.apache.poi.ss.util.CellRangeAddress;
22
import org.apache.poi.ss.usermodel.charts.ChartDataFactory;
23
24
/**
25
 * @author Roman Kashitsyn
26
 */
27
public interface ScatterChartSerie {
28
29
	/**
30
	 * @param sheet a sheet to take range from
31
	 * @param address a column or a row with values
32
	 */
33
	void setXValues(Sheet sheet, CellRangeAddress address);
34
	
35
	/**'
36
	 * @param sheet a sheet to take range from
37
	 * @param address a column or a row with values
38
	 */
39
	void setYValues(Sheet sheet, CellRangeAddress address);
40
41
}
(-)a/src/java/org/apache/poi/ss/usermodel/charts/ValueAxis.java (+34 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
/**
21
 * @author Roman Kashitsyn
22
 */
23
public interface ValueAxis extends ChartAxis {
24
25
	/**
26
	 * @return cross between type
27
	 */
28
	AxisCrossBetween getCrossBetween();
29
30
	/**
31
	 * @param crossBetween cross between type
32
	 */
33
	void setCrossBetween(AxisCrossBetween crossBetween);
34
}
(-)a/src/java/org/apache/poi/ss/util/CellRangeAddress.java (-2 / +16 lines)
Lines 80-89 public class CellRangeAddress extends CellRangeAddressBase { Link Here
80
     *         like single cell references (e.g. 'A1' instead of 'A1:A1').
80
     *         like single cell references (e.g. 'A1' instead of 'A1:A1').
81
     */
81
     */
82
    public String formatAsString() {
82
    public String formatAsString() {
83
        return formatAsString(null, false);
84
    }
85
86
    /**
87
     * @return the text format of this range using specified sheet name.
88
     */
89
    public String formatAsString(String sheetName, boolean useAbsoluteAddress) {
83
        StringBuffer sb = new StringBuffer();
90
        StringBuffer sb = new StringBuffer();
84
        CellReference cellRefFrom = new CellReference(getFirstRow(), getFirstColumn());
91
        if (sheetName != null) {
85
        CellReference cellRefTo = new CellReference(getLastRow(), getLastColumn());
92
            sb.append(sheetName);
93
            sb.append("!");
94
        }
95
        CellReference cellRefFrom = new CellReference(getFirstRow(), getFirstColumn(),
96
                useAbsoluteAddress, useAbsoluteAddress);
97
        CellReference cellRefTo = new CellReference(getLastRow(), getLastColumn(),
98
                useAbsoluteAddress, useAbsoluteAddress);
86
        sb.append(cellRefFrom.formatAsString());
99
        sb.append(cellRefFrom.formatAsString());
100
87
        //for a single-cell reference return A1 instead of A1:A1
101
        //for a single-cell reference return A1 instead of A1:A1
88
        if(!cellRefFrom.equals(cellRefTo)){
102
        if(!cellRefFrom.equals(cellRefTo)){
89
            sb.append(':');
103
            sb.append(':');
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFChart.java (-23 / +69 lines)
Lines 21-26 import java.io.IOException; Link Here
21
import java.io.OutputStream;
21
import java.io.OutputStream;
22
import java.util.HashMap;
22
import java.util.HashMap;
23
import java.util.Map;
23
import java.util.Map;
24
import java.util.List;
25
import java.util.ArrayList;
24
26
25
import javax.xml.namespace.QName;
27
import javax.xml.namespace.QName;
26
28
Lines 28-33 import org.apache.poi.POIXMLDocumentPart; Link Here
28
import org.apache.poi.openxml4j.opc.PackagePart;
30
import org.apache.poi.openxml4j.opc.PackagePart;
29
import org.apache.poi.openxml4j.opc.PackageRelationship;
31
import org.apache.poi.openxml4j.opc.PackageRelationship;
30
import org.apache.poi.util.Internal;
32
import org.apache.poi.util.Internal;
33
import org.apache.poi.ss.usermodel.Chart;
34
import org.apache.poi.ss.usermodel.charts.ChartAxis;
35
import org.apache.poi.ss.usermodel.charts.ChartAxisFactory;
36
import org.apache.poi.xssf.usermodel.charts.XSSFChartDataFactory;
37
import org.apache.poi.xssf.usermodel.charts.XSSFChartAxis;
38
import org.apache.poi.xssf.usermodel.charts.XSSFValueAxis;
39
import org.apache.poi.ss.usermodel.charts.ChartData;
40
import org.apache.poi.ss.usermodel.charts.AxisPosition;
31
import org.apache.xmlbeans.XmlException;
41
import org.apache.xmlbeans.XmlException;
32
import org.apache.xmlbeans.XmlObject;
42
import org.apache.xmlbeans.XmlObject;
33
import org.apache.xmlbeans.XmlOptions;
43
import org.apache.xmlbeans.XmlOptions;
Lines 51-57 import org.w3c.dom.Text; Link Here
51
 * @author Nick Burch
61
 * @author Nick Burch
52
 * @author Roman Kashitsyn
62
 * @author Roman Kashitsyn
53
 */
63
 */
54
public final class XSSFChart extends POIXMLDocumentPart {
64
public final class XSSFChart extends POIXMLDocumentPart implements Chart, ChartAxisFactory {
55
65
56
	/**
66
	/**
57
	 * Parent graphic frame.
67
	 * Parent graphic frame.
Lines 67-77 public final class XSSFChart extends POIXMLDocumentPart { Link Here
67
	 */
77
	 */
68
	private CTChart chart;
78
	private CTChart chart;
69
79
80
	List<XSSFChartAxis> axis;
81
70
	/**
82
	/**
71
	 * Create a new SpreadsheetML chart
83
	 * Create a new SpreadsheetML chart
72
	 */
84
	 */
73
	protected XSSFChart() {
85
	protected XSSFChart() {
74
		super();
86
		super();
87
		axis = new ArrayList<XSSFChartAxis>();
75
		createChart();
88
		createChart();
76
	}
89
	}
77
90
Lines 143-170 public final class XSSFChart extends POIXMLDocumentPart { Link Here
143
	}
156
	}
144
157
145
	@Override
158
	@Override
146
		protected void commit() throws IOException {
159
	protected void commit() throws IOException {
147
			XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
160
		XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
148
161
149
			/*
162
		/*
150
			   Saved chart space must have the following namespaces set:
163
		   Saved chart space must have the following namespaces set:
151
			   <c:chartSpace
164
		   <c:chartSpace
152
			      xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
165
		      xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
153
			      xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
166
		      xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
154
			      xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
167
		      xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
155
			 */
168
		 */
156
			xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
169
		xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c"));
157
			Map<String, String> map = new HashMap<String, String>();
170
		Map<String, String> map = new HashMap<String, String>();
158
			map.put(XSSFDrawing.NAMESPACE_A, "a");
171
		map.put(XSSFDrawing.NAMESPACE_A, "a");
159
			map.put(XSSFDrawing.NAMESPACE_C, "c");
172
		map.put(XSSFDrawing.NAMESPACE_C, "c");
160
			map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
173
		map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
161
			xmlOptions.setSaveSuggestedPrefixes(map);
174
		xmlOptions.setSaveSuggestedPrefixes(map);
162
175
163
			PackagePart part = getPackagePart();
176
		PackagePart part = getPackagePart();
164
			OutputStream out = part.getOutputStream();
177
		OutputStream out = part.getOutputStream();
165
			chartSpace.save(out, xmlOptions);
178
		chartSpace.save(out, xmlOptions);
166
			out.close();
179
		out.close();
167
		}
180
	}
168
181
169
	/**
182
	/**
170
	 * Returns the parent graphic frame.
183
	 * Returns the parent graphic frame.
Lines 181-186 public final class XSSFChart extends POIXMLDocumentPart { Link Here
181
		this.frame = frame;
194
		this.frame = frame;
182
	}
195
	}
183
196
197
	@Override
198
	public XSSFChartDataFactory getChartDataFactory() {
199
		return XSSFChartDataFactory.getInstance();
200
	}
201
202
	@Override
203
	public XSSFChart getChartAxisFactory() {
204
		return this;
205
	}
206
207
	@Override
208
	public void plot(ChartData data, ChartAxis... axis) {
209
		data.fillChart(this, axis);
210
	}
211
212
	@Override
213
	public XSSFValueAxis createValueAxis(AxisPosition pos) {
214
		long id = axis.size() + 1;
215
		XSSFValueAxis valueAxis = new XSSFValueAxis(this, id, pos);
216
		if (axis.size() == 1) {
217
			ChartAxis ax = axis.get(0);
218
			ax.crossAxis(valueAxis);
219
			valueAxis.crossAxis(ax);
220
		}
221
		axis.add(valueAxis);
222
		return valueAxis;
223
	}
224
225
	@Override
226
	public List<? extends XSSFChartAxis> getAxis() {
227
		return axis;
228
	}
229
184
	/**
230
	/**
185
	 * Sets the width ratio of the chart.
231
	 * Sets the width ratio of the chart.
186
	 * Chart width is ratio multiplied by parent frame width.
232
	 * Chart width is ratio multiplied by parent frame width.
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFChartAxis.java (+225 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 org.apache.poi.ss.usermodel.charts.ChartAxis;
21
import org.apache.poi.ss.usermodel.charts.AxisPosition;
22
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
23
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
24
import org.apache.poi.xssf.usermodel.XSSFChart;
25
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
26
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
27
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
28
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
29
import org.openxmlformats.schemas.drawingml.x2006.chart.CTOrientation;
30
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLogBase;
31
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
32
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
33
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
34
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses;
35
36
/**
37
 * Base class for all axis types.
38
 *
39
 * @author Roman Kashitsyn
40
 */
41
public abstract class XSSFChartAxis implements ChartAxis {
42
43
	protected XSSFChart chart;
44
45
	private static final double MIN_LOG_BASE = 2.0;
46
	private static final double MAX_LOG_BASE = 1000.0;
47
48
	protected XSSFChartAxis(XSSFChart chart) {
49
		this.chart = chart;
50
	}
51
52
	@Override
53
	public AxisPosition getPosition() {
54
		return toAxisPosition(getCTAxPos());
55
	}
56
57
	@Override
58
	public void setPosition(AxisPosition position) {
59
		getCTAxPos().setVal(fromAxisPosition(position));
60
	}
61
62
	@Override
63
	public void setNumberFormat(String format) {
64
		getCTNumFmt().setFormatCode(format);
65
		getCTNumFmt().setSourceLinked(true);
66
	}
67
68
	@Override
69
	public String getNumberFormat() {
70
		return getCTNumFmt().getFormatCode();
71
	}
72
73
	@Override
74
	public void setLogBase(Double logBase) {
75
		if (logBase < MIN_LOG_BASE ||
76
			MAX_LOG_BASE > logBase) {
77
			throw new IllegalArgumentException("Axis log base must be between 2 and 1000 (inclusive), got: " + logBase);
78
		}
79
		CTScaling scaling = getCTScaling();
80
		if (scaling.isSetLogBase()) {
81
			scaling.getLogBase().setVal(logBase.doubleValue());
82
		} else {
83
			scaling.addNewLogBase().setVal(logBase.doubleValue());
84
		}
85
	}
86
87
	@Override
88
	public Double getLogBase() {
89
		CTLogBase logBase = getCTScaling().getLogBase();
90
		if (logBase != null) {
91
			return Double.valueOf(logBase.getVal());
92
		}
93
		return null;
94
	}
95
96
	@Override
97
	public void setMinimum(Double min) {
98
		CTScaling scaling = getCTScaling();
99
		if (scaling.isSetMin()) {
100
			scaling.getMin().setVal(min.doubleValue());
101
		} else {
102
			scaling.addNewMin().setVal(min.doubleValue());
103
		}
104
	}
105
106
	@Override
107
	public Double getMinimum() {
108
		CTScaling scaling = getCTScaling();
109
		if (scaling.isSetMin()) {
110
			return Double.valueOf(scaling.getMin().getVal());
111
		} else {
112
			return null;
113
		}
114
	}
115
116
	@Override
117
	public void setMaximum(Double max) {
118
		CTScaling scaling = getCTScaling();
119
		if (scaling.isSetMax()) {
120
			scaling.getMax().setVal(max.doubleValue());
121
		} else {
122
			scaling.addNewMax().setVal(max.doubleValue());
123
		}
124
	}
125
126
	@Override
127
	public Double getMaximum() {
128
		CTScaling scaling = getCTScaling();
129
		if (scaling.isSetMax()) {
130
			return Double.valueOf(scaling.getMax().getVal());
131
		} else {
132
			return null;
133
		}
134
	}
135
136
	@Override
137
	public AxisOrientation getOrientation() {
138
		return toAxisOrientation(getCTScaling().getOrientation());
139
	}
140
141
	@Override
142
	public void setOrientation(AxisOrientation orientation) {
143
		CTScaling scaling = getCTScaling();
144
		STOrientation.Enum stOrientation = fromAxisOrientation(orientation);
145
		if (scaling.isSetOrientation()) {
146
			scaling.getOrientation().setVal(stOrientation);
147
		} else {
148
			getCTScaling().addNewOrientation().setVal(stOrientation);
149
		}
150
	}
151
152
	@Override
153
	public AxisCrosses getCrosses() {
154
		return toAxisCrosses(getCTCrosses());
155
	}
156
157
	@Override
158
	public void setCrosses(AxisCrosses crosses) {
159
		getCTCrosses().setVal(fromAxisCrosses(crosses));
160
	}
161
162
	protected abstract CTAxPos getCTAxPos();
163
	protected abstract CTNumFmt getCTNumFmt();
164
	protected abstract CTScaling getCTScaling();
165
	protected abstract CTCrosses getCTCrosses();
166
167
	private static STOrientation.Enum fromAxisOrientation(AxisOrientation orientation) {
168
		switch (orientation) {
169
			case MIN_MAX: return STOrientation.MIN_MAX;
170
			case MAX_MIN: return STOrientation.MAX_MIN;
171
			default:
172
				throw new IllegalArgumentException();
173
		}
174
	}
175
176
	private static AxisOrientation toAxisOrientation(CTOrientation ctOrientation) {
177
		switch (ctOrientation.getVal().intValue()) {
178
			case STOrientation.INT_MIN_MAX: return AxisOrientation.MIN_MAX;
179
			case STOrientation.INT_MAX_MIN: return AxisOrientation.MAX_MIN;
180
			default:
181
				throw new IllegalArgumentException();
182
		}
183
	}
184
185
	private static STCrosses.Enum fromAxisCrosses(AxisCrosses crosses) {
186
		switch (crosses) {
187
			case AUTO_ZERO: return STCrosses.AUTO_ZERO;
188
			case MIN: return STCrosses.MIN;
189
			case MAX: return STCrosses.MAX;
190
			default:
191
				throw new IllegalArgumentException();
192
		}
193
	}
194
195
	private static AxisCrosses toAxisCrosses(CTCrosses ctCrosses) {
196
		switch (ctCrosses.getVal().intValue()) {
197
			case STCrosses.INT_AUTO_ZERO: return AxisCrosses.AUTO_ZERO;
198
			case STCrosses.INT_MAX: return AxisCrosses.MAX;
199
			case STCrosses.INT_MIN: return AxisCrosses.MIN;
200
			default:
201
				throw new IllegalArgumentException();
202
		}
203
	}
204
205
	private static STAxPos.Enum fromAxisPosition(AxisPosition position) {
206
		switch (position) {
207
			case BOTTOM: return STAxPos.B;
208
			case LEFT: return STAxPos.L;
209
			case RIGHT: return STAxPos.R;
210
			case TOP: return STAxPos.T;
211
			default:
212
				throw new IllegalArgumentException();
213
		}
214
	}
215
216
	private static AxisPosition toAxisPosition(CTAxPos ctAxPos) {
217
		switch (ctAxPos.getVal().intValue()) {
218
			case STAxPos.INT_B: return AxisPosition.BOTTOM;
219
			case STAxPos.INT_L: return AxisPosition.LEFT;
220
			case STAxPos.INT_R: return AxisPosition.RIGHT;
221
			case STAxPos.INT_T: return AxisPosition.TOP;
222
			default: return AxisPosition.BOTTOM;
223
		}
224
	}
225
}
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFChartDataFactory.java (+50 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 org.apache.poi.ss.usermodel.charts.*;
21
22
/**
23
 * @author Roman Kashitsyn
24
 */
25
public class XSSFChartDataFactory implements ChartDataFactory {
26
27
	private static XSSFChartDataFactory instance;
28
29
	private XSSFChartDataFactory() {
30
		super();
31
	}
32
33
	/**
34
	 * @return new scatter chart data instance
35
	 */
36
	public XSSFScatterChartData getScatterChartData() {
37
		return new XSSFScatterChartData();
38
	}
39
40
	/**
41
	 * @return factory instance
42
	 */
43
	public static XSSFChartDataFactory getInstance() {
44
		if (instance == null) {
45
			instance = new XSSFChartDataFactory();
46
		}
47
		return instance;
48
	}
49
50
}
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFScatterChartData.java (+152 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.List;
21
import java.util.ArrayList;
22
23
import org.apache.poi.ss.usermodel.Chart;
24
import org.apache.poi.ss.usermodel.Sheet;
25
import org.apache.poi.ss.util.CellRangeAddress;
26
import org.apache.poi.ss.usermodel.charts.ScatterChartData;
27
import org.apache.poi.ss.usermodel.charts.ScatterChartSerie;
28
import org.apache.poi.ss.usermodel.charts.ChartDataFactory;
29
import org.apache.poi.ss.usermodel.charts.ChartAxis;
30
31
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
32
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart;
33
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterStyle;
34
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterSer;
35
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
36
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
37
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
38
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
39
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
40
import org.openxmlformats.schemas.drawingml.x2006.chart.STScatterStyle;
41
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses;
42
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrossBetween;
43
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
44
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
45
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
46
47
import org.apache.poi.xssf.usermodel.XSSFChart;
48
49
/**
50
 * Represents DrawingML scatter chart.
51
 *
52
 * @author Roman Kashitsyn
53
 */
54
public class XSSFScatterChartData implements ScatterChartData {
55
56
	/**
57
	 * List of all data series.
58
	 */
59
	private List<Serie> series;
60
61
	public XSSFScatterChartData() {
62
		series = new ArrayList<Serie>();
63
	}
64
65
	public static class Serie implements ScatterChartSerie {
66
		private int id;
67
		private int order;
68
		private boolean useCache;
69
		private Sheet xSheet;
70
		private Sheet ySheet;
71
		private CellRangeAddress xAddress;
72
		private CellRangeAddress yAddress;
73
74
		public Serie(int id, int order) {
75
			super();
76
			this.id = id;
77
			this.order = order;
78
			this.useCache = false;
79
		}
80
81
		@Override
82
		public void setXValues(Sheet sheet, CellRangeAddress address) {
83
			this.xSheet = sheet;
84
			this.xAddress = address;
85
		}
86
87
		@Override
88
		public void setYValues(Sheet sheet, CellRangeAddress address) {
89
			this.ySheet = sheet;
90
			this.yAddress = address;
91
		}
92
93
		/**
94
		 * @param useCache if true, cached results will be added on plot
95
		 */
96
		public void setUseCache(boolean useCache) {
97
			this.useCache = useCache;
98
		}
99
100
		protected void addToChart(CTScatterChart ctScatterChart) {
101
			CTScatterSer scatterSer = ctScatterChart.addNewSer();
102
			scatterSer.addNewIdx().setVal(this.id);
103
			scatterSer.addNewOrder().setVal(this.order);
104
105
			CTAxDataSource xVal = scatterSer.addNewXVal();
106
			CTNumRef numRef = xVal.addNewNumRef();
107
			numRef.setF(xAddress.formatAsString(xSheet.getSheetName(), true));
108
109
			CTNumDataSource yVal = scatterSer.addNewYVal();
110
			numRef = yVal.addNewNumRef();
111
			numRef.setF(yAddress.formatAsString(ySheet.getSheetName(), true));
112
		}
113
	}
114
115
	@Override
116
	public XSSFScatterChartData.Serie addSerie() {
117
		int numOfSeries = series.size();
118
		Serie newSerie = new Serie(numOfSeries, numOfSeries);
119
		series.add(newSerie);
120
		return newSerie;
121
	}
122
123
	@Override
124
	public void fillChart(Chart chart, ChartAxis... axis) {
125
		if (!(chart instanceof XSSFChart)) {
126
			throw new IllegalArgumentException("Chart must be instance of XSSFChart");
127
		}
128
129
		XSSFChart xssfChart = (XSSFChart) chart;
130
		CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
131
		CTScatterChart scatterChart = plotArea.addNewScatterChart();
132
		addStyle(scatterChart);
133
134
		for (Serie s : series) {
135
			s.addToChart(scatterChart);
136
		}
137
138
		for (ChartAxis ax : axis) {
139
			scatterChart.addNewAxId().setVal(ax.getId());
140
		}
141
	}
142
143
	@Override
144
	public List<? extends Serie> getSeries() {
145
		return series;
146
	}
147
148
	private void addStyle(CTScatterChart ctScatterChart) {
149
		CTScatterStyle scatterStyle = ctScatterChart.addNewScatterStyle();
150
		scatterStyle.setVal(STScatterStyle.LINE_MARKER);
151
	}
152
}
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/charts/XSSFValueAxis.java (+127 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 org.apache.poi.ss.usermodel.charts.ChartAxis;
21
import org.apache.poi.ss.usermodel.charts.ValueAxis;
22
import org.apache.poi.ss.usermodel.charts.AxisPosition;
23
import org.apache.poi.ss.usermodel.charts.AxisOrientation;
24
import org.apache.poi.ss.usermodel.charts.AxisCrossBetween;
25
import org.apache.poi.ss.usermodel.charts.AxisCrosses;
26
27
import org.apache.poi.xssf.usermodel.XSSFChart;
28
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
29
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
30
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
31
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
32
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
33
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
34
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrossBetween;
35
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;
36
37
/**
38
 * Value axis type.
39
 *
40
 * @author Roman Kashitsyn
41
 */
42
public class XSSFValueAxis extends XSSFChartAxis implements ValueAxis {
43
44
	private CTValAx ctValAx;
45
46
	public XSSFValueAxis(XSSFChart chart, long id, AxisPosition pos) {
47
		super(chart);
48
		createAxis(id, pos);
49
	}
50
51
	@Override
52
	public long getId() {
53
		return ctValAx.getAxId().getVal();
54
	}
55
56
	@Override
57
	public void setCrossBetween(AxisCrossBetween crossBetween) {
58
		ctValAx.getCrossBetween().setVal(fromCrossBetween(crossBetween));
59
	}
60
61
	@Override
62
	public AxisCrossBetween getCrossBetween() {
63
		return toCrossBetween(ctValAx.getCrossBetween().getVal());
64
	}
65
66
	@Override
67
	protected CTAxPos getCTAxPos() {
68
		return ctValAx.getAxPos();
69
	}
70
71
	@Override
72
	protected CTNumFmt getCTNumFmt() {
73
		if (ctValAx.isSetNumFmt()) {
74
			return ctValAx.getNumFmt();
75
		}
76
		return ctValAx.addNewNumFmt();
77
	}
78
79
	@Override
80
	protected CTScaling getCTScaling() {
81
		return ctValAx.getScaling();
82
	}
83
84
	@Override
85
	protected CTCrosses getCTCrosses() {
86
		return ctValAx.getCrosses();
87
	}
88
89
	@Override
90
	public void crossAxis(ChartAxis axis) {
91
		ctValAx.getCrossAx().setVal(axis.getId());
92
	}
93
94
	private void createAxis(long id, AxisPosition pos) {
95
		ctValAx = chart.getCTChart().getPlotArea().addNewValAx();
96
		ctValAx.addNewAxId().setVal(id);
97
		ctValAx.addNewAxPos();
98
		ctValAx.addNewScaling();
99
		ctValAx.addNewCrossBetween();
100
		ctValAx.addNewCrosses();
101
		ctValAx.addNewCrossAx();
102
		ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
103
104
		setPosition(pos);
105
		setOrientation(AxisOrientation.MIN_MAX);
106
		setCrossBetween(AxisCrossBetween.MIDPOINT_CATEGORY);
107
		setCrosses(AxisCrosses.AUTO_ZERO);
108
	}
109
110
	private static STCrossBetween.Enum fromCrossBetween(AxisCrossBetween crossBetween) {
111
		switch (crossBetween) {
112
			case BETWEEN: return STCrossBetween.BETWEEN;
113
			case MIDPOINT_CATEGORY: return STCrossBetween.MID_CAT;
114
			default:
115
				throw new IllegalArgumentException();
116
		}
117
	}
118
119
	private static AxisCrossBetween toCrossBetween(STCrossBetween.Enum ctCrossBetween) {
120
		switch (ctCrossBetween.intValue()) {
121
			case STCrossBetween.INT_BETWEEN: return AxisCrossBetween.BETWEEN;
122
			case STCrossBetween.INT_MID_CAT: return AxisCrossBetween.MIDPOINT_CATEGORY;
123
			default:
124
				throw new IllegalArgumentException();
125
		}
126
	}
127
}

Return to bug 51196