• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/declarative_frontend/jsview/models/data_panel_model_impl.h"
17 
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/data_panel/data_panel_component.h"
20 
21 namespace OHOS::Ace::Framework {
22 
23 constexpr size_t MAX_COUNT = 9;
Create(const std::vector<double> & values,double max,int32_t type)24 void DataPanelModelImpl::Create(const std::vector<double>& values, double max, int32_t type)
25 {
26     RefPtr<PercentageDataPanelComponent> component =
27         AceType::MakeRefPtr<PercentageDataPanelComponent>(ChartType::RAINBOW);
28     double valueSum = 0.0;
29     // values
30     size_t length = static_cast<size_t>(values.size());
31     for (size_t i = 0; i < length && i < MAX_COUNT; i++) {
32         auto value = values[i];
33         if (LessOrEqual(value, 0.0)) {
34             value = 0.0;
35         }
36         valueSum += value;
37         if (GreatOrEqual(valueSum, max) && max > 0) {
38             value = max - (valueSum - value);
39             if (NearEqual(value, 0.0)) {
40                 break;
41             }
42             Segment segment;
43             segment.SetValue(value);
44             segment.SetColorType(SegmentStyleType::NONE);
45             component->AppendSegment(segment);
46             break;
47         }
48         Segment segment;
49         segment.SetValue(value);
50         segment.SetColorType(SegmentStyleType::NONE);
51         component->AppendSegment(segment);
52     }
53     if (LessOrEqual(max, 0.0)) {
54         max = valueSum;
55     }
56     component->SetMaxValue(max);
57     if (type == 1) {
58         component->SetPanelType(ChartType::LINE);
59     } else if (type == 0) {
60         component->SetPanelType(ChartType::RAINBOW);
61     }
62     RefPtr<ThemeManager> dataPanelManager = AceType::MakeRefPtr<ThemeManagerImpl>();
63     component->InitalStyle(dataPanelManager);
64     ViewStackProcessor::GetInstance()->ClaimElementId(component);
65     ViewStackProcessor::GetInstance()->Push(component);
66 }
67 
SetEffect(bool isCloseEffect)68 void DataPanelModelImpl::SetEffect(bool isCloseEffect)
69 {
70     auto dataPanelComponent =
71         AceType::DynamicCast<DataPanelComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
72     CHECK_NULL_VOID(dataPanelComponent);
73     dataPanelComponent->SetEffects(!isCloseEffect);
74 }
75 
76 } // namespace OHOS::Ace::Framework