diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index bd17dc9a3..eb27dd501 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,5 +1,19 @@ + + +**问题** + + + +**运行环境(系统环境及 pyecharts 版本)** + + + +**代码及截图** + + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 137bf0f50..e00532572 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,6 +1,14 @@ + + +本次 PR 内容, + + diff --git a/docs/zh-cn/changelog.md b/docs/zh-cn/changelog.md index 4441bcf04..c6193e523 100644 --- a/docs/zh-cn/changelog.md +++ b/docs/zh-cn/changelog.md @@ -1,7 +1,9 @@ # 版本日志 * ### version 0.5.5(dev) - * TODO + + ### Added + * [issue#565](https://github.com/pyecharts/pyecharts/issues/565) Geolines 图数据项可以新增数值维度 * ### version 0.5.4 - 2018.05.15(current) diff --git a/docs/zh-cn/charts.md b/docs/zh-cn/charts.md index 578a2e22d..6c249fcd6 100644 --- a/docs/zh-cn/charts.md +++ b/docs/zh-cn/charts.md @@ -1137,7 +1137,7 @@ add(name, data, * name -> str 图例名称 * data -> [list], 包含列表的列表 - 数据项,数据中,每一行是一个『数据项』,每一列属于一个『维度』。每一行包含两个数据, 如 ["广州", "北京"],则指定从广州到北京。 + 数据项,数据中,每一行是一个『数据项』,每一列属于一个『维度』。每一行包含两个或三个数据,如 ["广州", "北京"] 或 ["广州", "北京",100],则指定从广州到北京。第三个值用于表示该 line 的数值,该值可省略。 * maptype -> str 地图类型。 从 v0.3.2+ 起,地图已经变为扩展包,支持全国省份,全国城市,全国区县,全球国家等地图,具体请参考 [地图自定义篇](zh-cn/customize_map) * symbol -> str @@ -1218,6 +1218,26 @@ geolines.render() ``` ![geolines-1](https://user-images.githubusercontent.com/19553554/35082102-fd8d884a-fc52-11e7-9e40-5f94098d4493.gif) +指定数值 +```python +from pyecharts import GeoLines, Style + +data_guangzhou = [ + ["广州", "上海", 10], + ["广州", "北京", 20], + ["广州", "南京", 30], + ["广州", "重庆", 40], + ["广州", "兰州", 50], + ["广州", "杭州", 60], +] +lines = GeoLines("GeoLines 示例", **style.init_style) +lines.add( + "从广州出发", data_guangzhou, tooltip_formatter="{a} : {c}", **style_geo +) +lines.render() +``` +![](https://user-images.githubusercontent.com/19553554/40048098-eaa7b3aa-5863-11e8-98cd-dcd8526fe820.gif) + 多例模式 ```python from pyecharts import GeoLines, Style diff --git a/pyecharts/charts/geolines.py b/pyecharts/charts/geolines.py index 26266637f..3135f87cd 100644 --- a/pyecharts/charts/geolines.py +++ b/pyecharts/charts/geolines.py @@ -40,8 +40,9 @@ def add( :param name: 系列名称,用于 tooltip 的显示,legend 的图例筛选。 :param data: - 数据项,数据中,每一行是一个『数据项』,每一列属于一个『维度』。每一行包含两个数据, - 如 ["广州", "北京"],则指定从广州到北京。 + 数据项,数据中,每一行是一个『数据项』,每一列属于一个『维度』。每一行包含两个或 + 三个数据,如 ["广州", "北京"] 或 ["广州", "北京",100],则指定从广州到北京。第 + 三个值用于表示该 line 的数值,该值可省略。 :param maptype: 地图类型。 从 v0.3.2+ 起,地图已经变为扩展包,支持全国省份,全国城市,全国区县, 全球国家等地图,具体请参考 [地图自定义篇](zh-cn/customize_map) @@ -88,8 +89,15 @@ def add( geo_effect_symbol = SYMBOL["plane"] _data_lines, _data_scatter = [], [] - for d in data: - _from_name, _to_name = d + for element in data: + assert len(element) >= 2 + _line_value = None + + if len(element) == 2: + _from_name, _to_name = element + else: + _from_name, _to_name, _line_value = element + _from_coordinate = self.get_coordinate( _from_name, raise_exception=True ) @@ -100,6 +108,7 @@ def add( { "fromName": _from_name, "toName": _to_name, + "value": _line_value, "coords": [_from_coordinate, _to_coordinate], } ) @@ -161,6 +170,7 @@ def add( "symbolSize": 10, "data": _data_scatter, "label": chart["label"], + "tooltip": {"formatter": "{b}"}, } ) diff --git a/pyecharts/echarts/option.py b/pyecharts/echarts/option.py index dd215bd3a..640fc3606 100644 --- a/pyecharts/echarts/option.py +++ b/pyecharts/echarts/option.py @@ -301,6 +301,37 @@ def xy_axis( x 坐标轴标签字体大小 :param xaxis_label_textcolor: x 坐标轴标签字体颜色 + :param xaxis_formatter: + x 轴标签格式器,如 '天',则 x 轴的标签为数据加'天'(3 天,4 天),默认为 "" + xaxis_formatter -> function + ```python + def label_formatter(params): + return params.value + ' [Good!]' + ``` + 回调函数格式,更多内容请参考 [高级用法篇](zh-cn/advanced) + ``` + (params: Object|Array) => string + 参数 params 是 formatter 需要的单个数据集。格式如下: + { + componentType: 'series', + // 系列类型 + seriesType: string, + // 系列在传入的 option.series 中的 index + seriesIndex: number, + // 系列名称 + seriesName: string, + // 数据名,类目名 + name: string, + // 数据在传入的 data 数组中的 index + dataIndex: number, + // 传入的原始数据项 + data: Object, + // 传入的数据值 + value: number|Array, + // 数据图形的颜色 + color: string, + } + ``` :param yaxis_margin: y 轴刻度标签与轴线之间的距离。默认为 8 :param yaxis_name_size: @@ -342,6 +373,35 @@ def xy_axis( y 坐标轴标签字体颜色 :param yaxis_formatter: y 轴标签格式器,如 '天',则 y 轴的标签为数据加'天'(3 天,4 天),默认为 "" + yaxis_formatter -> function + ```python + def label_formatter(params): + return params.value + ' [Good!]' + ``` + 回调函数格式,更多内容请参考 [高级用法篇](zh-cn/advanced) + ``` + (params: Object|Array) => string + 参数 params 是 formatter 需要的单个数据集。格式如下: + { + componentType: 'series', + // 系列类型 + seriesType: string, + // 系列在传入的 option.series 中的 index + seriesIndex: number, + // 系列名称 + seriesName: string, + // 数据名,类目名 + name: string, + // 数据在传入的 data 数组中的 index + dataIndex: number, + // 传入的原始数据项 + data: Object, + // 传入的数据值 + value: number|Array, + // 数据图形的颜色 + color: string, + } + ``` :param is_convert: 是否交换 x 轴与 y 轴 :param is_xaxis_inverse: diff --git a/test/fixtures/geolines.json b/test/fixtures/geolines.json index d45b664d4..84be47f8a 100644 --- a/test/fixtures/geolines.json +++ b/test/fixtures/geolines.json @@ -1,289 +1,295 @@ { - "backgroundColor": "#404a59", + "backgroundColor": "#404a59", "color": [ - "#a6c84c", - "#ffa022", - "#46bee9", - "#a6c84c", - "#ffa022", - "#46bee9", - "#c23531", - "#2f4554", - "#61a0a8", - "#d48265", - "#749f83", - "#ca8622", - "#bda29a", - "#6e7074", - "#546570", - "#c4ccd3", - "#f05b72", - "#ef5b9c", - "#f47920", - "#905a3d", - "#fab27b", - "#2a5caa", - "#444693", - "#726930", - "#b2d235", - "#6d8346", - "#ac6767", - "#1d953f", - "#6950a1", - "#918597", + "#a6c84c", + "#ffa022", + "#46bee9", + "#a6c84c", + "#ffa022", + "#46bee9", + "#c23531", + "#2f4554", + "#61a0a8", + "#d48265", + "#749f83", + "#ca8622", + "#bda29a", + "#6e7074", + "#546570", + "#c4ccd3", + "#f05b72", + "#ef5b9c", + "#f47920", + "#905a3d", + "#fab27b", + "#2a5caa", + "#444693", + "#726930", + "#b2d235", + "#6d8346", + "#ac6767", + "#1d953f", + "#6950a1", + "#918597", "#f6f5ec" - ], + ], "geo": { "itemStyle": { "emphasis": { "areaColor": "#2a333d" - }, + }, "normal": { - "areaColor": "#323c48", + "areaColor": "#323c48", "borderColor": "#111" } - }, + }, "label": { "emphasis": { - "show": true, + "show": true, "textStyle": { "color": "#eee" } } - }, - "map": "china", + }, + "map": "china", "roam": true - }, + }, "legend": [ { "data": [ - "\u4ece\u5e7f\u5dde\u51fa\u53d1", + "\u4ece\u5e7f\u5dde\u51fa\u53d1", "\u4ece\u5317\u4eac\u51fa\u53d1" - ], - "left": "right", - "orient": "horizontal", - "selectedMode": "single", - "show": true, + ], + "left": "right", + "orient": "horizontal", + "selectedMode": "single", + "show": true, "textStyle": { - "color": "#eee", + "color": "#eee", "fontSize": 12 - }, + }, "top": "top" } - ], + ], "series": [ { "data": [ { "coords": [ [ - 113.23, + 113.23, 23.16 - ], + ], [ - 121.48, + 121.48, 31.22 ] - ], - "fromName": "\u5e7f\u5dde", + ], + "fromName": "\u5e7f\u5dde", "toName": "\u4e0a\u6d77" } - ], + ], "effect": { - "color": "#fff", - "period": 6, - "show": true, - "symbol": "path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z", - "symbolSize": 15, + "color": "#fff", + "period": 6, + "show": true, + "symbol": "path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z", + "symbolSize": 15, "trailLength": 0 - }, + }, "lineStyle": { "normal": { - "curveness": 0.2, - "opacity": 0.6, - "type": "solid", + "curveness": 0.2, + "opacity": 0.6, + "type": "solid", "width": 1 } - }, - "name": "\u4ece\u5e7f\u5dde\u51fa\u53d1", + }, + "name": "\u4ece\u5e7f\u5dde\u51fa\u53d1", "symbol": [ - "none", + "none", "arrow" - ], - "symbolSize": 12, - "type": "lines", + ], + "symbolSize": 12, + "type": "lines", "zlevel": 2 - }, + }, { - "coordinateSystem": "geo", + "coordinateSystem": "geo", "data": [ { - "name": "\u5e7f\u5dde", + "name": "\u5e7f\u5dde", "value": [ - 113.23, - 23.16, + 113.23, + 23.16, 0 ] - }, + }, { - "name": "\u4e0a\u6d77", + "name": "\u4e0a\u6d77", "value": [ - 121.48, - 31.22, + 121.48, + 31.22, 0 ] } - ], + ], "label": { "emphasis": { - "show": true, + "show": true, "textStyle": { "fontSize": 12 } - }, + }, "normal": { - "formatter": "{b}", - "position": "right", - "show": true, + "formatter": "{b}", + "position": "right", + "show": true, "textStyle": { - "color": "#eee", + "color": "#eee", "fontSize": 12 } } - }, - "name": "\u4ece\u5e7f\u5dde\u51fa\u53d1", - "symbolSize": 10, - "type": "scatter", + }, + "name": "\u4ece\u5e7f\u5dde\u51fa\u53d1", + "symbolSize": 10, + "tooltip": { + "formatter": "{b}" + }, + "type": "scatter", "zlevel": 2 - }, + }, { "data": [ { "coords": [ [ - 116.46, + 116.46, 39.92 - ], + ], [ - 121.48, + 121.48, 31.22 ] - ], - "fromName": "\u5317\u4eac", + ], + "fromName": "\u5317\u4eac", "toName": "\u4e0a\u6d77" } - ], + ], "effect": { - "color": "#fff", - "period": 6, - "show": true, - "symbol": "path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z", - "symbolSize": 15, + "color": "#fff", + "period": 6, + "show": true, + "symbol": "path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z", + "symbolSize": 15, "trailLength": 0 - }, + }, "lineStyle": { "normal": { - "curveness": 0.2, - "opacity": 0.6, - "type": "solid", + "curveness": 0.2, + "opacity": 0.6, + "type": "solid", "width": 1 } - }, - "name": "\u4ece\u5317\u4eac\u51fa\u53d1", + }, + "name": "\u4ece\u5317\u4eac\u51fa\u53d1", "symbol": [ - "none", + "none", "arrow" - ], - "symbolSize": 12, - "type": "lines", + ], + "symbolSize": 12, + "type": "lines", "zlevel": 3 - }, + }, { - "coordinateSystem": "geo", + "coordinateSystem": "geo", "data": [ { - "name": "\u5317\u4eac", + "name": "\u5317\u4eac", "value": [ - 116.46, - 39.92, + 116.46, + 39.92, 0 ] - }, + }, { - "name": "\u4e0a\u6d77", + "name": "\u4e0a\u6d77", "value": [ - 121.48, - 31.22, + 121.48, + 31.22, 0 ] } - ], + ], "label": { "emphasis": { - "show": true, + "show": true, "textStyle": { "fontSize": 12 } - }, + }, "normal": { - "formatter": "{b}", - "position": "right", - "show": true, + "formatter": "{b}", + "position": "right", + "show": true, "textStyle": { - "color": "#eee", + "color": "#eee", "fontSize": 12 } } - }, - "name": "\u4ece\u5317\u4eac\u51fa\u53d1", - "symbolSize": 10, - "type": "scatter", + }, + "name": "\u4ece\u5317\u4eac\u51fa\u53d1", + "symbolSize": 10, + "tooltip": { + "formatter": "{b}" + }, + "type": "scatter", "zlevel": 3 } - ], - "series_id": "1", + ], + "series_id": "1", "title": [ { - "left": "center", + "left": "center", "subtextStyle": { "fontSize": 12 - }, - "text": "GeoLines \u793a\u4f8b", + }, + "text": "GeoLines \u793a\u4f8b", "textStyle": { "fontSize": 18 - }, + }, "top": "#fff" } - ], + ], "toolbox": { "feature": { "dataView": { "show": true - }, + }, "restore": { "show": true - }, + }, "saveAsImage": { - "show": true, + "show": true, "title": "\u4e0b\u8f7d\u56fe\u7247" } - }, - "left": "95%", - "orient": "vertical", - "show": true, + }, + "left": "95%", + "orient": "vertical", + "show": true, "top": "center" - }, + }, "tooltip": { "axisPointer": { "type": "line" - }, - "backgroundColor": "rgba(50,50,50,0.7)", - "borderColor": "#333", - "borderWidth": 0, + }, + "backgroundColor": "rgba(50,50,50,0.7)", + "borderColor": "#333", + "borderWidth": 0, "textStyle": { "fontSize": 14 - }, - "trigger": "item", + }, + "trigger": "item", "triggerOn": "mousemove|click" } } \ No newline at end of file diff --git a/test/test_geolines.py b/test/test_geolines.py index ba556a197..048f2bff0 100644 --- a/test/test_geolines.py +++ b/test/test_geolines.py @@ -51,7 +51,6 @@ def test_geolines(): lines = GeoLines("GeoLines 示例", **style.init_style) lines.add("从广州出发", data_guangzhou, **style_geo) lines.add("从北京出发", data_beijing, **style_geo) - lines.print_echarts_options() lines.render() @@ -92,3 +91,21 @@ def test_with_full_example(): lines.add_coordinate("A市", 119.3, 26.08) lines.add("从广州出发", line_data, **style_geo) lines.render() + + +def test_geolines_value_and_formatter(): + data_guangzhou = [ + ["广州", "上海", 10], + ["广州", "北京", 20], + ["广州", "南京", 30], + ["广州", "重庆", 40], + ["广州", "兰州", 50], + ["广州", "杭州", 60], + ] + lines = GeoLines("GeoLines 示例", **style.init_style) + lines.add( + "从广州出发", data_guangzhou, tooltip_formatter="{a} : {c}", **style_geo + ) + html_content = lines._repr_html_() + assert '"value": 10' in html_content + assert '"value": 60' in html_content