openharmony 鸿蒙 js-lite-components-basic-chart

2025-06-12 浏览 (1)

chart

The <chart> component displays line charts and bar charts.

NOTE

This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

Child Components

Not supported

Attributes

NameTypeMandatoryDescription
typestringNoChart type. Dynamic modification is not supported. Available values include:
- bar: bar chart
- line: line chart
Default value: line
optionsChartOptionsYesChart parameters. You can set the minimum value, maximum value, scale, and line width of the x-axis or y-axis, whether to display the x-axis and y-axis, and whether the line is smooth. Dynamic modification is not supported.
datasetsArray<ChartDataset>YesData sets. You can set multiple datasets and their background colors.
idstringNoUnique ID of the component.
stylestringNoStyle declaration of the component.
classstringNoStyle class of the component, which is used to refer to a style table.
refstringNoReference information of child elements, which is registered with the parent component on $refs.

Table 1 ChartOptions

NameTypeMandatoryDescription
xAxisChartAxisYesX-axis parameters. You can set the minimum value, maximum value, and scale of the x-axis, and whether to display the x-axis.
yAxisChartAxisYesY-axis parameters. You can set the minimum value, maximum value, and scale of the y-axis, and whether to display the y-axis.
seriesChartSeriesNoData series parameters which cover the following:
- Line style, such as the line width and whether the line is smooth.
- Style and size of the white point at the start of the line.
NOTE
Only line charts support this attribute.

Table 2 ChartDataset

NameTypeDefault ValueMandatoryDescription
backgroundColor(deprecated)<color>#ff6384NoColor of a line or bar. This attribute is not recommended.
strokeColor<color>#ff6384NoLine color. Only line charts support this attribute.
fillColor<color>#ff6384NoFill color. For line charts, the value indicates the gradient color to fill.
dataArray<number>-YesData of the drawn line or bar.
gradientbooleanfalseNoWhether to display the gradient color. Only line charts support this attribute.

Table 3 ChartAxis

NameTypeDefault ValueMandatoryDescription
minnumber0NoMinimum value of the axis.
Negative numbers are not supported. Only line charts support this attribute.
maxnumber100NoMaximum value of the axis.
Negative numbers are not supported. Only line charts support this attribute.
axisTicknumber10NoNumber of scales displayed on the axis.
NOTE
The value ranges from 1 to 20. The display effect depends on the calculation result of Number of pixels occupied by the image width/(max-min).
Lite wearables support integer calculation, and an error may occur in the case of inexhaustible division. Specifically, a segment of space may be left at the end of the x-axis.
In the bar chart, the number of bars in each group of data is the same as the number of scales, and the bars are displayed at the scales.
displaybooleanfalseNoWhether to display the axis.
color<color>#c0c0c0NoAxis color.

Table 4 ChartSeries

NameTypeMandatoryDescription
lineStyleChartLineStyleNoLine style, such as the line width and whether the line is smooth.
headPointPointStyleNoStyle and size of the white point at the start of the line.
topPointPointStyleNoStyle and size of the top point.
bottomPointPointStyleNoStyle and size of the bottom point.
loopChartLoopNoWhether to start drawing again when the screen is looped.

Table 5 ChartLineStyle

NameTypeDefault ValueMandatoryDescription
width<length>1pxNoLine width.
smoothbooleanfalseNoWhether the line is smooth.

Table 6 PointStyle

NameTypeDefault ValueMandatoryDescription
shapestringcircleNoShape of the highlight point. Available values are as follows:
- circle
size<length>5pxNoSize of the highlight point.
strokeWidth<length>1pxNoStroke width.
strokeColor<color>#ff0000NoStroke color.
fillColor<color>#ff0000NoFill color.
displaybooleantrueNoWhether to display the highlight spot.

Table 7 ChartLoop

NameTypeDefault ValueMandatoryDescription
margin<length>1NoNumber of erased points (horizontal distance between the latest drawn point and the earliest point). You are not advised to use margin together with topPoint, bottomPoint, or headPoint for mini-, small- and standard-system devices. If you do so, there is a possibility that the point is in the erase area and invisible.

Methods

MethodsParameterDescription
append{
serial: number, // Set the data subscript of the line chart to be updated.
data: Array<number>, // Set the new data.
}
Dynamically add data to an existing data series. The target series is specified based on serial, which is the subscript of the datasets array and starts from 0. datasets[index].data is not updated. Only line charts support this attribute. The value is incremented by 1 based on the horizontal coordinate and is related to the xAxis min/max setting.

Events

NameParameterDescription
click-Triggered when the component is clicked.
longpress-Triggered when the component is long pressed.
swipe5+SwipeEventTriggered when a user quickly swipes on the component.

Styles

NameTypeDefault ValueMandatoryDescription
width<length> |<percentage>5+-NoComponent width.

