• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file ui_axis.h
28  *
29  * @brief Defines the attributes and functions of the x- and y-axises. This class is used in {@link UIChart}.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_UI_AXIS_H
36 #define GRAPHIC_LITE_UI_AXIS_H
37 
38 #include "components/ui_label.h"
39 #include "components/ui_view_group.h"
40 
41 namespace OHOS {
42 /**
43  * @brief Represents the coordinate axis base class, which defines the basic attributes of coordinate axis,
44  *        sets whether a coordinate axis is visible, and sets the number of scales on a coordinate axis.
45  *        This class is used in {@link UIChart}.
46  *
47  * @since 1.0
48  * @version 1.0
49  */
50 class UIAxis : public UIViewGroup {
51 public:
52     /**
53      * @brief A constructor used to create a <b>UIAxis</b> instance.
54      *
55      * @since 1.0
56      * @version 1.0
57      */
58     UIAxis();
59 
60     /**
61      * @brief A destructor used to delete the <b>UIAxis</b> instance.
62      *
63      * @since 1.0
64      * @version 1.0
65      */
~UIAxis()66     virtual ~UIAxis() {}
67 
68     /**
69      * @brief Obtains the view type.
70      *
71      * @return Returns the view type. For details, see {@link UIViewType}.
72      * @since 1.0
73      * @version 1.0
74      */
GetViewType()75     UIViewType GetViewType() const override
76     {
77         return UI_AXIS;
78     }
79 
80     /**
81      * @brief Sets the value range of a coordinate axis.
82      *
83      * The maximum value must be greater than the minimum value. Otherwise, the setting fails.
84      *
85      * @param min Indicates the minimum value to set.
86      * @param max Indicates the maximum value to set.
87      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
88      * @since 1.0
89      * @version 1.0
90      */
91     virtual bool SetDataRange(uint16_t min, uint16_t max) = 0;
92 
93     /**
94      * @brief Sets the number of scales on a coordinate axis.
95      *
96      * For a bar chart, the number of scales must be the same as that of bars so that each bar
97      * can be properly displayed between two scales.
98      *
99      * @param count Indicates the number of scales to set. The default value is <b>5</b>.
100      * @since 1.0
101      * @version 1.0
102      */
103     virtual void SetMarkNum(uint16_t count) = 0;
104 
EnableReverse(bool enable)105     void EnableReverse(bool enable)
106     {
107         enableReverse_ = enable;
108     }
109 
GetStartPoint()110     const Point& GetStartPoint() const
111     {
112         return start_;
113     }
114 
GetEndPoint()115     const Point& GetEndPoint() const
116     {
117         return end_;
118     }
119 
GetMarkInterval()120     float GetMarkInterval() const
121     {
122         return markInterval_;
123     }
124 
125     /**
126      * @brief Sets the line color of the coordinate axis.
127      *
128      * @param color Indicates the line color to set. For details, see {@link ColorType}.
129      * @since 1.0
130      * @version 1.0
131      */
132     void SetLineColor(const ColorType& color);
133 
134     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
135 
OnPreDraw(Rect & invalidatedArea)136     bool OnPreDraw(Rect& invalidatedArea) const override
137     {
138         return false;
139     }
140 
141     virtual bool UpdateAxis() = 0;
142 
143     /**
144      * @brief Translates data into pixel coordinates.
145      *
146      * This function calculates the relative position of a pixel in the corresponding {@link UIChart}
147      * based on the value of <b>value</b> and the data range of the coordinate axis.
148      *
149      * @param value Indicates the current value. The coordinate value obtained after translation is
150      *              also printed using this parameter.
151      * @since 1.0
152      * @version 1.0
153      */
154     virtual void TranslateToPixel(int16_t& value) = 0;
155 
156     virtual void UpdateAxisPoints() = 0;
157 
158 protected:
159     float maxRange_;
160     float minRange_;
161     Point start_;
162     Point end_;
163     float markInterval_;
164     float dataPerMark_;
165     float dataInterval_;
166     uint16_t markDataCount_;
167     bool enableReverse_;
168 
169     static constexpr uint8_t AXIS_DEFAULT_MARK_INTERVAL = 5;
170     static constexpr uint8_t AXIS_DEFAULT_MARK_LENGTH = 1;
171 
172     virtual void DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) = 0;
173 };
174 
175 /**
176  * @brief Defines the unique attributes and functions for the x-axis. This class is used in {@link UIChart}.
177  *
178  * @see UIAxis
179  * @since 1.0
180  * @version 1.0
181  */
182 class UIXAxis : public UIAxis {
183 public:
184     /**
185      * @brief A constructor used to create a <b>UIXAxis</b> instance.
186      *
187      * @since 1.0
188      * @version 1.0
189      */
UIXAxis()190     UIXAxis() {}
191 
192     /**
193      * @brief A destructor used to delete the <b>UIXAxis</b> instance.
194      *
195      * @since 1.0
196      * @version 1.0
197      */
~UIXAxis()198     virtual ~UIXAxis() {}
199 
200     bool UpdateAxis() override;
201 
202     /**
203      * @brief Translates data into the x coordinate of a pixel.
204      *
205      * This function calculates the position of the corresponding pixel (relative position in the chart)
206      * based on the value of <b>value</b> and the data range of the x-axis.
207      *
208      * @param value Indicates the current value. The x coordinate obtained after translation is also
209      *              printed using this parameter.
210      * @since 1.0
211      * @version 1.0
212      */
213     void TranslateToPixel(int16_t& value) override;
214 
215     /**
216      * @brief Sets the value range of the X axis.
217      *
218      * The maximum value must be greater than the minimum value. Otherwise, the setting fails.
219      *
220      * @param min Indicates the minimum value to set.
221      * @param max Indicates the maximum value to set.
222      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
223      * @since 1.0
224      * @version 1.0
225      */
226     bool SetDataRange(uint16_t min, uint16_t max) override;
227 
228     /**
229      * @brief Sets the number of scales on the x-axis.
230      *
231      * For a bar chart, the number of scales must be the same as that of bars so that each bar can be properly
232      * displayed between two scales.
233      *
234      * @param count Indicates the number of scales to set. The default value is <b>5</b>.
235      * @since 1.0
236      * @version 1.0
237      */
238     void SetMarkNum(uint16_t count) override;
239 
240     void UpdateAxisPoints() override;
241 
242 private:
243     void DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
244 };
245 
246 /**
247  * @brief Defines the unique attributes and functions for the y-axis. This class is used in {@link UIChart}.
248  *
249  * @see UIAxis
250  * @since 1.0
251  * @version 1.0
252  */
253 class UIYAxis : public UIAxis {
254 public:
255     /**
256      * @brief A constructor used to create a <b>UIYAxis</b> instance.
257      *
258      * @since 1.0
259      * @version 1.0
260      */
UIYAxis()261     UIYAxis() {}
262 
263     /**
264      * @brief A destructor used to delete the <b>UIYAxis</b> instance.
265      *
266      * @since 1.0
267      * @version 1.0
268      */
~UIYAxis()269     virtual ~UIYAxis() {}
270 
271     bool UpdateAxis() override;
272 
273     /**
274      * @brief Translates data into the y coordinate of a pixel.
275      *
276      * Calculates the position of the corresponding pixel (relative position in the chart) based on the value
277      * of <b>value</b> and the data range of the Y axis.
278      *
279      * @param value Indicates the current value. The y coordinate obtained after translation is also printed
280      *              using this parameter.
281      * @since 1.0
282      * @version 1.0
283      */
284     void TranslateToPixel(int16_t& value) override;
285 
286     /**
287      * @brief Sets the value range of the y-axis.
288      *
289      * The maximum value must be greater than the minimum value. Otherwise, the setting fails.
290      *
291      * @param min Indicates the minimum value to set.
292      * @param max Indicates the maximum value to set.
293      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
294      * @since 1.0
295      * @version 1.0
296      */
297     bool SetDataRange(uint16_t min, uint16_t max) override;
298 
299     /**
300      * @brief Sets the number of scales on the Y axis.
301      *
302      * @param count Indicates the number of scales to set. The default value is <b>5</b>.
303      * @since 1.0
304      * @version 1.0
305      */
306     void SetMarkNum(uint16_t count) override;
307 
308     void UpdateAxisPoints() override;
309 
310 private:
311     void DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
312 };
313 } // namespace OHOS
314 #endif // GRAPHIC_LITE_UI_AXIS_H
315