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