If this attribute is not set, the default value 0 is used.
height<length> |<percentage>5+-NoComponent height.

If this attribute is not set, the default value 0 is used.
padding<length>0NoShorthand attribute to set the padding for all sides.
The attribute can have one to four values:
- If you set only one value, it specifies the padding for all the four sides.
- If you set two values, the first value specifies the top and bottom padding, and the second value specifies the left and right padding.
- If you set three values, the first value specifies the top padding, the second value specifies the left and right padding, and the third value specifies the bottom padding.
- If you set four values, they respectively specify the padding for top, right, bottom, and left sides (in clockwise order).
padding-[left|top|right|bottom]<length>0NoLeft, top, right, and bottom padding.
margin<length> |<percentage>5+0NoShorthand attribute to set the margin for all sides. The attribute can have one to four values:
- If you set only one value, it specifies the margin for all the four sides.
- If you set two values, the first value specifies the top and bottom margins, and the second value specifies the left and right margins.
- If you set three values, the first value specifies the top margin, the second value specifies the left and right margins, and the third value specifies the bottom margin.
- If you set four values, they respectively specify the margin for top, right, bottom, and left sides (in clockwise order).
margin-[left|top|right|bottom]<length> |<percentage>5+0NoLeft, top, right, and bottom margins.
border-width<length>0NoShorthand attribute to set the margin for all sides.
border-color<color>blackNoShorthand attribute to set the color for all borders.
border-radius<length>-NoRadius of round-corner borders.
background-color<color>-NoBackground color.
displaystringflexNoHow and whether to display the box containing an element. Available values are as follows:
- flex: flexible layout
- none: not rendered
[left|top]<length> |<percentage>6+-NoEdge of the element.
- left: left edge position of the element. This attribute defines the offset between the left edge of the margin area of a positioned element and left edge of its containing block.
- top: top edge position of the element. This attribute defines the offset between the top edge of a positioned element and that of a block included in the element.

Example

  1. Line chart

    <!-- xxx.hml -->
    <div class="container">
      <chart class="chart" type="line" ref="linechart" options="{{lineOps}}" datasets="{{lineData}}"></chart>
      <input class="button" type="button" value="Add data" onclick="addData"/>
    </div>
    
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 454px;
      height: 454px;
      background-color: white;
    }
    .chart {
      width: 300px;
      height: 300px;
    }
    .button {
      width: 280px;
      border-radius: 0px;
    }
    
    // xxx.js
    export default {
      data: {
        lineData: [
          {
            strokeColor: '#0081ff',
            fillColor: '#cce5ff',
            data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628, 791, 505, 613, 575, 475, 553, 491, 680, 657, 716],
            gradient: false,
          }
        ],
        lineOps: {
          xAxis: {
            min: 0,
            max: 20,
            display: false,
          },
          yAxis: {
            min: 0,
            max: 1000,
            display: false,
          },
          series: {
            lineStyle: {
              width: "5px",
              smooth: true,
            },
            headPoint: {
              shape: "circle",
              size: 10,
              strokeWidth: 5,
              fillColor: '#ffffff',
              strokeColor: '#007aff',
              display: true,
            },
            loop: {
              margin: 2,
              gradient: true,
            }
          }
        },
      },
      addData() {
        this.$refs.linechart.append({
          serial: 0,
          data: [Math.floor(Math.random() * 400) + 400]
        })
      }
    }
    

    lite_line

  2. Bar chart

    <!-- xxx.hml -->
    <div class="container">
      <chart class="chart" type="bar" id="bar-chart" options="{{barOps}}" datasets="{{barData}}"></chart>
    </div>
    
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 454px;
      height: 454px;
      background-color: white;
    }
    .chart {
      width: 300px;
      height: 300px;
    }
    
    // xxx.js
    export default {
      data: {
        barData: [
          {
            fillColor: '#f07826',
            data: [763, 550, 551, 554, 731, 654, 525, 696, 595, 628],
          },
          {
            fillColor: '#cce5ff',
            data: [535, 776, 615, 444, 694, 785, 677, 609, 562, 410],
          },
          {
            fillColor: '#ff88bb',
            data: [673, 500, 574, 483, 702, 583, 437, 506, 693, 657],
          },
        ],
        barOps: {
          xAxis: {
            min: 0,
            max: 20,
            display: false,
            axisTick: 10
          },
          yAxis: {
            min: 0,
            max: 1000,
            display: false,
          },
        },
      }
    }
    

    lite_bar

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Lite)

harmony 鸿蒙Universal Attributes

harmony 鸿蒙Universal Events

harmony 鸿蒙Universal Styles

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙marquee

harmony 鸿蒙picker-view

harmony 鸿蒙progress

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/t3g5QI