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>

  

vue echart例——柱状图及高度自适应的更多相关文章

  1. autoHeight.vue 高度自适应

    autoHeight.vue 高度自适应 <!-- * @description 自适应高度 * @fileName autoHeight.vue * @author 彭成刚 * @date 2 ...

  2. Vue. 之 Element table 高度自适应

    Vue. 之 Element table 高度自适应 使用vue创建table后,其高度自适应浏览器高度. 在创建的 el-table 中添加:height属性,其值为一个变量(tableHeight ...

  3. element el-table表格的vue组件二次封装(附表格高度自适应)

    基于vue的el-table表格二次封装组件方法 前言 在公司实习使用vue+element-ui框架进行前端开发,使用表格el-table较为多,有些业务逻辑比较相似,有些地方使用的重复性高,如果多 ...

  4. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  5. 巧用margin/padding的百分比值实现高度自适应(多用于占位,避免闪烁)

    本文依赖于一个基础却又容易混淆的css知识点:当margin/padding取形式为百分比的值时,无论是left/right,还是top/bottom,都是以父元素的width为参照物的!也许你会说, ...

  6. JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)

    参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...

  7. CSS: 解决Div float后,父Div无法高度自适应的问题

    在用CSS+DIV的布局中,常常会发现,当一个DIV float之后,假设他的高度超过了其父DIV的高度时,其父DIV的高度并不会对应的进行调整.要解决问题(也叫做闭合(清除)浮动),我们有四种办法: ...

  8. 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  9. css 实现文字自动换行切同行元素高度自适应

    1.实现div行内布局所有行跟随最大高度自适应 html代码样例: <div class="row-single"> <div class="colsp ...

随机推荐

  1. 基于Python玩转人工智能最火框架 TensorFlow应用实践✍✍✍

    基于Python玩转人工智能最火框架  TensorFlow应用实践 随着 TensorFlow 在研究及产品中的应用日益广泛,很多开发者及研究者都希望能深入学习这一深度学习框架.而在昨天机器之心发起 ...

  2. json传参报错

    restful接口报错: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('e' (code 101)): w ...

  3. 文件上传到ftp服务工具类

    直接引用此java工具类就好 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcep ...

  4. layerui ios不适应问题

    .admin-main {-webkit-overflow-scrolling: touch; overflow: scroll; position: absolute; left: 0; top:  ...

  5. Effective C++之条款2:尽量以const enum inline替换 #define

    本文的标题也可以改成“用编译器替换预处理器”: const double AspectRatio = 1.653; //最好使用上述代码替换下述代码: #define ASPECT_RATIO 1.6 ...

  6. Mysql的Event

    Mysql的Event Event简介 Event是mysql中的一个事件,和触发器类似,触发器是在某条sql语句执行后可能会触发,而Event是每隔一段时间或某个特定的时间点执行,可以精确到秒. 准 ...

  7. vue App项目 首页

    1. 下载 npm i mint-ui -S 2. 引入 import MintUI from 'mint-ui' import 'mint-ui/lib/style.css' Vue.use(Min ...

  8. python模块:typing

    很多人在写完代码一段时间后回过头看代码,很可能忘记了自己写的函数需要传什么参数,返回什么类型的结果,就不得不去阅读代码的具体内容,降低了阅读的速度,加上Python本身就是一门弱类型的语言,这种现象就 ...

  9. CF838C(博弈+FWT子集卷积+多项式ln、exp)

    传送门: http://codeforces.com/problemset/problem/838/C 题解: 如果一个字符串的排列数是偶数,则先手必胜,因为如果下一层有后手必赢态,直接转移过去,不然 ...

  10. 好久不见的博客咯!——没有可持久化的可持久化treap

    每每想要去了解可持久化treap这个好写好调的东东,然后就发现网上只有一个人的——SymenYang的!在此我必须得把他批判一番——写方法不贴代码是什么心态!而且写出来的是有问题的呀!害人不浅! 好吧 ...