1 /*
2  * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_CHART_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_CHART_H
18 
19 #include "core/components/chart/chart_component.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/data_panel/data_panel_component.h"
22 #include "core/components/progress/progress_component.h"
23 #include "frameworks/bridge/common/dom/dom_node.h"
24 
25 namespace OHOS::Ace::Framework {
26 
27 class ChartOptions {
28 public:
SetHeadPoint(const PointInfo & point)29     void SetHeadPoint(const PointInfo& point)
30     {
31         headPoint_ = point;
32     }
33 
GetHeadPoint()34     const PointInfo& GetHeadPoint() const
35     {
36         return headPoint_;
37     }
38 
SetTopPoint(const PointInfo & point)39     void SetTopPoint(const PointInfo& point)
40     {
41         topPoint_ = point;
42     }
43 
GetTopPoint()44     const PointInfo& GetTopPoint() const
45     {
46         return topPoint_;
47     }
48 
SetBottomPoint(const PointInfo & point)49     void SetBottomPoint(const PointInfo& point)
50     {
51         bottomPoint_ = point;
52     }
53 
GetBottomPoint()54     const PointInfo& GetBottomPoint() const
55     {
56         return bottomPoint_;
57     }
58 
GetSmoothFlag()59     bool GetSmoothFlag() const
60     {
61         return smoothFlag_;
62     }
63 
SetSmoothFlag(bool flag)64     void SetSmoothFlag(bool flag)
65     {
66         smoothFlag_ = flag;
67     }
68 
GetLineWidth()69     double GetLineWidth() const
70     {
71         return lineWidth_;
72     }
73 
SetLineWidth(double lineWidth)74     void SetLineWidth(double lineWidth)
75     {
76         lineWidth_ = lineWidth;
77     }
78 
GetXAxis()79     const AxisOption& GetXAxis() const
80     {
81         return xAxis_;
82     }
83 
SetXAxis(const AxisOption & xAxis)84     void SetXAxis(const AxisOption& xAxis)
85     {
86         xAxis_ = xAxis;
87     }
88 
GetYAxis()89     const AxisOption& GetYAxis() const
90     {
91         return yAxis_;
92     }
93 
SetYAxis(const AxisOption & yAxis)94     void SetYAxis(const AxisOption& yAxis)
95     {
96         yAxis_ = yAxis;
97     }
98 
GetErasePointNumber()99     int32_t GetErasePointNumber() const
100     {
101         return erasePointNumber_;
102     }
103 
SetErasePointNumber(int32_t erasePointNumber)104     void SetErasePointNumber(int32_t erasePointNumber)
105     {
106         erasePointNumber_ = erasePointNumber;
107     }
108 
GetLineGradient()109     bool GetLineGradient() const
110     {
111         return lineGradient_;
112     }
113 
SetLineGradient(bool lineGradient)114     void SetLineGradient(bool lineGradient)
115     {
116         lineGradient_ = lineGradient;
117     }
118 
GetLoop()119     bool GetLoop() const
120     {
121         return loop_;
122     }
123 
SetLoop(bool loop)124     void SetLoop(bool loop)
125     {
126         loop_ = loop;
127     }
128 
GetWholeLineGradient()129     bool GetWholeLineGradient() const
130     {
131         return wholeLineGradient_;
132     }
133 
SetWholeLineGradient(bool wholeLineGradient)134     void SetWholeLineGradient(bool wholeLineGradient)
135     {
136         wholeLineGradient_ = wholeLineGradient;
137     }
138 
GetTargetColor()139     const Color& GetTargetColor() const
140     {
141         return targetColor_;
142     }
143 
SetTargetColor(const Color & targetColor)144     void SetTargetColor(const Color& targetColor)
145     {
146         targetColor_ = targetColor;
147     }
148 
149 private:
150     AxisOption xAxis_;
151     AxisOption yAxis_;
152     double lineWidth_ = 1.0;
153     bool smoothFlag_ = false;
154     PointInfo headPoint_;
155     PointInfo topPoint_;
156     PointInfo bottomPoint_;
157     int32_t erasePointNumber_ = 0;
158     bool lineGradient_ = false;
159     bool wholeLineGradient_ = false;
160     bool loop_ = false;
161     Color targetColor_ = Color::TRANSPARENT;
162 };
163 
164 class DOMChart : public DOMNode {
165     DECLARE_ACE_TYPE(DOMChart, DOMNode);
166 
167 public:
168     DOMChart(NodeId nodeId, const std::string& nodeName);
169     ~DOMChart() override = default;
170 
171     RefPtr<Component> GetSpecializedComponent() override;
172 
173     void SetChartAttrOptions(const ChartOptions& chartOptions);
174     void SetChartAttrDatasets(const std::vector<MainChart>& datasets);
175     void SetChartAttrSegments(const std::vector<Segment>& segments);
176     void CallSpecializedMethod(const std::string& method, const std::string& args) override;
177 
178 protected:
179     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
180     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
181     void OnSetStyleFinished() override;
182     void PrepareSpecializedComponent() override;
183 
184 private:
185     void UpdateTopBottomPoint(std::vector<MainChart>& data);
186     void UpdateChartData(int32_t coorY, std::vector<MainChart>& data);
187     void SetPoint(PointInfo& pointInfo, PointInfo getPointInfo);
188     void SetChart(MainChart& chartDataset);
189 
190     RefPtr<ProgressComponent> progressChild_;
191     RefPtr<ChartComponent> chartChild_;
192     RefPtr<DataPanelComponent> dataPanelChild_;
193 
194     ChartType chartType_ { ChartType::LINE };
195     ChartOptions chartOptions_;
196     std::vector<MainChart> chartDatasets_;
197     std::vector<Segment> segments_;
198 
199     double min_ = 0.0;
200     double max_ = 100.0;
201     double percent_ = 0.0;
202     std::pair<Dimension, bool> strokeWidth_ = { Dimension(32.0, DimensionUnit::PX), false };
203     std::pair<double, bool> startAngle_ = { 240.0, false };
204     std::pair<double, bool> totalAngle_ = { 240.0, false };
205 
206     std::pair<double, bool> radius_ = { -1.0, false };
207     std::pair<double, bool> centerX_ = { -1.0, false };
208     std::pair<double, bool> centerY_ = { -1.0, false };
209 
210     std::vector<Color> colors_;
211     std::vector<double> weights_;
212     std::vector<std::pair<int32_t, bool>> lineData_;
213 
214     bool isResetPosition_ = false;
215     int32_t position_ = 0;
216     int32_t seriesNum_ = 0;
217     bool isSetFirst_ = false;
218 
219     Color textColor_;
220     Color backgroundColor_ = Color::FromString("#08000000");
221     bool trackColorSet_ = false;
222     double textSize_ = 18.0;
223     std::vector<std::string> fontFamily_;
224     bool showEffect_ = false;
225     bool autoScale_ = false;
226     double animationDuration_ = -1.0;
227 };
228 
229 } // namespace OHOS::Ace::Framework
230 
231 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_CHART_H
232