chart

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

Permission List

None

Child Component

Not supported

Attribute

Name

Type

Default Value

Mandatory

Description

type

string

line

No

Chart type. Dynamic modification is not supported. Available values include:

  • bar: bar chart
  • line: line chart
  • gauge: gauge chart
  • progress5+: circle chart of progresses
  • loading5+: circle chart of loading processes
  • rainbow5+: circle chart of proportions

options

ChartOptions

-

No

Chart parameters. You must set parameters for bar charts and line charts. Parameter settings for gauge charts do not take effect. 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.

datasets

Array<ChartDataset>

-

No

Data set. You must set data sets for bar charts and line charts. Data set for a gauge chart does not take effect. You can set multiple datasets and their background colors.

segments5+

DataSegment | Array<DataSegment>

-

No

Data structures used by progress, loading, and rainbow charts.

DataSegment is available for progress and loading charts.

Array<DataSegment> is available for rainbow charts. A maximum of nine DataSegment are supported in the array.

effects5+

boolean

true

No

Whether to enable the effects for progress and rainbow charts.

id

string

-

No

Unique ID of a component.

style

string

-

No

Style declaration of a component.

class

string

-

No

Style class of a component, which is used to refer to a style table.

ref

string

-

No

Used to register reference information of child elements or child components. The reference information is registered with the parent component on $refs.

disabled

boolean

false

No

Whether a component is disabled. If it is disabled, it cannot respond to user interaction.

data

string

-

No

Attribute set for a component to facilitate data storage and reading.

Table 1 ChartOptions

Name

Type

Default Value

Mandatory

Description

xAxis

ChartAxis

-

Yes

X-axis parameters. You can set the minimum value, maximum value, and scale of the x-axis, and whether to display the x-axis.

yAxis

ChartAxis

-

Yes

Y-axis parameters. You can set the minimum value, maximum value, and scale of the y-axis, and whether to display the y-axis.

series

ChartSeries

-

No

Data sequence parameters.

  • 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

Name

Type

Default Value

Mandatory

Description

strokeColor

<color>

#ff6384

No

Line color.

NOTE:

Only line charts support this attribute.

fillColor

<color>

#ff6384

No

Fill color. For line charts, the value indicates the gradient color to fill.

data

Array<number> | Array<Point>5+

-

Yes

Data of the drawn line or bar.

gradient

boolean

false

No

Whether to display the gradient color.

NOTE:

Only line charts support this attribute.

Table 3 ChartAxis

Name

Type

Default Value

Mandatory

Description

min

number

0

No

Minimum value of the axis.

NOTE:

Negative numbers are not supported. Only line charts support this attribute.

max

number

100

No

Maximum value of the axis.

NOTE:

Negative numbers are not supported. Only line charts support this attribute.

axisTick

number

10

No

Number 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).

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.

display

boolean

false

No

Whether to display the axis.

color

<color>

#c0c0c0

No

Axis color.

Table 4 ChartSeries

Name

Type

Default Value

Mandatory

Description

lineStyle

ChartLineStyle

-

No

Line style, such as the line width and whether the line is smooth.

headPoint

PointStyle

-

No

Style and size of the white point at the start of the line.

topPoint

PointStyle

-

No

Style and size of the top point.

bottomPoint

PointStyle

-

No

Style and size of the bottom point.

loop

ChartLoop

-

No

Whether to start drawing again when the screen is looped.

Table 5 ChartLineStyle

Name

Type

Default Value

Mandatory

Description

width

<length>

1px

No

Line width.

smooth

boolean

false

No

Whether the line is smooth.

Table 6 PointStyle

Name

Type

Default Value

Mandatory

Description

shape

string

circle

No

Shape of the highlight point. Available values are as follows:

  • Circle
  • Square
  • Triangle

size

<length>

5px

No

Size of the highlight point.

strokeWidth

<length>

1px

No

Stroke width.

strokeColor

<color>

#ff0000

No

Frame color.

fillColor

<color>

#ff0000

No

Fill color.

Table 7 ChartLoop

Name

Type

Default Value

Mandatory

Description

margin

<length>

1

No

Number 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 lite devices. If you do so, there is a possibility that the point is in the erase area and invisible.

gradient

boolean

No

No

Whether to perform gradient erase.

Table 8 Point5+

Name

Type

Default Value

Mandatory

Description

value

number

0

Yes

Y coordinate of the point to draw.

pointStyle

PointStyle

No

No

Style of the point.

description

string

No

No

Description text of the point.

textLocation

string

No

No

Description text position relative to the point. Available values are as follows:

top: above the point

bottom: below the point

none: not displayed

