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

ECharts Asynchronous Data Loading

ECharts Asynchronous Data Loading

ECharts usually sets data in setOption. If we need to load data asynchronously, we can use tools such as jQuery, and fill in the data and configuration items via setOption after asynchronously fetching the data.

ECharts usually sets data in setOption. If we need to load data asynchronously, we can use tools such as jQuery, and fill in the data and configuration items via setOption after asynchronously fetching the data. json data:

echarts_test_data.json data:

{

   

"data_pie"

:

[

   

{

"value"

:

235

,

"name"

:

"Video Ads"

}

,

   

{

"value"

:

274

,

"name"

:

"Affiliate Ads"

}

,

   

{

"value"

:

310

,

"name"

:

"Email Marketing"

}

,

   

{

"value"

:

335

,

"name"

:

"Direct Access"

}

,

   

{

"value"

:

400

,

"name"

:

"Search Engine"

}

   

]

}

Example

var

myChart

=

echarts.

init

(

document.

getElementById

(

'main'

)

)

;

$.

get

(

'https://www.runoob.com/static/js/echarts_test_data.json'

,

function

(

data

)

{

    myChart.

setOption

(

{

        series

:

[

           

{

                name

:

'Traffic Source'

,

                type

:

'pie'

,

   

// Set the chart type to pie

                radius

:

'55%'

,

 

// The radius of the pie chart; the outer radius is 55% of the smaller of the container's height and width.

                data

:

data.

data_pie

           

}

       

]

   

}

)

}

,

'json'

)

Try it »

If asynchronous loading takes some time, we can add a loading effect; ECharts provides a simple loading animation by default. Just call the showLoading method to display it, and call the hideLoading method to hide it after the data finishes loading:

Example

var

myChart

=

echarts.

init

(

document.

getElementById

(

'main'

)

)

;

myChart.

showLoading

(

)

;

 

// Enable the loading effect

$.

get

(

'https://www.runoob.com/static/js/echarts_test_data.json'

,

function

(

data

)

{

    myChart.

hideLoading

(

)

;

 

// Hide the loading effect

    myChart.

setOption

(

{

        series

:

[

           

{

                name

:

'Traffic Source'

,

                type

:

'pie'

,

   

// Set the chart type to pie

                radius

:

'55%'

,

 

// The radius of the pie chart; the outer radius is 55% of the smaller of the container's height and width.

                data

:

data.

data_pie

           

}

       

]

   

}

)

}

,

'json'

)

Try it »

Dynamic Data Update

ECharts is data-driven; changes in data drive changes in the chart's presentation, so implementing dynamic data becomes extremely simple.

All data updates are done through setOption. You only need to periodically fetch data and fill it in via setOption, without worrying about what exactly changed. ECharts finds the difference between the two datasets and expresses the change through appropriate animations.

Example

var

base

=

new

Date

(

2014

,

9

,

3

)

;

var

oneDay

=

24

3600

1000

;

var

date

=

[

]

;

var

data

=

[

Math

.

random

(

)

150

]

;

var

now

=

new

Date

(

base

)

;

function

addData

(

shift

)

{

    now

=

[

now.

getFullYear

(

)

,

now.

getMonth

(

)

1

,

now.

getDate

(

)

]

.

join

(

'/'

)

;

    date.

push

(

now

)

;

    data.

push

(

(

Math

.

random

(

)

0.4

)

10

data

[

data.

length

1

]

)

;

   

if

(

shift

)

{

        date.

shift

(

)

;

        data.

shift

(

)

;

   

}

    now

=

new

Date

(

new

Date

(

now

)

oneDay

)

;

}

for

(

var

i

=

1

;

i

<

100

;

i

++

)

{

    addData

(

)

;

}

option

=

{

    xAxis

:

{

        type

:

'category'

,

        boundaryGap

:

false

,

        data

:

date

   

}

,

    yAxis

:

{

        boundaryGap

:

[

0

,

'50%'

]

,

        type

:

'value'

   

}

,

    series

:

[

       

{

            name

:

'Transaction'

,

            type

:

'line'

,

            smooth

:

true

,

            symbol

:

'none'

,

            stack

:

'a'

,

            areaStyle

:

{

                normal

:

{

}

           

}

,

            data

:

data

       

}

   

]

}

;

setInterval

(

function

(

)

{

    addData

(

true

)

;

    myChart.

setOption

(

{

        xAxis

:

{

            data

:

date

       

}

,

        series

:

[

{

            name

:

'Transaction'

,

            data

:

data

       

}

]

   

}

)

;

}

,

500

)

;

Try it »