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 #include "frameworks/bridge/js_frontend/engine/jsi/jsi_chart_bridge.h"
17 
18 namespace OHOS::Ace::Framework {
19 namespace {
20 
21 constexpr int32_t VALID_ARRAY_LENGTH = 2;
22 constexpr uint32_t ARRAY_X_VALUE = 0;
23 constexpr uint32_t ARRAY_Y_VALUE = 1;
24 
GetAttrOptionsAxis(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,AxisOption & axisOption)25 void GetAttrOptionsAxis(
26     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, AxisOption& axisOption)
27 {
28     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
29         LOGE("none found attrs");
30         return;
31     }
32     shared_ptr<JsValue> propertyNames;
33     int32_t len = 0;
34     valObject->GetPropertyNames(runtime, propertyNames, len);
35     for (auto i = 0; i < len; i++) {
36         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
37         if (!key) {
38             LOGW("key is null. Ignoring!");
39             continue;
40         }
41         std::string keyStr = key->ToString(runtime);
42         shared_ptr<JsValue> item = valObject->GetProperty(runtime, key);
43         if (!item) {
44             LOGW("item value is null. Ignoring!");
45             continue;
46         }
47         if (item->IsNumber(runtime) || item->IsBoolean(runtime) || item->IsString(runtime)) {
48             std::string valStr = item->ToString(runtime);
49             // must sorted by key.
50             static const LinearMapNode<void (*)(const std::string&, AxisOption&)> chartOptionsAxisMap[] = {
51                 { "axisTick",
52                     [](const std::string& valStr, AxisOption& axis) { axis.tickNumber = StringToInt(valStr); } },
53                 { "color",
54                     [](const std::string& valStr, AxisOption& axis) { axis.color = Color::FromString(valStr); } },
55                 { "display", [](const std::string& valStr, AxisOption& axis) { axis.display = StringToBool(valStr); } },
56                 { "max", [](const std::string& valStr, AxisOption& axis) { axis.max = StringToDouble(valStr); } },
57                 { "min", [](const std::string& valStr, AxisOption& axis) { axis.min = StringToDouble(valStr); } },
58             };
59             auto iter = BinarySearchFindIndex(chartOptionsAxisMap, ArraySize(chartOptionsAxisMap), keyStr.c_str());
60             if (iter != -1) {
61                 chartOptionsAxisMap[iter].value(valStr, axisOption);
62             }
63         }
64     }
65 }
66 
GetAttrOptionsSeriesPoint(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,PointInfo & pointInfo)67 void GetAttrOptionsSeriesPoint(
68     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, PointInfo& pointInfo)
69 {
70     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
71         LOGE("none found attrs");
72         return;
73     }
74     shared_ptr<JsValue> propertyNames;
75     int32_t len = 0;
76     valObject->GetPropertyNames(runtime, propertyNames, len);
77     pointInfo.SetDisplay(true);
78     for (auto i = 0; i < len; i++) {
79         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
80         if (!key) {
81             LOGW("key is null. Ignoring!");
82             continue;
83         }
84         std::string keyStr = key->ToString(runtime);
85         shared_ptr<JsValue> item = valObject->GetProperty(runtime, key);
86         if (!item) {
87             LOGW("item value is null. Ignoring!");
88             continue;
89         }
90         if (item->IsNumber(runtime) || item->IsBoolean(runtime) || item->IsString(runtime)) {
91             std::string valStr = item->ToString(runtime);
92             static const LinearMapNode<void (*)(const std::string&, PointInfo&)> chartOptionsPointMap[] = {
93                 { "display", [](const std::string& valStr,
94                                  PointInfo& pointInfo) { pointInfo.SetDisplay(StringToBool(valStr)); } },
95                 { "fillColor", [](const std::string& valStr,
96                                    PointInfo& pointInfo) { pointInfo.SetFillColor(Color::FromString(valStr)); } },
97                 { "shape",
98                     [](const std::string& valStr, PointInfo& pointInfo) {
99                         PointShape shape = (valStr == "circle")
100                                                ? PointShape::CIRCLE
101                                                : (valStr == "square") ? PointShape::SQUARE : PointShape::TRIANGLE;
102                         pointInfo.SetPointShape(shape);
103                     } },
104                 { "size", [](const std::string& valStr,
105                               PointInfo& pointInfo) { pointInfo.SetPointSize(StringToDimension(valStr)); } },
106                 { "strokeColor", [](const std::string& valStr,
107                                      PointInfo& pointInfo) { pointInfo.SetStrokeColor(Color::FromString(valStr)); } },
108                 { "strokeWidth",
109                     [](const std::string& valStr, PointInfo& pointInfo) {
110                         pointInfo.SetPointStrokeWidth(StringToDimension(valStr));
111                     } },
112             };
113             auto iter = BinarySearchFindIndex(chartOptionsPointMap, ArraySize(chartOptionsPointMap), keyStr.c_str());
114             if (iter != -1) {
115                 chartOptionsPointMap[iter].value(valStr, pointInfo);
116             }
117         }
118     }
119 }
120 
GetChartAttrOptionsSeriesLineStyle(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,ChartOptions & chartOptions)121 void GetChartAttrOptionsSeriesLineStyle(
122     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, ChartOptions& chartOptions)
123 {
124     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
125         LOGE("none found attrs");
126         return;
127     }
128     shared_ptr<JsValue> propertyNames;
129     int32_t len = 0;
130     valObject->GetPropertyNames(runtime, propertyNames, len);
131     for (auto i = 0; i < len; i++) {
132         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
133         if (!key) {
134             LOGW("key is null. Ignoring!");
135             continue;
136         }
137         std::string keyStr = key->ToString(runtime);
138         shared_ptr<JsValue> item = valObject->GetProperty(runtime, key);
139         if (!item) {
140             LOGW("item value is null. Ignoring!");
141             continue;
142         }
143         if (item->IsNumber(runtime) || item->IsBoolean(runtime) || item->IsString(runtime)) {
144             std::string valStr = item->ToString(runtime);
145             static const LinearMapNode<void (*)(const std::string&, ChartOptions&)> chartOptionsSeriesLineStyleMap[] = {
146                 { "lineGradient", [](const std::string& valStr, ChartOptions& chartOptions) {
147                     chartOptions.SetWholeLineGradient(StringToBool(valStr)); } },
148                 { "smooth", [](const std::string& valStr,
149                                 ChartOptions& chartOptions) { chartOptions.SetSmoothFlag(StringToBool(valStr)); } },
150                 { "targetColor", [](const std::string& valStr, ChartOptions& chartOptions) {
151                     chartOptions.SetTargetColor(Color::FromString(valStr)); } },
152                 { "width", [](const std::string& valStr,
153                                ChartOptions& chartOptions) { chartOptions.SetLineWidth(StringToDouble(valStr)); } },
154             };
155             auto iter = BinarySearchFindIndex(
156                 chartOptionsSeriesLineStyleMap, ArraySize(chartOptionsSeriesLineStyleMap), keyStr.c_str());
157             if (iter != -1) {
158                 chartOptionsSeriesLineStyleMap[iter].value(valStr, chartOptions);
159             }
160         }
161     }
162 }
163 
GetChartAttrOptionsSeriesLoop(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,ChartOptions & chartOptions)164 void GetChartAttrOptionsSeriesLoop(
165     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, ChartOptions& chartOptions)
166 {
167     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
168         LOGE("none found attrs");
169         return;
170     }
171     shared_ptr<JsValue> propertyNames;
172     int32_t len = 0;
173     valObject->GetPropertyNames(runtime, propertyNames, len);
174     chartOptions.SetLoop(true);
175     for (auto i = 0; i < len; i++) {
176         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
177         if (!key) {
178             LOGW("key is null. Ignoring!");
179             continue;
180         }
181         std::string keyStr = key->ToString(runtime);
182         shared_ptr<JsValue> val = valObject->GetProperty(runtime, key);
183         if (!val) {
184             LOGW("val is null. Ignoring!");
185             continue;
186         }
187         if (val->IsNumber(runtime) || val->IsBoolean(runtime) || val->IsString(runtime)) {
188             std::string valStr = val->ToString(runtime);
189             static const LinearMapNode<void (*)(const std::string&, ChartOptions&)> chartOptionsSeriesLoopMap[] = {
190                 { "gradient", [](const std::string& valStr,
191                                   ChartOptions& chartOptions) { chartOptions.SetLineGradient(StringToBool(valStr)); } },
192                 { "margin",
193                     [](const std::string& valStr, ChartOptions& chartOptions) {
194                         chartOptions.SetErasePointNumber(StringToInt(valStr));
195                     } },
196             };
197             auto iter =
198                 BinarySearchFindIndex(chartOptionsSeriesLoopMap, ArraySize(chartOptionsSeriesLoopMap), keyStr.c_str());
199             if (iter != -1) {
200                 chartOptionsSeriesLoopMap[iter].value(valStr, chartOptions);
201             }
202         }
203     }
204 }
205 
GetChartAttrOptionsSeries(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,ChartOptions & chartOptions)206 void GetChartAttrOptionsSeries(
207     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, ChartOptions& chartOptions)
208 {
209     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
210         LOGE("none found attrs");
211         return;
212     }
213     shared_ptr<JsValue> propertyNames;
214     int32_t len = 0;
215     valObject->GetPropertyNames(runtime, propertyNames, len);
216     for (auto i = 0; i < len; i++) {
217         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
218         if (!key) {
219             LOGW("key is null. Ignoring!");
220             continue;
221         }
222         std::string keyStr = key->ToString(runtime);
223         shared_ptr<JsValue> val = valObject->GetProperty(runtime, key);
224         if (!val) {
225             LOGW("val is null. Ignoring!");
226             continue;
227         }
228         if (val->IsObject(runtime)) {
229             static const LinearMapNode<void (*)(
230                 const shared_ptr<JsRuntime>&, const shared_ptr<JsValue>&, ChartOptions&)>
231                 chartOptionsSeriesMap[] = {
232                     { "bottomPoint",
233                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
234                             ChartOptions& chartOptions) {
235                             PointInfo pointInfo;
236                             GetAttrOptionsSeriesPoint(runtime, valObject, pointInfo);
237                             chartOptions.SetBottomPoint(pointInfo);
238                         } },
239                     { "headPoint",
240                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
241                             ChartOptions& chartOptions) {
242                             PointInfo pointInfo;
243                             GetAttrOptionsSeriesPoint(runtime, valObject, pointInfo);
244                             chartOptions.SetHeadPoint(pointInfo);
245                         } },
246                     { "lineStyle",
247                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
248                             ChartOptions& chartOptions) {
249                             GetChartAttrOptionsSeriesLineStyle(runtime, valObject, chartOptions);
250                         } },
251                     { "loop",
252                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
253                             ChartOptions& chartOptions) {
254                             GetChartAttrOptionsSeriesLoop(runtime, valObject, chartOptions);
255                         } },
256 
257                     { "topPoint",
258                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
259                             ChartOptions& chartOptions) {
260                             PointInfo pointInfo;
261                             GetAttrOptionsSeriesPoint(runtime, valObject, pointInfo);
262                             chartOptions.SetTopPoint(pointInfo);
263                         } },
264                 };
265             auto iter = BinarySearchFindIndex(chartOptionsSeriesMap, ArraySize(chartOptionsSeriesMap), keyStr.c_str());
266             if (iter != -1) {
267                 chartOptionsSeriesMap[iter].value(runtime, val, chartOptions);
268             }
269         }
270     }
271 }
272 
ParseTextInfoAndSegmentInfo(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & val,const std::string & key,TextInfo & textInfo,SegmentInfo & segmentInfo)273 void ParseTextInfoAndSegmentInfo(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& val,
274     const std::string& key, TextInfo& textInfo, SegmentInfo& segmentInfo)
275 {
276     if (!runtime || !val) {
277         LOGE("runtime or val is null.");
278         return;
279     }
280     std::string valStr = val->ToString(runtime);
281     if (key == "description") {
282         textInfo.SetTextValue(valStr);
283     } else if (key == "textLocation") {
284         if (valStr == "top") {
285             textInfo.SetPlacement(Placement::TOP);
286         } else if (valStr == "bottom") {
287             textInfo.SetPlacement(Placement::BOTTOM);
288         } else if (valStr == "none") {
289             textInfo.SetPlacement(Placement::NONE);
290         }
291     } else if (key == "lineDash") {
292         std::vector<std::string> dash;
293         StringUtils::StringSplitter(valStr, ',', dash);
294         if (!dash.empty()) {
295             segmentInfo.SetLineType(dash[0] == "dashed" ? LineType::DASHED : LineType::SOLID);
296         }
297         if (dash.size() > 1) {
298             segmentInfo.SetSolidWidth(StringToDouble(dash[1]));
299         }
300         if (dash.size() > 2) {
301             segmentInfo.SetSpaceWidth(StringToDouble(dash[2]));
302         }
303     } else if (key == "lineColor") {
304         segmentInfo.SetSegmentColor(Color::FromString(valStr));
305     } else if (key == "textColor") {
306         textInfo.SetColor(Color::FromString(valStr));
307     } else {
308         LOGW("key is %{public}s. Ignoring!", key.c_str());
309     }
310 }
311 
ParseAttrDataStyle(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,double index,LineInfo & line)312 void ParseAttrDataStyle(
313     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, double index, LineInfo& line)
314 {
315     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
316         LOGE("none found attrs");
317         return;
318     }
319     PointInfo pointInfo;
320     TextInfo textInfo;
321     SegmentInfo segmentInfo;
322     shared_ptr<JsValue> propertyNames;
323     int32_t len = 0;
324     valObject->GetPropertyNames(runtime, propertyNames, len);
325     for (auto i = 0; i < len; i++) {
326         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
327         if (!key) {
328             LOGW("key is null. Ignoring!");
329             continue;
330         }
331         std::string keyStr = key->ToString(runtime);
332         shared_ptr<JsValue> val = valObject->GetProperty(runtime, key);
333         if (!val) {
334             LOGW("val value is null. Ignoring!");
335             continue;
336         }
337         if (val->IsString(runtime)) {
338             ParseTextInfoAndSegmentInfo(runtime, val, keyStr, textInfo, segmentInfo);
339         } else if (val->IsObject(runtime)) {
340             if (keyStr == "pointStyle") {
341                 GetAttrOptionsSeriesPoint(runtime, val, pointInfo);
342             }
343         } else if (val->IsNumber(runtime)) {
344             if (keyStr == "value") {
345                 pointInfo.SetX(index);
346                 pointInfo.SetY(StringToDouble(val->ToString(runtime)));
347             }
348         } else {
349             LOGW("key is %{public}s. Ignoring!", keyStr.c_str());
350         }
351         line.SetPointInfo(pointInfo);
352         line.SetTextInfo(textInfo);
353         line.SetSegmentInfo(segmentInfo);
354     }
355 }
356 
GetAttrDataSetData(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & dataArrayVal,MainChart & dataSet)357 void GetAttrDataSetData(
358     const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& dataArrayVal, MainChart& dataSet)
359 {
360     if (!runtime || !dataArrayVal) {
361         LOGE("runtime or dataArrayVal is null.");
362         return;
363     }
364     int32_t length = dataArrayVal->GetArrayLength(runtime);
365     std::vector<LineInfo> line;
366     for (int32_t j = 0; j < length; ++j) {
367         auto dataArrayItemVal = dataArrayVal->GetProperty(runtime, j);
368         LineInfo lineInfo;
369         PointInfo pointInfo;
370         if (dataArrayItemVal->IsArray(runtime)) { // Coordinates are two-dimensional arrays
371             int32_t dataArrayIterLen = dataArrayItemVal->GetArrayLength(runtime);
372             if (dataArrayIterLen != VALID_ARRAY_LENGTH) { // Check coordinates are not two-dimensional arrays
373                 LOGW("Attr Datasets data type unsupported!");
374                 continue;
375             }
376             auto xVal = dataArrayItemVal->GetProperty(runtime, ARRAY_X_VALUE);
377             pointInfo.SetX(StringToDouble(xVal->ToString(runtime)));
378 
379             auto yVal = dataArrayItemVal->GetProperty(runtime, ARRAY_Y_VALUE);
380             pointInfo.SetX(StringToDouble(yVal->ToString(runtime)));
381             lineInfo.SetPointInfo(pointInfo);
382         } else if (dataArrayItemVal->IsNumber(runtime)) { // Coordinates as a one-dimensional array
383             // When only the Y value is passed in, the X value is sequentially +1
384             pointInfo.SetX(static_cast<double>(j));
385             pointInfo.SetY(StringToDouble(dataArrayItemVal->ToString(runtime)));
386             lineInfo.SetPointInfo(pointInfo);
387         } else if (dataArrayItemVal->IsObject(runtime)) {
388             ParseAttrDataStyle(runtime, dataArrayItemVal, static_cast<double>(j), lineInfo);
389         } else {
390             LOGW("value of unsupported type. Ignoring!");
391             continue;
392         }
393         line.push_back(lineInfo);
394     }
395     dataSet.SetData(line);
396 }
397 
GetAttrDataset(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,MainChart & dataSet)398 void GetAttrDataset(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, MainChart& dataSet)
399 {
400     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
401         LOGE("none found attrs");
402         return;
403     }
404     shared_ptr<JsValue> propertyNames;
405     int32_t len = 0;
406     valObject->GetPropertyNames(runtime, propertyNames, len);
407     for (auto i = 0; i < len; i++) {
408         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
409         if (!key) {
410             LOGW("key is null. Ignoring!");
411             continue;
412         }
413         std::string keyStr = key->ToString(runtime);
414         shared_ptr<JsValue> val = valObject->GetProperty(runtime, key);
415         if (!val) {
416             LOGW("val is null. Ignoring!");
417             continue;
418         }
419         if (val->IsNumber(runtime) || val->IsBoolean(runtime) || val->IsString(runtime)) {
420             std::string valStr = val->ToString(runtime);
421             if (keyStr == "gradient") {
422                 dataSet.SetGradient(StringToBool(valStr));
423             } else if (keyStr == "strokeColor") {
424                 dataSet.SetStrokeColor(Color::FromString(valStr));
425             } else if (keyStr == "fillColor") {
426                 dataSet.SetFillColor(Color::FromString(valStr));
427             }
428         } else if (val->IsArray(runtime) && keyStr == "data") {
429             GetAttrDataSetData(runtime, val, dataSet);
430         }
431     }
432 }
433 
ParseAttrSegment(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,Segment & segment)434 void ParseAttrSegment(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, Segment& segment)
435 {
436     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
437         LOGE("none found attrs");
438         return;
439     }
440     shared_ptr<JsValue> propertyNames;
441     int32_t len = 0;
442     valObject->GetPropertyNames(runtime, propertyNames, len);
443     for (auto i = 0; i < len; i++) {
444         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
445         if (!key) {
446             LOGW("key is null. Ignoring!");
447             continue;
448         }
449         std::string keyStr = key->ToString(runtime);
450         shared_ptr<JsValue> val = valObject->GetProperty(runtime, key);
451         if (!val) {
452             LOGW("val is null. Ignoring!");
453             continue;
454         }
455         if (val->IsNumber(runtime) || val->IsString(runtime)) {
456             std::string valStr = val->ToString(runtime);
457             if (keyStr == "value") {
458                 segment.SetValue(StringToDouble(valStr));
459             } else if (keyStr == "startColor") {
460                 segment.SetStartColor(Color::FromString(valStr));
461                 segment.SetColorType(SegmentStyleType::USE_COLOR);
462             } else if (keyStr == "endColor") {
463                 segment.SetEndColor(Color::FromString(valStr));
464                 segment.SetColorType(SegmentStyleType::USE_COLOR);
465             } else if (keyStr == "name") {
466                 segment.SetSegmentName(valStr);
467             }
468         }
469     }
470 }
471 
472 } // namespace
473 
GetAttrOptionsObject(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject)474 void JsiChartBridge::GetAttrOptionsObject(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject)
475 {
476     if (!runtime || !valObject || !valObject->IsObject(runtime)) {
477         LOGE("none found attrs");
478         return;
479     }
480     shared_ptr<JsValue> propertyNames;
481     int32_t len = 0;
482     valObject->GetPropertyNames(runtime, propertyNames, len);
483     for (auto i = 0; i < len; i++) {
484         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
485         if (!key) {
486             LOGW("key is null. Ignoring!");
487             continue;
488         }
489         std::string keyStr = key->ToString(runtime);
490         shared_ptr<JsValue> val = valObject->GetProperty(runtime, key);
491         if (val->IsString(runtime) || val->IsNumber(runtime) || val->IsBoolean(runtime) || val->IsObject(runtime)) {
492             static const LinearMapNode<void (*)(
493                 const shared_ptr<JsRuntime>&, const shared_ptr<JsValue>&, ChartOptions&)>
494                 chartOptionsMap[] = {
495                     { "series",
496                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
497                             ChartOptions& chartOptions) {
498                             GetChartAttrOptionsSeries(runtime, valObject, chartOptions);
499                         } },
500                     { "xAxis",
501                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
502                             ChartOptions& chartOptions) {
503                             AxisOption xAxis;
504                             GetAttrOptionsAxis(runtime, valObject, xAxis);
505                             chartOptions.SetXAxis(xAxis);
506                         } },
507                     { "yAxis",
508                         [](const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject,
509                             ChartOptions& chartOptions) {
510                             AxisOption yAxis;
511                             GetAttrOptionsAxis(runtime, valObject, yAxis);
512                             chartOptions.SetYAxis(yAxis);
513                         } },
514                 };
515             auto iter = BinarySearchFindIndex(chartOptionsMap, ArraySize(chartOptionsMap), keyStr.c_str());
516             if (iter != -1) {
517                 chartOptionsMap[iter].value(runtime, val, chartOptions_);
518             }
519         }
520     }
521 }
522 
GetAttrDatasets(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valArray)523 void JsiChartBridge::GetAttrDatasets(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valArray)
524 {
525     int32_t len = valArray->GetArrayLength(runtime);
526     for (int32_t i = 0; i < len; ++i) {
527         shared_ptr<JsValue> itemVal = valArray->GetProperty(runtime, i);
528         if (itemVal->IsObject(runtime)) {
529             MainChart chart;
530             GetAttrDataset(runtime, itemVal, chart);
531             datasets_.push_back(chart);
532         }
533     }
534 }
535 
ParseAttrSegmentArray(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valArray)536 void JsiChartBridge::ParseAttrSegmentArray(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valArray)
537 {
538     int32_t len = valArray->GetArrayLength(runtime);
539     for (int32_t i = 0; i < len; ++i) {
540         shared_ptr<JsValue> itemVal = valArray->GetProperty(runtime, i);
541         if (itemVal->IsObject(runtime)) {
542             Segment segment;
543             ParseAttrSegment(runtime, itemVal, segment);
544             segments_.emplace_back(std::move(segment));
545         }
546     }
547 }
548 
ParseAttrSingleSegment(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject)549 void JsiChartBridge::ParseAttrSingleSegment(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject)
550 {
551     if (!valObject->IsObject(runtime)) {
552         LOGW("fail to parse segment, because it is not an object");
553         return;
554     }
555     segments_.clear();
556     Segment segment;
557     ParseAttrSegment(runtime, valObject, segment);
558     segments_.emplace_back(std::move(segment));
559 }
560 
561 } // namespace OHOS::Ace::Framework