ECharts记录

本文介绍如何使用ECharts创建基本图表,包括配置项说明及不同图表类型的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ECharts简单的例子:
1、新建一个echarts.html文件,为ECharts准备一个具备大小(宽高)的Dom。
<!DOCTYPE html>
<head>
   <meta charset="utf-8">
   <title>ECharts</title>
</head>


<body>
   <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
   <div id="main" style="height:400px"></div>
</body>


2 新建<script>标签引入模块化单文件echarts.js
<!DOCTYPE html>
<head>
   <meta charset="utf-8">
   <title>ECharts</title>
</head>
<body>
   <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
   <div id="main" style="height:400px"></div>
   <!-- ECharts单文件引入 -->
   <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
</body>


3 新建<script>标签中为模块加载器配置echarts和所需图表的路径(相对路径为从当前页面链接到echarts.js)
<!DOCTYPE html>
<head>
   <meta charset="utf-8">
   <title>ECharts</title>
</head>
<body>
   <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
   <div id="main" style="height:400px"></div>
   <!-- ECharts单文件引入 -->
   <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
   <script type="text/javascript">
// 路径配置
require.config({
   paths: {
echarts: 'http://echarts.baidu.com/build/dist'
   }
});
   </script>
</body>


4 <script>标签内动态加载echarts和所需图表,回调函数中可以初始化图表并驱动图表的生成
<!DOCTYPE html>
<head>
   <meta charset="utf-8">
   <title>ECharts</title>
</head>
<body>
   <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
   <div id="main" style="height:400px"></div>
   <!-- ECharts单文件引入 -->
   <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
   <script type="text/javascript">
// 路径配置
require.config({
   paths: {
echarts: 'http://echarts.baidu.com/build/dist'
   }
});

// 使用
require(
   [
'echarts',
'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
   ],
   function (ec) {
// 基于准备好的dom,初始化echarts图表
var myChart = ec.init(document.getElementById('main')); 

var option = {
   tooltip: {
show: true
   },
   legend: {
data:['销量']
   },
   xAxis : [
{
   type : 'category',
   data : ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
}
   ],
   yAxis : [
{
   type : 'value'
}
   ],
   series : [
{
   "name":"销量",
   "type":"bar",
   "data":[5, 20, 40, 10, 10, 20]
}
   ]
};

// 为echarts对象加载数据 
myChart.setOption(option); 
   }
);
   </script>
</body>


5 浏览器中打开echarts.html,就可看到效果


option参数说明:
tooltip表示提示
legend表示图表的说明
xAxis表示x轴
yAxis表示y轴
series表示一系列的数据设置


更详细的说明:
tooltip:
tooltip: {
show: true //显示工具提示信息。即鼠标放上去时的显示信息
}


legend:
legend: {
data:['销量'] //显示图表的说明信息
}


xAxis:
xAxis : [
   {
type : 'category', //表示x轴的类型
data : ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] //表示x轴的数据
   }
]


yAxis:
yAxis : [
   {
type : 'value' //表示y轴的类型。是显示值的
   }
]


series:
series : [
   {
"name":"销量", //尽量和legend的data保持一致,该name是用于工具提示信息:即鼠标放上去时的显示信息。
"type":"bar", //图表类型,bar表示柱状图
"data":[5, 20, 40, 10, 10, 20] //表示图表的数据
   }
]




接下来说明在工程中的实例讲解:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户统计</title>
</head>
<body>
   <div id="main" style="height:400px"></div>
   <script type="text/javascript" src="js/dist/echarts.js"></script>
   <script type="text/javascript" src="js/jquery-1.5.1.js"></script>
   <script type="text/javascript" src="js/json2.js"></script>
   <script type="text/javascript">
   
$(function(){
   // 路径配置
require.config({
   paths: {
echarts: 'js/dist'
   }
});

loadData();
});

function loadData(){
$.ajax({
   type:"POST",
   url:"customerStatistics.action",
   success:function(data){
//success start
var sipUserNum=data.sipUserNum;
var customerNum=data.customerNum;
// 使用
   require(
[
   'echarts',
   'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
],
function (ec) {
   // 基于准备好的dom,初始化echarts图表
   var myChart = ec.init(document.getElementById('main')); 
   
   var option = {
tooltip: {
   show: true
},
legend: {
   data:['数量']
},
xAxis : [
   {
type : 'category',
data : ["sip用户","客户"]
   }
],
yAxis : [
   {
type : 'value'
   }
],
series : [
   {
"name":"数量",
"type":"bar",
"data":[sipUserNum, customerNum],
"barWidth":30,//柱图宽度
"itemStyle":{"normal":{"color":'rgb(89, 195, 255)'}}
   }
]
   };
   
   // 为echarts对象加载数据 
   myChart.setOption(option); 
}
   );
   
   //success end
   }
});
   }
   </script>
   
