Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ECharts Dataset

ECharts Dataset

ECharts uses dataset to manage data.

The dataset component is used for separate dataset declarations, so data can be managed independently, reused by multiple components, and mapped from data to visuals based on the data.

The following is a simplest example of dataset:

Example

option

=

{

    legend

:

{

}

,

    tooltip

:

{

}

,

    dataset

:

{

       

// Provide a set of data.

        source

:

[

           

[

'product'

,

'2015'

,

'2016'

,

'2017'

]

,

           

[

'Matcha Latte'

,

43.3

,

85.8

,

93.7

]

,

           

[

'Milk Tea'

,

83.1

,

73.4

,

55.1

]

,

           

[

'Cheese Cocoa'

,

86.4

,

65.2

,

82.5

]

,

           

[

'Walnut Brownie'

,

72.4

,

53.9

,

39.1

]

       

]

   

}

,

   

// Declare an X axis, a category axis. By default, the category axis maps to the first column of the dataset.

    xAxis

:

{

type

:

'category'

}

,

   

// Declare a Y axis, a value axis.

    yAxis

:

{

}

,

   

// Declare multiple bar series; by default, each series automatically maps to a column of the dataset.

    series

:

[

       

{

type

:

'bar'

}

,

       

{

type

:

'bar'

}

,

       

{

type

:

'bar'

}

   

]

}

Try it »

Alternatively, you can also use the common object-array format:

Example

option

=

{

    legend

:

{

}

,

    tooltip

:

{

}

,

    dataset

:

{

       

// Here the order of dimension names is specified, so the default dimension-to-axis mapping can be used.

       

// If dimensions are not specified, the mapping can also be completed by specifying series.encode; see later sections.

        dimensions

:

[

'product'

,

'2015'

,

'2016'

,

'2017'

]

,

        source

:

[

           

{

product

:

'Matcha Latte'

,

'2015'

:

43.3

,

'2016'

:

85.8

,

'2017'

:

93.7

}

,

           

{

product

:

'Milk Tea'

,

'2015'

:

83.1

,

'2016'

:

73.4

,

'2017'

:

55.1

}

,

           

{

product

:

'Cheese Cocoa'

,

'2015'

:

86.4

,

'2016'

:

65.2

,

'2017'

:

82.5

}

,

           

{

product

:

'Walnut Brownie'

,

'2015'

:

72.4

,

'2016'

:

53.9

,

'2017'

:

39.1

}

       

]

   

}

,

    xAxis

:

{

type

:

'category'

}

,

    yAxis

:

{

}

,

    series

:

[

       

{

type

:

'bar'

}

,

       

{

type

:

'bar'

}

,

       

{

type

:

'bar'

}

   

]

}

;

Try it »

Mapping Data to Graphics

We can map data into graphics in the configuration.

We can use the series.seriesLayoutBy property to configure whether the dataset is mapped to graphic series (series) by column or by row; by default it maps by column.

In the following example, we configure whether the data is displayed by column or by row through the seriesLayoutBy property.

Example

option

=

{

    legend

:

{

}

,

    tooltip

:

{

}

,

    dataset

:

{

        source

:

[

           

[

'product'

,

'2012'

,

'2013'

,

'2014'

,

'2015'

]

,

           

[

'Matcha Latte'

,

41.1

,

30.4

,

65.1

,

53.3

]

,

           

[

'Milk Tea'

,

86.5

,

92.1

,

85.7

,

83.1

]

,

           

[

'Cheese Cocoa'

,

24.1

,

67.2

,

79.5

,

86.4

]

       

]

   

}

,

    xAxis

:

[

       

{

type

:

'category'

,

gridIndex

:

0

}

,

       

{

type

:

'category'

,

gridIndex

:

1

}

   

]

,

    yAxis

:

[

       

{

gridIndex

:

0

}

,

       

{

gridIndex

:

1

}

   

]

,

    grid

:

[

       

{

bottom

:

'55%'

}

,

       

{

top

:

'55%'

}

   

]

,

    series

:

[

       

// These series will be in the first Cartesian coordinate system, each series corresponding to a row of the dataset.

       

{

type

:

'bar'

,

seriesLayoutBy

:

'row'

}

,

       

{

type

:

'bar'

,

seriesLayoutBy

:

'row'

}

,

       

{

type

:

'bar'

,

seriesLayoutBy

:

'row'

}

,

       

// These series will be in the second Cartesian coordinate system, each series corresponding to a column of the dataset.

       

{

type

:

'bar'

,

xAxisIndex

:

1

,

yAxisIndex

:

1

}

,

       

{

type

:

'bar'

,

xAxisIndex

:

1

,

yAxisIndex

:

1

}

,

       

{

type

:

'bar'

,

xAxisIndex

:

1

,

yAxisIndex

:

1

}

,

       

{

type

:

'bar'

,

xAxisIndex

:

1

,

yAxisIndex

:

1

}

   

]

}

