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 |
} |