textColor

<color>

#000000

No

Color of the description text.

lineDash

string

solid

No

Dashed line pattern. You can set the dash length and space length between the dashes. For example, "dashed, 5, 5" indicates a dashed line with each dash in 5 px and a 5 px space between each two dashes. Default value "solid" indicates a solid line.

lineColor

<color>

#000000

No

Line color. If this attribute is not set, the strokeColor is used by default.

Table 9 DataSegment5+

Name

Type

Default Value

Mandatory

Description

startColor

Color

-

No

Color of the start position. If this attribute is set, endColor must be set. If this attribute is not set, the default color array preset in the system is used. For details about the color values, see the next table.

endColor

Color

-

No

Color of the end position. If this attribute is set, startColor must be set.

If this attribute is not set, the default color array preset in the system is used.

value

double

0

Yes

Percentage for the current data segment. The maximum value is 100.

name

string

-

No

Name of this data segment.

Data Segment

Light Mode

Dark Mode

0

Start color: #f7ce00; end color: #f99b11

Start color: #d1a738; end color: #eb933d

1

Start color: #f76223; end color: #f2400a

Start color: #e67d50; end color: #d9542b

2

Start color: #f772ac; end color: #e65392

Start color: #d5749e; end color: #d6568d

3

Start color: #a575eb; end color: #a12df7

Start color: #9973d1; end color: #5552d9

4

Start color: #7b79f7; end color: #4b48f7

Start color: #7977d9; end color: #f99b11

5

Start color: #4b8af3; end color: #007dff

Start color: #4c81d9; end color: #217bd9

6

Start color: #73c1e6; end color: #4fb4e3

Start color: #5ea6d1; end color: #4895c2

7

Start color: #a5d61d; end color: #69d14f

Start color: #91c23a; end color: #70ba5d

8

Start color: #a2a2b0; end color: #8e8e93

Start color: #8c8c99; end color: #6b6b76

For gauge charts, the following attribute is supported.

Name

Type

Default Value

Mandatory

Description

percent

number

0

No

Percentage of the current value to the total value. The value ranges from 0 to 100.

Method

Method

Parameter

Description

append

{

serial: number, // Set the data subscript of the line chart to be updated.

data: Array<number>, // Set the new data.

}

Data is dynamically added to an existing data sequence. The target sequence 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.

Event

Name

Parameter

Triggered when

touchstart

TouchEvent

The tapping starts

touchmove

TouchEvent

The tapping moves

touchcancel

TouchEvent

The tapping is interrupted

touchend

TouchEvent

The tapping ends

click

-

A component is clicked

longpress

-

A component is long pressed

swipe5+

SwipeEvent

A user quickly swipes on a component.

Style

Name

Type

Default Value

Mandatory

Description

stroke-width

<length>

32px (gauge charts)

24px (rainbow charts)

No

Width of the scale bar for gauge and rainbow charts.

start-angle

<deg>

240 (gauge charts)

0 (rainbow charts)

No

Start angle of the scale bar for gaugeand rainbow charts, which starts from the direction of zero o'clock. The value ranges from 0 to 360.

total-angle

<deg>

240 (gauge charts)

360 (rainbow charts)

No

Total length of the scale bar for gauge and rainbow charts. The value ranges from –360 to 360. A negative number indicates the anticlockwise direction.

center-x

<length>

-

No

Center of the scale bar of the gauge component. This style is supported by the gauge chart only. This style takes precedence over the position style in the common styles, and must be used together with center-y and radius.

center-y

<length>

-

No

Center of the scale bar of the gauge component. This style is supported by the gauge chart only. This style takes precedence over the position style in the common styles, and must be used together with center-x and radius.

radius

<length>

-

No

Radius of the scale bar of the gauge component. This style is supported by the gauge chart only. This style takes precedence over the width and height in the common styles, and must be used together with center-x and center-y.

colors

Array

-

No

Color of each section for the scale bar of the gauge component.

For example, colors: #ff0000, #00ff00. This style is supported by the gauge chart only.

weights

Array

-

No

Weight of each section for the scale bar of the gauge component.

For example, weights: 2, 2. This style is supported by the gauge chart only.

font-family5+

Array

-

No

Font style of the description text. You can use Custom Font Styles.

font-size5+

<length>

-

No

Font size of the description text.

width

<length> | <percentage>

-

No

Component width.

If this attribute is not set, the width required for the element content is used.

height

<length> | <percentage>

-

No

Component height.

If this length attribute is not set, the length required for the element content is used.

padding

<length> | <percentage>5+

0

No

Shorthand attribute to set all padding attributes.