Try it »

Most data described by common charts has a "two-dimensional table" structure. We can use the series.encode property to map the corresponding data to coordinate axes (such as X and Y axes):

Example

var

option

=

{

    dataset

:

{

        source

:

[

           

[

'score'

,

'amount'

,

'product'

]

,

           

[

89.3

,

58212

,

'Matcha Latte'

]

,

           

[

57.1

,

78254

,

'Milk Tea'

]

,

           

[

74.4

,

41032

,

'Cheese Cocoa'

]

,

           

[

50.1

,

12755

,

'Cheese Brownie'

]

,

           

[

89.7

,

20145

,

'Matcha Cocoa'

]

,

           

[

68.1

,

79146

,

'Tea'

]

,

           

[

19.6

,

91852

,

'Orange Juice'

]

,

           

[

10.6

,

101852

,

'Lemon Juice'

]

,

           

[

32.7

,

20112

,

'Walnut Brownie'

]

       

]

   

}

,

    grid

:

{

containLabel

:

true

}

,

    xAxis

:

{

}

,

    yAxis

:

{

type

:

'category'

}

,

    series

:

[

       

{

            type

:

'bar'

,

            encode

:

{

               

// Map the "amount" column to the X axis.

                x

:

'amount'

,

               

// Map the "product" column to the Y axis.

                y

:

'product'

           

}

       

}

   

]

}

;

Try it »

The basic structure of the encode declaration is as follows. The left side of the colon is a specific name such as a coordinate system or label, e.g., 'x', 'y', 'tooltip', etc. The right side is the dimension name (string format) or the dimension index (number format, counting from 0) in the data; one or more dimensions (using an array) can be specified. Usually, you do not need to write all of the following information; write as needed.

The following are the properties supported by encode:

// 在任何坐标系和系列中,都支持:

encode: {

    // Display the values of the "product" dimension and the "score" dimension in the tooltip

    tooltip: ['product', 'score']

    // Concatenate the dimension names of "Dimension 1" and "Dimension 3" as the series name. (Sometimes the names are long, and this can avoid having to input these names repeatedly in series.name)

    seriesName: [1, 3],

    // Indicates that the value in "dimension 2" is used as the id. This is useful when dynamically updating data using setOption, as it allows new and old data to be mapped by id, thus generating appropriate data update animations.

    itemId: 2,

    // Specify the name of the data item using "Dimension 3" which is useful in charts like pie charts, allowing this name to be displayed in the legend.

    itemName: 3

}



// Properties specific to the rectangular coordinate system (grid/cartesian):

encode: {

    // Map "Dimension 1", "Dimension 5", and "Dimension named score" to the X-axis:

    x: [1, 5, 'score'],

    // Map "dimension 0" to the Y-axis.

    y: 0

}



// Properties specific to singleAxis:

encode: {

    single: 3

}



// Properties specific to the polar coordinate system:

encode: {

    radius: 3,

    angle: 2

}



// Properties specific to the geographic coordinate system (geo):

encode: {

    lng: 3,

    lat: 2

}



// For charts without a coordinate system, such as pie charts, funnel charts, etc., it can be:

encode: {

    value: 3

}

More encode examples:

Example

$.

get

