vue echart例——柱状图及高度自适应
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例——柱状图及高度自适应的更多相关文章
- autoHeight.vue 高度自适应
autoHeight.vue 高度自适应 <!-- * @description 自适应高度 * @fileName autoHeight.vue * @author 彭成刚 * @date 2 ...
- Vue. 之 Element table 高度自适应
Vue. 之 Element table 高度自适应 使用vue创建table后,其高度自适应浏览器高度. 在创建的 el-table 中添加:height属性,其值为一个变量(tableHeight ...
- element el-table表格的vue组件二次封装(附表格高度自适应)
基于vue的el-table表格二次封装组件方法 前言 在公司实习使用vue+element-ui框架进行前端开发,使用表格el-table较为多,有些业务逻辑比较相似,有些地方使用的重复性高,如果多 ...
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- 巧用margin/padding的百分比值实现高度自适应(多用于占位,避免闪烁)
本文依赖于一个基础却又容易混淆的css知识点:当margin/padding取形式为百分比的值时,无论是left/right,还是top/bottom,都是以父元素的width为参照物的!也许你会说, ...
- JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)
参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...
- CSS: 解决Div float后,父Div无法高度自适应的问题
在用CSS+DIV的布局中,常常会发现,当一个DIV float之后,假设他的高度超过了其父DIV的高度时,其父DIV的高度并不会对应的进行调整.要解决问题(也叫做闭合(清除)浮动),我们有四种办法: ...
- 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- css 实现文字自动换行切同行元素高度自适应
1.实现div行内布局所有行跟随最大高度自适应 html代码样例: <div class="row-single"> <div class="colsp ...
随机推荐
- 简单介绍Collection框架的结构
Collection:List列表,Set集 Map:Hashtable,HashMap,TreeMap Collection 是单列集合 List 元素是有序的.可重复 有序的 collect ...
- 冲上云霄,Dubbo Go!
来源:开源中国社区 5 月 21 日,经过一年多的孵化,Apache Dubbo 从 Apache 软件基金会毕业,成为 Apache 顶级项目.推荐:厉害了,Dubbo 正式毕业! Dubbo 是阿 ...
- 梯度提升树GBD
转自 http://blog.csdn.net/u014568921/article/details/49383379 另外一个很容易理解的文章 :http://www.jianshu.com/p/0 ...
- 42-Ubuntu-用户管理-07-修改权限命令介绍
修改文件权限 序号 命令 作用 01 chown 修改文件/目录拥有者 02 chgrp 修改文件/目录所在主组 03 chmod 修改文件/目录权限 chmod chown chgrp ...
- js 万能判断
console.log(Object.prototype.toString.call(123)) //[object Number] console.log(Object.prototype.toSt ...
- Vue登录登出以及JWT认证
数据模型 主要用户名,密码,邮箱,头像,身份 const mongoose = require('mongoose') const schema = new mongoose.Schema({ use ...
- js 购物车的数量加减,对应的总价也随机变化
html相关的源码: <div class="goods_num clearfix"> <div class="num_name fl"> ...
- javascript基础总结之实例(一)
样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- delphi窗体透明但上面的控件不透明怎么实现
我不知道LAZARUS是什么玩意.纯用DELPHI的话.procedure TForm1.FormCreate(Sender: TObject);var mStyle, mExStyle: Longi ...
- 暑假集训test-8-31(pm)
以为可以AK,结果t3没有调出来,然后被林巨踩了. everyday被踩,很开心. 林巨真的好巨啊,这么多天已经总计虐我75分了. 1.玩具装箱 第一眼还以为是那道斜率优化dp,结果是个签到水题. / ...