最近在学习使用echarts制作图标,简单使用还是很快就学会了的,简单的东西总是很容易的,我这里写了一些自己最近写的的案列,和大家分享下
echats文件下载http://echarts.baidu.com/,下下来有很多东西,可以自己研究研究
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<title>ECharts</title>
</head>
<body οnlοad="b()">
<!--Step:1 为ECharts准备一个具备大小(宽高)的div-->
<div id="main" style="height:300px;border:1px solid #ccc;padding:10px;"></div>
<!--Step:2 引入echarts.js-->
<script src="js/echarts.js"></script>
<script type="text/javascript">
function a(){
// Step:3 为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径
require.config({
paths: {
//我这里是引入本地文件,也可以引入网上文件http://echarts.baidu.com/build/dist
}
});
// Step:4 require echarts and use it in the callback.
// Step:4 动态加载echarts然后在回调函数中开始使用,注意保持按需加载结构定义图表路径
require(
[
'echarts',
'echarts/chart/bar',
'echarts/chart/line'
],
function (ec) {
//--- 折柱 ---
var myChart = ec.init(document.getElementById('main'));
myChart.setOption({
tooltip : {
trigger: 'axis'
},
toolbox: {
show : true,
feature : {
//辅助线
mark : {show: true},
//折线,柱形,饼形
magicType : {show: true, type: ['line', 'bar']},
//自定义饼形
selfButtons:{//自定义按钮 danielinbiti,这里增加,selfbuttons可以随便取名字
show:true,//是否显示
title:'饼形切换', //鼠标移动上去显示的文字
icon:'img/2.png', //图标
option:{},
onclick:function(option1) {//点击事件,这里的option1是chart的option信息
c();//这里可以加入自己的处理代码,切换不同的图形
}
},
//换原
restore : {show: true},
//保存
saveAsImage : {show: true}
}
}
});
}
);
}
function b(){
// Step:3 为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径
require.config({
paths: {
echarts: './js'
}
});
// Step:4 require echarts and use it in the callback.
// Step:4 动态加载echarts然后在回调函数中开始使用,注意保持按需加载结构定义图表路径
require(
[
'echarts',
'echarts/chart/bar',
'echarts/chart/line'
],
function (ec) {
//--- 折柱 ---
var myChart = ec.init(document.getElementById('main'));
myChart.on('click',function(){
alert(0);
})
// 过渡---------------------
/*myChart.showLoading({
text: '正在努力的读取数据中...', //loading话术
});*/
myChart.setOption({
tooltip : {
trigger: 'axis'
},
toolbox: {
show : true,
feature : {
//辅助线
mark : {show: true},
//折线,柱形,饼形
magicType : {show: true, type: ['line', 'bar']},
//自定义左柱形
/*selfButtons:{//自定义按钮 danielinbiti,这里增加,selfbuttons可以随便取名字
show:true,//是否显示
title:'左柱形切换', //鼠标移动上去显示的文字
icon:'img/1.png', //图标
option:{},
onclick:function(option1) {//点击事件,这里的option1是chart的option信息
c();//这里可以加入自己的处理代码,切换不同的图形
}
},*/
//自定义饼形
selfButtons:{//自定义按钮 danielinbiti,这里增加,selfbuttons可以随便取名字
show:true,//是否显示
title:'饼形切换', //鼠标移动上去显示的文字
icon:'img/2.png', //图标
option:{},
onclick:function(option1) {//点击事件,这里的option1是chart的option信息
c();//这里可以加入自己的处理代码,切换不同的图形
}
},
//换原
restore : {show: true},
//保存
saveAsImage : {show: true}
}
},
calculable : true,
xAxis : [
{
type : 'category',
data : ['1月','2月','3月','4月']
}
],
yAxis : [
{
type : 'value',
splitArea : {show : true}
}
],
series : [
{
name:'蒸发量',
type:'bar',
data:[2.0, 4.9, 7.0, 23.2],
},
{
name:'降水量',
type:'bar',
data:[2.6, 5.9, 9.0, 26.4],
}
]
});
}
);
}
function c(){
require.config({
paths: {
echarts: './js'
}
});
// 使用
require(
[
'echarts',
'echarts/chart/pie',
'echarts/chart/funnel'
],
function (ec) {
// 基于准备好的dom,初始化echarts图表
var myChart = ec.init(document.getElementById('main'));
var option = {
tooltip : {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
toolbox: {
show : true,
feature : {
mark : {show: true},
magicType : {
show: true,
type: ['pie', 'funnel']
},
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name:'访问来源',
type:'pie',
radius : '55%',
center: ['50%', '60%'],
data:[
{value:335, name:'直接访问'},
{value:310, name:'邮件营销'},
{value:234, name:'联盟广告'},
{value:135, name:'视频广告'},
{value:1548, name:'搜索引擎'}
]
}
]
};
// 为echarts对象加载数据
myChart.setOption(option);
}
);
}
</script>
</body>
</html>