| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <template>
- <div>
- <div class="title" v-if="title">{{title}}</div>
- <div class="barChart" :id='"barChart"+index' :key="index"/>
- </div>
- </template>
- <script>
- import theme from '../../../styles/element-ui.scss';
- import elementResizeDetectorMaker from 'element-resize-detector'
- import * as echarts from 'echarts';
- export default {
- name: "BarChart",
- props: {
- index: {
- type: Number|String,
- default: 0
- },
- chartData: {
- type: Object,
- default: () => ({
- xdata: [],
- ydata: []
- })
- },
- title: {
- type: String
- },
- unit: {
- type: String,
- default: ''
- },
- leftUnit: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- chart: null
- }
- },
- watch: {
- chartData: {
- deep: true,
- handler(val) {
- this.setOptions()
- }
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.initChart()
- })
- const _this = this;
- const erd = elementResizeDetectorMaker()
- erd.listenTo(document.getElementById('barChart'+this.index), element => {
- _this.$nextTick(() => {
- _this.chart.resize()
- })
- })
- },
- methods: {
- initChart() {
- this.chart = echarts.init(document.getElementById('barChart'+this.index), 'macarons')
- this.setOptions()
- },
- setOptions() {
- var ydata = this.chartData.ydata
- if (ydata && ydata.length > 1) {
- const p = [ydata[0], {
- value: ydata[1],
- itemStyle: {
- color: theme.colorPrimary
- }
- }]
- ydata = p
- }
- this.chart.setOption({
- xAxis: {
- data: this.chartData.xdata,
- axisTick: {
- show: false
- },
- axisLabel: {
- color: '#333',
- fontSize: 12
- },
- axisLine: {
- lineStyle: {
- color: '#ffffff'
- }
- },
- splitLine: {
- show: false,
- lineStyle: {
- color: '#FDEDFA'
- }
- }
- },
- yAxis: {
- show: false,
- axisLabel: {
- show: false,
- color: '#5E5E5E'
- },
- axisLine: {
- lineStyle: {
- color: '#fff'
- }
- },
- splitLine: {
- show: false
- }
- },
- grid: {
- show: true,
- bottom: 5,
- top: 35,
- borderColor: '#fff',
- containLabel: true
- },
- series: [{
- type: 'bar',
- itemStyle: {
- color: theme.colorAccent,
- barBorderRadius: [4, 4, 0, 0]
- },
- label: {
- show: true,
- color: '#000',
- position: 'top',
- fontWeight: 'bold',
- formatter: params => {
- var v = params.data.value !== undefined ? params.data.value : params.data
- if (this.leftUnit)
- return this.unit + v
- else
- return v + this.unit
- }
- },
- barWidth: 40,
- data: ydata,
- coordinateSystem: 'cartesian2d'
- }]
- })
- }
- }
- }
- </script>
- <style scoped="scoped" lang="scss">
- @import '../../../styles/element-ui.scss';
- .title {
- color: #333;
- text-align: center;
- font-size: 14px;
- position: relative;
- padding-bottom: 5px;
- }
- .title::after {
- left: 50%;
- bottom: -5px;
- width: 50px;
- height: 5px;
- content: '';
- border-radius: 10px;
- margin-left: -25px;
- background-color: $--color-primary;
- position: absolute;
- }
- .barChart {
- width: 100%;
- height: 18vh;
- }
- </style>
|