Echarts是什么?
Echarts是一款基于JavaScript的数据可视化图表库,提供直观、交互丰富、可个性化定制的数据可视化图表。
通俗的理解:
- 是JS的一个插件
- 性能好可以流畅运行PC端和移动设备
- 兼容主流浏览器
- 提供很多图表:折线图、柱状图、散点图、饼图等,且支持自制(自定义)
Echarts入门
下载echarts.js
链接:echarts CDN by jsDelivr - A CDN for npm and GitHub
引入echarts.js文件
<script src="echarts.min.js"></script>
准备一个呈现图表的盒子
<div style="width: 600px;height: 400px;"></div>
初始化echarts对象
var mCharts = echarts.init(document.querySelector('div'));
设置配置项
var option = {
xAxis:{
type: 'category',
data:['小明','小红','小王']
},
yAxis:{
type:'value'
},
series:[
{
name:'语文',
type:'bar',
data:[70,92,87]
}
]
};
将配置项设置给echarts实例对象
mCharts.setOption(option);
Echarts主要配置
- series
- 系列列表。每个系列通过type觉得自己的图表类型
- xAxis
- 直角坐标系grid中的x轴
- boundaryGap: 是否让线条(或柱子)和坐标轴有缝隙
- yAxis
- 直角坐标系grid中的y轴
- grid
- 直角坐标系内绘图网格
- title标题组件
- tooltip
- 提示框组件
- legend
- 图例组件
- color
- 调色盘颜色列表
图表标题
title: {
x: 'left', // 水平安放位置,默认为左对齐,可选为:
// 'center' ¦ 'left' ¦ 'right'
// ¦ {number}(x坐标,单位px)
y: 'top', // 垂直安放位置,默认为全图顶端,可选为:
// 'top' ¦ 'bottom' ¦ 'center'
// ¦ {number}(y坐标,单位px)
//textAlign: null // 水平对齐方式,默认根据x设置自动调整
backgroundColor: 'rgba(0,0,0,0)',
borderColor: '#ccc', // 标题边框颜色
borderWidth: 0, // 标题边框线宽,单位px,默认为0(无边框)
padding: 5, // 标题内边距,单位px,默认各方向内边距为5,
// 接受数组分别设定上右下左边距,同css
itemGap: 10, // 主副标题纵向间隔,单位px,默认为10,
textStyle: {
fontSize: 18,
fontWeight: 'bolder',
color: '#333' // 主标题文字颜色
},
subtextStyle: {
color: '#aaa' // 副标题文字颜色
}
},
图例
legend: {
orient: 'horizontal', // 布局方式,默认为水平布局,可选为:
// 'horizontal' ¦ 'vertical'
x: 'center', // 水平安放位置,默认为全图居中,可选为:
// 'center' ¦ 'left' ¦ 'right'
// ¦ {number}(x坐标,单位px)
y: 'top', // 垂直安放位置,默认为全图顶端,可选为:
// 'top' ¦ 'bottom' ¦ 'center'
// ¦ {number}(y坐标,单位px)
backgroundColor: 'rgba(0,0,0,0)',
borderColor: '#ccc', // 图例边框颜色
borderWidth: 0, // 图例边框线宽,单位px,默认为0(无边框)
padding: 5, // 图例内边距,单位px,默认各方向内边距为5,
// 接受数组分别设定上右下左边距,同css
itemGap: 10, // 各个item之间的间隔,单位px,默认为10,
// 横向布局时为水平间隔,纵向布局时为纵向间隔
itemWidth: 20, // 图例图形宽度
itemHeight: 14, // 图例图形高度
textStyle: {
color: '#333' // 图例文字颜色
}
},
提示框
tooltip: {
trigger: 'item', // 触发类型,默认数据触发,见下图,可选为:'item' ¦ 'axis'
showDelay: 20, // 显示延迟,添加显示延迟可以避免频繁切换,单位ms
hideDelay: 100, // 隐藏延迟,单位ms
transitionDuration : 0.4, // 动画变换时间,单位s
backgroundColor: 'rgba(0,0,0,0.7)', // 提示背景颜色,默认为透明度为0.7的黑色
borderColor: '#333', // 提示边框颜色
borderRadius: 4, // 提示边框圆角,单位px,默认为4
borderWidth: 0, // 提示边框线宽,单位px,默认为0(无边框)
padding: 5, // 提示内边距,单位px,默认各方向内边距为5,
// 接受数组分别设定上右下左边距,同css
axisPointer : { // 坐标轴指示器,坐标轴触发有效
type : 'line', // 默认为直线,可选为:'line' | 'shadow'
lineStyle : { // 直线指示器样式设置
color: '#48b',
width: 2,
type: 'solid'
},
shadowStyle : { // 阴影指示器样式设置
width: 'auto', // 阴影大小
color: 'rgba(150,150,150,0.3)' // 阴影颜色
}
},
textStyle: {
color: '#fff'
}
},
网格
grid: {
x: 80,
y: 60,
x2: 80,
y2: 60,
// width: {totalWidth} - x - x2,
// height: {totalHeight} - y - y2,
backgroundColor: 'rgba(0,0,0,0)',
borderWidth: 1,
borderColor: '#ccc'
},
值域
dataRange: {
orient: 'vertical', // 布局方式,默认为垂直布局,可选为:
// 'horizontal' ¦ 'vertical'
x: 'left', // 水平安放位置,默认为全图左对齐,可选为:
// 'center' ¦ 'left' ¦ 'right'
// ¦ {number}(x坐标,单位px)
y: 'bottom', // 垂直安放位置,默认为全图底部,可选为:
// 'top' ¦ 'bottom' ¦ 'center'
// ¦ {number}(y坐标,单位px)
backgroundColor: 'rgba(0,0,0,0)',
borderColor: '#ccc', // 值域边框颜色
borderWidth: 0, // 值域边框线宽,单位px,默认为0(无边框)
padding: 5, // 值域内边距,单位px,默认各方向内边距为5,
// 接受数组分别设定上右下左边距,同css
itemGap: 10, // 各个item之间的间隔,单位px,默认为10,
// 横向布局时为水平间隔,纵向布局时为纵向间隔
itemWidth: 20, // 值域图形宽度,线性渐变水平布局宽度为该值 * 10
itemHeight: 14, // 值域图形高度,线性渐变垂直布局高度为该值 * 10
splitNumber: 5, // 分割段数,默认为5,为0时为线性渐变
color:['#1e90ff','#f0ffff'],//颜色
//text:['高','低'], // 文本,默认为数值文本
textStyle: {
color: '#333' // 值域文字颜色
}
},
toolbox: {
orient: 'horizontal', // 布局方式,默认为水平布局,可选为:
// 'horizontal' ¦ 'vertical'
x: 'right', // 水平安放位置,默认为全图右对齐,可选为:
// 'center' ¦ 'left' ¦ 'right'
// ¦ {number}(x坐标,单位px)
y: 'top', // 垂直安放位置,默认为全图顶端,可选为:
// 'top' ¦ 'bottom' ¦ 'center'
// ¦ {number}(y坐标,单位px)
color : ['#1e90ff','#22bb22','#4b0082','#d2691e'],
backgroundColor: 'rgba(0,0,0,0)', // 工具箱背景颜色
borderColor: '#ccc', // 工具箱边框颜色
borderWidth: 0, // 工具箱边框线宽,单位px,默认为0(无边框)
padding: 5, // 工具箱内边距,单位px,默认各方向内边距为5,
// 接受数组分别设定上右下左边距,同css
itemGap: 10, // 各个item之间的间隔,单位px,默认为10,
// 横向布局时为水平间隔,纵向布局时为纵向间隔
itemSize: 16, // 工具箱图形宽度
featureImageIcon : {}, // 自定义图片icon
featureTitle : {
mark : '辅助线开关',
markUndo : '删除辅助线',
markClear : '清空辅助线',
dataZoom : '区域缩放',
dataZoomReset : '区域缩放后退',
dataView : '数据视图',
lineChart : '折线图切换',
barChart : '柱形图切换',
restore : '还原',
saveAsImage : '保存为图片'
}
},
区域缩放控制器
dataZoom: {
orient: 'horizontal', // 布局方式,默认为水平布局,可选为:
// 'horizontal' ¦ 'vertical'
// x: {number}, // 水平安放位置,默认为根据grid参数适配,可选为:
// {number}(x坐标,单位px)
// y: {number}, // 垂直安放位置,默认为根据grid参数适配,可选为:
// {number}(y坐标,单位px)
// width: {number}, // 指定宽度,横向布局时默认为根据grid参数适配
// height: {number}, // 指定高度,纵向布局时默认为根据grid参数适配
backgroundColor: 'rgba(0,0,0,0)', // 背景颜色
dataBackgroundColor: '#eee', // 数据背景颜色
fillerColor: 'rgba(144,197,237,0.2)', // 填充颜色
handleColor: 'rgba(70,130,180,0.8)' // 手柄颜色
},
下面是我据此做的一个小案例,希望对你们有所帮助
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="echarts/echarts.min.js"></script>
<script src="script/moment.js"></script>
</head>
<body>
<div id="echarts" style="width: 1300px;height: 600px;margin: auto"></div>
<script>
var myEcharts = echarts.init(document.getElementById("echarts"));
var option = {
//标题组
title:[{
x:"center",
y:"top",
text:"城市AQI空气质量数据图",
textAlign:"auto",
textVerticalAlign:'auto',
textStyle:{
color:"#757077",
fontSize:"20px",
align:"center",
}
},
],
//提示框
tooltip:{
trigger:"axis",
borderColor:"#aed1b9",
borderWidth:"1px",
},
legend:{
x:"right",
y:"top"
},
//x轴
xAxis: {
type: "time",
axisLabel:{
formatter: function (val) {
var hourTime = moment(val).format("HH") + "时";
if (hourTime[1] ==="0" && hourTime[0] === "0"){
hourTime = moment(val).format("MM") + "月" + moment(val).format("DD") + "日";
}
return hourTime
}
}
},
//y轴
yAxis:{
type:"value",
},
//参数
series:[
{
name:"邢台",
itemStyle : {
normal: {
label : {
show: true
}
},
},
type:"line",
data:[
['2022-05-01 08', '70'],
['2022-05-01 09', '68'],
['2022-05-01 10', '45'],
['2022-05-01 11', '53'],
['2022-05-01 12', '49'],
['2022-05-01 13', '54'],
['2022-05-01 14', '58'],
['2022-05-01 15', '55'],
['2022-05-01 16', '48'],
['2022-05-01 17', '34'],
['2022-05-01 18', '35'],
['2022-05-01 19', '33'],
['2022-05-01 20', '31'],
['2022-05-01 21', '32'],
['2022-05-01 22', '36'],
['2022-05-01 23', '37'],
['2022-05-02 00', '40'],
['2022-05-02 01', '45'],
['2022-05-02 02', '43'],
['2022-05-02 03', '42'],
['2022-05-02 04', '46'],
['2022-05-02 05', '47'],
['2022-05-02 06', '45'],
['2022-05-02 07', '48'],
]
},
{
name:"保定",
itemStyle : {
normal: {
label : {
show: true
}
},
},
type:"line",
data:[
['2022-05-01 08', '45'],
['2022-05-01 09','46'],
['2022-05-01 10', '54'],
['2022-05-01 11', '55'],
['2022-05-01 12', '64'],
['2022-05-01 13', '53'],
['2022-05-01 14', '46'],
['2022-05-01 15', '47'],
['2022-05-01 16', '45'],
['2022-05-01 17', '39'],
['2022-05-01 18', '43'],
['2022-05-01 19', '44'],
['2022-05-01 20', '42'],
['2022-05-01 21', '38'],
['2022-05-01 22', '40'],
['2022-05-01 23', '41'],
['2022-05-02 00', '26'],
['2022-05-02 01', '38'],
['2022-05-02 02', '40'],
['2022-05-02 03', '46'],
['2022-05-02 04', '50'],
['2022-05-02 05', '57'],
['2022-05-02 06', '63'],
['2022-05-02 07', '66'],
]
},
{
name:"邯郸",
itemStyle : {
normal: {
label : {
show: true
}
},
},
type:"line",
data:[
['2022-05-01 08', '44'],
['2022-05-01 09','38'],
['2022-05-01 10', '56'],
['2022-05-01 11', '62'],
['2022-05-01 12', '58'],
['2022-05-01 13', '60'],
['2022-05-01 14', '64'],
['2022-05-01 15', '70'],
['2022-05-01 16', '65'],
['2022-05-01 17', '64'],
['2022-05-01 18', '62'],
['2022-05-01 19', '59'],
['2022-05-01 20', '56'],
['2022-05-01 21', '54'],
['2022-05-01 22', '24'],
['2022-05-01 23', '22'],
['2022-05-02 00', '30'],
['2022-05-02 01', '35'],
['2022-05-02 02', '33'],
['2022-05-02 03', '34'],
['2022-05-02 04', '40'],
['2022-05-02 05', '64'],
['2022-05-02 06', '60'],
['2022-05-02 07', '70'],
]
},
],
//网格缩放控制器
dataZoom: {
orient: 'horizontal',
backgroundColor: 'rgba(0,0,0,0)',
dataBackgroundColor: '#eee',
fillerColor: 'rgba(144,197,237,0.2)',
handleColor: 'rgba(70,130,180,0.8)'
},
//值域
dataRange: {
orient: 'vertical',
x: 'left',
y: 'bottom',
backgroundColor: 'rgba(0,0,0,0)',
borderColor: '#ccc',
borderWidth: 0,
padding: 5,
itemGap: 10,
itemWidth: 20,
itemHeight: 14,
splitNumber: 5,
color:['#1e90ff','#28ffb9'],
// text:['高','低'],
textStyle: {
color: '#333'
}
},
//画布
grid: {
show:true,
backgroundColor: "#e3fffd",
// borderColor: "rgba(96, 67, 67, 1)",
// containLabel: true
},
};
myEcharts.setOption(option);
</script>
</body>
</html>
预览效果如下: