ECharts数据可视化(第三章实训)

实训

实训销售经理能力对比分析
1.训练要点(1)掌握直角坐标系下的网格及坐标轴的配置方法。(2)掌握标题组件与图例组件的配置方法。
(3)掌握工具箱组件与详情提示框组件的配置方法。
(4)掌握标记点和标记线的配置方法。
2.需求说明
“销售经理能力考核表. xlsx ”文件上的数据为某公司对王斌、刘倩、袁波3个销售代表从多方面进行考核得到的评分数据,评分项具体包括销售、沟通、服务、协作、培训和组织。绘制柱状图,并配置直角坐标系下的网格及坐标轴、标题组件、图例组件、工具箱组
件、详情提示框组件、标记点和标记线,实现更清晰、更便捷地分析销售经理的能力。
3.实现思路及步骤
(1)在 Eclipse 中创建销售经理能力对比分析. html 文件。(2)绘制柱状图。首先,在销售经理能力对比分析. html 文件中引人 echarts . js 库文件。其次,准备一个指定了大小的 div 容器,并使用init0方法初始化容器。最后,设置柱状图的配置项、“销售经理能力考核表. xlsx ”数据,完成柱状图绘制。
(3)配置网格及坐标轴。利用网格组件为绘制的柱状图设置网格边框和背景颜色,并利用坐标轴组件为标轴设置坐标轴刻度标记和坐标轴文本标签。
(4)配置标题组件和图例组件。利用标题组件在绘制的柱状图正上方设置红色字体的主标题“销售经理能力对比分析”,并利用图例组件在柱状图的左上角配置图例。
(5)配置工具箱组件和详情提示框组件。利用工具箱组件在绘制的柱状图右上角配置含有边框的工具箱,并利用详情提示框组件为绘制的柱状图配置详情提示框。
(6)配置标记点和标记线。利用标记点标记出考核评分中的最高分和最低分,并利用
际记线标记出考核评分中的平均分。

 

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<script src="js/echarts.js"></script>
</head>

<body>
	<div id="main" style="width: 800px; height: 600px"></div>
	<script type="text/javascript">
		var myChart = echarts.init(document.getElementById("main"));
		var mytextStyle = {
			color: 'blue',
			fontStyle: 'normal',
			fontWeight: 'normal',
			fontFamily: '黑体',
			fontSize: 14,
		};
		var option = {
			color: ['palegreen', 'turquoise', 'skyblue'],
			title: {
				text: '销售经理能力对比分析',
				target: 'blank',
				left: "center",
				top: "top",
				textStyle: mytextStyle,
				padding: 5,
				itemGap: 10,
				zlevel: 0,
				z: 2,
				left: '40%',
				top: '10',
				right: 'auto',
				bottom: 'auto',
				backgroundColor: 'yellow',
				borderColor: '#ccc',
				borderWidth: 2,
				shadowColor: 'red',
				shadowOffsetX: 0,
				shadowOffsetY: 0,
				shadowBlur: 10,
			},
			grid: {
				show: true,
				x: 130,
				y: 66,
				x2: 100,
				y2: 100,
				width: '80%', height: '70%',
				borderColor: 'red',
				backgroundColor: '#f7f7f7',
			},
			tooltip: {
				trigger: 'axis',
				axisPointer:
				{
					type: 'shadow',
					lineStyle: {
						color: '#48b', width: 2, type: 'solid'
					},
					crossStyle: {
						color: '#1e90ff', width: 1, type: 'dashed'
					},
					shadowStyle: {
						color: 'rgba(150,150,150,0.2)', width: 'auto', type: 'default'
					}
				},
				showDelay: 0, hideDelay: 0, transitionDuration: 0,
				backgroundColor: 'rgba(0,222,0,0.5)',
				borderColor: '#f50', borderRadius: 8, borderWidth: 2,
				padding: 10,
				position: function (p) {
					return [p[0] + 10, p[1] - 10];
				},
				textStyle: {
					color: ' blue ', decoration: 'none', fontFamily: 'sans-serif',
					fontSize: 15, fontStyle: ' normal ', fontWeight: 'bold'
				},
				formatter: function (params, ticket, callback) {
					console.log(params)
					var res = '详情提示框 : <br/>' + params[0].name;
					for (var i = 0, l = params.length; i < l; i++) {
						res += '<br/>' + params[i].seriesName + ' : ' + params[i].value;
					}
					setTimeout(function () {
						callback(ticket, res);
					}, 500)
					return 'loading';
				}
			},
			legend: {
				orient: 'horizontal',
				backgroundColor: '#eee',
				borderColor: 'rgba(178,34,34,0.8)',
				borderWidth: 4,
				padding: 10,
				itemGap: 20,
				textStyle: { color: 'red' },
				data: ['王斌', '刘倩', '袁波'],
				x: 'left',
				y: 'top'
			},
			toolbox: {
				show: true,
				borderWidth: 1.5,
				feature: {
					mark: { show: true }, dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar', 'stack', 'tiled'] },
					restore: { show: true }, saveAsImage: { show: true }
				}
			},
			xAxis:[
			{   
					type: 'category', position: 'bottom',
					boundaryGap: true, show: true,
					axisLine: { 
						lineStyle: {
							color: 'green', type: 'solid', width: 2
						}
					},
					axisTick: { 
						show: true, length: 10,
						lineStyle: {
							color: 'red', type: 'solid', width: 2
						}
					},
					axisLabel: {  
						show: true, interval: 'auto',
						rotate: 0, margin: 8,
						formatter: '{value}月',
						textStyle: {
							color: 'blue', fontFamily: 'sans-serif',
							fontSize: 15, fontStyle: 'italic', fontWeight: 'bold'
						}
					},
					splitLine: {  
						show: true,
						lineStyle: {
							color: '#483d8b', type: 'dashed', width: 1
						}
					},
					splitArea: {  
						show: true,
						areaStyle: {
							color: ['rgba(144,238,144,0.3)', 'rgba(135,200,250,0.3)']
						}
					},
					data: [
					"销售", "沟通", "服务", "协作", "培训", "组织"
					]
				}
			],
			yAxis:[
			{ 
					type: 'value', position: 'left',
					boundaryGap: [0, 0.1],
					axisLine: {  
						show: true,
						lineStyle: {
							color: 'red', type: 'dashed', width: 2
						}
					},
					axisTick: { 
						show: true,
						length: 10,
						lineStyle: {
							color: 'green', type: 'solid', width: 2
						}
					},
					axisLabel: { 
						show: true, interval: 'auto', rotate: -45, margin: 18,
						formatter: '{value}',
						textStyle: {
							color: '#1e90ff', fontFamily: 'verdana',
							fontSize: 10, fontStyle: 'normal',
							fontWeight: 'bold'
						}
					},
					splitLine: {  
						show: true,
						lineStyle: {
							color: '#483d8b', type: 'dotted', width: 2
						}
					},
					splitArea: {  
						show: true,
						areaStyle: {
							color: ['rgba(205,92,92,0.3)', 'rgba(255,215,0,0.3)']
						}
					}
				}
			],
			series: [
				{
					name: '王斌',
					type: 'bar',
					data: [87.50, 87.50, 90.00, 91.25, 85.00, 87.50],
					markPoint: {
						data: [
							{
								type: 'min', name: '最小值', symbol: 'arrow', symbolSize: 20,
								itemStyle: {
									normal: { color: 'blue' }
								},
							},
						]
					},
					markLine: {
						data: [
							{
								type: 'average', name: '平均值',
								itemStyle:
								{
									normal: { borderType: 'dotted', color: 'darkred' }
								},
							}],
					},
				},
				{
					name: '刘倩',
					type: 'bar',
					data: [90.00, 88.75, 85.00, 87.50, 88.75, 91.25],
					markPoint: {
						data: [
							{
								type: 'min', name: '最小值', symbol: 'arrow', symbolSize: 20,
								itemStyle: {
									normal: { color: 'blue' }
								},
							},
						]
					},
					markLine: {
						data: [
							{
								type: 'average', name: '平均值',
								itemStyle:
								{
									normal: { borderType: 'dotted', color: 'darkred' }
								},
							}],
					},
				},
				{
					name: '袁波',
					type: 'bar',
					data: [92.50, 91.25, 88.75, 92.50, 91.25, 88.75],
					markPoint: {
						data: [
							{
								type: 'max', name: '最大值', symbol: 'diamond', symbolSize: 25,
								itemStyle: {
									normal: { color: 'red' }
								},
							},
						]
					},
					markLine: {
						data: [
							{
								type: 'average', name: '平均值',
								itemStyle:
								{
									normal: { borderType: 'dotted', color: 'darkred' }
								},
							}],
					},
				},
			]
		};
		myChart.setOption(option);
	</script>
</body>

</html>

 或者是下面这个(样式比较第一)

 

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <!--引入ECharts脚本-->
    <script src="js/echarts.js"></script>
</head>

<body>
    <!---为ECharts准备一个具备大小(宽高)的DOM-->
    <div id="main" style="width: 600px; height: 400px"></div>
    <script type="text/javascript">
        //基于准备好的DOM,初始化ECharts图表
        var myChart = echarts.init(document.getElementById("main"));
        //指定图表的配置项和数据
        var option = {
            title: {  //配置标题组件,包含主标题和副标题
                text: '销售经理能力对比分析',
				left: "center",  //设置组件离容器左侧的距离
                top: "top",  //设置组件离容器上侧的距离
                textStyle: {   //设置标题的颜色
					color: 'red'
					}
            },
			grid: {  //配置网格组件
                show: true,  //设置网格组件是否显示
                width: '80%', height: '70%',  //设置网格的宽度和高度
                backgroundColor: '#f7f7f7',  //设置背景整个网格的颜色
            },
            tooltip: {//配置提示框组件
					trigger: 'axis',
				axisPointer:
				{
					type: 'shadow',
					lineStyle: {
						color: '#48b', width: 2, type: 'solid'
					},
					crossStyle: {
						color: '#1e90ff', width: 1, type: 'dashed'
					},
					shadowStyle: {
						color: 'rgba(150,150,150,0.2)', width: 'auto', type: 'default'
					}
				},
				showDelay: 0, hideDelay: 0, transitionDuration: 0,
				backgroundColor: 'rgba(0,222,0,0.5)',
				borderColor: '#f50', borderRadius: 8, borderWidth: 2,
				padding: 10,
				position: function (p) {
					//位置回调
					//console.log && console.log(p);
					return [p[0] + 10, p[1] - 10];
				},
				textStyle: {
					color: ' blue ', decoration: 'none', fontFamily: 'sans-serif',
					fontSize: 15, fontStyle: ' normal ', fontWeight: 'bold'
				},
				formatter: function (params, ticket, callback) {
					console.log(params)
					var res = '详情提示框 : <br/>' + params[0].name;
					for (var i = 0, l = params.length; i < l; i++) {
						res += '<br/>' + params[i].seriesName + ' : ' + params[i].value;
					}
					setTimeout(function () {
						//仅为了模拟异步回调
						callback(ticket, res);
					}, 500)
					return 'loading';
				}
				//formatter: "Template formatter: <br/>{b}<br/>{a}:{c}<br/>{a1}:{c1}"
			},
            legend: {  //配置图例组件,一个echarts图表中可以存在多个图例组件
                data: ['王斌', '刘倩', '袁波'],
				x:'left',
				y:'top'
            },
			toolbox: {  //配置工具箱组件
				show: true,
				borderWidth: 1.5,
				feature: {
					mark: { show: true }, dataView: { show: true, readOnly: false },
					magicType: { show: true, type: ['line', 'bar', 'stack', 'tiled'] },
					restore: { show: true }, saveAsImage: { show: true }
				}
			},
            xAxis: {  //配置x轴坐标系
                data: ["销售", "沟通", "服务", "协作", "培训", "组织"],
				splitLine: {
                show: true
              }
            },
            yAxis: {},  //配置y轴坐标系
            series: [
				{  //配置系列列表,每个系列通过type控制该系列图表类型
					name: '王斌',
					type: 'bar',  //设置柱状图
					data: [87.50, 87.50, 90.00, 91.25, 85.00, 87.50],
					markPoint: {  //设置标记点
					data: [
						{
							type: 'min', name: '最小值', symbol: 'arrow', symbolSize: 20,
							itemStyle: {  //设置标记点的样式
								normal: { color: 'blue' }
							},
						},
					]
				},
					markLine: {  //设置标记线
					data: [
						{
							type: 'average', name: '平均值',
							itemStyle:  //设置标记点的样式
							{
								normal: { borderType: 'dotted', color: 'darkred' }
							},
						}],
				},
				},
				{
                    name: '刘倩',
                    type: 'bar',
                    data: [90.00, 88.75, 85.00, 87.50, 88.75, 91.25],
					markPoint: {  //设置标记点
					data: [
						{
							type: 'min', name: '最小值', symbol: 'arrow', symbolSize: 20,
							itemStyle: {  //设置标记点的样式
								normal: { color: 'blue' }
							},
						},
					]
				},
					markLine: {  //设置标记线
					data: [
						{
							type: 'average', name: '平均值',
							itemStyle:  //设置标记点的样式
							{
								normal: { borderType: 'dotted', color: 'darkred' }
							},
						}],
				},
				},
				{
                    name: '袁波',
                    type: 'bar',
                    data: [92.50, 91.25, 88.75, 92.50, 91.25, 88.75],
					markPoint: {  //设置标记点
					data: [
						{
							type: 'max', name: '最大值', symbol: 'diamond', symbolSize: 25,
							itemStyle: {  //设置标记点的样式
								normal: { color: 'red' }
							},
						},
					]
				},
					markLine: {  //设置标记线
					data: [
						{
							type: 'average', name: '平均值',
							itemStyle:  //设置标记点的样式
							{
								normal: { borderType: 'dotted', color: 'darkred' }
							},
						}],
				},
				},
			]
        };
        //使用刚指定的配置项和数据显示图表
        myChart.setOption(option);
    </script>