(

'https://www.runoob.com/static/js/life-expectancy-table.json'

,

function

(

data

)

{

   

var

sizeValue

=

'57%'

;

   

var

symbolSize

=

2.5

;

    option

=

{

        legend

:

{

}

,

        tooltip

:

{

}

,

        toolbox

:

{

            left

:

'center'

,

            feature

:

{

                dataZoom

:

{

}

           

}

       

}

,

        grid

:

[

           

{

right

:

sizeValue

,

bottom

:

sizeValue

}

,

           

{

left

:

sizeValue

,

bottom

:

sizeValue

}

,

           

{

right

:

sizeValue

,

top

:

sizeValue

}

,

           

{

left

:

sizeValue

,

top

:

sizeValue

}

       

]

,

        xAxis

:

[

           

{

type

:

'value'

,

gridIndex

:

0

,

name

:

'Income'

,

axisLabel

:

{

rotate

:

50

,

interval

:

0

}

}

,

           

{

type

:

'category'

,

gridIndex

:

1

,

name

:

'Country'

,

boundaryGap

:

false

,

axisLabel

:

{

rotate

:

50

,

interval

:

0

}

}

,

           

{

type

:

'value'

,

gridIndex

:

2

,

name

:

'Income'

,

axisLabel

:

{

rotate

:

50

,

interval

:

0

}

}

,

           

{

type

:

'value'

,

gridIndex

:

3

,

name

:

'Life Expectancy'

,

axisLabel

:

{

rotate

:

50

,

interval

:

0

}

}

       

]

,

        yAxis

:

[

           

{

type

:

'value'

,

gridIndex

:

0

,

name

:

'Life Expectancy'

}

,

           

{

type

:

'value'

,

gridIndex

:

1

,

name

:

'Income'

}

,

           

{

type

:

'value'

,

gridIndex

:

2

,

name

:

'Population'

}

,

           

{

type

:

'value'

,

gridIndex

:

3

,

name

:

'Population'

}

       

]

,

        dataset

:

{

            dimensions

:

[

               

'Income'

,

               

'Life Expectancy'

,

               

'Population'

,

               

'Country'

,

               

{

name

:

'Year'

,

type

:

'ordinal'

}

           

]

,

            source

:

data

       

}

,

        series

:

[

           

{

                type

:

'scatter'

,

                symbolSize

:

symbolSize

,

                xAxisIndex

:

0

,

                yAxisIndex

:

0

,

                encode

:

{

                    x

:

'Income'

,

                    y

:

'Life Expectancy'

,

                    tooltip

:

[

0

,

1

,

2

,

3

,

4

]

               

}

           

}

,

           

{

                type

:

'scatter'

,

                symbolSize

:

symbolSize

,

                xAxisIndex

:

1

,

                yAxisIndex

:

1

,

                encode

:

{

                    x

:

'Country'

,

                    y

:

'Income'

,

                    tooltip

:

[

0

,

1

,

2

,

3

,

4

]

               

}

           

}

,

           

{

                type

:

'scatter'

,

                symbolSize

:

symbolSize

,

                xAxisIndex

:

2

,

                yAxisIndex

:

2

,

                encode

:

{

                    x

:

'Income'

,

                    y

:

'Population'

,

                    tooltip

:

[

0

,

1

,

2

,

3

,

4

]

               

}

           

}

,

           

{

                type

:

'scatter'

,

                symbolSize

:

symbolSize

,

                xAxisIndex

:

3

,

                yAxisIndex

:

3

,

                encode

:

{

                    x

:

'Life Expectancy'

,

                    y

:

'Population'

,

                    tooltip

:

[

0

,

1

,

2

,

3

,

4

]

               

}

           

}

       

]

   

}

;

    myChart.

setOption

(

option

)

;

}

)

;

Try it »

Mapping of Visual Channels (Color, Size, etc.)

We can use the visualMap component for visual channel mapping.

Visual elements can be:

Multiple visualMap components can be defined, so that multiple dimensions of the data can be visually mapped at the same time.

Example

var

option

=

