ui_chart.h 24.2 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @addtogroup UI_Components
 * @{
 *
 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
 *
 * @since 1.0
 * @version 1.0
 */

/**
 * @file ui_chart.h
 *
 * @brief Defines the attributes of the chart component and provides functions for adding and deleting
 *        data sets to display a chart.
 *
 * @since 1.0
 * @version 1.0
 */

#ifndef GRAPHIC_LITE_UI_CHART_H
#define GRAPHIC_LITE_UI_CHART_H

#include "components/ui_axis.h"
#include "components/ui_view_group.h"
Z
zhangguyuan 已提交
41
#include "gfx_utils/list.h"
M
mamingshuai 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

namespace OHOS {
class UIChart;
/**
 * @brief Defines a data set and provides functions such as adding and deleting data points.
 *
 * @since 1.0
 * @version 1.0
 */
class UIChartDataSerial : public HeapBase {
public:
    /**
     * @brief A constructor used to create a <b>UIChartDataSerial</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UIChartDataSerial();

    /**
     * @brief A destructor used to delete the <b>UIChartDataSerial</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~UIChartDataSerial()
    {
        if (pointArray_ != nullptr) {
            UIFree(pointArray_);
            pointArray_ = nullptr;
        }
    }

    /**
     * @brief Sets the maximum number of data points that can be stored in a data set.
     *
     * This function must be called before data is added, deleted, or modified. Otherwise, data operations will fail.
     *
     * @param maxCount Indicates the number of data points. The default value is <b>0</b>.
     *
     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool SetMaxDataCount(uint16_t maxCount);

    /**
     * @brief Modifies the value of a data point in the data set.
     *
     * @param index Indicates the index of the data point to modify.
     * @param point Indicates the new value of the data point.
     *
     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool ModifyPoint(uint16_t index, const Point& point);

    /**
     * @brief Obtains the coordinates in the chart for a data point in the data set.
     *
     * @param index Indicates the index of the data point to obtain.
     * @param point Indicates the obtained coordinates. If the data set is not added to the chart,
     *              the original value of the data point is printed.
     *
     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool GetPoint(uint16_t index, Point& point);

    /**
     * @brief Adds data points.
     *
     * The new data points are appended to the last added data. \n
     * No more data points can be added if the maximum number is reached \n
     *
     * @param data  Indicates the pointer to the start address of the data point.
     * @param count Indicates the number of data points to add.
     * @return Returns <b>true</b> if the data points are added successfully; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool AddPoints(const Point* data, uint16_t count);

    /**
     * @brief Clears all data points.
     *
     * @since 1.0
     * @version 1.0
     */
    void ClearData();

    /**
     * @brief Obtains the number of data points available in the data set.
     *
     * @return Returns the number of data points.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetDataCount() const
    {
        return dataCount_;
    }

    /**
     * @brief Sets whether to smooth a polyline.
     *
     * This function applies only to line charts. After the smoothing, some data is discarded.
     * Therefore, the polyline does not pass through all data points. \n
     * If <b>smooth</b> is set to <b>true</b>, the filling color, top point, and bottom point of a line chart have
     * deviations. Therefore, you are advised not to use these functions at the same time. \n
     *
     * @param smooth Specifies whether to smooth a polyline. Value <b>true</b> means to smooth a polyline, and value
     *               <b>false</b> means not to smooth a polyline. The default value is <b>false</b>.
     * @since 1.0
     * @version 1.0
     */
    void EnableSmooth(bool smooth)
    {
        smooth_ = smooth;
    }

    /**
     * @brief Checks whether smoothing is performed on a polyline.
     *
     * @return Returns <b>true</b> if smooth processing is performed on the polyline; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool IsSmooth() const
    {
        return smooth_;
    }

    /**
     * @brief Enables the fill color of a line chart.
     *
     * This function applies only to line charts. By default, the area between the polyline and the x-axis is filled.
     * You can use {@link SetGradientBottom} to modify the filled region. \n
     *
     * @param enable Specifies whether to enable the fill color. Value <b>true</b> means to enable the fill color,
     *               and value <b>false</b> means to disable the fill color. The default value is <b>false</b>.
     * @since 1.0
     * @version 1.0
     */
    void EnableGradient(bool enable)
    {
        enableGradient_ = enable;
    }

    /**
     * @brief Checks whether a polyline has a fill color.
     *
     * @return Returns <b>true</b> if there is a fill color; returns <b>false</b> otherwise.
     * @since 1.0
     * @version 1.0
     */
    bool IsGradient() const
    {
        return enableGradient_;
    }

    /**
     * @brief Obtains the index of the top point in the data set.
     *
     * @return Returns the index of the top point. If there are multiple top points, the first one is returned.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetPeakIndex() const
    {
        return peakPointIndex_;
    }

    /**
     * @brief Obtains the index of the frontmost point (the latest added or modified data point in a data set).
     *
     * @return Returns the index of the frontmost point.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetLatestIndex() const
    {
        return latestIndex_;
    }

    /**
     * @brief Obtains the index of the bottom point in a data set.
     *
     * @return Returns the index of the bottom point. If there are multiple bottom points, the first one is returned.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetValleyIndex() const
    {
        return valleyPointIndex_;
    }

    /**
     * @brief Obtains the Y value of the top point in a data set.
     *
     * The Y value is the data added by users, not the pixel coordinate.
     *
     * @return Returns the Y value.
     * @since 1.0
     * @version 1.0
     */
    int16_t GetPeakData() const
    {
        return peakData_;
    }

    /**
     * @brief Obtains the Y value of the bottom point in a data set.
     *
     * The Y value is the data added by users, not the pixel coordinate.
     *
     * @return Returns the Y value.
     * @since 1.0
     * @version 1.0
     */
    int16_t GetValleyData() const
    {
        return valleyData_;
    }

    void SetLastPointIndex(uint16_t value)
    {
        lastPointIndex_ = value;
    }

    uint16_t GetLastPointIndex() const
    {
        return lastPointIndex_;
    }

    /**
     * @brief Obtains the polyline color of the data set in a line chart.
     *
     * @return Returns the polyline color of the data set.
     * @see SetLineColor
     * @since 1.0
     * @version 1.0
     */
    ColorType GetLineColor() const
    {
        return serialColor_;
    }

    /**
     * @brief Obtains the fill color of the data set.
     *
     * @return Returns the fill color.
     * @see SetFillColor
     * @since 1.0
     * @version 1.0
     */
    ColorType GetFillColor() const
    {
        return fillColor_;
    }

    /**
     * @brief Sets the fill color of the data set.
     *
     * For a line chart, <b>color</b> refers to the fill color between the line and the x-axis.
     * For a bar chart, <b>color</b> refers to the color of the bars.
     *
     * @param color Indicates the fill color to set.
     * @see GetFillColor
     * @since 1.0
     * @version 1.0
     */
    void SetFillColor(const ColorType& color)
    {
        fillColor_ = color;
    }

    /**
     * @brief Sets the polyline color of the data set in the line chart.
     *
     * This function applies only to line charts.
     *
     * @param color Indicates the polyline color to set.
     * @see GetLineColor
     * @since 1.0
     * @version 1.0
     */
    void SetLineColor(const ColorType& color)
    {
        serialColor_ = color;
    }

    void BindToChart(UIChart* chart)
    {
        chart_ = chart;
    }

    /**
     * @brief Hides some points in the data set.
     *
     * This function applies only to line charts. After the points are hidden, the line connected by the points
     * is not displayed. \n
     * The top and bottom points may appear in the hidden region. If this method is enabled,
     * you are not advised to enable the display of the top and bottom points.
     *
     * @param index Indicates the point from which the hide starts.
     * @param count Indicates the number of points to hide.
     * @since 1.0
     * @version 1.0
     */
    void HidePoint(uint16_t index, uint16_t count);

    /**
     * @brief Obtains the index from which the data set starts to hide.
     *
     * @return Returns the index.
     * @see HidePoint
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetHideIndex() const
    {
        return hideIndex_;
    }

    /**
     * @brief Obtains the number of hidden points in the data set.
     *
     * @return Returns the number of hidden points.
     * @see HidePoint
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetHideCount() const
    {
        return hideCount_;
    }

    /**
     * @brief Defines the style for the top, bottom, and frontmost points in a line chart.
     */
    struct PointStyle : public HeapBase {
        /** Fill color */
        ColorType fillColor;
        /** Border color */
        ColorType strokeColor;
        /** Inner radius */
        uint16_t radius;
        /** Border width, which extends outwards from the inner radius */
        uint16_t strokeWidth;
    };

    /**
     * @brief Sets the style of the frontmost point on a polyline.
     *
     * @param style Indicates the style to set. For details, see {@link PointStyle}.
     * @since 1.0
     * @version 1.0
     */
    void SetHeadPointStyle(const PointStyle& style)
    {
        headPointStyle_ = style;
    }

    /**
     * @brief Sets the style of the top point of a polyline.
     *
     * @param style Indicates the style to set. For details, see {@link PointStyle}.
     * @since 1.0
     * @version 1.0
     */
    void SetTopPointStyle(const PointStyle& style)
    {
        topPointStyle_ = style;
    }

    /**
     * @brief Sets the style of the bottom point of a polyline.
     *
     * @param style Indicates the style to set. For details, see {@link PointStyle}.
     * @since 1.0
     * @version 1.0
     */
    void SetBottomPointStyle(const PointStyle& style)
    {
        bottomPointStyle_ = style;
    }

    /**
     * @brief Obtains the style of the frontmost point on a polyline.
     *
     * @return Returns the style of the point. For details, see {@link PointStyle}.
     * @since 1.0
     * @version 1.0
     */
    const PointStyle& GetHeadPointStyle() const
    {
        return headPointStyle_;
    }

    /**
     * @brief Obtains the style of the top point of a polyline.
     *
     * @return Returns the style of the point. For details, see {@link PointStyle}.
     * @since 1.0
     * @version 1.0
     */
    const PointStyle& GetTopPointStyle() const
    {
        return topPointStyle_;
    }

    /**
     * @brief Obtains the style of the bottom point of a polyline.
     *
     * @return Returns the style of the point. For details, see {@link PointStyle}.
     * @since 1.0
     * @version 1.0
     */
    const PointStyle& GetBottomPointStyle() const
    {
        return bottomPointStyle_;
    }

    /**
     * @brief Enables the feature of drawing the frontmost point on a polyline.
     *
     * @param enable Specifies whether to draw the frontmost point. Value <b>true</b> means to draw the frontmost
     *               point, and value <b>false</b> means not to draw the frontmost point.
     * @since 1.0
     * @version 1.0
     */
    void EnableHeadPoint(bool enable)
    {
        enableHeadPoint_ = enable;
    }

    /**
     * @brief Enables the feature of drawing the top point of a polyline. If there are multiple top points,
     *        only the first one is drawn.
     *
     * @param enable Specifies whether to draw the top point. Value <b>true</b> means to draw the top point,
     *               and value <b>false</b> means not to draw the top point.
     * @since 1.0
     * @version 1.0
     */
    void EnableTopPoint(bool enable)
    {
        enableTopPoint_ = enable;
    }

    /**
     * @brief Enables the feature of drawing the bottom point of a polyline. If there are multiple bottom points,
     *        only the first one is drawn.
     *
     * @param enable Specifies whether to draw the bottom point. Value <b>true</b> means to draw the bottom point,
     *               and value <b>false</b> means not to draw the bottom point.
     * @since 1.0
     * @version 1.0
     */
    void EnableBottomPoint(bool enable)
    {
        enableBottomPoint_ = enable;
    }

N
niulihua 已提交
509
    void DrawPoint(BufferInfo& gfxDstBuffer, const Rect& mask);
M
mamingshuai 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

    void Refresh();

protected:
    uint16_t maxCount_;
    Point* pointArray_;

private:
    constexpr static uint16_t DEFAULT_POINT_RADIUS = 5;
    constexpr static uint16_t MAX_POINTS_COUNT = 512;

    ColorType serialColor_;
    ColorType fillColor_;
    uint16_t dataCount_;
    uint16_t peakPointIndex_;
    int16_t peakData_;
    int16_t valleyData_;
    uint16_t valleyPointIndex_;
    uint16_t lastPointIndex_;
    uint16_t latestIndex_;
    uint16_t hideIndex_;
    uint16_t hideCount_;
    bool smooth_ : 1;
    bool enableGradient_ : 1;
    bool enableHeadPoint_ : 1;
    bool enableTopPoint_ : 1;
    bool enableBottomPoint_ : 1;
    PointStyle headPointStyle_;
    PointStyle topPointStyle_;
    PointStyle bottomPointStyle_;
    UIChart* chart_;
    Rect invalidateRect_;
    void RefreshInvalidateRect(uint16_t startIndex, uint16_t endIndex);
    void RefreshInvalidateRect(uint16_t pointIndex, const PointStyle& style);
    bool UpdatePeakAndValley(uint16_t startPos, uint16_t endPos);
N
niulihua 已提交
545
    void DoDrawPoint(BufferInfo& gfxDstBuffer, const Point& point, const PointStyle& style, const Rect& mask);
M
mamingshuai 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
};

/**
 * @brief Defines the chart class and provides functions such as adding and deleting data sets to display a chart.
 *
 * @since 1.0
 * @version 1.0
 */
class UIChart : public UIViewGroup {
public:
    /**
     * @brief A constructor used to create a <b>UIChart</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UIChart() : enableReverse_(false), needRefresh_(false), mixData_(nullptr)
    {
        Add(&xAxis_);
        Add(&yAxis_);
        SetStyle(STYLE_LINE_WIDTH, 1);
        SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
    }

    /**
     * @brief A destructor used to delete the <b>UIChart</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~UIChart();

    /**
     * @brief Obtains the view type.
     *
     * @return Returns the view type. For details, see {@link UIViewType}.
     * @since 1.0
     * @version 1.0
     */
    UIViewType GetViewType() const override
    {
        return UI_CHART;
    }

    /**
     * @brief Sets the height for this component.
     *
     * @param height Indicates the height to set.
     * @since 1.0
     * @version 1.0
     */
    void SetHeight(int16_t height) override;

    /**
     * @brief Sets the width for this component.
     *
     * @param width Indicates the width to set.
     * @since 1.0
     * @version 1.0
     */
    void SetWidth(int16_t width) override;

    bool OnPreDraw(Rect& invalidatedArea) const override
    {
        return false;
    }

N
niulihua 已提交
613
    void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
M
mamingshuai 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

    /**
     * @brief Adds a data set.
     *
     * @param dataSerial Indicates the pointer to the data set class. For details, see {@link UIChartDataSerial}.
     * @return Returns <b>true</b> if the data set is added successfully; returns <b>false</b> otherwise.
     * @see DeleteDataSerial
     * @since 1.0
     * @version 1.0
     */
    virtual bool AddDataSerial(UIChartDataSerial* dataSerial);

    /**
     * @brief Deletes a data set.
     *
     * @param dataSerial Indicates the pointer to the data set class. For details, see {@link UIChartDataSerial}.
     * @return Returns <b>true</b> if the data set is deleted successfully; returns <b>false</b> otherwise.
     * @see AddDataSerial
     * @since 1.0
     * @version 1.0
     */
    virtual bool DeleteDataSerial(UIChartDataSerial* dataSerial);

    /**
     * @brief Clears all data sets.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual void ClearDataSerial();

    /**
     * @brief Refreshes a chart and redraws the dirty region.
     *
     * Only the parts that need to be redrawn are refreshed, for example, new data points.
     * This function provides better performance than {@link Invalidate}.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual void RefreshChart() = 0;

    /**
     * @brief Obtains the x-axis instance.
     *
     * @return Returns the x-axis instance.
     * @since 1.0
     * @version 1.0
     */
    UIXAxis& GetXAxis()
    {
        return xAxis_;
    }

    /**
     * @brief Obtains the y-axis instance.
     *
     * @return Returns the y-axis instance.
     * @since 1.0
     * @version 1.0
     */
    UIYAxis& GetYAxis()
    {
        return yAxis_;
    }

    /**
     * @brief Enables chart reverse.
     *
     * After the chart is reversed, the x-axis aligns with the top of the chart. The pixel position corresponding
     * to the data point remains unchanged. Complementary filling is performed on the chart
     * (only the part that is not filled previously will be filled).
     *
     * @param enable Specifies whether to enable chart reverse. Value <b>true</b> means to enable chart reverse,
     *               and value <b>false</b> means not to enable chart reverse. The default value is <b>false</b>.
     * @since 1.0
     * @version 1.0
     */
    void EnableReverse(bool enable)
    {
        if (enableReverse_ != enable) {
            enableReverse_ = enable;
            xAxis_.EnableReverse(enable);
            yAxis_.EnableReverse(enable);
        }
    }

protected:
    List<UIChartDataSerial*> list_;
    UIXAxis xAxis_;
    UIYAxis yAxis_;
    bool enableReverse_;
    bool needRefresh_;
    uint8_t* mixData_;
N
niulihua 已提交
708
    virtual void DrawDataSerials(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) = 0;
M
mamingshuai 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
};

/**
 * @brief Provides special functions for implementing a bar chart.
 *
 * @since 1.0
 * @version 1.0
 */
class UIChartPillar : public UIChart {
public:
    /**
     * @brief A constructor used to create a <b>UIChartPillar</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UIChartPillar() {}

    /**
     * @brief A destructor used to delete the <b>UIChartPillar</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~UIChartPillar() {}

    /**
     * @brief Refreshes a bar chart and redraws the dirty region.
     *
     * Only the parts that need to be redrawn are refreshed, for example, new data points.
     * This function provides better performance than {@link Invalidate}.
     *
     * @since 1.0
     * @version 1.0
     */
    void RefreshChart() override;

protected:
N
niulihua 已提交
747
    void DrawDataSerials(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
M
mamingshuai 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

private:
    static constexpr float DEFAULT_MARK_PERCENTAGE = 0.1f;
};

/**
 * @brief Provides special functions for implementing a polyline.
 *
 * @since 1.0
 * @version 1.0
 */
class UIChartPolyline : public UIChart {
public:
    /**
     * @brief A constructor used to create a <b>UIChartPolyline</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UIChartPolyline() : minOpa_(OPA_TRANSPARENT), maxOpa_(OPA_OPAQUE), gradientBottom_(0) {}

    /**
     * @brief A destructor used to delete the <b>UIChartPolyline</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~UIChartPolyline() {}

    /**
     * @brief Refreshes a line chart and redraws the dirty region.
     *
     * Only the parts that need to be redrawn are refreshed, for example, new data points.
     * This function provides better performance than {@link Invalidate}.
     *
     * @since 1.0
     * @version 1.0
     */
    void RefreshChart() override;

    /**
     * @brief Sets the opacity range of the fill color gradient.
     *
     * This function sets the opacity range between the top point and bottom point of the line chart.
     * The opacity of each horizontal line is calculated based on the ratio.
     *
     * @param minOpa Indicates the opacity closest to the x-axis.
     * @param maxOpa Indicates the opacity farthest away from the x-axis.
     * @since 1.0
     * @version 1.0
     */
    void SetGradientOpacity(uint8_t minOpa, uint8_t maxOpa)
    {
        minOpa_ = minOpa;
        maxOpa_ = maxOpa;
        needRefresh_ = true;
    }

    /**
     * @brief Sets the distance between the bottom edge of the fill color range and the x-axis.
     *
     * This function fills in the area between the polyline and bottom of the line chart. For a chart that is not
     * reversed, if the bottom is above the polyline, there is no filling. For a reversed chart,
     * if the bottom is below the polyline, there is no filling.
     *
     * @param bottom Indicates the bottom of the filling range. The value is the distance to the x-axis.
     * @since 1.0
     * @version 1.0
     */
    void SetGradientBottom(uint16_t bottom)
    {
        gradientBottom_ = bottom;
    }

protected:
N
niulihua 已提交
823
    void DrawDataSerials(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
M
mamingshuai 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

private:
    struct ChartLine {
        Point start;
        Point end;
    };

    struct CrossPointSet {
        Point first;
        Point second;
        Point nextFirst;
        bool firstFind;
        bool secondFind;
    };

    constexpr static uint8_t SMOOTH_SLOPE_ANGLE = 3;
    constexpr static uint8_t LINE_JOIN_WIDTH = 3;
    uint8_t minOpa_;
    uint8_t maxOpa_;
    uint16_t gradientBottom_;

N
niulihua 已提交
845 846 847
    void GradientColor(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, UIChartDataSerial* data);
    void DrawGradientColor(BufferInfo& gfxDstBuffer,
                           const Rect& invalidatedArea,
M
mamingshuai 已提交
848 849 850 851
                           UIChartDataSerial* data,
                           const ChartLine& linePoints,
                           const ChartLine& limitPoints,
                           int16_t startY);
N
niulihua 已提交
852 853
    void DrawSmoothPolyLine(BufferInfo& gfxDstBuffer,
                            uint16_t startIndex,
M
mamingshuai 已提交
854 855 856
                            uint16_t endIndex,
                            const Rect& invalidatedArea,
                            UIChartDataSerial* data);
N
niulihua 已提交
857 858
    void DrawPolyLine(BufferInfo& gfxDstBuffer, uint16_t startIndex, uint16_t endIndex,
                      const Rect& invalidatedArea, UIChartDataSerial* data);
M
mamingshuai 已提交
859 860 861 862 863 864 865
    bool GetLineCrossPoint(const Point& p1, const Point& p2, const Point& p3, const Point& p4, Point& cross);
    void FindCrossPoints(const ChartLine& line, const ChartLine& polyLine, CrossPointSet& cross);
    void ReMeasure() override;
    void CalcVerticalInfo(int16_t top, int16_t bottom, int16_t start, int16_t end, int16_t& y, int16_t& yHeight);
};
} // namespace OHOS
#endif // GRAPHIC_LITE_UI_CHART_H