1、首先新建一个vue2的项目,项目中安装Echarts
cnpm install echarts --save
2、新建一个ref
- <template>
- <div ref="myecharts" id="myecharts">div>
- template>
3、引入echarts
- <script>
- import * as echarts from 'echarts';
- export default {
- mounted() {
- let myEcharts = echarts.init(this.$refs.myecharts)
- myEcharts.setOption({
- title: {
- text: 'helloword'
- },
- xAxis: {
- data: [
- {value:'李玉',age:'21'},
- {value:'简隋英',age:'23'},
- {value:'江停',age:'24'},
- {value:'严峫',age:'23'}
- ]
- },
- yAxis: {
-
- },
- series: [
- {
- name:'男主',
- type: 'bar',
- data:[21, 23, 24,23]
- }
- ]
- })
- }
- }
- script>
4、设置#myecharts的样式
- <style scoped>
- #myecharts {
- width: 500px;
- height: 500px;
- border: 1px solid red;
- }
- style>
5、执行npm run dev显示如下:
1、新建一个ref
- <template>
- <div ref="myecharts" id="myecharts">div>
- template>
2、引入echarts
- <script>
- import * as echarts from 'echarts';
- export default {
- mounted() {
- let myEcharts = echarts.init(this.$refs.myecharts)
- let xData = ['李玉', '简隋英', '江停', '严峫']
- let yData = [21, 23, 24, 23]
- let option = {
- title: {
- text: '小说人物'
- },
- xAxis: {
- data: xData,
- type:'category',//坐标轴类型,value数据轴,category类目轴
- },
- yAxis: {
-
- },
- series: [
- {
- name: '男主',
- type: 'bar',//系列类
- data: yData
- }
- ]
- }
- myEcharts.setOption(option)
- }
- }
- script>
3、设置#myecharts的样式
- <style scoped>
- #myecharts {
- width: 500px;
- height: 500px;
- border: 1px solid red;
- }
- style>
4、执行npm run dev显示如下:
注意:在 series : [ { markPoint : { ... } } ]
- markPoint: {
- data: [
- {
- type: 'max',
- name: '最大值'
- },
- {
- type: 'min',
- name: '最小值'
- }
- ]
- }
注意:在 series : [ { markLine : { ... } } ]
- markLine:{
- data:[
- {
- type:'average',
- name:'平均值'
- }
- ]
- },
注意:将xAxis和yAxis设置交换即可
- xAxis: {
- type:'value'
- },
- yAxis: {
- data: xData,
- type: 'category',//坐标轴类型,value数据轴,category类目轴
- },
注意:series : [ { barWidth : ... } ]
barWidth:20,
注意:series : [ { color : ... } ]
color:'green',
注意: series : [ { itemStyle : normal : { color : function (params) {...} } } ]
- itemStyle:{
- normal:{
- color:function(params){
- let colorList = [
- 'red',
- 'green',
- 'pink',
- 'yellow'
- ]
- return colorList[params.dataIndex]
- }
- }
- },
注意:新建一个基本的饼图
<div ref="bar" id="bar">div>
- #bar{
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.bar)
- let data = [
- {value:33,name:'香蕉'},
- {value:31,name:'苹果'},
- {value:32,name:'葡萄'},
- {value:22,name:'荔枝'}
- ]
- let option = {
- title: {
- text: '饼状图',
- left: 'center'
- },
- series:[
- {
- name: '统计',
- type: 'pie',
- data
- }
- ]
- }
- myEcharts.setOption(option)
- }
注意:legend:{}
legend:{},
注意:legend : { ... },
- legend:{
- left:'left',
- orient: 'verical'
- },
注意:series [ { radius : [ '' " ,'' " ] } ]
//设置饼图的半径,一项为内半径,第二项为外半径
- //设置饼图的半径,一项为内半径,第二项为外半径
- radius:['40%','70%'],
注意:series [ { label : { show: false} } ]
- label:{
- show: false//默认值是true
- }
- label: {
- show: true,//默认值是true
- // outsize:外展示,inside内展示,center中心展示
- position:'inside',
- }
注意:series [ { roseType : 'area' } ]
roseType: 'area',
注意:series [ { itemStyle : { ... } } ]
- itemStyle:{
- shadowBlur: 200,
- shadowColor: '#000'
- }
注意:在data里加itemstyle.normal.color
- let data = [
- { value: 33, name: '香蕉', itemStyle: { normal: { color: 'red' } } },
- { value: 31, name: '苹果', itemStyle: { normal: { color: 'blue' } } },
- { value: 32, name: '葡萄', itemStyle: { normal: { color: 'yellow' } } },
- { value: 22, name: '荔枝', itemStyle: { normal: { color: 'green' } } }
- ]
注意:新建一个基本的折线图
<div ref="line" id="line">div>
- #line {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.line)
- let xData = ['星期一', '星期二', '星期三', '星期四', '星期五']
- let data = [130, 25, 30, 45, 100]
- let option = {
- xAxis: {
- type: 'category',
- data: xData
- },
- yAxis: {
- type: 'value'
- },
- series: [
- {
- type: 'line',
- data
- }
- ]
- }
- myEcharts.setOption(option)
- }
注意:series [ { smooth: true } ]
smooth: true
注意:series [ { areaStyle:{} } ]
areaStyle:{}
注意:series [ { markPoint:{} } ]
- markPoint: {
- data: [
- { type: 'max', name: '最大值' },
- { type: 'min', name: '最小值' }
- ]
- }
注意:series [ { markLine:{} } ]
- markLine: {
- data: [
- { type: 'average', name: '平均值' }
- ]
- }
注意:新建一个基本的折线图
<div ref="line" id="line">div>
- #line {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.line)
- let xData = ['星期一', '星期二', '星期三', '星期四', '星期五']
-
- let dataA = [130, 25, 30, 45, 100]
- let dataB = [12, 54, 87, 56, 123]
- let dataC = [34, 44, 65, 123, 23]
- let dataD = [12, 65, 98, 121, 132]
-
- let option = {
- title:{
- text:'销量'
- },
- xAxis: {
- type: 'category',
- data: xData
- },
- yAxis: {
- type: 'value'
- },
- series: [
- {
- name:'香蕉',
- type:'line',
- stack:'num',
- data:dataA
- },
- {
- name:'苹果',
- type:'line',
- stack:'num',
- data:dataB
- },
- {
- name:'荔枝',
- type:'line',
- stack:'num',
- data:dataC
- },
- {
- name:'芒果',
- type:'line',
- stack:'num',
- data:dataD
- },
- ]
- }
- myEcharts.setOption(option)
- }
注意:在每一个里面设置areaStyle:{}
- series: [
- {
- name:'香蕉',
- type:'line',
- stack:'num',
- data:dataA,
- areaStyle:{}
- },
- {
- name:'苹果',
- type:'line',
- stack:'num',
- data:dataB,
- areaStyle:{}
- },
- {
- name:'荔枝',
- type:'line',
- stack:'num',
- data:dataC,
- areaStyle:{}
- },
- {
- name:'芒果',
- type:'line',
- stack:'num',
- data:dataD,
- areaStyle:{}
- },
- ]
注意:如果每一个都需要设置高亮效果,就将每一个系列都设置上
- emphasis: {
- focus: 'series'
- }
注意:新建一个基本的散点图
<div ref="scatter" id="scatter">div>
- #scatter {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.scatter)
- let option = {
- xAxis: {},
- yAxis: {},
- series: [
- {
- data: [
- [18.2, 6.83],
- [6.32, 5.63],
- [17.0, 6.55],
- [8.18, 5.12],
- [15.0, 7.56]
- ],
- type:'scatter',//散点图
- }
-
- ]
- }
- myEcharts.setOption(option)
- }
注意:series [ { symbolSize : 数值 } ]
symbolSize: 50,
注意:series [ { color :' 色值' } ]
color:'red'
注意:series [ { color :' 色值' } ]
- color: {
- type: 'linear',//线性渐变
- x: 0,//相当于图形包围盒中的百分比
- y: 0,
- x2: 1,
- y2: 0,
- colorStops: [
- {
- offset: 0,
- color: 'green'
- }, {
- offset: 1,
- color: 'rgba(255,233,9)'
- }
- ]
- }
注意:series [ { emphasis : ... } ]
- emphasis: {
- itemStyle: {
- borderColor: 'rgba(100,200,50,0.5)',
- borderWidth: 30
- }
- },
注意:tooltip:{},
tooltip:{},
注意:grid : { ... }
- grid:{
- left:'70px',
- right:'20%',
- top:'30px',
- bottom:'20%'
- },
注意:grid : { show : true }
show:true,
注意:grid : { backgroundColor : ' 色值 ', }
// 设置背景色时show:true,不然设置项不显示
- grid: {
- show:true,
- backgroundColor:'yellow',
- }
注意:grid : { borderColor : ' 色值 ', }
// 设置背景色时show:true,不然设置项不显示
borderColor:'red',
注意:新建一个基本的k线图
<div ref="candlestick" id="candlestick">div>
- #candlestick {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- export default {
- mounted() {
- let myEcharts = echarts.init(this.$refs.candlestick)
- let option = {
- xAxis: {
- data: ['香蕉', '苹果', '葡萄', '荔枝', '芒果']
- },
- yAxis: {},
- series: [
- {
- type: 'candlestick',
- data: [
- [20,34,10,38],
- [40,35,30,50],
- [31,38,33,44],
- [38,15,5,42]
- ]
- }
- ]
- }
- myEcharts.setOption(option)
- }
- }
注意:tooltip : { ... }
- tooltip: {
- trgger: 'axis',
- axisPointer: {
- type: 'cross'
- }
- },
注意:上涨:series [ { itemStyle : { color : ' 色值 ' } } ]
下跌:series [ { itemStyle : { color0 : ' 色值 ' } } ]
- itemStyle: {
- color: 'blue',
- color0: 'yellow'
- },
注意:上涨:series [ { itemStyle : { borderColor : ' 色值 ' } } ]
下跌:series [ { itemStyle : { borderColor0 : ' 色值 ' } } ]
- itemStyle: {
- borderColor:'#000',
- borderColor0:'#000',
- },
注意:series [ { markPoint : { data : [ ... ] } } ]
- markPoint: {
- data: [
- {
- name: '最大值',
- type: 'max',
- valueDim: 'highest'//在那个维度上设置最发值或最小值
- },
- {
- name: '最小值',
- type: 'min',
- valueDim: 'lowest'
- },
- {
- name: '平均值',
- type: 'average',
- valueDim: 'close'
- }
- ]
- },
注意:新建一个基本的雷达图
<div ref="radar" id="myRadar">div>
- #myRadar {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.radar)
- let option = {
- title: {
- title: '雷达图'
- },
- radar: [
- {
- indicator: [
- { name: '芒果', max: 6500 },
- { name: '葡萄', max: 16000 },
- { name: '香蕉', max: 30000 },
- { name: '苹果', max: 52000 },
- { name: '荔枝', max: 38000 }
- ]
- }
- ],
- series: [
- {
- type: 'radar',
- data: [
- {
- value: [4300, 6400, 5400, 3400, 2300]
- }
- ]
- }
- ]
- }
- myEcharts.setOption(option)
- }
注意:radar : [ { shape : 'circle' } ]
shape: 'circle',
注意:radar : [ { radius : 大小 } ]
radius:100,
注意:radar : [ { startAngle : 旋转度数 } ]
startAngle:200,
注意:radar : [ { splitNumber : 层数 } ]
splitNumber:10,
注意:
- axisName: {
- formatter: '{value}',
- color: 'red'
- },
- splitArea:{
- areaStyle:{
- color:['green','yellow','pink','red','blue'],
- shadowColor:'0,0,0,0.2',
- shadowBlur:10
- }
- },
注意:
- lineStyle: {
- type: 'dashed'
- },
注意:
- areaStyle:{
- color:'rgba(255,255,0,0.5)'
- }
注意:新建一个基本的漏斗图
<div id="funnel" ref="funnel">div>
- #funnel {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.funnel);
- let option = {
- title: {
- text: "漏斗图",
- },
- tooltip: {
- trigger: "item",
- formatter: "{a}
{b}:{c}%", - },
- series: [
- {
- type: "funnel",
- data: [
- { value: 60, name: "芒果" },
- { value: 40, name: "青柠" },
- { value: 20, name: "柚子" },
- { value: 80, name: "橘子" },
- { value: 100, name: "苹果" },
- ],
- },
- ],
- };
- myEcharts.setOption(option);
- },
注意:
right:'50%'
注意:
sort: "ascending", //默认降序排、ascending递增排序、none按照写的顺序排
注意:
- itemStyle: {
- borderColor: "red",
- borderWidth: 3,
- },
注意:
- label: {
- //提示信息
- show: true,
- position: "inside",
- },
- emphasis: {
- label: {
- fontSize: 30,
- },
- },
注意:新建一个基本的仪表盘
<div id="gauge" ref="gauge">div>
- #gauge {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- mounted() {
- let myEcharts = echarts.init(this.$refs.gauge);
- let option = {
- title: {
- text: "漏斗图",
- },
- series: [
- {
- type: "gauge",
- },
- ],
- };
- myEcharts.setOption(option);
- },
注意:
- data: [
- {
- value: 50,
- name: "信息",
- },
- ],
注意:
- detail: {
- calueAnimation: true,
- },
注意:
- progress: {
- show: true,
- },
注意:新建一个基本的仪表盘
<div id="graph" ref="graph">div>
- #graph {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- import * as echarts from "echarts";
- export default {
- data(){
- return{
- list:[
- {
- name:'江停',
- id:'1'
- },
- {
- name:'严峫',
- id:'2'
- },
- {
- name:'吴雩',
- id:'3'
- },
- {
- name:'步重华',
- id:'4'
- },
- ]
- }
- },
- mounted() {
- let myEcharts = echarts.init(this.$refs.graph);
- let option = {
- title: {
- text: "关系图",
- },
- series: [
- {
- type: "graph",
- data: this.list,
- layout: 'force'
- },
- ],
- };
- myEcharts.setOption(option);
- },
- };
注意:
symbolSize: 30,
注意:
- itemStyle: {
- color: "#95dcb2",
- },
注意:
- label: {
- //图形上的文字
- show: true,
- position: "bottom",
- distance: 20, //距离图形的位置
- fontSize: 16,
- },
注意:
- force: {
- repulsion: 100,
- qravity: 0.01,
- edgeLength: 200,
- },
注意:
- data() {
- return {
- num:[
- {
- source:'1',//边的节点
- target:'2',//目标节点
- relation:{
- name:'夫夫'
- }
- },
- {
- source:'3',//边的节点
- target:'4',//目标节点
- relation:{
- name:'夫夫'
- }
- },
- {
- source:'1',//边的节点
- target:'3',//目标节点
- relation:{
- name:'朋友'
- }
- },
- {
- source:'2',//边的节点
- target:'4',//目标节点
- relation:{
- name:'表兄弟'
- }
- }
- ]
- }
-
-
- series: [
- {
- links:this.num
- }
- ]
注意:
- edgeLabel: {
- show: true,
- position: "middle",
- formatter: (params) => {
- return params.data.relation.name;
- },
- },
注意:新建折线图 dataZoom
- dataZoom: [
- {//设置横向的
- type: "slider",
- xAxisIndex: 0,
- fliterMode: "none",
- },
- {//设置纵向的
- type: "slider",
- yAxisIndex: 0,
- fliterMode: "none",
- },
- ],
注意:新建一个基本的树状图
<div ref="tree" id="tree">div>
- #tree {
- height: 500px;
- width: 500px;
- border: 1px solid #ccc;
- }
import * as echarts from 'echarts';
- import * as echarts from "echarts";
- export default {
- data() {
- return {
- list: {
- //最外层的数据就是树的根节点
- name: "根节点",
- children: [
- {
- name: "层1",
- children: [
- { name: "层2" },
- { name: "层2" },
- { name: "层2" },
- { name: "层2" },
- ],
- },
- {
- name: "层1",
- children: [
- { name: "层2" },
- { name: "层2" },
- { name: "层2" },
- { name: "层2" },
- ],
- },
- ],
- },
- };
- },
- mounted() {
- let myEcharts = echarts.init(this.$refs.tree);
- let option = {
- tooltip: {
- trigger: "item",
- },
- series: [
- {
- type: "tree",
- data: [this.list],
- },
- ],
- };
- myEcharts.setOption(option);
- },
- };
注意:
symbolSize:50
注意:
- label:{
- position:'left'
- }
注意:
- emphasis:{
- focus:'descendant'
- }
注意:利用大写的字母来表示方向
- // orient:'LR',//从左到右
- // orient:'RL',//从右到左
- // orient:'TB',//从上到下
- orient:'BT',//从下到上
注意:
- label: {
- rotate:90,
- },
- dataset: [
- {
- //数据的分类
- dimensions: ["类别", "数量"],
- //分类数据
- source: [
- ["苹果", 20],
- ["香蕉", 28],
- ["荔枝", 34],
- ],
- },
- {
- transform: {
- type: "sort", //数据排序,sort根据大小排序
- config: {
- dimension: "数量", //排序基于谁排序
- order:'desc'
- },
- },
- },
- ],
- series:[
- {
- type: 'bar',
- encode:{//数据的映射
- x:'类别',
- y:'数量',
- },
- datasetIndex:1//使用dataset的第一条数据
- }
- ]
let myEcharts = echarts.init(this.$refs.tree,'dark');
去官网下载自定义主题