1 /*
2  * Copyright (c) 2021-2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHART_ROSEN_RENDER_CHART_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHART_ROSEN_RENDER_CHART_H
18 
19 #ifndef USE_ROSEN_DRAWING
20 #include "include/core/SkPaint.h"
21 #include "include/core/SkPath.h"
22 #else
23 #include "core/components_ng/render/drawing.h"
24 #endif
25 
26 #include "base/geometry/offset.h"
27 #include "base/geometry/rect.h"
28 #include "core/components/chart/render_chart.h"
29 
30 namespace OHOS::Ace {
31 
32 class RosenRenderChart : public RenderChart {
33     DECLARE_ACE_TYPE(RosenRenderChart, RenderChart);
34 
35 public:
36     RosenRenderChart() = default;
37 
38     ~RosenRenderChart() override = default;
39 
40 private:
41     void Update(const RefPtr<Component>& component) override;
42 
43     void Paint(RenderContext& context, const Offset& offset) override;
44 
45     void PaintDatas(RenderContext& context, const Rect& paintRect);
46 
47     void PaintHorizontalAxis(RenderContext& context, const Rect& paintRect);
48 
49     void PaintVerticalAxis(RenderContext& context, const Offset& offset, const Rect& paintRect);
50 
51     Offset ConvertDataToPosition(const Rect& paintRegion, const PointInfo& point);
52 #ifndef USE_ROSEN_DRAWING
53     void SetEdgeStyle(const PointInfo& point, SkPaint& paint) const;
54 
55     void PaintStylePoints(SkCanvas* canvas, const Rect& paintRegion, const MainChart& chartData);
56 
57     void PaintPoint(SkCanvas* canvas, const Offset& offset, SkPaint paint, const PointInfo& point);
58 
59     void PaintText(SkCanvas* canvas, const Rect& paintRegion, const MainChart& chartData);
60 
61     void PaintLinearGraph(SkCanvas* canvas, const Rect& paintRect);
62 
63     void PaintLineEdge(
64         SkCanvas* canvas, SkPath& path, const SegmentInfo segmentInfo, double thickness, bool drawGradient = false);
65 
66     void UpdateLineGradientPoint(const std::vector<LineInfo>& pointInfo, const MainChart& line, const Rect& paintRect);
67 
68     void PaintLineGradient(
69         SkCanvas* canvas, SkPath& path, const Rect& paintRect, Color fillColor, const PointInfo& peekPoint);
70 
71     int32_t PaintLine(
72         uint32_t index, const std::vector<LineInfo>& line, SkPath& path, const MainChart& data, const Rect& paintRect);
73 
74     void AddCubicPath(
75         SkPath& path, const Rect& paintRect, const std::vector<LineInfo>& line, uint32_t index, bool isEnd);
76 
77     sk_sp<SkShader> CreateFillGradientShader(const Rect& paintRect, const Color& fillColor, double top);
78 
79     void PaintBar(SkCanvas* canvas, SkPaint& paint, const std::vector<LineInfo>& barGroupData, const Rect& paintRect,
80         int32_t barGroupNum, int32_t barsAreaNum, int32_t barGroupIndex);
81 #else
82     void SetEdgeStyle(const PointInfo& point, RSPen& pen) const;
83 
84     void PaintStylePoints(
85         RSCanvas* canvas, const Rect& paintRegion, const MainChart& chartData);
86 
87     void PaintPoint(RSCanvas* canvas, const Offset& offset, RSPen pen,
88         RSBrush brush, const PointInfo& point);
89 
90     void PaintText(RSCanvas* canvas, const Rect& paintRegion, const MainChart& chartData);
91 
92     void PaintLinearGraph(RSCanvas* canvas, const Rect& paintRect);
93 
94     void PaintLineEdge(RSCanvas* canvas, RSPath& path, const SegmentInfo segmentInfo,
95         double thickness, bool drawGradient = false);
96 
97     void UpdateLineGradientPoint(const std::vector<LineInfo>& pointInfo, const MainChart& line, const Rect& paintRect);
98 
99     void PaintLineGradient(RSCanvas* canvas, RSPath& path, const Rect& paintRect,
100         Color fillColor, const PointInfo& peekPoint);
101 
102     int32_t PaintLine(uint32_t index, const std::vector<LineInfo>& line, RSPath& path,
103         const MainChart& data, const Rect& paintRect);
104 
105     void AddCubicPath(RSPath& path, const Rect& paintRect, const std::vector<LineInfo>& line,
106         uint32_t index, bool isEnd);
107 
108     std::shared_ptr<RSShaderEffect> CreateFillGradientShader(
109         const Rect& paintRect, const Color& fillColor, double top);
110 
111     void PaintBar(RSCanvas* canvas, RSPen& pen, RSBrush brush,
112         const std::vector<LineInfo>& barGroupData, const Rect& paintRect, int32_t barGroupNum, int32_t barsAreaNum,
113         int32_t barGroupIndex);
114 #endif
115 
116     Rect GetBarsAreaPaintRect(const Rect& paintRect, int32_t barsAreaIndex);
117 
118     Rect GetBarAreaPaintRect(
119         const Rect& barsAreaPaintRect, int32_t barGroupIndex, int32_t barGroupNumber, double barInterval);
120 
121     Offset CalculateControlA(const Offset& prev, const Offset& cur, const Offset& next);
122 
123     Offset CalculateControlB(const Offset& prev, const Offset& cur, const Offset& next);
124 
125     double tickOffset_ = 0.0;
126     double tickHorizontalOffset_ = 0.0;
127 
128     bool drawLine_ = false;
129     uint32_t startIndex_ = 0;
130 
131     Offset startGradientPoint_;
132     Offset endGradientPoint_;
133     size_t startGradientIndex_ = 0;
134     size_t endGradientIndex_ = 0;
135     bool gradientOfLine_ = false;
136     Offset startOffset_;
137     double paintWidth_ = 0;
138     Color targetColor_;
139     bool wholeLineGradient_ = false;
140 };
141 
142 } // namespace OHOS::Ace
143 
144 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHART_ROSEN_RENDER_CHART_H
145