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 ...
随机推荐
- hive元数据格式化 在hive中执行sql语句:SemanticException org.apache.hadoop.hive.ql.metadata.HiveException:
https://blog.csdn.net/xiaoqiu_cr/article/details/80913437
- Codeforces 1119E Pavel and Triangles (贪心)
Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal t ...
- HDU 5443 The Water Problem (ST算法)
题目链接:HDU 5443 Problem Description In Land waterless, water is a very limited resource. People always ...
- Codeforces 1183A Nearest Interesting Number
题目链接:http://codeforces.com/problemset/problem/1183/A 题意:求不小于 a 且每位数和能被 4 整除的 最小 n 思路:暴力模拟就好. AC代码: # ...
- java-day27
## Bootstrap: 1. 概念: 一个前端开发的框架,Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JavaScri ...
- git branch 分支和分支合并
一般一个项目有一个默认的分支 master 主分支,然后可以有许多个分支,在别的分支上的操作不会影响到主分支.使用git branch查看当前多多少分支以及当前处于哪个分支上:执行git branch ...
- oh my zsh 如何启用插件
注 根据自己的需求启用插件.但是,插件具体实现什么功能就得自己看啦. 官网说明 实践 其实默认oh my zsh(以下简称zsh)已经在安装的时候就帮我们下载好了所有插件,只不过需要用户自己选择启用哪 ...
- css 给图片添加滤镜效果,透明层毛玻璃效果
我们用的第一个滤镜是sepia(),他会给图片增加一整降饱和度的橙色染色效果 原图 添加sepia滤镜的效果 img{ width:100%; transition: .5s filter; filt ...
- Mysql优化-典型的服务器配置
内存配置相关参数 mysql内存分配需要考虑到操作系统需要使用的内存,其他应用程序所要使用的内存,mysql的会话数以及每个会话使用的内存,然后就是操作系统实例所使用的内存.生产环境的mysql往往都 ...
- PHP算法之有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认为是 ...