使用Echarts-Python API生成Options并显示-PartI

Hello World

首先来尝试一下官方的例子:

1
2
3
4
5
6
7
8
# 在IPython环境下
from echarts import Echart, Legend, Bar, Axis
chart = Echart('GDP', 'This is a fake chart')
chart.use(Bar('China', [2, 3, 4, 5]))
chart.use(Legend(['GDP']))
chart.use(Axis('category', 'bottom', data=['Nov', 'Dec', 'Jan', 'Feb']))
chart.plot()

兼容IPython

常用图表: 饼图,柱状图,折线图和散点图

饼图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from echarts import Echart, Legend, Bar, Axis, Pie
chart = Echart('Map Picks in MLG', 'In group A')
chart.use(Pie('Map Picks in MLG',
[{'value': 2, 'name': 'de_dust2'},
{'value': 3, 'name': 'de_cache'},
{'value': 12, 'name': 'de_mirage'},
{'value': 9, 'name': 'de_inferno'}
],
radius=["50%", "70%"])
)
chart.use(Legend(["de_dust2", "de_cache", "de_mirage", "de_inferno"]))
del chart.json["xAxis"]
del chart.json["yAxis"]
chart.plot()

结果:

柱状图:

1
2
3
4
5
6
7
8
from echarts import Echart, Legend, Bar, Axis
chart = Echart("The Winrate of Tyloo and VG in this year", "The data is faked")
chart.use(Bar("Tyloo", ["50", "72", "64", "42", "30", "72"], ))
chart.use(Bar("VG", ["55", "48", "56", "63", "70", "55"]))
chart.use(Axis("category", "bottom", "map", ["de_dust2", "de_cache", "de_mirage", "de_cbble", "de_nuke", "de_cbble"]))
chart.use(Legend(["Tyloo", "VG"], position=('center', "bottom")))
chart.json
chart.plot()

散点图:

1
2
3
4
5
6
7
8
import numpy
from echarts import Echart, Legend, Scatter, Axis
data = numpy.random.uniform(0, 100, 100).reshape(50, 2).tolist()
chart = Echart("Random Number Distribute", "just a rundom number")
for x in data:
chart.use(Scatter("{}".format(numpy.random.uniform(0, 100, 1).tolist()[0]), [x]))
chart.use(Legend(["data"]))
chart.plot()

结果:

折线图

1
2
3
4
5
6
7
8
9
10
11
12
13
from echarts import Echart, Legend, Line, Axis, Tooltip
import numpy as np
import math
data = [[round(t, 4) for t in np.random.uniform(15 * x, 25 * (x + 1), 7).tolist()] for x in range(4)]
chart = Echart("The expression level in four organsim", "random data")
organism = ["Spleen", "Skin", "HK", "Jill"]
for x in range(4):
chart.use(Line(organism[x], data[x]))
chart.use(Axis('category', 'bottom', data=["0H", "6H", "12H", "2Day", "3Day", "5Day", "7Day"], boundaryGap=False))
chart.use(Axis('value', 'left'))
chart.use(Legend(data=["Spleen", "Skin", "HK", "Jill"], position=('center', 'bottom')))
chart.use(Tooltip())
chart.plot()

结果:

问题:

  • 嵌入IFrame大小问题width:, height: ?
  • axis 无法隐藏问题,特别是在饼图中。
  • 没有代码提示。
  • 交互设置困难。