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 
110     /**
111      * @brief Obtains the enableReverse_.
112      *
113      * @return Returns the enableReverse_.
114      */
GetEnableReverse()115     bool GetEnableReverse() const
116     {
117         return enableReverse_;
118     }
119 
GetStartPoint()120     const Point& GetStartPoint() const
121     {
122         return start_;
123     }
124 
GetEndPoint()125     const Point& GetEndPoint() const
126     {
127         return end_;
128     }
129 
130     /**
131      * @brief Sets the markInterval_.
132      *
133      * @param Sets the markInterval_.
134      */
SetMarkInterval(float Interval)135     void SetMarkInterval(float Interval)
136     {
137         markInterval_ = Interval;
138     }
139 
GetMarkInterval()140     float GetMarkInterval() const
141     {
142         return markInterval_;
143     }
144 
145     /**
146      * @brief Sets the line color of the coordinate axis.
147      *
148      * @param color Indicates the line color to set. For details, see {@link ColorType}.
149      * @since 1.0
150      * @version 1.0
151      */
152     void SetLineColor(const ColorType& color);
153 
154     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
155 
OnPreDraw(Rect & invalidatedArea)156     bool OnPreDraw(Rect& invalidatedArea) const override
157     {
158         return false;
159     }
160 
161     virtual bool UpdateAxis() = 0;
162 
163     /**
164      * @brief Translates data into pixel coordinates.
165      *
166      * This function calculates the relative position of a pixel in the corresponding {@link UIChart}
167      * based on the value of <b>value</b> and the data range of the coordinate axis.
168      *
169      * @param value Indicates the current value. The coordinate value obtained after translation is
170      *              also printed using this parameter.
171      * @since 1.0
172      * @version 1.0
173      */
174     virtual void TranslateToPixel(int16_t& value) = 0;
175 
176     virtual void UpdateAxisPoints() = 0;
177 
178 protected:
179     float maxRange_;
180     float minRange_;
181     Point start_;
182     Point end_;
183     float markInterval_;
184     float dataPerMark_;
185     float dataInterval_;
186     uint16_t markDataCount_;
187     bool enableReverse_;
188 
189     static constexpr uint8_t AXIS_DEFAULT_MARK_INTERVAL = 5;
190     static constexpr uint8_t AXIS_DEFAULT_MARK_LENGTH = 1;
191 
192     virtual void DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) = 0;
193 };
194 
195 /**
196  * @brief Defines the unique attributes and functions for the x-axis. This class is used in {@link UIChart}.
197  *
198  * @see UIAxis
199  * @since 1.0
200  * @version 1.0
201  */
202 class UIXAxis : public UIAxis {
203 public:
204     /**
205      * @brief A constructor used to create a <b>UIXAxis</b> instance.
206      *
207      * @since 1.0
208      * @version 1.0
209      */
UIXAxis()210     UIXAxis() {}
211 
212     /**
213      * @brief A destructor used to delete the <b>UIXAxis</b> instance.
214      *
215      * @since 1.0
216      * @version 1.0
217      */
~UIXAxis()218     virtual ~UIXAxis() {}
219 
220     bool UpdateAxis() override;
221 
222     /**
223      * @brief Translates data into the x coordinate of a pixel.
224      *
225      * This function calculates the position of the corresponding pixel (relative position in the chart)
226      * based on the value of <b>value</b> and the data range of the x-axis.
227      *
228      * @param value Indicates the current value. The x coordinate obtained after translation is also
229      *              printed using this parameter.
230      * @since 1.0
231      * @version 1.0
232      */
233     void TranslateToPixel(int16_t& value) override;
234 
235     /**
236      * @brief Sets the value range of the X axis.
237      *
238      * The maximum value must be greater than the minimum value. Otherwise, the setting fails.
239      *
240      * @param min Indicates the minimum value to set.
241      * @param max Indicates the maximum value to set.
242      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
243      * @since 1.0
244      * @version 1.0
245      */
246     bool SetDataRange(uint16_t min, uint16_t max) override;
247 
248     /**
249      * @brief Sets the number of scales on the x-axis.
250      *
251      * For a bar chart, the number of scales must be the same as that of bars so that each bar can be properly
252      * displayed between two scales.
253      *
254      * @param count Indicates the number of scales to set. The default value is <b>5</b>.
255      * @since 1.0
256      * @version 1.0
257      */
258     void SetMarkNum(uint16_t count) override;
259 
260     /**
261      * @brief Obtains the markDataCount_.
262      *
263      * @return Returns the markDataCount_.
264      */
GetMarkNum()265     uint16_t GetMarkNum() const
266     {
267         return markDataCount_;
268     }
269 
270     void UpdateAxisPoints() override;
271 
272 private:
273     void DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
274 };
275 
276 /**
277  * @brief Defines the unique attributes and functions for the y-axis. This class is used in {@link UIChart}.
278  *
279  * @see UIAxis
280  * @since 1.0
281  * @version 1.0
282  */
283 class UIYAxis : public UIAxis {
284 public:
285     /**
286      * @brief A constructor used to create a <b>UIYAxis</b> instance.
287      *
288      * @since 1.0
289      * @version 1.0
290      */
UIYAxis()291     UIYAxis() {}
292 
293     /**
294      * @brief A destructor used to delete the <b>UIYAxis</b> instance.
295      *
296      * @since 1.0
297      * @version 1.0
298      */
~UIYAxis()299     virtual ~UIYAxis() {}
300 
301     bool UpdateAxis() override;
302 
303     /**
304      * @brief Translates data into the y coordinate of a pixel.
305      *
306      * Calculates the position of the corresponding pixel (relative position in the chart) based on the value
307      * of <b>value</b> and the data range of the Y axis.
308      *
309      * @param value Indicates the current value. The y coordinate obtained after translation is also printed
310      *              using this parameter.
311      * @since 1.0
312      * @version 1.0
313      */
314     void TranslateToPixel(int16_t& value) override;
315 
316     /**
317      * @brief Sets the value range of the y-axis.
318      *
319      * The maximum value must be greater than the minimum value. Otherwise, the setting fails.
320      *
321      * @param min Indicates the minimum value to set.
322      * @param max Indicates the maximum value to set.
323      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
324      * @since 1.0
325      * @version 1.0
326      */
327     bool SetDataRange(uint16_t min, uint16_t max) override;
328 
329     /**
330      * @brief Sets the number of scales on the Y axis.
331      *
332      * @param count Indicates the number of scales to set. The default value is <b>5</b>.
333      * @since 1.0
334      * @version 1.0
335      */
336     void SetMarkNum(uint16_t count) override;
337 
338     /**
339      * @brief Obtains the markDataCount_.
340      *
341      * @return Returns the markDataCount_.
342      */
GetMarkNum()343     uint16_t GetMarkNum() const
344     {
345         return markDataCount_;
346     }
347 
348     void UpdateAxisPoints() override;
349 
350 private:
351     void DrawAxisMark(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
352 };
353 } // namespace OHOS
354 #endif // GRAPHIC_LITE_UI_AXIS_H
355