</body>
</html>


说明(series中的barWidth和itemStyle):
"barWidth":30,//柱图宽度
"itemStyle":{"normal":{"color":'rgb(89, 195, 255)'}} //表示柱图的颜色


!!!前提:将ECharts的js插件添加到项目中的/js/目录下


示例(非常好):
http://echarts.baidu.com/echarts2/doc/example.html


参数说明(非常好)
http://echarts.baidu.com/echarts2/doc/doc.html#Option


以下是各种图形的Option说明:
标准折线图(http://echarts.baidu.com/echarts2/doc/example/line1.html):
option = {
   title : { //title标题
text: '未来一周气温变化', //标题的内容
subtext: '纯属虚构' //标题的子标题的内容
   },
   tooltip : { //工具提示
trigger: 'axis' //触发显示工具提示的触发器。
   },
   legend: { //图标说明
data:['最高气温','最低气温'] //图标说明数据
   },
   toolbox: { //工具集
show : true, //是否显示
feature : { //设置显示
   mark : {show: true}, //设置可操作增加辅助线等
   dataView : {show: true, readOnly: false}, //设置显示数据
   magicType : {show: true, type: ['line', 'bar']}, //设置显示折线图和柱状图之间切换
   restore : {show: true}, //设置还原
   saveAsImage : {show: true} //设置保存为图片
}
   },
   calculable : true, //设置是否可操作,改变值
   xAxis : [ //x轴
{
   type : 'category', //x轴的类型
   boundaryGap : false, //是否在开头和结尾有空白
   data : ['周一','周二','周三','周四','周五','周六','周日'] //x轴的数据
}
   ],
   yAxis : [ //y轴
{
   type : 'value', //y轴的数据类型
   axisLabel : { //y轴的显示的文本
formatter: '{value} °C' //格式化显示的文本
   }
}
   ],
   series : [ //一系列数据项配置
{
   name:'最高气温', //工具条提示时的name
   type:'line', //图标的类型
   data:[11, 11, 15, 13, 12, 13, 10], //图标的值
   markPoint : { //增加标注点的配置
data : [ //增加标注点的数据
   {type : 'max', name: '最大值'}, //最大值
   {type : 'min', name: '最小值'} //最小值
]
   },
   markLine : { //增加标注线的配置
data : [ //增加标注线的数据
   {type : 'average', name: '平均值'} //平均值
]
   }
},
{
   name:'最低气温', //工具条提示时的name
   type:'line', //图标的类型
   data:[1, -2, 2, 5, 3, 2, 0], //图标的值
   markPoint : { //增加标注点的配置
data : [ //增加标注点的数据
   {name : '周最低', value : -2, xAxis: 1, yAxis: -1.5} //自定义标注点
]
   },
   markLine : { //增加标注线的配置
data : [ //增加标注线的数据
   {type : 'average', name : '平均值'} //平均值
]
   }
}
   ]
};
                    
面积折线图(http://echarts.baidu.com/echarts2/doc/example/line3.html):
option = {
   title : { //title标题
text: '某楼盘销售情况', //标题的内容
subtext: '纯属虚构' //标题的子标题的内容
   },
   tooltip : { //工具提示
trigger: 'axis' //触发显示工具提示的触发器。
   },
   legend: { //图标说明
data:['意向','预购','成交'] //图标说明数据
   },
   toolbox: { //工具集
show : true, //是否显示
feature : { //设置显示
   mark : {show: true}, //设置可操作增加辅助线等
   dataView : {show: true, readOnly: false}, //设置显示数据
   magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']}, //设置显示line、bar、stack、tiled之间切换
   restore : {show: true}, //设置还原
   saveAsImage : {show: true} //设置保存为图片
}
   },
   calculable : true, //设置是否可操作,改变值
   xAxis : [ //x轴
{
   type : 'category', //x轴的类型
   boundaryGap : false, //是否在开头和结尾有空白
   data : ['周一','周二','周三','周四','周五','周六','周日'] //x轴的数据
}
   ],
   yAxis : [ //y轴
{
   type : 'value' //y轴的数据类型
}
   ],
   series : [ //一系列数据项配置
{
   name:'成交', //工具条提示时的name
   type:'line', //图标的类型
   smooth:true, //平滑的曲线
   itemStyle: {normal: {areaStyle: {type: 'default'}}}, //区域填充,不再是一条线,而是一套闲和轴之间的区域填充
   data:[10, 12, 21, 54, 260, 830, 710] //图标的值
},
{
   name:'预购', //工具条提示时的name
   type:'line', //图标的类型
   smooth:true, //平滑的曲线
   itemStyle: {normal: {areaStyle: {type: 'default'}}}, //区域填充,不再是一条线,而是一套闲和轴之间的区域填充
   data:[30, 182, 434, 791, 390, 30, 10] //图标的值
},
{
   name:'意向', //工具条提示时的name
   type:'line', //图标的类型
   smooth:true, //平滑的曲线
   itemStyle: {normal: {areaStyle: {type: 'default'}}}, //区域填充,不再是一条线,而是一套闲和轴之间的区域填充
   data:[1320, 1132, 601, 234, 120, 90, 20] //图标的值
}
   ]
};
            
柱状图(http://echarts.baidu.com/echarts2/doc/example/bar1.html):
option = {
   title : { //title标题
text: '某地区蒸发量和降水量', //标题的内容
subtext: '纯属虚构' //标题的子标题的内容
   },
   tooltip : { //工具提示
trigger: 'axis' //触发显示工具提示的触发器。
   },
   legend: { //图标说明
data:['蒸发量','降水量'] //图标说明数据
   },
   toolbox: { //工具集
show : true, //是否显示
feature : { //设置显示
   mark : {show: true}, //设置可操作增加辅助线等
   dataView : {show: true, readOnly: false}, //设置显示数据
   magicType : {show: true, type: ['line', 'bar']}, //设置显示折线图和柱状图之间切换
   restore : {show: true}, //设置还原
   saveAsImage : {show: true} //设置保存为图片
}
   },
   calculable : true, //设置是否可操作,改变值
   xAxis : [ //x轴
{
   type : 'category', //x轴的类型
   data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'] //x轴的数据
}
   ],
   yAxis : [ //y轴
{
   type : 'value' //y轴的数据类型
}
   ],
   series : [ //一系列数据项配置
{
   name:'蒸发量', //工具条提示时的name
   type:'bar', //图标的类型
   data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3], //图标的值
   markPoint : { //增加标注点的配置
data : [ //增加标注点的数据
   {type : 'max', name: '最大值'}, //最大值
   {type : 'min', name: '最小值'} //最小值
]
   },
   markLine : { //增加标注线的配置
data : [ //增加标注线的数据
   {type : 'average', name: '平均值'} //平均值
]
   }
},
{
   name:'降水量', //工具条提示时的name
   type:'bar', //图标的类型
   data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3], //图标的值
   markPoint : { //增加标注点的配置
data : [ //增加标注点的数据
   {name : '年最高', value : 182.2, xAxis: 7, yAxis: 183, symbolSize:18}, //自定义标注点
   {name : '年最低', value : 2.3, xAxis: 11, yAxis: 3} //自定义标注点
]
   },
   markLine : { //增加标注线的配置
data : [ //增加标注线的数据
   {type : 'average', name : '平均值'} //平均值
]
   }
}
   ]
};


标准饼图(http://echarts.baidu.com/echarts2/doc/example/pie1.html):
option = {
   title : { //title标题
text: '某站点用户访问来源', //标题的内容
subtext: '纯属虚构', //标题的子标题的内容
x:'center' //位置居中
   },
   tooltip : { //工具提示
trigger: 'item', //触发显示工具提示的触发器。
formatter: "{a} <br/>{b} : {c} ({d}%)" //格式化工具提示信息。a:图标说明,b:名称,c:值,d:百分比
   },
   legend: { //图标说明
orient : 'vertical', //位置:垂直
x : 'left', //左对齐
data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎'] //图标说明数据
   },
   toolbox: { //工具集
show : true, //是否显示
feature : { //设置显示
   mark : {show: true}, //设置可操作增加辅助线等
   dataView : {show: true, readOnly: false}, //设置显示数据
   magicType : { //设置显示pie和funnel之间切换
show: true, 
type: ['pie', 'funnel'],
option: {
   funnel: {
x: '25%',
width: '50%',
funnelAlign: 'left',
max: 1548
   }
}
   },
   restore : {show: true}, //设置还原
   saveAsImage : {show: true} //设置保存为图片
}
   },
   calculable : true, //设置是否可操作,改变值
   series : [ //一系列数据项配置
{
   name:'访问来源', //工具条提示时的name
   type:'pie', //图标的类型
   radius : '55%', //饼状图的半径大小
   center: ['50%', '60%'], //饼状图的圆心坐标位置:宽度,高度
   data:[ //图标的值
{value:335, name:'直接访问'}, //数据
{value:310, name:'邮件营销'}, //数据
{value:234, name:'联盟广告'}, //数据
{value:135, name:'视频广告'}, //数据
{value:1548, name:'搜索引擎'} //数据
   ]
}
   ]
};
                    


示例(非常好):
http://echarts.baidu.com/echarts2/doc/example.html


参数说明(非常好)
http://echarts.baidu.com/echarts2/doc/doc.html#Option













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值