Commit 813cafa39c824eac887bf82fa3e13800fb5ca4f1
1 parent
e7c25d67
bar echarts
Showing
5 changed files
with
173 additions
and
2 deletions
package-lock.json
| ... | ... | @@ -2675,6 +2675,14 @@ |
| 2675 | 2675 | "stream-shift": "^1.0.0" |
| 2676 | 2676 | } |
| 2677 | 2677 | }, |
| 2678 | + "echarts": { | |
| 2679 | + "version": "4.2.0-rc.2", | |
| 2680 | + "resolved": "http://registry.npm.taobao.org/echarts/download/echarts-4.2.0-rc.2.tgz", | |
| 2681 | + "integrity": "sha1-apg5eq+oG2XL8LwV2a/b+yRN+R4=", | |
| 2682 | + "requires": { | |
| 2683 | + "zrender": "4.0.5" | |
| 2684 | + } | |
| 2685 | + }, | |
| 2678 | 2686 | "ee-first": { |
| 2679 | 2687 | "version": "1.1.1", |
| 2680 | 2688 | "resolved": "http://registry.npm.taobao.org/ee-first/download/ee-first-1.1.1.tgz", |
| ... | ... | @@ -10760,6 +10768,11 @@ |
| 10760 | 10768 | "dev": true |
| 10761 | 10769 | } |
| 10762 | 10770 | } |
| 10771 | + }, | |
| 10772 | + "zrender": { | |
| 10773 | + "version": "4.0.5", | |
| 10774 | + "resolved": "http://registry.npm.taobao.org/zrender/download/zrender-4.0.5.tgz", | |
| 10775 | + "integrity": "sha1-bo9ziXHOLNYkqsgrIVZymxwOWoI=" | |
| 10763 | 10776 | } |
| 10764 | 10777 | } |
| 10765 | 10778 | } | ... | ... |
package.json
src/components/barChart.vue
0 → 100644
| 1 | +<template> | |
| 2 | + <div :class="className" :style="{height:height,width:width}"></div> | |
| 3 | +</template> | |
| 4 | + | |
| 5 | +<script> | |
| 6 | +import echarts from 'echarts' | |
| 7 | + | |
| 8 | +require('echarts/theme/macarons') // echarts theme | |
| 9 | +import {debounce} from '../utils/debounce' | |
| 10 | + | |
| 11 | +export default { | |
| 12 | + name: 'barChart', | |
| 13 | + props: { | |
| 14 | + className: { | |
| 15 | + type: String, | |
| 16 | + default: 'chart' | |
| 17 | + }, | |
| 18 | + width: { | |
| 19 | + type: String, | |
| 20 | + default: '100%' | |
| 21 | + }, | |
| 22 | + height: { | |
| 23 | + type: String, | |
| 24 | + default: '350px' | |
| 25 | + }, | |
| 26 | + autoResize: { | |
| 27 | + type: Boolean, | |
| 28 | + default: true | |
| 29 | + }, | |
| 30 | + chartData: { | |
| 31 | + type: Object, | |
| 32 | + required: true | |
| 33 | + } | |
| 34 | + }, | |
| 35 | + data() { | |
| 36 | + return { | |
| 37 | + chart: null, | |
| 38 | + sidebarElm: null | |
| 39 | + } | |
| 40 | + }, | |
| 41 | + watch: { | |
| 42 | + chartData: { | |
| 43 | + deep: true, | |
| 44 | + } | |
| 45 | + }, | |
| 46 | + mounted() { | |
| 47 | + this.initChart() | |
| 48 | + if (this.autoResize) { | |
| 49 | + this.__resizeHandler = debounce(() => { | |
| 50 | + if (this.chart) { | |
| 51 | + this.chart.resize() | |
| 52 | + } | |
| 53 | + }, 100) | |
| 54 | + window.addEventListener('resize', this.__resizeHandler) | |
| 55 | + } | |
| 56 | + }, | |
| 57 | + beforeDestroy() { | |
| 58 | + if (!this.chart) { | |
| 59 | + return | |
| 60 | + } | |
| 61 | + if (this.autoResize) { | |
| 62 | + window.removeEventListener('resize', this.__resizeHandler) | |
| 63 | + } | |
| 64 | + | |
| 65 | + this.chart.dispose() | |
| 66 | + this.chart = null | |
| 67 | + }, | |
| 68 | + methods: { | |
| 69 | + setOptions({xData, yData} = {}) { | |
| 70 | + this.chart.setOption({ | |
| 71 | + xAxis: { | |
| 72 | + data: xData, | |
| 73 | + boundaryGap: false, | |
| 74 | + axisTick: { | |
| 75 | + show: false | |
| 76 | + } | |
| 77 | + }, | |
| 78 | + grid: { | |
| 79 | + left: 10, | |
| 80 | + right: 10, | |
| 81 | + bottom: 20, | |
| 82 | + top: 30, | |
| 83 | + containLabel: true | |
| 84 | + }, | |
| 85 | + tooltip: { | |
| 86 | + trigger: 'axis', | |
| 87 | + axisPointer: { | |
| 88 | + type: 'cross' | |
| 89 | + }, | |
| 90 | + padding: [5, 10] | |
| 91 | + }, | |
| 92 | + yAxis: { | |
| 93 | + axisTick: { | |
| 94 | + show: false | |
| 95 | + } | |
| 96 | + }, | |
| 97 | + legend: { | |
| 98 | + data: ['expected', 'actual'] | |
| 99 | + }, | |
| 100 | + series: [{ | |
| 101 | + data: yData, | |
| 102 | + type: 'bar' | |
| 103 | + }] | |
| 104 | + }) | |
| 105 | + }, | |
| 106 | + initChart() { | |
| 107 | + this.chart = echarts.init(this.$el, 'macarons') | |
| 108 | + this.setOptions(this.chartData) | |
| 109 | + } | |
| 110 | + } | |
| 111 | +} | |
| 112 | +</script> | ... | ... |
src/utils/debounce.js
0 → 100644
| 1 | +export function debounce(func, wait, immediate) { | |
| 2 | + let timeout, args, context, timestamp, result | |
| 3 | + | |
| 4 | + const later = function() { | |
| 5 | + // 据上一次触发时间间隔 | |
| 6 | + const last = +new Date() - timestamp | |
| 7 | + | |
| 8 | + // 上次被包装函数被调用时间间隔last小于设定时间间隔wait | |
| 9 | + if (last < wait && last > 0) { | |
| 10 | + timeout = setTimeout(later, wait - last) | |
| 11 | + } else { | |
| 12 | + timeout = null | |
| 13 | + // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用 | |
| 14 | + if (!immediate) { | |
| 15 | + result = func.apply(context, args) | |
| 16 | + if (!timeout) context = args = null | |
| 17 | + } | |
| 18 | + } | |
| 19 | + } | |
| 20 | + | |
| 21 | + return function(...args) { | |
| 22 | + context = this | |
| 23 | + timestamp = +new Date() | |
| 24 | + const callNow = immediate && !timeout | |
| 25 | + // 如果延时不存在,重新设定延时 | |
| 26 | + if (!timeout) timeout = setTimeout(later, wait) | |
| 27 | + if (callNow) { | |
| 28 | + result = func.apply(context, args) | |
| 29 | + context = args = null | |
| 30 | + } | |
| 31 | + | |
| 32 | + return result | |
| 33 | + } | |
| 34 | +} | ... | ... |
src/views/pdasection.vue
| ... | ... | @@ -3,23 +3,34 @@ |
| 3 | 3 | <titlesection></titlesection> |
| 4 | 4 | <totalsection :totalNum="totalVal"></totalsection> |
| 5 | 5 | <div class="flexfm"></div> |
| 6 | + <bar-chart :chart-data="lineChartData"/> | |
| 6 | 7 | </div> |
| 7 | 8 | </template> |
| 8 | 9 | |
| 9 | 10 | <script> |
| 10 | 11 | import titlesection from '../components/titlesection' |
| 11 | 12 | import totalsection from '../components/total' |
| 13 | +import barChart from '../components/barChart' | |
| 12 | 14 | import {fetchList} from '../api/api' |
| 13 | 15 | |
| 16 | +const lineChartData = { | |
| 17 | + newVisitis: { | |
| 18 | + yData: [100, 120, 161], | |
| 19 | + xData: ['正常', '异常', '故障'] | |
| 20 | + } | |
| 21 | +} | |
| 22 | + | |
| 14 | 23 | export default { |
| 15 | 24 | name: 'pdasection', |
| 16 | 25 | components: { |
| 17 | 26 | titlesection, |
| 18 | - totalsection | |
| 27 | + totalsection, | |
| 28 | + barChart | |
| 19 | 29 | }, |
| 20 | 30 | data() { |
| 21 | 31 | return { |
| 22 | - totalVal: '1000', | |
| 32 | + totalVal: '213734', | |
| 33 | + lineChartData: lineChartData.newVisitis | |
| 23 | 34 | } |
| 24 | 35 | }, |
| 25 | 36 | created() { | ... | ... |