The attribute can have one to four values:
  • If you set only one value, it specifies the padding for 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> | <percentage>5+

0

No

Left, top, right, and bottom padding (in px).

padding-[start|end]

<length> | <percentage>5+

0

No

Start and end padding.

margin

<length> | <percentage>5+

0

No

Shorthand attribute to set margins for all sides in a declaration. 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 is for the top and bottom sides and the second value for the left and right sides.

  • If you set three values, the first value is for the top, the second value for the left and right, and the third value for the bottom.

  • If you set four values, they are margins for top, right, bottom, and left sides, respectively.

margin-[left|top|right|bottom]

<length> | <percentage>5+

0

No

Left, top, right, and bottom margins.

margin-[start|end]

<length> | <percentage>5+

0

No

Start and end margins.

border

-

0

No

Shorthand attribute to set all borders. You can set border-width, border-style, and border-color in sequence. Default values are used for attributes that are not set.

border-style

string

solid

No

Shorthand attribute to set the style for all borders. Available values are as follows:

  • dotted: Dotted border. The radius of a dot is half of border-width.
  • dashed: Dashed border
  • solid: Solid border

border-[left|top|right|bottom]-style

string

solid

No

Styles of the left, top, right, and bottom borders. The available values are dotted, dashed, and solid.

border-[left|top|right|bottom]

-

-

No

Shorthand attribute to set the borders for every side respectively. You can set border-width, border-style, and border-color in sequence. Default values are used for attributes that are not set.

border-width

<length>

0

No

Shorthand attribute to set the width of all borders, or separately set the width of each border.

border-[left|top|right|bottom]-width

<length>

0

No

Attribute to set widths of left, top, right, and bottom borders.

border-color

<color>

black

No

Shorthand attribute to set the color of all borders, or separately set the color of each border.

border-[left|top|right|bottom]-color

<color>

black

No

Attribute to set colors of left, top, right, and bottom borders.

border-radius

<length>

-

No

Attribute to set the radius of round borders of an element. This attribute cannot be used to set the width, color, or style of a specific border. To set the width or color, you need to set border-width, border-color, or border-style for all the borders at the same time.

border-[top|bottom]-[left|right]-radius

<length>

-

No

Attribute to receptively set the radii of upper-left, upper-right, lower-right, and lower-left rounded corners

background

<linear-gradient>

-

No

This attribute supports Gradient Styles only but is not compatible with background-color or background-image.

background-color

<color>

-

No

Background color.

background-image

string

-

No

Background image. Currently, this attribute is not compatible with background-color or background. Local image resources are supported.

Example:

background-image: url("/common/background.png")

background-size

  • string
  • <length> <length>
  • <percentage> <percentage>

auto

No

Background image size.

  • The string values are as follows:
    • contain: Expands the image to the maximum size so that the height and width of the image are applicable to the content area.
    • cover: Extends the background image to a large enough size so that the background image completely covers the background area. Some parts of the image may not be displayed in the background area.
    • auto: The original image width-height ratio is retained.
  • The two <length> values are as follows:

    Width and height of the background image. The first value indicates the width, and the second value indicates the height. If you only set one value, the other value is set to auto by default.

  • The two <percentage> values are as follows:

    Width and height of the background image in percentage of the parent element. The first value indicates the width, and the second value indicates the height. If you only set one value, the other value is set to auto by default.

background-repeat

string

repeat

No

How a background image is repeatedly drawn. By default, a background image is repeated both horizontally and vertically.

  • repeat: Repeatedly draws images along the x-axis and y-axis at the same time.
  • repeat-x: Repeatedly draws images along the x-axis.
  • repeat-y: Repeatedly draws images along the y-axis.
  • no-repeat: The image is not drawn repeatedly.

background-position

  • string string
  • <length> <length>
  • <percentage> <percentage>

0px 0px

No

  • Using keywords: If only one keyword is specified, the other value is center by default. The two values define the horizontal position and vertical position, respectively.
    • left: leftmost in the horizontal direction
    • right: rightmost in the horizontal direction
    • top: top in the vertical direction
    • bottom: bottom in the vertical direction
    • center: center position
  • Using <length>: The first value indicates the horizontal position, and the second value indicates the vertical position. 0 0 indicates the upper left corner. The unit is pixel. If only one value is specified, the other one is 50%.
  • Using <percentage>: The first value indicates the horizontal position, and the second value indicates the vertical position. 0% 0% indicates the upper left corner. 100% 100% indicates the lower right corner. If only one value is specified, the other one is 50%.
  • Using both <percentage> and <length>.

opacity

number

1

No

Transparency of an element. The value ranges from 0 to 1. The value 1 means opaque, and 0 means completely transparent.

