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