</body>

</html>


<think>好的,用户之前问过关于在Open WebUI中自定义工具函数输出ECharts图表的问题,现在他们又想知道如何用Python结合ECharts。我需要先理清楚用户的需求。可能他们想用Python生成数据,然后用ECharts进行可视化,或者是在Python环境中直接使用ECharts库。 首先,用户可能知道ECharts本身是JavaScript的库,所以Python结合的话,可能需要一些库来桥接。比如Pyecharts是一个常见的选择,允许用Python生成ECharts图表。但用户可能还不太清楚具体的实现方式。 接下来,我需要考虑用户的使用场景。他们可能是在做数据分析,需要生成交互式图表,或者在Web应用中嵌入可视化。如果是Web应用,可能需要考虑如何将生成的图表嵌入到HTML中,或者导出为静态文件。 然后,用户的身份可能是一个数据分析师或开发者,希望通过Python快速生成可视化,而不需要深入学习JavaScript。他们可能对Pyecharts有基本了解,但需要具体的步骤和示例。 用户可能没有提到的深层需求是希望有一个简单易用的方法,能够利用Python的数据处理能力,结合ECharts的丰富图表类型和交互功能。或者他们可能需要在Jupyter Notebook中直接展示图表,或者生成HTML报告。 我需要分步骤介绍不同的方法,比如使用Pyecharts库,或者结合Flask/Django框架,或者在Jupyter中使用。同时要给出代码示例,说明如何安装和基本使用,让用户能够快速上手。 另外,用户可能关心如何自定义图表的样式,如何处理动态数据,或者如何将图表保存为图片或HTML文件。这些方面也需要涵盖,但需要保持回答简洁,突出重点。 最后,需要比较不同方法的优缺点,帮助用户根据需求选择合适的方式。比如Pyecharts适合快速生成,而使用JavaScript直接渲染适合需要高度定制化的场景。确保回答结构清晰,步骤明确,让用户能够逐步解决问题。</think>在 Python 中使用 ECharts 数据可视化,主要通过以下三种方式实现: $$ \text{实现路径} = \text{PyEcharts 原生库} \oplus \text{Web 框架集成} \oplus \text{Jupyter 交互} $$ 一、**PyEcharts 原生方案**(推荐) 1. 安装核心库 ```bash pip install pyecharts ``` 2. 基础柱状图示例 ```python from pyecharts.charts import Bar from pyecharts import options as opts bar = ( Bar() .add_xaxis(["衬衫", "羊毛衫", "雪纺衫"]) .add_yaxis("商家A", [5, 20, 36]) .set_global_opts(title_opts=opts.TitleOpts(title="销售统计")) ) bar.render("sales.html") # 生成独立 HTML 文件 ``` 二、**Web 框架集成**(以 Flask 为例) ```python from flask import Flask from pyecharts.charts import Line app = Flask(__name__) @app.route("/") def show_chart(): line = ( Line() .add_xaxis(list(range(10))) .add_yaxis("指数曲线", [i**2 for i in range(10)]) ) return line.render_embed() # 直接返回 HTML 片段 if __name__ == "__main__": app.run() ``` 三、**Jupyter Notebook 交互** ```python from pyecharts.charts import Pie from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "https://cdn.jsdelivr.net/npm/echarts@5.4.2/dist/" pie = ( Pie() .add("", [("苹果", 45), ("香蕉", 30), ("橙子", 25)]) .set_global_opts(title_opts=opts.TitleOpts(title="水果占比")) ) pie.load_javascript() # 加载依赖库 pie.render_notebook() # 直接在单元格输出 ``` **技术对比表**: | 方案 | 优点 | 适用场景 | |----------------|---------------------------|-------------------| | PyEcharts 原生 | 生成独立文件,兼容性强 | 静态报告/邮件附件 | | Web 框架集成 | 动态渲染,实时更新 | Web 应用开发 | | Jupyter | 交互式调试,即时可视化 | 数据分析探索 | **进阶技巧**: 1. 使用 `ThemeType` 切换主题: ```python from pyecharts.globals import ThemeType Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)) ``` 2. 组合图表实现: ```python from pyecharts.charts import Grid grid = Grid() grid.add(bar, grid_opts=opts.GridOpts(pos_left="5%")) grid.add(line, grid_opts=opts.GridOpts(pos_right="5%")) ``` 3. 地理坐标系集成: ```python from pyecharts.charts import Geo geo = Geo().add_schema(maptype="china") geo.add("城市数据", [("北京",100), ("上海",80)], type_="heatmap") ``` 完整解决方案需根据具体需求选择实现方式,建议优先使用 PyEcharts 官方文档(https://pyecharts.org)作为开发参考。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殊迟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值