1 /* This file is part of the KDE project
2 
3    Copyright 2007 Johannes Simon <johannes.simon@gmail.com>
4    Copyright 2009 Inge Wallin    <inge@lysator.liu.se>
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public
8    License as published by the Free Software Foundation; either
9    version 2 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public License
17    along with this library; see the file COPYING.LIB.  If not, write to
18    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef KCHART_AXIS_H
23 #define KCHART_AXIS_H
24 
25 
26 // Qt
27 #include <QObject>
28 
29 // KoChart
30 #include "ChartShape.h"
31 
32 #include <KChartCartesianAxis.h>
33 
34 namespace KoOdfNumberStyles {
35     struct NumericStyleFormat;
36 }
37 
38 namespace KoChart {
39 
40 enum OdfGridClass {
41     OdfMajorGrid,
42     OdfMinorGrid
43 };
44 
45 /**
46  * @brief The Axis class handles axis as well as grid settings.
47  *
48  * Data series can be attached to axes that represent an
49  * ordinate. This is done to customize the scaling, i.e., the relation
50  * in which the data of a series is visualized.
51  */
52 
53 class Axis : public QObject
54 {
55     Q_OBJECT
56 
57 public:
58     Axis(PlotArea *parent, AxisDimension dimension);
59     ~Axis();
60 
61     // odf chart:name that identifies the axis
62     QString name() const;
63     void setName(const QString &name);
64 
65     PlotArea *plotArea() const;
66     KoShape *title() const;
67     QString titleText() const;
68     bool showLabels() const;
69     bool showOverlappingDataLabels() const;
70     QString id() const;
71     AxisDimension dimension() const;
72     QList<DataSet*> dataSets() const;
73     qreal majorInterval() const;
74     qreal minorInterval() const;
75     int minorIntervalDivisor() const;
76     bool useAutomaticMajorInterval() const;
77     bool useAutomaticMinorInterval() const;
78     bool showInnerMinorTicks() const;
79     bool showOuterMinorTicks() const;
80     bool showInnerMajorTicks() const;
81     bool showOuterMajorTicks() const;
82     bool scalingIsLogarithmic() const;
83     bool showMajorGrid() const;
84     bool showMinorGrid() const;
85     Qt::Orientation orientation() const;
86     QFont font() const;
87     qreal fontSize() const;
88     bool isVisible() const;
89     bool showRuler() const;
90 
91     void setTitleText(const QString &text);
92     void setShowLabels(bool show);
93     void setShowOverlappingDataLabels(bool show);
94 
95     /**
96      * Attaches a data set to this axis, adding it to a diagram
97      * of its chart type, creating it if necessary
98      */
99     bool attachDataSet(DataSet *dataSet);
100 
101     /**
102      * Detaches a data set from this axis, removing it from the diagram of
103      * its chart type, and deleting it if it was the last data set in this diagram.
104      */
105     bool detachDataSet(DataSet *dataSet, bool silent = false);
106 
107     /**
108      * Detaches all data sets in this axis, deleting any diagram
109      * that this axis might have owned.
110      */
111     void clearDataSets();
112 
113     void setMajorInterval(qreal interval);
114     void setMinorInterval(qreal interval);
115     void setMinorIntervalDivisor(int divisor);
116     void setUseAutomaticMajorInterval(bool automatic);
117     void setUseAutomaticMinorInterval(bool automatic);
118     void setShowInnerMinorTicks(bool showTicks);
119     void setShowOuterMinorTicks(bool showTicks);
120     void setShowInnerMajorTicks(bool showTicks);
121     void setShowOuterMajorTicks(bool showTicks);
122     void setScalingLogarithmic(bool logarithmicScaling);
123     void setShowMajorGrid(bool showGrid);
124     void setShowMinorGrid(bool showGrid);
125     void setThreeD(bool threeD);
126     void setFont(const QFont &font);
127     void setFontSize(qreal size);
128     void setVisible(bool visible);
129     void setShowRuler(bool show);
130 
131     KoOdfNumberStyles::NumericStyleFormat *numericStyleFormat() const;
132     void SetNumericStyleFormat(KoOdfNumberStyles::NumericStyleFormat *numericStyleFormat) const;
133 
134     bool loadOdf(const KoXmlElement &axisElement, KoShapeLoadingContext &context);
135     bool loadOdfChartSubtypeProperties(const KoXmlElement &axisElement,
136                                         KoShapeLoadingContext &context);
137     void saveOdf(KoShapeSavingContext &context);
138     void saveOdfGrid(KoShapeSavingContext &context, OdfGridClass gridClass);
139 
140     // KChart stuff
141     KChart::CartesianAxis *kdAxis() const;
142     KChart::AbstractCoordinatePlane *kdPlane() const;
143 
144     void plotAreaChartTypeChanged(ChartType chartType);
145     void plotAreaChartSubTypeChanged(ChartSubtype chartSubType);
146     void plotAreaIsVerticalChanged();
147 
148     void registerAxis(Axis *axis);
149     void registerDiagram(KChart::AbstractCartesianDiagram *diagram);
150     void removeAxisFromDiagrams(bool clear = false);
151 
152     void update() const;
153     void requestRepaint() const;
154     void layoutPlanes();
155 
156     /**
157      * Returns the gap between bars as a  percent of the width of one bar
158      *
159      * Negative numbers signify that bars overlap
160      * This is the negative representation of the chart:overlap property.
161      *
162      * From odf 1.2 spec:
163      * The chart:overlap attribute specifies how much bars within the same category in a bar chart overlap.
164      * The attribute value is an integer that is interpreted as a percentage relative to the width of a single bar.
165      * Negative values specify gaps between bars.
166      * This attribute is evaluated for chart styles that are applied to a <chart:axis> element when a chart:dimension attribute set to y.
167      * Note: This allows bars to be attached to different axis arranged differently as long as they are grouped per axis (chart:group-bars-per-axis attribute is true).
168      */
169     int gapBetweenBars() const;
170     /**
171      * Returns the gap between sets of bars as a  percent of the width of one bar
172      * This represents the chart:gap-width property.
173      *
174      * From the odf 1.2 spec:
175      * The chart:gap-width attribute specifies a gap between neighboring groups of bars in a bar chart
176      * (that is the distance between the last bar in one category and t*he first bar in the following category).
177      * It is specified as an integer percentage relative to the width of a single bar.
178      * This attribute is evaluated for chart styles that are applied to a <chart:axis> element with chart:dimension attribute set to y.
179      * Note: Bars attached to different axis can be arranged differently as long as they are grouped per axis (chart:group-bars-per-axis attribute has the value true).
180      */
181     int gapBetweenSets() const;
182 
183     /**
184      * Set axis position to @p odfpos
185      * odfpos can be "start", "end" or a double value
186      */
187     void setOdfAxisPosition(const QString &odfpos);
188     /// @return the axis position in odf format
189     /// @see setOdfAxisPosition()
190     QString odfAxisPosition() const;
191     /**
192      * Update the chart axis position from the odf position
193      * ODF defines:
194      * end: Translates to Top for x-axes and Right for y-axes
195      *      If reversed: Translates to Bottom for x-axes and Left for y-axes
196      * start: Translates to Bottom for x-axes and Left for y-axes
197      *        If reversed: Translates to Top for x-axes and Right for y-axes
198      * value: Not supported, defaults to "start"
199      *
200      * @see odfAxisPosition()
201      */
202     void updateKChartAxisPosition();
203     /// @return the KChart axis position
204     KChart::CartesianAxis::Position kchartAxisPosition() const;
205     // @return the actual KChart axis position
206     // This is not quite kchartAxisPosition() as it also depends on bar diagram orientation
207     KChart::CartesianAxis::Position actualAxisPosition() const;
208 
209     /// @return true if direction is reversed
210     bool axisDirectionReversed() const;
211 
212     void setOdfAxisLabelsPosition(const QString &odfpos);
213     QString odfAxisLabelsPosition() const;
214 
215 public Q_SLOTS:
216     void setGapBetweenBars(int percent);
217     void setGapBetweenSets(int percent);
218     void setAngleOffset(qreal angle);
219     void setHoleSize(qreal value);
220 
221     void updateKChartStockAttributes();
222 
223 private:
224     class Private;
225     Private *const d;
226 };
227 
228 } // Namespace KoChart
229 
230 QDebug operator<<(QDebug dbg, KoChart::Axis *a);
231 
232 #endif
233