display

string

flex

No

How and whether to display the box containing an element. Available values are as follows:

  • flex: flexible layout
  • none: The element is hidden.

visibility

string

visible

No

Whether to display an element. Invisible borders occupy layout space. (To remove the borders, set the display attribute to none.) Available values are as follows:

  • visible: The element is visible.
  • hidden: The element is hidden but still takes up space.
NOTE:

If both visibility and display are set, only display takes effect.

flex

-

-

No

How to divide available space of the parent component for a child component.

You can set one, two5+, or three5+ values for this style.

Set one value in either of the following ways:

  • A unitless number to set flex-grow.
  • A valid width value5+ to set flex-basis.

Set two values5+ in the following ways:

The first value must be a unitless number used to set flex-grow. The second value must be either of the following:

  • A unitless number to set flex-shrink.
  • A valid width value to set flex-basis.

Set three values5+ in the following ways:

The first value must be a unitless number used to set flex-grow. The second value must be a unitless number used to set flex-shrink. The third value must be a valid width value used to set flex-basis.

NOTE:

This style takes effect only when the container is any of the following components: <div>, <list-item>, <refresh>, <stepper-item>5+, and <tabs>.

flex-grow

number

0

No

How much a child component will grow. The value specifies allocation of the remaining space on the main axis of the parent component. Size of available space = Container size - Total size of all child components. Value 0 indicates that the child component does not grow.

NOTE:

This style takes effect only when the container is any of the following components: <div>, <list-item>, <refresh>, <stepper-item>5+, and <tabs>.

flex-shrink

number

1

No

How much a child component will shrink. The shrink occurs only when the sum of default child component widths is greater than that of the parent component. Value 0 indicates that the child component does not shrink.

NOTE:

This style takes effect only when the container is any of the following components: <div>, <list-item>, <refresh>, <stepper-item>5+, and <tabs>.

flex-basis

<length>

-

No

Initial length of the flex item on the main axis.

NOTE:

This style takes effect only when the container is any of the following components: <div>, <list-item>, <refresh>, <stepper-item>5+, and <tabs>.

position

string

relative

No

Positioning type of an element. Dynamic changes are not supported.

  • fixed: The element is positioned related to the browser window.
  • absolute: The element is positioned absolutely to its parent element.
  • relative: The element is positioned relative to its normal position.
NOTE:

The absolute attribute takes effect only when the parent component is <div> or <stack>.

[left|top|right|bottom]

<length>

-

No

left|top|right|bottom must be used together with position to determine the offset position of an element.

  • The left attribute specifies the left edge position of the element. This attribute defines the offset between the left edge of a positioned element and that of a block included in the element.
  • The top attribute specifies the 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.
  • The right attribute specifies the right edge position of the element. This attribute defines the offset between the right edge of a positioned element and that of a block included in the element.
  • The bottom attribute specifies the bottom edge position of the element. This attribute defines the offset between the bottom edge of a positioned element and that of a block included in the element.

Example Code

  1. Line chart

    <!-- xxx.hml -->
    <div class="container">
      <stack class="chart-region">
        <image class="chart-background" src="common/background.png"></image>
        <chart class="chart-data" type="line" ref="linechart" options="{{lineOps}}" datasets="{{lineData}}"></chart>
      </stack>
      <button value="Add data" onclick="addData"></button>
    </div>
    
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .chart-region {
      height: 400px;
      width: 700px;
    }
    .chart-background {
      object-fit: fill;
    }
    .chart-data {
      width: 700px;
      height: 600px;
    }
    
    // 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: true,
          }
        ],
        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: 20,
              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]
        })
      }
    }
    

  2. Bar chart

    <!-- xxx.hml -->
    <div class="container">
      <stack class="data-region">
        <image class="data-background" src="common/background.png"></image>
        <chart class="data-bar" type="bar" id="bar-chart" options="{{barOps}}" datasets="{{barData}}"></chart>
      </stack>
    </div>
    
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .data-region {
      height: 400px;
      width: 700px;
    }
    .data-background {
      object-fit: fill;
    }
    .data-bar {
      width: 700px;
      height: 400px;
    }
    
    // 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,
          },
        },
      }
    }
    

  3. Gauge chart

    <!-- xxx.hml -->
    <div class="container">
      <div class="gauge-region">
        <chart class="data-gauge" type="gauge" percent = "50"></chart>
      </div>
    </div>
    
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .gauge-region {
      height: 400px;
      width: 400px;
    }
    .data-gauge {
      colors: #83f115, #fd3636, #3bf8ff;
      weights: 4, 2, 1;
    }