1 /*
2  * Copyright (c) 2020 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 OHOS_ACELITE_CHART_COMPONENT_H
17 #define OHOS_ACELITE_CHART_COMPONENT_H
18 
19 #include "component.h"
20 #include "non_copyable.h"
21 #include "ui_chart.h"
22 #include "ui_label.h"
23 
24 namespace OHOS {
25 namespace ACELite {
26 struct DataSerials : public MemoryHeap {
27     UIChartDataSerial *dataSerial;
28     DataSerials *next;
29     ACE_DISALLOW_COPY_AND_MOVE(DataSerials);
DataSerialsDataSerials30     DataSerials() : dataSerial(nullptr), next(nullptr) {}
31 };
32 
33 enum class PointType : uint8_t { HEAD, TOP, BOTTOM };
34 
35 struct PointOptions : public MemoryHeap {
36     uint8_t size;
37     uint8_t strokeWidth;
38     uint32_t strokeColor;
39     uint32_t fillColor;
40     ACE_DISALLOW_COPY_AND_MOVE(PointOptions);
PointOptionsPointOptions41     PointOptions() : size(POINT_SIZE), strokeWidth(1), strokeColor(0xFF0000), fillColor(0xFF0000) {}
42 };
43 
44 struct SeriesOptions : public MemoryHeap {
45     uint8_t width;
46     uint8_t margin;
47     PointOptions *head;
48     PointOptions *top;
49     PointOptions *bottom;
50     bool smooth;
51     bool isHeadSet;
52     bool isTopSet;
53     bool isBottomSet;
54     bool isLoopSet;
55     ACE_DISALLOW_COPY_AND_MOVE(SeriesOptions);
SeriesOptionsSeriesOptions56     SeriesOptions() : width(1), margin(1), head(nullptr), top(nullptr), bottom(nullptr), smooth(false),
57         isHeadSet(false), isTopSet(false), isBottomSet(false), isLoopSet(false) {}
58 };
59 
60 class ChartComponent final : public Component {
61 public:
62     ACE_DISALLOW_COPY_AND_MOVE(ChartComponent);
63     ChartComponent() = delete;
64     ChartComponent(jerry_value_t options, jerry_value_t children, AppStyleManager *styleManager);
~ChartComponent()65     ~ChartComponent() {}
66     bool CreateNativeViews() override;
67     bool SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue) override;
68     void PostUpdate(uint16_t attrKeyId) override;
69     void PostRender() override;
70     void ReleaseNativeViews() override;
71 
72 protected:
73     UIView *GetComponentRootView() const override;
74 
75 private:
76     bool Init();
77     void UpdateData(jerry_value_t dataSets, jerry_value_t keyName, uint8_t serialsCount);
78     bool UpdataDataInOrder(jerry_value_t dataSet, UIChartDataSerial &dataserial, uint16_t dataLen);
79     void AppendData(jerry_value_t dataSet, DataSerials *dataSerialNode);
80     void UpdateColorToSerial(jerry_value_t dataSet, UIChartDataSerial &dataserial, bool isStrokeColor);
81     void UpdateStrokeColorToSerial(jerry_value_t dataSet, UIChartDataSerial &dataserial);
82     void UpdateFillColorToSerial(jerry_value_t dataSet, UIChartDataSerial &dataserial);
83     void UpdateStrokeSmoothToSerial(UIChartDataSerial &dataserial);
84     bool UpdatePointToSerial(UIChartDataSerial *dataserial, PointType type);
85     bool UpdatePoints(UIChartDataSerial *dataserial);
86     void UpdateGradientToSerial(jerry_value_t dataSet, UIChartDataSerial *dataserial);
87     bool UpdateValuesToSerial(jerry_value_t dataSet, UIChartDataSerial *dataserial, uint16_t dataLen);
88     bool AppendValuesToSerial(jerry_value_t dataSet, UIChartDataSerial *dataserial, uint16_t dataLen);
89     void AppendValues(UIChartDataSerial &dataserial, Point *pointArray, uint16_t dataLen);
90     bool ParseDataValue(jerry_value_t dataValue, uint16_t interval, Point *pointArray, uint16_t arrayLen);
91     bool SetOptions(jerry_value_t options);
92     void SetOptionsInfo(jerry_value_t jSeriesVal, PointType type, char* styleName);
93     bool SetOptionsAxis(jerry_value_t options, bool isXAxis);
94     bool SetOptionsAxisDataRange(jerry_value_t options, bool isXAxis);
95     bool SetOptionsAxisDataRange(uint16_t minValue, uint16_t maxValue, uint8_t defaultMinValue,
96         uint8_t defaultMaxValue, bool isXaxis);
97     void SetOptionsAxisTick(jerry_value_t options, bool isXAxis);
98     bool SetOptionsAxisDisplay(jerry_value_t options, bool isXAxis);
99     void SetOptionsAxisColor(jerry_value_t options, bool isXAxis);
100     UIChartDataSerial::PointStyle *UpdatePointStyle(UIChartDataSerial::PointStyle *pointStyle, uint32_t fillColor,
101         uint32_t strokeColor, uint32_t radius, uint32_t strokeWidth);
102     /**
103      * @brief record series options in seriesOptions_
104      *
105      * @param [in] options.
106      */
107     void RecordOptionsSeries(jerry_value_t options);
108     void RecordOptionsSeriesLineStyle(jerry_value_t jSeriesVal);
109     void RecordOptionsSeriesPoint(jerry_value_t jSeriesVal, PointType type);
110     void RecordOptionsSeriesLoop(jerry_value_t jSeriesVal);
111     void Reset();
112     /**
113      * @brief record DataSerials into linked list
114      *
115      * @param [in] serial.
116      */
117     void AddDataSerial(DataSerials *serial);
118     void ClearDataSerialsNode(DataSerials *serial);
119     void ClearAllDataSerials();
120     uint16_t GetSizeVal(jerry_value_t obj, const char * const styleName, uint16_t defaultValue);
121     uint32_t GetColor(jerry_value_t obj, const char * const styleName, uint32_t defaultValue);
122     bool GetDisplayStatus(jerry_value_t obj);
123     static jerry_value_t AppendDatas(const jerry_value_t func, const jerry_value_t context,
124         const jerry_value_t *args, const jerry_length_t argsNum);
125     static ChartComponent *GetChartComponent(jerry_value_t nativeElement);
126     static const DataSerials *GetDataSerial(ChartComponent *chartComponent, uint8_t index);
127 
128     UIXAxis *xAxis_;
129     UIYAxis *yAxis_;
130     size_t yMinValue_;
131     size_t yMaxValue_;
132     uint16_t xMinValue_;
133     uint16_t xMaxValue_;
134     jerry_value_t options_;
135     UIChart *chartView_;
136     DataSerials *serials_;
137     uint8_t totalSerialsNum_;
138     jerry_value_t dataSetsRecord_;
139     SeriesOptions *seriesOptions_;
140     char *chartType_;
141     UIChartDataSerial::PointStyle *headPointStyle_;
142     UIChartDataSerial::PointStyle *topPointStyle_;
143     UIChartDataSerial::PointStyle *bottomPointStyle_;
144     static const char * const DATA;
145     static const char * const APPEND_SERIAL_INDEX;
146     static const char * const LINE;
147     static const char * const BAR;
148     static const char * const FUNC_JS_API;
149     static const char * const STROKE_COLOR;
150     static const char * const STROKE_WIDTH;
151     static const char * const FILL_COLOR;
152     static const char * const GRADIENT;
153     static const char * const LINE_STYLE;
154     static const char * const SMOOTH;
155     static const char * const HEAD_POINT;
156     static const char * const SHAPE;
157     static const char * const SIZE;
158     static const char * const TOP_POINT;
159     static const char * const BOTTOM_POINT;
160     static const char * const X_AXIS;
161     static const char * const Y_AXIS;
162     static const char * const MIN;
163     static const char * const MAX;
164     static const char * const TYPE;
165     static const char * const SERIES;
166     static const char * const COLOR;
167     static const char * const WIDTH;
168     static const char * const LOOP;
169     static const char * const MARGIN;
170     static const char * const DISPLAY;
171 };
172 } // namespace ACELite
173 } // namespace OHOS
174 
175 #endif // !OHOS_ACELITE_CHART_COMPONENT_H
176