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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHART_CHART_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHART_CHART_COMPONENT_H 18 19 #include <vector> 20 21 #include "base/geometry/point.h" 22 #include "core/components/chart/chart_element.h" 23 #include "core/components/common/properties/color.h" 24 #include "core/components/common/properties/placement.h" 25 #include "core/pipeline/base/component.h" 26 #include "core/pipeline/base/render_node.h" 27 28 namespace OHOS::Ace { 29 30 enum class PointShape { 31 CIRCLE, 32 SQUARE, 33 TRIANGLE, 34 }; 35 36 enum class ChartType { 37 LINE, 38 BAR, 39 GAUGE, 40 PROGRESS, 41 RAINBOW, 42 LOADING, 43 }; 44 45 struct AxisOption { 46 double min = 0.0; 47 double max = 100.0; 48 int32_t tickNumber = 10; 49 bool display = false; 50 Color color = Color::GRAY; 51 std::string colorString; 52 }; 53 54 class TextInfo final { 55 public: SetTextValue(const std::string & value)56 void SetTextValue(const std::string& value) 57 { 58 value_ = value; 59 } 60 GetTextValue()61 const std::string& GetTextValue() const 62 { 63 return value_; 64 } 65 SetPlacement(Placement placement)66 void SetPlacement(Placement placement) 67 { 68 placement_ = placement; 69 } 70 GetPlacement()71 Placement GetPlacement() const 72 { 73 return placement_; 74 } 75 SetColor(const Color & color)76 void SetColor(const Color& color) 77 { 78 color_ = color; 79 } 80 GetColor()81 const Color& GetColor() const 82 { 83 return color_; 84 } 85 SetColorString(std::string color)86 void SetColorString(std::string color) 87 { 88 colorString_ = color; 89 } 90 GetColorString()91 std::string GetColorString() const 92 { 93 return colorString_; 94 } 95 96 private: 97 std::string value_ = ""; 98 Placement placement_ = Placement::BOTTOM; 99 Color color_ = Color::WHITE; 100 std::string colorString_; 101 }; 102 103 enum class LineType { 104 SOLID, 105 DASHED, 106 }; 107 108 class SegmentInfo final { 109 public: SetSegmentColor(const Color & color)110 void SetSegmentColor(const Color& color) 111 { 112 color_ = color; 113 } 114 GetSegmentColor()115 const Color& GetSegmentColor() const 116 { 117 return color_; 118 } 119 GetSolidWidth()120 double GetSolidWidth() const 121 { 122 return solidWidth_; 123 } 124 SetSolidWidth(double width)125 void SetSolidWidth(double width) 126 { 127 solidWidth_ = width; 128 } 129 GetSpaceWidth()130 double GetSpaceWidth() const 131 { 132 return spaceWidth_; 133 } 134 SetSpaceWidth(double width)135 void SetSpaceWidth(double width) 136 { 137 spaceWidth_ = width; 138 } 139 GetLineType()140 LineType GetLineType() const 141 { 142 return type_; 143 } 144 SetLineType(LineType type)145 void SetLineType(LineType type) 146 { 147 type_ = type; 148 } 149 150 bool operator==(const SegmentInfo& segmentInfo) const 151 { 152 return color_ == segmentInfo.color_ && NearEqual(solidWidth_, segmentInfo.solidWidth_) && 153 NearEqual(spaceWidth_, segmentInfo.spaceWidth_); 154 } 155 156 bool operator!=(const SegmentInfo& segmentInfo) const 157 { 158 return !operator==(segmentInfo); 159 } 160 GetColorString()161 std::string GetColorString() const 162 { 163 return colorString_; 164 } 165 SetColorString(const std::string color)166 void SetColorString(const std::string color) 167 { 168 colorString_ = color; 169 } 170 171 private: 172 Color color_ = Color::TRANSPARENT; 173 std::string colorString_; 174 double solidWidth_ = 5.0; 175 double spaceWidth_ = 5.0; 176 LineType type_ = LineType::SOLID; 177 }; 178 179 class PointInfo final { 180 public: 181 PointInfo() = default; PointInfo(const Point & point)182 explicit PointInfo(const Point& point) 183 { 184 coordinate_ = point; 185 } 186 ~PointInfo() = default; 187 GetPointShape()188 PointShape GetPointShape() const 189 { 190 return pointShape_; 191 } 192 SetPointShape(PointShape shape)193 void SetPointShape(PointShape shape) 194 { 195 pointShape_ = shape; 196 } 197 GetPointSize()198 const Dimension& GetPointSize() const 199 { 200 return pointSize_; 201 } 202 SetPointSize(const Dimension & size)203 void SetPointSize(const Dimension& size) 204 { 205 pointSize_ = size; 206 } 207 GetStrokeColor()208 const Color& GetStrokeColor() const 209 { 210 return strokeColor_; 211 } 212 SetStrokeColor(const Color & color)213 void SetStrokeColor(const Color& color) 214 { 215 strokeColor_ = color; 216 } 217 GetFillColor()218 const Color& GetFillColor() const 219 { 220 return fillColor_; 221 } 222 SetFillColor(const Color & color)223 void SetFillColor(const Color& color) 224 { 225 fillColor_ = color; 226 } 227 GetDisplay()228 bool GetDisplay() const 229 { 230 return display_; 231 } 232 SetDisplay(bool display)233 void SetDisplay(bool display) 234 { 235 display_ = display; 236 } 237 GetX()238 double GetX() const 239 { 240 return coordinate_.GetX(); 241 } 242 GetY()243 double GetY() const 244 { 245 return coordinate_.GetY(); 246 } 247 SetX(double x)248 void SetX(double x) 249 { 250 coordinate_.SetX(x); 251 } 252 SetY(double y)253 void SetY(double y) 254 { 255 coordinate_.SetY(y); 256 } 257 SetPointStrokeWidth(const Dimension & dimension)258 void SetPointStrokeWidth(const Dimension& dimension) 259 { 260 pointStrokeWidth_ = dimension; 261 } 262 GetPointStrokeWidth()263 const Dimension& GetPointStrokeWidth() const 264 { 265 return pointStrokeWidth_; 266 } 267 SetStrokeColorString(const std::string colorString)268 void SetStrokeColorString(const std::string colorString) 269 { 270 strokeColorString_ = colorString; 271 } 272 GetStrokeColorString()273 std::string GetStrokeColorString() const 274 { 275 return strokeColorString_; 276 } 277 SetFillColorString(const std::string colorString)278 void SetFillColorString(const std::string colorString) 279 { 280 fillColorString_ = colorString; 281 } 282 GetFillColorString()283 std::string GetFillColorString() const 284 { 285 return fillColorString_; 286 } 287 288 private: 289 PointShape pointShape_ = PointShape::CIRCLE; 290 Dimension pointStrokeWidth_ = Dimension(1.0, DimensionUnit::PX); 291 Dimension pointSize_ = Dimension(5.0, DimensionUnit::PX); 292 Color strokeColor_ = Color::FromString("#ff0000"); 293 std::string strokeColorString_; 294 Color fillColor_ = Color::FromString("#ff0000"); 295 std::string fillColorString_; 296 Point coordinate_; 297 bool display_ = false; 298 }; 299 300 class LineInfo final { 301 public: 302 LineInfo() = default; 303 LineInfo(const TextInfo & textInfo)304 explicit LineInfo(const TextInfo& textInfo) 305 { 306 textInfo_ = textInfo; 307 } 308 LineInfo(const PointInfo & pointInfo)309 explicit LineInfo(const PointInfo& pointInfo) 310 { 311 pointInfo_ = pointInfo; 312 } 313 LineInfo(const SegmentInfo & segmentInfo)314 explicit LineInfo(const SegmentInfo& segmentInfo) 315 { 316 segmentInfo_ = segmentInfo; 317 } 318 319 ~LineInfo() = default; 320 SetTextInfo(const TextInfo & textInfo)321 void SetTextInfo(const TextInfo& textInfo) 322 { 323 textInfo_ = textInfo; 324 } 325 GetTextInfo()326 TextInfo GetTextInfo() const 327 { 328 return textInfo_; 329 } 330 SetPointInfo(const PointInfo & pointInfo)331 void SetPointInfo(const PointInfo& pointInfo) 332 { 333 pointInfo_ = pointInfo; 334 } 335 GetPointInfo()336 PointInfo GetPointInfo() const 337 { 338 return pointInfo_; 339 } 340 SetSegmentInfo(const SegmentInfo & segmentInfo)341 void SetSegmentInfo(const SegmentInfo& segmentInfo) 342 { 343 segmentInfo_ = segmentInfo; 344 } 345 GetSegmentInfo()346 SegmentInfo GetSegmentInfo() const 347 { 348 return segmentInfo_; 349 } 350 351 private: 352 TextInfo textInfo_; 353 PointInfo pointInfo_; 354 SegmentInfo segmentInfo_; 355 }; 356 357 class MainChart final { 358 public: 359 MainChart() = default; 360 ~MainChart() = default; 361 362 // add data into an exist dataset AppendData(const LineInfo & lineInfo)363 void AppendData(const LineInfo& lineInfo) 364 { 365 dataSets_.emplace_back(lineInfo); 366 } 367 368 // clear data in data set ClearData()369 void ClearData() 370 { 371 dataSets_.clear(); 372 } 373 374 // create and add a new data set SetData(const std::vector<LineInfo> & lineInfo)375 void SetData(const std::vector<LineInfo>& lineInfo) 376 { 377 dataSets_ = lineInfo; 378 } 379 ReplaceData(uint32_t index,const LineInfo & lineInfo)380 bool ReplaceData(uint32_t index, const LineInfo& lineInfo) 381 { 382 if (index >= dataSets_.size()) { 383 return false; 384 } 385 dataSets_[index] = lineInfo; 386 return true; 387 } 388 GetData()389 std::vector<LineInfo> GetData() const 390 { 391 return dataSets_; 392 } 393 GetFillColor()394 const Color& GetFillColor() const 395 { 396 return fillColor_; 397 } 398 SetFillColor(const Color & color)399 void SetFillColor(const Color& color) 400 { 401 fillColor_ = color; 402 } 403 GetStrokeColor()404 const Color& GetStrokeColor() const 405 { 406 return strokeColor_; 407 } 408 SetStrokeColor(const Color & color)409 void SetStrokeColor(const Color& color) 410 { 411 strokeColor_ = color; 412 } 413 GetGradient()414 bool GetGradient() const 415 { 416 return gradient_; 417 } 418 SetGradient(bool gradient)419 void SetGradient(bool gradient) 420 { 421 gradient_ = gradient; 422 } 423 SetHeadPoint(const PointInfo & point)424 void SetHeadPoint(const PointInfo& point) 425 { 426 headPoint_ = point; 427 } 428 GetHeadPoint()429 const PointInfo& GetHeadPoint() const 430 { 431 return headPoint_; 432 } 433 SetTopPoint(const PointInfo & point)434 void SetTopPoint(const PointInfo& point) 435 { 436 topPoint_ = point; 437 } 438 GetTopPoint()439 const PointInfo& GetTopPoint() const 440 { 441 return topPoint_; 442 } 443 SetBottomPoint(const PointInfo & point)444 void SetBottomPoint(const PointInfo& point) 445 { 446 bottomPoint_ = point; 447 } 448 GetBottomPoint()449 const PointInfo& GetBottomPoint() const 450 { 451 return bottomPoint_; 452 } 453 GetSmoothFlag()454 bool GetSmoothFlag() const 455 { 456 return smoothFlag_; 457 } 458 SetSmoothFlag(bool flag)459 void SetSmoothFlag(bool flag) 460 { 461 smoothFlag_ = flag; 462 } 463 GetLineWidth()464 double GetLineWidth() const 465 { 466 return lineWidth_; 467 } 468 SetLineWidth(double lineWidth)469 void SetLineWidth(double lineWidth) 470 { 471 lineWidth_ = lineWidth; 472 } 473 SetErasePointNumber(int32_t number)474 void SetErasePointNumber(int32_t number) 475 { 476 erasePointNumber_ = number; 477 } 478 GetErasePointNumber()479 int32_t GetErasePointNumber() const 480 { 481 return erasePointNumber_; 482 } 483 SetLineGradient(bool flag)484 void SetLineGradient(bool flag) 485 { 486 lineGradient_ = flag; 487 } 488 GetLineGradient()489 bool GetLineGradient() const 490 { 491 return lineGradient_; 492 } 493 SetWholeLineGradient(bool flag)494 void SetWholeLineGradient(bool flag) 495 { 496 wholeLineGradient_ = flag; 497 } 498 GetWholeLineGradient()499 bool GetWholeLineGradient() const 500 { 501 return wholeLineGradient_; 502 } 503 GetTargetColor()504 const Color& GetTargetColor() const 505 { 506 return targetColor_; 507 } 508 SetTargetColor(const Color & targetColor)509 void SetTargetColor(const Color& targetColor) 510 { 511 targetColor_ = targetColor; 512 } 513 SetHeadPointIndex(int32_t index)514 void SetHeadPointIndex(int32_t index) 515 { 516 headPointIndex_ = index; 517 } 518 GetHeadPointIndex()519 int32_t GetHeadPointIndex() const 520 { 521 return headPointIndex_; 522 } 523 GetTextColor()524 const Color& GetTextColor() const 525 { 526 return textColor_; 527 } 528 SetTextColor(const Color & textColor)529 void SetTextColor(const Color& textColor) 530 { 531 textColor_ = textColor; 532 } 533 GetTextSize()534 double GetTextSize() const 535 { 536 return textSize_; 537 } 538 SetTextSize(double textSize)539 void SetTextSize(double textSize) 540 { 541 textSize_ = textSize; 542 } 543 GetFontFamily()544 std::vector<std::string> GetFontFamily() const 545 { 546 return fontFamily_; 547 } 548 SetFontFamily(const std::vector<std::string> & fonts)549 void SetFontFamily(const std::vector<std::string>& fonts) 550 { 551 fontFamily_ = fonts; 552 } 553 554 private: 555 // data set name, vector of data 556 std::vector<LineInfo> dataSets_; 557 double lineWidth_ = 1.0; 558 bool smoothFlag_ = false; 559 bool gradient_ = false; 560 PointInfo headPoint_; 561 PointInfo topPoint_; 562 PointInfo bottomPoint_; 563 Color fillColor_ = Color::FromString("#ff6384"); 564 Color strokeColor_ = Color::FromString("#ff6384"); 565 int32_t erasePointNumber_ = 0; 566 int32_t headPointIndex_ = -1; 567 bool lineGradient_ = false; 568 bool wholeLineGradient_ = false; 569 570 Color textColor_; 571 double textSize_ = 18.0; 572 std::vector<std::string> fontFamily_; 573 Color targetColor_ = Color::TRANSPARENT; 574 }; 575 576 class ChartComponent : public RenderComponent { 577 DECLARE_ACE_TYPE(ChartComponent, RenderComponent); 578 579 public: 580 ChartComponent() = default; ChartComponent(ChartType type)581 explicit ChartComponent(ChartType type) 582 { 583 type_ = type; 584 } 585 ~ChartComponent() override = default; 586 587 RefPtr<RenderNode> CreateRenderNode() override; 588 GetHorizontalOption()589 const AxisOption& GetHorizontalOption() const 590 { 591 return horizontal_; 592 } 593 SetHorizontalOption(const AxisOption & xOption)594 void SetHorizontalOption(const AxisOption& xOption) 595 { 596 horizontal_ = xOption; 597 } 598 GetVerticalOption()599 const AxisOption& GetVerticalOption() 600 { 601 return vertical_; 602 } 603 SetVerticalOption(const AxisOption & yOption)604 void SetVerticalOption(const AxisOption& yOption) 605 { 606 vertical_ = yOption; 607 } 608 GetMainCharts()609 std::vector<MainChart> GetMainCharts() const 610 { 611 return mainCharts_; 612 } 613 SetMainCharts(const std::vector<MainChart> & mainCharts)614 void SetMainCharts(const std::vector<MainChart>& mainCharts) 615 { 616 mainCharts_ = mainCharts; 617 } 618 GetChartType()619 ChartType GetChartType() const 620 { 621 return type_; 622 } 623 CreateElement()624 RefPtr<Element> CreateElement() override 625 { 626 return AceType::MakeRefPtr<ChartElement>(); 627 } 628 629 private: 630 ChartType type_ = ChartType::LINE; 631 AxisOption horizontal_; 632 AxisOption vertical_; 633 std::vector<MainChart> mainCharts_; 634 }; 635 636 } // namespace OHOS::Ace 637 638 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHART_CHART_COMPONENT_H 639