echarts学习1----格式整理以及地图入门
来源:互联网 发布:玲珑网游加速器mac 编辑:程序博客网 时间:2024/04/27 08:36
首先,Echarts是结合html/ javascript一起运用的,可以在线也可以离线使用。
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>ECharts 学习</title>
</head>
在<body></body>里面的第一段:
<div id="main" style="height:400px"></div> //搞一个合适大小的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'
}
});
下面是离线版本1:
我下载了那个包,把它放在同一个文件夹:
<div id="main" style="height:400px"></div>
<script src="echarts/echarts.js"></script>
<script type="text/javascript">
require.config({
paths:{
'bar' : 'echarts/chart/bar',
'line': 'echarts/chart/line',
'map' : 'echarts/chart/map'
}
});//我自己写是这样的,呵呵,下面有解析
假设我们的项目结构如下:
index.html
main.js
libs
--haha.js
--cores
--cores1.js
--core2.js
--utils
--util1.js
--util2.js
--services
--service1.js
--service2.js
如果项目规模比较大,那么js文件将会非常多,通常我们会按照目录进行组织和分组。在上面的代码中如果我们想使用core1.js、core2.js、util1.js、util2.js、service1.js、service2.js这6个模块。那么我可以在main.js中做如下配置:
requirejs.config({
baseUrl: 'libs',
paths: {
"core1": 'cores/core1',
"core2": 'cores/core2',
"util1": 'utils/util1',
"util2": 'utils/util2',
"service1": 'services/service1',
"service2": 'services/service2',
}
});
第二个是真正的require函数:
require(['jquery'], function($) { // 这个函数加载 jquery 依赖,然后执行回调代码
console.log($);
});
// 使用
require(
[
'echarts',
'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
],
function (ec) {
//都要先获取一下上面那个div
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>
例子2:一个地图的
require(
[
'echarts',
'echarts/chart/map' //这里用map,就把map模块弄进来
function (ec) {
var myChart=ec.init(document.getElementById('main'));
//option主要是图标的一些设置,这不是这篇文章的重点,关于具体的设置可以参考官方的文档说明文档
option= {
title: {
text:'iphone销量',
subtext:'纯属虚构',
x:'center'
},
//这样就能做一个标题
//接下来是tooltip控件,就是你放上去它会提示东西
tooltip: {
trigger:'item'
},
//如果像上面这样写的话,它显示的会是所有的总和。像下面这样写就不会
tooltip: {
trigger: 'item',
formatter: function(params) {
var res = params.name+'<br/>';
var myseries = option.series;
for (var i = 0; i < myseries.length; i++) {
res+= myseries[i].name;
for(var j=0;j<myseries[i].data.length;j++){
if(myseries[i].data[j].name==params.name){
res+=' : '+myseries[i].data[j].value+'</br>';
}
}
}
return res;
}
},
//legend就是左上角的那个东西
legend: {
orient:'vertical',
x:'left',
data: ['iphone3','iphone4','iphone5']
},
//这个就是左下角那个柱状图
dataRange: {
min:0,
max:2500,
text: ['高','低'],
calculable:true,//这个要是true的话就可以像个柱子一样随时显示,但是要是false的话就是断的,而且显示不了数字,只有等级颜色
textStyle: {
color:'orange'//那个数字的颜色
}
},
//这个暂时不知道是干嘛的……
toolbox: {
show:true,
orient:'vertical',
x:'right',
y:'center',
feature: {
mark:true,
dataView: { readOnly:false },
restore:true,
saveAsImage:true
}
},
//接下来这个就是显示那个地图的那些部分了
series: [ //这个里面分成iphone3,iphone4和iphone5三个部分
{
name:'iphone3',
type:'map',
mapType:'china',
selectedMode: 'single',//大概意思是说选择模式是单选吧
itemStyle: {
normal: { label: { show:true },color:'#ffd700' },//这个就是给那个legend用的,也会标在地图上
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'重庆',value:Math.round(Math.random() *1000) },
{ name:'河北',value:Math.round(Math.random() *1000) },
{ name:'河南',value:Math.round(Math.random() *1000) },
{ name:'云南',value:Math.round(Math.random() *1000) },
{ name:'辽宁',value:Math.round(Math.random() *1000) },
{ name:'黑龙江',value:Math.round(Math.random() *1000) },
{ name:'湖南',value:Math.round(Math.random() *1000) },
{ name:'安徽',value:Math.round(Math.random() *1000) },
{ name:'山东',value:Math.round(Math.random() *1000) },
{ name:'新疆',value:Math.round(Math.random() *1000) },
{ name:'江苏',value:Math.round(Math.random() *1000) },
{ name:'浙江',value:Math.round(Math.random() *1000) },
{ name:'江西',value:Math.round(Math.random() *1000) },
{ name:'湖北',value:Math.round(Math.random() *1000) },
{ name:'广西',value:Math.round(Math.random() *1000) },
{ name:'甘肃',value:Math.round(Math.random() *1000) },
{ name:'山西',value:Math.round(Math.random() *1000) },
{ name:'内蒙古',value:Math.round(Math.random() *1000) },
{ name:'陕西',value:Math.round(Math.random() *1000) },
{ name:'吉林',value:Math.round(Math.random() *1000) },
{ name:'福建',value:Math.round(Math.random() *1000) },
{ name:'贵州',value:Math.round(Math.random() *1000) },
{ name:'广东',value:Math.round(Math.random() *1000) },
{ name:'青海',value:Math.round(Math.random() *1000) },
{ name:'西藏',value:Math.round(Math.random() *1000) },
{ name:'四川',value:Math.round(Math.random() *1000) },
{ name:'宁夏',value:Math.round(Math.random() *1000) },
{ name:'海南',value:Math.round(Math.random() *1000) },
{ name:'台湾',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳门',value:Math.round(Math.random() *1000) }
]
},
{
name:'iphone4',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#ff8c00' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'重庆',value:Math.round(Math.random() *1000) },
{ name:'河北',value:Math.round(Math.random() *1000) },
{ name:'安徽',value:Math.round(Math.random() *1000) },
{ name:'新疆',value:Math.round(Math.random() *1000) },
{ name:'浙江',value:Math.round(Math.random() *1000) },
{ name:'江西',value:Math.round(Math.random() *1000) },
{ name:'山西',value:Math.round(Math.random() *1000) },
{ name:'内蒙古',value:Math.round(Math.random() *1000) },
{ name:'吉林',value:Math.round(Math.random() *1000) },
{ name:'福建',value:Math.round(Math.random() *1000) },
{ name:'广东',value:Math.round(Math.random() *1000) },
{ name:'西藏',value:Math.round(Math.random() *1000) },
{ name:'四川',value:Math.round(Math.random() *1000) },
{ name:'宁夏',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳门',value:Math.round(Math.random() *1000) }
]
},
{
name:'iphone5',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#da70d6' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'广东',value:Math.round(Math.random() *1000) },
{ name:'台湾',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳门',value:Math.round(Math.random() *1000) }
]
}
]
};
//以上大同小异,就是从iphone3到iphone5的一些数据。
//以下的这段代码主要是用来处理用户的选择,应用场景是可以做地图的交互,比如点击地图上的某一个省,获取相应的省的指标变化等。
//需要特别注意的是,如果需要点击某一省作地图的操作交互的话,还需要为series属性的每一项添加一个selectedMode属性,这里的属性值为 'single'即单选,也可多选
var ecConfig= require('echarts/config');
myChart.on(ecConfig.EVENT.MAP_SELECTED,function (param) {
varselected=param.selected;
varmapSeries=option.series[0];
vardata= [];
varlegendData= [];
varname;
for (varp=0,len=mapSeries.data.length; p<len; p++) {
name=mapSeries.data[p].name;
mapSeries.data[p].selected=selected[name];
if (selected[name]) {
alert(name); //这里只是简单的做一个事例说明,弹出用户所选的省,如需做其他的扩展,可以在这里边添加相应的操作
}
}
});
myChart.setOption(option);
}
);
下面是一个.html文件的代码及解析:
<!DOCTYPE html>
<head><meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>ECharts 学习</title>
</head>
前面就先这样固定,保证中文也能识别,其它下面的都包含在<body></body>里面
在<body></body>里面的第一段:
<div id="main" style="height:400px"></div> //搞一个合适大小的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'
}
});
下面是离线版本1:
我下载了那个包,把它放在同一个文件夹:
<div id="main" style="height:400px"></div>
<script src="echarts/echarts.js"></script>
<script type="text/javascript">
把路径配置那个部分直接删掉就行了;
require.config({
paths:{
'bar' : 'echarts/chart/bar',
'line': 'echarts/chart/line',
'map' : 'echarts/chart/map'
}
});//我自己写是这样的,呵呵,下面有解析
<body>里面的第二段:
假设我们的项目结构如下:
index.html
main.js
libs
--haha.js
--cores
--cores1.js
--core2.js
--utils
--util1.js
--util2.js
--services
--service1.js
--service2.js
如果项目规模比较大,那么js文件将会非常多,通常我们会按照目录进行组织和分组。在上面的代码中如果我们想使用core1.js、core2.js、util1.js、util2.js、service1.js、service2.js这6个模块。那么我可以在main.js中做如下配置:
requirejs.config({
baseUrl: 'libs',
paths: {
"core1": 'cores/core1',
"core2": 'cores/core2',
"util1": 'utils/util1',
"util2": 'utils/util2',
"service1": 'services/service1',
"service2": 'services/service2',
}
});
第二个是真正的require函数:
require(['jquery'], function($) { // 这个函数加载 jquery 依赖,然后执行回调代码
console.log($);
});
整个用require ();包括
// 使用
require(
[
'echarts',
'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
],
function (ec) {
//都要先获取一下上面那个div
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>
例子2:一个地图的
//动态加载echarts,在回掉函数中使用,要注意保持按需加载结构定义图表路径
require(
[
'echarts',
'echarts/chart/map' //这里用map,就把map模块弄进来
],
function (ec) {
var myChart=ec.init(document.getElementById('main'));
//option主要是图标的一些设置,这不是这篇文章的重点,关于具体的设置可以参考官方的文档说明文档
option= {
title: {
text:'iphone销量',
subtext:'纯属虚构',
x:'center'
},
//这样就能做一个标题
//接下来是tooltip控件,就是你放上去它会提示东西
tooltip: {
trigger:'item'
},
//如果像上面这样写的话,它显示的会是所有的总和。像下面这样写就不会
tooltip: {
trigger: 'item',
formatter: function(params) {
var res = params.name+'<br/>';
var myseries = option.series;
for (var i = 0; i < myseries.length; i++) {
res+= myseries[i].name;
for(var j=0;j<myseries[i].data.length;j++){
if(myseries[i].data[j].name==params.name){
res+=' : '+myseries[i].data[j].value+'</br>';
}
}
}
return res;
}
},
//legend就是左上角的那个东西
legend: {
orient:'vertical',
x:'left',
data: ['iphone3','iphone4','iphone5']
},
//这个就是左下角那个柱状图
dataRange: {
min:0,
max:2500,
text: ['高','低'],
calculable:true,//这个要是true的话就可以像个柱子一样随时显示,但是要是false的话就是断的,而且显示不了数字,只有等级颜色
textStyle: {
color:'orange'//那个数字的颜色
}
},
//这个暂时不知道是干嘛的……
toolbox: {
show:true,
orient:'vertical',
x:'right',
y:'center',
feature: {
mark:true,
dataView: { readOnly:false },
restore:true,
saveAsImage:true
}
},
//接下来这个就是显示那个地图的那些部分了
series: [ //这个里面分成iphone3,iphone4和iphone5三个部分
{
name:'iphone3',
type:'map',
mapType:'china',
selectedMode: 'single',//大概意思是说选择模式是单选吧
itemStyle: {
normal: { label: { show:true },color:'#ffd700' },//这个就是给那个legend用的,也会标在地图上
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'重庆',value:Math.round(Math.random() *1000) },
{ name:'河北',value:Math.round(Math.random() *1000) },
{ name:'河南',value:Math.round(Math.random() *1000) },
{ name:'云南',value:Math.round(Math.random() *1000) },
{ name:'辽宁',value:Math.round(Math.random() *1000) },
{ name:'黑龙江',value:Math.round(Math.random() *1000) },
{ name:'湖南',value:Math.round(Math.random() *1000) },
{ name:'安徽',value:Math.round(Math.random() *1000) },
{ name:'山东',value:Math.round(Math.random() *1000) },
{ name:'新疆',value:Math.round(Math.random() *1000) },
{ name:'江苏',value:Math.round(Math.random() *1000) },
{ name:'浙江',value:Math.round(Math.random() *1000) },
{ name:'江西',value:Math.round(Math.random() *1000) },
{ name:'湖北',value:Math.round(Math.random() *1000) },
{ name:'广西',value:Math.round(Math.random() *1000) },
{ name:'甘肃',value:Math.round(Math.random() *1000) },
{ name:'山西',value:Math.round(Math.random() *1000) },
{ name:'内蒙古',value:Math.round(Math.random() *1000) },
{ name:'陕西',value:Math.round(Math.random() *1000) },
{ name:'吉林',value:Math.round(Math.random() *1000) },
{ name:'福建',value:Math.round(Math.random() *1000) },
{ name:'贵州',value:Math.round(Math.random() *1000) },
{ name:'广东',value:Math.round(Math.random() *1000) },
{ name:'青海',value:Math.round(Math.random() *1000) },
{ name:'西藏',value:Math.round(Math.random() *1000) },
{ name:'四川',value:Math.round(Math.random() *1000) },
{ name:'宁夏',value:Math.round(Math.random() *1000) },
{ name:'海南',value:Math.round(Math.random() *1000) },
{ name:'台湾',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳门',value:Math.round(Math.random() *1000) }
]
},
{
name:'iphone4',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#ff8c00' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'重庆',value:Math.round(Math.random() *1000) },
{ name:'河北',value:Math.round(Math.random() *1000) },
{ name:'安徽',value:Math.round(Math.random() *1000) },
{ name:'新疆',value:Math.round(Math.random() *1000) },
{ name:'浙江',value:Math.round(Math.random() *1000) },
{ name:'江西',value:Math.round(Math.random() *1000) },
{ name:'山西',value:Math.round(Math.random() *1000) },
{ name:'内蒙古',value:Math.round(Math.random() *1000) },
{ name:'吉林',value:Math.round(Math.random() *1000) },
{ name:'福建',value:Math.round(Math.random() *1000) },
{ name:'广东',value:Math.round(Math.random() *1000) },
{ name:'西藏',value:Math.round(Math.random() *1000) },
{ name:'四川',value:Math.round(Math.random() *1000) },
{ name:'宁夏',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳门',value:Math.round(Math.random() *1000) }
]
},
{
name:'iphone5',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#da70d6' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'广东',value:Math.round(Math.random() *1000) },
{ name:'台湾',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳门',value:Math.round(Math.random() *1000) }
]
}
]
};
//以上大同小异,就是从iphone3到iphone5的一些数据。
//以下的这段代码主要是用来处理用户的选择,应用场景是可以做地图的交互,比如点击地图上的某一个省,获取相应的省的指标变化等。
//需要特别注意的是,如果需要点击某一省作地图的操作交互的话,还需要为series属性的每一项添加一个selectedMode属性,这里的属性值为 'single'即单选,也可多选
var ecConfig= require('echarts/config');
myChart.on(ecConfig.EVENT.MAP_SELECTED,function (param) {
varselected=param.selected;
varmapSeries=option.series[0];
vardata= [];
varlegendData= [];
varname;
for (varp=0,len=mapSeries.data.length; p<len; p++) {
name=mapSeries.data[p].name;
mapSeries.data[p].selected=selected[name];
if (selected[name]) {
alert(name); //这里只是简单的做一个事例说明,弹出用户所选的省,如需做其他的扩展,可以在这里边添加相应的操作
}
}
});
myChart.setOption(option);
}
);
0 0
- echarts学习1----格式整理以及地图入门
- echarts学习2----地图的改造以及主图副图的联动
- echarts地图扩展文件使用geoJson格式。
- ECharts学习手札(1)--入门介绍
- Echarts入门学习前必知的
- echarts地图
- Js插件ECharts,HighCharts学习网址整理
- Echarts地图使用扩展(1)
- ECharts.js学习(一) 简单入门
- ECharts.js学习(一) 简单入门
- echarts入门
- Echarts入门
- echarts入门
- ECharts入门
- 百度地图1 整理
- USACO入门以及提交格式
- MapServer6.4.1教程学习--改变地图输出格式(1-8)
- ECharts 浙江地图实践
- iOS-nsoperration的简单操作
- 栀子花如何修剪
- PHP.8-HTML+CSS(二)-HTML详解
- 几种常见的排序算法及实现(三)
- Gradle sync failed: Could not find method android() for arguments [build_aj7feyco0x5e5hj3g6wttaj6$_
- echarts学习1----格式整理以及地图入门
- 【数据库】小谈事务
- kali2016.2源
- Android ActionBar创建带图片和文字的menu
- 223. Rectangle Area
- iOS技巧之Category-赵鹏举
- Atitit.架构设计趋势 设计模式 ---微服务架构 soa
- hdoj 1514 Stars(树状数组类似求逆序)
- js正则表达式