{

    dataset

:

{

        source

:

[

           

[

'score'

,

'amount'

,

'product'

]

,

           

[

89.3

,

58212

,

'Matcha Latte'

]

,

           

[

57.1

,

78254

,

'Milk Tea'

]

,

           

[

74.4

,

41032

,

'Cheese Cocoa'

]

,

           

[

50.1

,

12755

,

'Cheese Brownie'

]

,

           

[

89.7

,

20145

,

'Matcha Cocoa'

]

,

           

[

68.1

,

79146

,

'Tea'

]

,

           

[

19.6

,

91852

,

'Orange Juice'

]

,

           

[

10.6

,

101852

,

'Lemon Juice'

]

,

           

[

32.7

,

20112

,

'Walnut Brownie'

]

       

]

   

}

,

    grid

:

{

containLabel

:

true

}

,

    xAxis

:

{

name

:

'amount'

}

,

    yAxis

:

{

type

:

'category'

}

,

    visualMap

:

{

        orient

:

'horizontal'

,

        left

:

'center'

,

        min

:

10

,

        max

:

100

,

        text

:

[

'High Score'

,

'Low Score'

]

,

       

// Map the score column to color

        dimension

:

0

,

        inRange

:

{

            color

:

[

'#D7DA8B'

,

'#E15457'

]

       

}

   

}

,

    series

:

[

       

{

            type

:

'bar'

,

            encode

:

{

               

// Map the "amount" column to X axis.

                x

:

'amount'

,

               

// Map the "product" column to Y axis

                y

:

'product'

           

}

       

}

   

]

}

;

Try it »

Interactive Linkage

In the following example, multiple charts share one dataset and have linked interactions:

Example

setTimeout

(

function

(

)

{

    option

=

{

        legend

:

{

}

,

        tooltip

:

{

            trigger

:

'axis'

,

            showContent

:

false

       

}

,

        dataset

:

{

            source

:

[

               

[

'product'

,

'2012'

,

'2013'

,

'2014'

,

'2015'

,

'2016'

,

'2017'

]

,

               

[

'Matcha Latte'

,

41.1

,

30.4

,

65.1

,

53.3

,

83.8

,

98.7

]

,

               

[

'Milk Tea'

,

86.5

,

92.1

,

85.7

,

83.1

,

73.4

,

55.1

]

,

               

[

'Cheese Cocoa'

,

24.1

,

67.2

,

79.5

,

86.4

,

65.2

,

82.5

]

,

               

[

'Walnut Brownie'

,

55.2

,

67.1

,

69.2

,

72.4

,

53.9

,

39.1

]

           

]

       

}

,

        xAxis

:

{

type

:

'category'

}

,

        yAxis

:

{

gridIndex

:

0

}

,

        grid

:

{

top

:

'55%'

}

,

        series

:

[

           

{

type

:

'line'

,

smooth

:

true

,

seriesLayoutBy

:

'row'

}

,

           

{

type

:

'line'

,

smooth

:

true

,

seriesLayoutBy

:

'row'

}

,

           

{

type

:

'line'

,

smooth

:

true

,

seriesLayoutBy

:

'row'

}

,

           

{

type

:

'line'

,

smooth

:

true

,

seriesLayoutBy

:

'row'

}

,

           

{

                type

:

'pie'

,

                id

:

'pie'

,

                radius

:

'30%'

,

                center

:

[

'50%'

,

'25%'

]

,

                label

:

{

                    formatter

:

'{b}: {@2012} ({d}%)'

               

}

,

                encode

:

{

                    itemName

:

'product'

,

                    value

:

'2012'

,

                    tooltip

:

'2012'

               

}

           

}

       

]

   

}

;

    myChart.

on

(

'updateAxisPointer'

,

function

(

event

)

{

       

var

xAxisInfo

=

event.

axesInfo

[

0

]

;

       

if

(

xAxisInfo

)

{

           

var

dimension

=

xAxisInfo.

value

1

;

            myChart.

setOption

(

{

                series

:

{

                    id

:

'pie'

,

                    label

:

{

                        formatter

:

'{b}: {@['

dimension

']} ({d}%)'

                   

}

,

                    encode

:

{

                        value

:

dimension

,

                        tooltip

:

dimension

                   

}

               

}

           

}

)

;

       

}

   

}

)

;

    myChart.

setOption

(

option

)

;

}

)

;

Try it »