1.安装

npm install echarts -s

2.例——点击tab切换时柱状图重绘,高度根据返回数据设置。

<template>
<div>
<ul id="tabs" class="tabs">
<li class="tab" :class="{'active':tabIndex=='1'}" @click="reGetCount('1')">周</li>
<li class="tab" :class="{'active':tabIndex=='2'}" @click="reGetCount('2')">月</li>
<li class="tab" :class="{'active':tabIndex=='3'}" @click="reGetCount('3')">年</li>
</ul> <div class="canvas">
<div id="chart_bar" :style="{width: winWid+'px'}"></div>
</div>
</div>
</template>
<script>
 //按需引入
// 引入基本模板
let echarts = require('echarts/lib/echarts')
// 引入图形组件
require('echarts/lib/chart/pie')
require('echarts/lib/chart/bar')
require('echarts/lib/chart/line')
// 引入提示框、title、图例组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend') export default {
name: 'Echart',
data() {
return {
tabIndex: '1',
winWid: screen.availWidth,
bar: {
list: [],
name: [],
data: []
},
barHeight: 0,
options_bar: {}
}
},
mounted() {
this.getCount();
},
methods: {
getCount() {
let _this = this,
getParam = {
param: {
param1: ***,
param2: _this.tabIndex
}
};
_this.axios.get('***', {
params: getParam
}).then(function(res) {
if (res.status == 200 && res.data.result == 0) {
let _data = res.data.message;
_this.bar.list = _data.list;
_this.drawBar();
} else {
console.log('获取数据失败');
}
}).catch(function(err) {
console.log(err);
})
},
reGetCount(tab) {
let _this = this;
if (_this.tabIndex == tab) {
return
} else {
_this.tabIndex = tab;
_this.getCount();
}
},
drawBar() {
let _this = this,
list = _this.bar.list;
for (let i = 0; i < list.length; i++) {
_this.bar.name[i] = list[i].name;
_this.bar.data[i] = list[i].num;
} let obj = document.getElementById('chart_bar'),
chart_bar = echarts.init(document.getElementById('chart_bar'), ); chart_bar.clear();//清空画布
chart_bar.setOption({
title: {
text: '排行榜'
},
tooltip: {//点击图形时显示详细数据
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
legend: {
data: ['金额']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'value',
name: '元',
boundaryGap: true,
axisLabel: {
interval: 0,
formatter: function(value) {//金额超过千显示k
var res = value;
if (res >= 1000) {
res = res / 1000 + res % 1000 + 'k';
}
return res;
}
}
},
yAxis: {
type: 'category',
name: '姓名',
minInterval: 1,
boundaryGap: [0, 0.1],
triggerEvent: true,
axisLabel: {
formatter: function(value) {//姓名超过3个字加省略号
var res = value;
if (res.length > 3) {
res = res.substring(0, 3) + "..";
}
return res;
}
},
data: _this.bar.name,
// data: ['王一王一', '张二', '吴三', '陈四', '张二', '吴三', '陈四', '王一', '张二', '吴三', '陈四']
},
series: [{
name: '金额',//注意与lengend名称必须一致,否则不显示图例
itemStyle: {//柱图背景色
color: 'lightcoral'
},
type: 'bar',
barWidth: 10, //柱图宽度
data: _this.bar.data,
// data: [7, 12, 8, 3, 7, 1000, 8, 3, 7, 8, 3]
}],
});
_this.barHeight = list.length * 50 + 100;
obj.style.height = _this.barHeight + "px";
     //******
// chart_bar.getDom().style.height = _this.barHeight + "px";
// chart_bar.getDom().childNodes[0].style.height = _this.barHeight + "px";
// chart_bar.getDom().childNodes[0].childNodes[0].setAttribute("height", _this.barHeight);
// chart_bar.getDom().childNodes[0].childNodes[0].style.height = _this.barHeight + "px";
     //******
     //我用*****部分设置高度有问题:一进页面是好的,但是tab一旦切换柱状图就会变形
chart_bar.resize();//区域更新
}
}
}
</script>

  

最新文章

  1. laydate兼容bootstrap
  2. js封装用户选项传递给Servlet之考试系统二
  3. iOS 字典与JSON相互转换
  4. iOS:根据日志去定位网络请求发生的错误是由于服务端造成的,还是客户端造成的?
  5. 模拟jQuery简单封装ajax
  6. Shogun网站上的关于主流机器学习工具包的比较
  7. htnl5中设置文本单行显示,超出部分打省略号,鼠标移到文本时alt出全部文本内容
  8. mouseOver与rollOver
  9. 就谈个py 的装饰器 decorator
  10. jQuery全选、反选、全不选
  11. C# Byte[]数组读取和写入文件
  12. POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
  13. spark第一篇--简介,应用场景和基本原理
  14. dedecms data文件夹外迁
  15. LCA算法解析-Tarjan&amp;倍增&amp;RMQ
  16. JavaSE| 面向对象-类的五大成员
  17. mvn -N和-U的用法
  18. NE555
  19. 探索 Python、机器学习和 NLTK 库 开发一个应用程序,使用 Python、NLTK 和机器学习对 RSS 提要进行分类
  20. 剑指offer--40.翻转单词顺序列

热门文章

  1. 面向对象(三)——组合、多态、封装、property装饰器
  2. 常用内置模块(四)——subprocess、re
  3. android webview 输入法键盘遮挡输入框的问题
  4. 2018-8-10-C#-配置文件存储-各种序列化算法性能比较
  5. 使用dd生成文件
  6. codeforces1175E Minimal Segment Cover 倍增
  7. 数组去重Set也可实现
  8. Qt 【无法打开 xxxx头文件】
  9. Codeforces Round #563 (Div. 2) E. Ehab and the Expected GCD Problem
  10. IDEA 注解开发流程