aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartAxis.java
blob: cac2864a4fce5137255a4462957d99a3fa4495ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.xddf.usermodel.chart;

import org.apache.poi.util.Beta;
import org.apache.poi.xddf.usermodel.HasShapeProperties;
import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
import org.apache.poi.xddf.usermodel.text.XDDFRunProperties;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartLines;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumFmt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickLblPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;

/**
 * Base class for all axis types.
 */
@Beta
public abstract class XDDFChartAxis implements HasShapeProperties {
    protected abstract CTUnsignedInt getCTAxId();

    protected abstract CTAxPos getCTAxPos();

    protected abstract CTNumFmt getCTNumFmt();

    protected abstract CTScaling getCTScaling();

    protected abstract CTCrosses getCTCrosses();

    protected abstract CTBoolean getDelete();

    protected abstract CTTickMark getMajorCTTickMark();

    protected abstract CTTickMark getMinorCTTickMark();

    protected abstract CTTickLblPos getCTTickLblPos();

    public abstract XDDFShapeProperties getOrAddMajorGridProperties();

    public abstract XDDFShapeProperties getOrAddMinorGridProperties();

    /**
     * @since POI 4.0.2
     */
    public abstract XDDFRunProperties getOrAddTextProperties();

    /**
     * @since 4.0.1
     */
    public abstract void setTitle(String text);

    /**
     * @return true if minor unit value is defined, false otherwise
     */
    public abstract boolean isSetMinorUnit();

    /**
     * @param minor
     *            axis minor unit
     */
    public abstract void setMinorUnit(double minor);

    /**
     * @return axis minor unit or NaN if not set
     */
    public abstract double getMinorUnit();

    /**
     * @return true if major unit value is defined, false otherwise
     */
    public abstract boolean isSetMajorUnit();

    /**
     * @param major
     *            axis major unit
     */
    public abstract void setMajorUnit(double major);

    /**
     * @return axis major unit or NaN if not set
     */
    public abstract double getMajorUnit();

    /**
     * @return axis id
     */
    public long getId() {
        return getCTAxId().getVal();
    }

    /**
     * @return axis position
     */
    public AxisPosition getPosition() {
        return AxisPosition.valueOf(getCTAxPos().getVal());
    }

    /**
     * @param position
     *            new axis position
     */
    public void setPosition(AxisPosition position) {
        getCTAxPos().setVal(position.underlying);
    }

    /**
     * Use this to check before retrieving a number format, as calling
     * {@link #getNumberFormat()} may create a default one if none exists.
     *
     * @return true if a number format element is defined, false if not
     */
    public abstract boolean hasNumberFormat();

    /**
     * @param format
     *            axis number format
     */
    public void setNumberFormat(String format) {
        getCTNumFmt().setFormatCode(format);
        getCTNumFmt().setSourceLinked(true);
    }

    /**
     * @return axis number format (can be null if no number format is set)
     */
    public String getNumberFormat() {
        return hasNumberFormat() ? getCTNumFmt().getFormatCode() : null;
    }

    /**
     * @return true if log base is defined, false otherwise
     */
    public boolean isSetLogBase() {
        return getCTScaling().isSetLogBase();
    }

    private static final double MIN_LOG_BASE = 2.0;
    private static final double MAX_LOG_BASE = 1000.0;

    /**
     * @param logBase
     *            a number between 2 and 1000 (inclusive)
     * @throws IllegalArgumentException
     *             if log base not within allowed range
     */
    public void setLogBase(double logBase) {
        if (logBase < MIN_LOG_BASE || MAX_LOG_BASE < logBase) {
            throw new IllegalArgumentException("Axis log base must be between 2 and 1000 (inclusive), got: " + logBase);
        }
        CTScaling scaling = getCTScaling();
        if (scaling.isSetLogBase()) {
            scaling.getLogBase().setVal(logBase);
        } else {
            scaling.addNewLogBase().setVal(logBase);
        }
    }

    /**
     * @return axis log base or NaN if not set
     */
    public double getLogBase() {
        CTScaling scaling = getCTScaling();
        if (scaling.isSetLogBase()) {
            return scaling.getLogBase().getVal();
        }
        return Double.NaN;
    }

    /**
     * @return true if minimum value is defined, false otherwise
     */
    public boolean isSetMinimum() {
        return getCTScaling().isSetMin();
    }

    /**
     * @param min
     *            axis minimum
     */
    public void setMinimum(double min) {
        CTScaling scaling = getCTScaling();
        if (Double.isNaN(min)) {
            if (scaling.isSetMin()) {
                scaling.unsetMin();
            }
        } else {
            if (scaling.isSetMin()) {
                scaling.getMin().setVal(min);
            } else {
                scaling.addNewMin().setVal(min);
            }
        }
    }

    /**
     * @return axis minimum or NaN if not set
     */
    public double getMinimum() {
        CTScaling scaling = getCTScaling();
        if (scaling.isSetMin()) {
            return scaling.getMin().getVal();
        } else {
            return Double.NaN;
        }
    }

    /**
     * @return true if maximum value is defined, false otherwise
     */
    public boolean isSetMaximum() {
        return getCTScaling().isSetMax();
    }

    /**
     * @param max
     *            axis maximum
     */
    public void setMaximum(double max) {
        CTScaling scaling = getCTScaling();
        if (Double.isNaN(max)) {
            if (scaling.isSetMax()) {
                scaling.unsetMax();
            }
        } else {
            if (scaling.isSetMax()) {
                scaling.getMax().setVal(max);
            } else {
                scaling.addNewMax().setVal(max);
            }
        }
    }

    /**
     * @return axis maximum or NaN if not set
     */
    public double getMaximum() {
        CTScaling scaling = getCTScaling();
        if (scaling.isSetMax()) {
            return scaling.getMax().getVal();
        } else {
            return Double.NaN;
        }
    }

    /**
     * @return axis orientation
     */
    public AxisOrientation getOrientation() {
        return AxisOrientation.valueOf(getCTScaling().getOrientation().getVal());
    }

    /**
     * @param orientation
     *            axis orientation
     */
    public void setOrientation(AxisOrientation orientation) {
        CTScaling scaling = getCTScaling();
        if (scaling.isSetOrientation()) {
            scaling.getOrientation().setVal(orientation.underlying);
        } else {
            scaling.addNewOrientation().setVal(orientation.underlying);
        }
    }

    /**
     * @return axis cross type
     */
    public AxisCrosses getCrosses() {
        return AxisCrosses.valueOf(getCTCrosses().getVal());
    }

    /**
     * @param crosses
     *            axis cross type
     */
    public void setCrosses(AxisCrosses crosses) {
        getCTCrosses().setVal(crosses.underlying);
    }

    /**
     * Declare this axis cross another axis.
     *
     * @param axis
     *            that this axis should cross
     */
    public abstract void crossAxis(XDDFChartAxis axis);

    /**
     * @return visibility of the axis.
     */
    public boolean isVisible() {
        return !getDelete().getVal();
    }

    /**
     * @param value
     *            visibility of the axis.
     */
    public void setVisible(boolean value) {
        getDelete().setVal(!value);
    }

    /**
     * @return major tick mark.
     */
    public AxisTickMark getMajorTickMark() {
        return AxisTickMark.valueOf(getMajorCTTickMark().getVal());
    }

    /**
     * @param tickMark
     *            major tick mark type.
     */
    public void setMajorTickMark(AxisTickMark tickMark) {
        getMajorCTTickMark().setVal(tickMark.underlying);
    }

    /**
     * @return minor tick mark.
     */
    public AxisTickMark getMinorTickMark() {
        return AxisTickMark.valueOf(getMinorCTTickMark().getVal());
    }

    /**
     * @param tickMark
     *            minor tick mark type.
     */
    public void setMinorTickMark(AxisTickMark tickMark) {
        getMinorCTTickMark().setVal(tickMark.underlying);
    }

    /**
     * @return tick label position.
     * @since POI 4.0.2
     */
    public AxisTickLabelPosition getTickLabelPosition() {
        return AxisTickLabelPosition.valueOf(getCTTickLblPos().getVal());
    }

    /**
     * @param labelPosition
     *            tick label position.
     * @since POI 4.0.2
     */
    public void setTickLabelPosition(AxisTickLabelPosition labelPosition) {
        getCTTickLblPos().setVal(labelPosition.underlying);
    }

    protected CTTextCharacterProperties getOrAddTextProperties(CTTextBody body) {
        CTTextCharacterProperties properties;
        if (body.getBodyPr() == null) {
            body.addNewBodyPr();
        }
        CTTextParagraph paragraph;
        if (body.sizeOfPArray() > 0) {
            paragraph = body.getPArray(0);
        } else {
            paragraph = body.addNewP();
        }
        CTTextParagraphProperties paraprops;
        if (paragraph.isSetPPr()) {
            paraprops = paragraph.getPPr();
        } else {
            paraprops = paragraph.addNewPPr();
        }
        if (paraprops.isSetDefRPr()) {
            properties = paraprops.getDefRPr();
        } else {
            properties = paraprops.addNewDefRPr();
        }
        return properties;
    }

    protected CTShapeProperties getOrAddLinesProperties(CTChartLines gridlines) {
        CTShapeProperties properties;
        if (gridlines.isSetSpPr()) {
            properties = gridlines.getSpPr();
        } else {
            properties = gridlines.addNewSpPr();
        }
        return properties;
    }

    protected long getNextAxId(CTPlotArea plotArea) {
        return 0L
                + plotArea.sizeOfValAxArray()
                + plotArea.sizeOfCatAxArray()
                + plotArea.sizeOfDateAxArray()
                + plotArea.sizeOfSerAxArray();
    }
}