Python Charts库(Highcharts API的封装)

个人博客
charts库实际是对调用Highcharts API 进行封装,通过python生成Highcharts脚本

Highcharts中文网:http://v1.hcharts.cn/demo/index.php?p=10

Highcharts官网:http://api.highcharts.com/highcharts/title

http://nbviewer.jupyter.org/github/arnoutaertgeerts/python-highcharts/blob/master/Tutorial.ipynb#Data-configuration

 

中文API文档

https://api.hcharts.cn/highcharts

 

1.安装


import charts
Server running in the folder /Users/TiM/PycharmProjects/58 at 127.0.0.1:61664 这个目录是你当前目录
如果报错,则需要
import sys
print(sys.path)
找到类似下面的路径
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages'
然后进入目录下的chart目录,拷贝下图这些文件替换

1.If you want to plot a single series, you can use the name argument:

charts.plot(data, name='My list')

show = 'inline',如果没有这个选项,会开启一个网页展示图表
 

2.If you want to plot multiple series, you have to use the series format. This format is a dictionary containing two properties: data and name:

charts.plot(dict(data=data, name='My series'))

 

 

The data itself has to be one of these two options:

 

  1. A single list (or numpy array):
    data = [1,2,5,9,6,3,4,8]
  2. A list containing x,y pairs:
    data = [[1,8],[2,7],[3,4],[4,3],[5,9],[6,0],[7,10],[8,5]]
data = [[1,8],[2,7],[3,4],[4,3],[5,9],[6,0],[7,10],[8,5]]
表示第一个值 x坐标1,y坐标8
如果不是x,y pairs形式的,那么默认第一个值x值为0 y值为data[0]
将两个seria一起展示就会发现区别
绿色的是List data 他从x坐标0开始. 黑色是x,y pair形式的,第一个值从x=1开始

 

可以再serie中指定color颜色,type显示的形式(column:柱状图,bar:横向柱状图,line:曲线,area:范围,spline:曲线,scatter:点状,pie:饼状图)

 

type也可以在plot中指定,此时会控制所有seria

 

series =  [{
    'type': 'pie',
    'name': 'Browser share',
    'data': [
        ['Firefox',   45.0],
        ['IE',       26.8],
        {
            'name': 'Chrome',
            'y': 12.8,
            'sliced': True,#控制是否脱离整个pie
            'selected': True #http://api.highcharts.com/highcharts/plotOptions.pie
        },
        ['Safari',    8.5],
        ['Opera',     6.2],
        ['Others',   0.7]
    ]
}]

charts.plot(series, options={'title': {'text': 'A pie chart'}}, show='inline')
 
混合
更多options选项示例

 

 
 
display选项可以用于选择需要展示的seria
 
subtitle子标题
xAxis yAxis 控制xy轴的设置

在一个例子

series = [
    {
    'name': 'OS X',
    'data': [11,2,3,4],
    'type': 'line',
    'y':5
}, {
    'name': 'Ubuntu',
    'data': [8,5,6,7],
    'type': 'line',
    'color':'#ff0066'
}, {
    'name': 'Windows',
    'data': [12,6,7,2],
    'type': 'line'
}, {
    'name': 'Others',
    'data': [29,24,68,23],
    'type': 'line'
}
         ]

options = {
    'chart'   : {'zoomType':'xy'},
    'title'   : {'text': 'Monthly Average Temperature'},
    'subtitle': {'text': 'Source: WorldClimate.com'},
    'xAxis'   : {'categories': ['周一', '周二', '周三', '周四']},
    'yAxis'   : {'title': {'text': '数量'}}
    }

charts.plot(series, options=options,show='inline')

 

 

 

参数文档

 

http://api.highcharts.com/highcharts/plotOptions

 

 


 

series = [{
    'name': 'John',
    'data': [5, 3, 4, 7, 2]
}, {
    'name': 'Jane',
    'data': [2, -2, -3, 2, 1]
}, {
    'name': 'Joe',
    'data': [3, 4, 4, -2, 5]
}]

#options = dict(title=dict(text='Area chart'))
options = {
    'title': {'text': 'A chart with two lines, wow!'},  #图标的标题
    'height':400, #整个图标的高度
    'chart':{'zoomType':'xy'}, #zoom是缩放,可以是 x,y或 xy
    'plotOptions': {
        'spline': { #这个area是你的type
            'dataLabels': {'enabled': True,
                           'shadow':True,
                           'backgroundColor': 'rgba(252, 255, 197, 0.7)',#lable颜色,这里是淡黄色
                           'borderRadius': 10, #圆角,默认是0,lable是方的,这里10已经比较园了
                           'borderWidth': 1,#不清楚
                           'padding': 5, #When either the borderWidth or 
                                        #the backgroundColor is set, this is the padding within the box
                                        #反正就是变大了
                           'style': {'fontWeight': 'bold'}
                          } #在图上直接显示数值,lable
        }
    }
}

charts.plot(series, options=options, show='inline', type='spline')
其实就是照着这个抄

Python Charts

分享到:
评论加载中,请稍后...
创APP如搭积木 - 创意无限,梦想即时!
回到顶部