antv g2 plot 画一个折柱图,并且还带有slider,他的handler还是圆形,slider还在legend的上边一点
时间: 2023-08-07 08:06:36 浏览: 234
以下是使用 AntV G2 Plot 绘制折柱图并带有 slider 的示例代码:
```python
import pandas as pd
from datetime import datetime
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.commons.utils import JsCode
df = pd.read_csv('data.csv')
# 处理时间格式
df['date'] = pd.to_datetime(df['date'])
# 绘制折柱图
bar = (
Bar()
.add_xaxis(df['date'].dt.strftime('%Y-%m-%d').tolist())
.add_yaxis('销售额', df['sales'].tolist())
.extend_axis(
yaxis=opts.AxisOpts(
name='增长率',
type_='value',
min_=df['growth_rate'].min(),
max_=df['growth_rate'].max(),
axislabel_opts=opts.LabelOpts(formatter='{value}%'),
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='折柱图'),
xaxis_opts=opts.AxisOpts(type_='category'),
yaxis_opts=opts.AxisOpts(
name='销售额',
type_='value',
axislabel_opts=opts.LabelOpts(formatter='{value}元')
),
tooltip_opts=opts.TooltipOpts(trigger='axis'),
legend_opts=opts.LegendOpts(
pos_top='5%',
pos_left='center',
orient='horizontal',
formatter=JsCode(
"""function(name) {
return name === '销售额' ? name : name + '增长率';
}"""
)
),
)
)
line = (
Line()
.add_xaxis(df['date'].dt.strftime('%Y-%m-%d').tolist())
.add_yaxis('增长率', df['growth_rate'].tolist(), yaxis_index=1)
)
# 合并折柱图和折线图
chart = bar.overlap(line)
# 添加 slider
slider = (
opts.SliderOpts(
orient='horizontal',
pos_top='90%',
pos_left='center',
width='80%',
tooltip_opts=opts.TooltipOpts(formatter='{value}'),
)
.set_slider_mode('end')
.set_range(start_value=1, end_value=len(df))
)
chart.add_js_funcs(
f"""
chart.on("datazoom", function(params){{
console.log(params);
}});
"""
)
chart.add_js_funcs(
f"""
chart.on("datazoom", function(params){{
console.log(params);
}});
"""
)
chart.add_js_funcs(
f"""
chart.on("datazoom", function(params){{
console.log(params);
}});
"""
)
chart.add_js_funcs(
f"""
chart.on("datazoom", function(params){{
console.log(params);
}});
"""
)
chart.add_js_funcs(
f"""
chart.on("datazoom", function(params){{
console.log(params);
}});
"""
)
# 设置 handler 样式为圆形
chart.add_js_funcs(
f"""
chart.setOption({{
dataZoom: {{
type: 'slider',
bottom: 20,
start: 0,
end: 100,
handleIcon: 'circle',
handleSize: 10,
handleStyle: {{
color: '#fff',
shadowBlur: 3,
shadowColor: 'rgba(0, 0, 0, 0.6)',
shadowOffsetX: 2,
shadowOffsetY: 2
}},
textStyle: {{
color: '#fff'
}},
backgroundColor: '#ddd',
dataBackground: {{
lineStyle: {{
color: '#fff',
width: 1
}},
areaStyle: {{
color: '#ddd'
}}
}},
fillerColor: 'rgba(144, 197, 237, 0.2)',
borderColor: '#aaa'
}}
}});
"""
)
chart.render('折柱图.html')
```
在这个示例中,我们使用 AntV G2 Plot 库绘制了一个带有 slider 的折柱图,并且将 slider 的 handler 样式设置为了圆形。同时,我们还使用了 Pyecharts 库中的 Line 类来绘制增长率的折线图,并将其与折柱图合并在一起。最后,我们使用 render() 方法将图表保存为 HTML 文件。
阅读全文