1 /*
2 * Copyright (c) 2024 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 "bridge/cj_frontend/interfaces/cj_ffi/cj_data_panel_ffi.h"
17
18 #include <vector>
19
20 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/components/chart/chart_component.h"
23 #include "core/components_ng/pattern/data_panel/data_panel_model_ng.h"
24
25 using namespace OHOS::Ace;
26 using namespace OHOS::Ace::Framework;
27
28 namespace {
29 const std::vector<ChartType> DataPanel_TYPE = { ChartType::LINE, ChartType::RAINBOW};
30 constexpr unsigned int MAX_VALUES_LENGTH = 9;
31 constexpr double DEFAULT_STROKE_WIDTH = 24.0;
32 }
33
34 extern "C" {
FFICJCreateVectorNativeLinearGradient(int64_t size)35 VectorNativeLinearGradientHandle FFICJCreateVectorNativeLinearGradient(int64_t size)
36 {
37 LOGI("Create NativeLinearGradient Vector");
38 return new std::vector<NativeLinearGradient>(size);
39 }
40
FFICJVectorNativeLinearGradientSetElement(VectorNativeLinearGradientHandle vec,int64_t index,NativeLinearGradient linearGradient)41 void FFICJVectorNativeLinearGradientSetElement(
42 VectorNativeLinearGradientHandle vec, int64_t index, NativeLinearGradient linearGradient)
43 {
44 LOGI("NativeLinearGradient Vector Set Element");
45 auto actualVec = reinterpret_cast<std::vector<NativeLinearGradient>*>(vec);
46 (*actualVec)[index] = linearGradient;
47 LOGI("NativeLinearGradient Vector Set Element Success");
48 }
49
FFICJVectorNativeLinearGradientDelete(VectorNativeLinearGradientHandle vec)50 void FFICJVectorNativeLinearGradientDelete(VectorNativeLinearGradientHandle vec)
51 {
52 auto actualVec = reinterpret_cast<std::vector<NativeLinearGradient>*>(vec);
53 delete actualVec;
54 }
55
FfiOHOSAceFrameworkDataPanelCreate(VectorDoubleHandle values,double max,int32_t panelType)56 void FfiOHOSAceFrameworkDataPanelCreate(VectorDoubleHandle values, double max, int32_t panelType)
57 {
58 if (!Utils::CheckParamsValid(panelType, DataPanel_TYPE.size())) {
59 LOGE("invalid value for data_panel type");
60 return;
61 }
62 std::vector<double>* pValues = (std::vector<double>*)values;
63 std::vector<double> dateValues;
64 unsigned int length = pValues->size() > MAX_VALUES_LENGTH ? MAX_VALUES_LENGTH : pValues->size();
65 double dataSum = 0.0;
66 double maxValue = max;
67 for (unsigned int i = 0; i < length; ++i) {
68 auto value = pValues->at(i);
69 if (value <= 0.0) {
70 value = 0.0;
71 }
72 dataSum += value;
73 if ((dataSum >= maxValue) && maxValue > 0) {
74 value = maxValue - (dataSum - value);
75 if (value <= 0.0) {
76 break;
77 }
78 }
79 dateValues.push_back(value);
80 }
81 if (maxValue <= 0.0) {
82 maxValue = dataSum;
83 }
84
85 DataPanelModel::GetInstance()->Create(dateValues, maxValue, panelType);
86 }
87
FfiOHOSAceFrameworkDataPanelSetCloseEffect(bool isCloseEffect)88 void FfiOHOSAceFrameworkDataPanelSetCloseEffect(bool isCloseEffect)
89 {
90 DataPanelModel::GetInstance()->SetEffect(isCloseEffect);
91 }
92
FfiOHOSAceFrameworkDataPanelSetValueColors(VectorStringPtr vecContent)93 void FfiOHOSAceFrameworkDataPanelSetValueColors(VectorStringPtr vecContent)
94 {
95 auto nativeLinearGradientVec = *reinterpret_cast<std::vector<NativeLinearGradient>*>(vecContent);
96
97 std::vector<OHOS::Ace::NG::Gradient> valueColors;
98
99 for (size_t i = 0; i < nativeLinearGradientVec.size(); i++) {
100 OHOS::Ace::NG::Gradient gradient;
101 OHOS::Ace::NG::GradientColor gradientColorStart;
102
103 gradientColorStart.SetLinearColor(LinearColor(nativeLinearGradientVec[i].firstColor));
104 gradientColorStart.SetDimension(Dimension(0.0));
105 gradient.AddColor(gradientColorStart);
106 OHOS::Ace::NG::GradientColor gradientColorEnd;
107
108 gradientColorEnd.SetLinearColor(LinearColor(nativeLinearGradientVec[i].secondColor));
109 gradientColorEnd.SetDimension(Dimension(1.0));
110 gradient.AddColor(gradientColorEnd);
111 valueColors.emplace_back(gradient);
112 }
113
114 DataPanelModel::GetInstance()->SetValueColors(valueColors);
115 }
116
FfiOHOSAceFrameworkDataPanelSetTrackBackgroundColor(uint32_t color)117 void FfiOHOSAceFrameworkDataPanelSetTrackBackgroundColor(uint32_t color)
118 {
119 DataPanelModel::GetInstance()->SetTrackBackground(Color(color));
120 }
121
FfiOHOSAceFrameworkDataPanelSetStrokeWidth(double strokeWidth,int32_t widthUnit)122 void FfiOHOSAceFrameworkDataPanelSetStrokeWidth(double strokeWidth, int32_t widthUnit)
123 {
124 if (strokeWidth < 0.0) {
125 strokeWidth = DEFAULT_STROKE_WIDTH;
126 }
127 Dimension dimStrokeWidth(strokeWidth, static_cast<DimensionUnit>(widthUnit));
128 DataPanelModel::GetInstance()->SetStrokeWidth(dimStrokeWidth);
129 }
130
FfiOHOSAceFrameworkDataPanelSetTrackShadow(NativeDataPanelShadow nativeDataPanelShadow)131 void FfiOHOSAceFrameworkDataPanelSetTrackShadow(NativeDataPanelShadow nativeDataPanelShadow)
132 {
133 auto nativeLinearGradientVec = *reinterpret_cast<std::vector<NativeLinearGradient>*>(nativeDataPanelShadow.colors);
134
135 std::vector<OHOS::Ace::NG::Gradient> valueColors;
136 for (size_t i = 0; i < nativeLinearGradientVec.size(); i++) {
137 OHOS::Ace::NG::Gradient gradient;
138 OHOS::Ace::NG::GradientColor gradientColorStart;
139
140 gradientColorStart.SetLinearColor(LinearColor(nativeLinearGradientVec[i].firstColor));
141 gradientColorStart.SetDimension(Dimension(0.0));
142 gradient.AddColor(gradientColorStart);
143 OHOS::Ace::NG::GradientColor gradientColorEnd;
144
145 gradientColorEnd.SetLinearColor(LinearColor(nativeLinearGradientVec[i].secondColor));
146 gradientColorEnd.SetDimension(Dimension(1.0));
147 gradient.AddColor(gradientColorEnd);
148 valueColors.emplace_back(gradient);
149 }
150 OHOS::Ace::NG::DataPanelShadow shadow;
151 shadow.radius = nativeDataPanelShadow.radius;
152 shadow.offsetX = nativeDataPanelShadow.offsetX;
153 shadow.offsetY = nativeDataPanelShadow.offsetY;
154 shadow.colors = valueColors;
155 DataPanelModel::GetInstance()->SetShadowOption(shadow);
156 }
157 }
158