123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <template>
- <div :class="className" :style="{ height: height, width: width }" />
- </template>
- <script>
- import echarts from 'echarts';
- require('echarts/theme/macarons'); // echarts theme
- import resize from './mixins/resize';
- const animationDuration = 6000;
- export default {
- mixins: [resize],
- props: {
- className: {
- type: String,
- default: 'chart'
- },
- width: {
- type: String,
- default: '100%'
- },
- type: {
- type: String,
- default: 'bar'
- },
- legend: {},
- chartData: {
- type: Array,
- required: true
- },
- height: {
- type: String,
- default: '215px'
- }
- },
- data() {
- return {
- chart: null
- };
- },
- watch: {
- chartData: {
- deep: true,
- handler(val) {
- this.setOptions(val);
- }
- },
- type: {
- deep: true,
- handler(val) {
- this.setOptions(this.chartData);
- }
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.initChart();
- });
- },
- beforeDestroy() {
- if (!this.chart) {
- return;
- }
- this.chart.dispose();
- this.chart = null;
- },
- methods: {
- initChart() {
- setTimeout(() => {
- this.chart = echarts.init(this.$el, 'macarons');
- this.setOptions(this.chartData);
- }, 300);
- },
- setOptions(chartData) {
- let xAxisData = [];
- let seriesData = [];
- if (chartData) {
- chartData.forEach((item, index) => {
- xAxisData.push(item.name+'月');
- seriesData.push(item.value);
- });
- }
- this.chart.setOption({
- color: ['rgba(37, 97, 239, 1)'],
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
- },
- formatter: '{b} : {c}'
- },
- legend: {
- data: [this.legend]
- },
- grid: {
- top: '13%',
- left: '0%',
- right: '0%',
- bottom: '2%',
- containLabel: true,
- show: false
- },
- xAxis: [
- {
- type: 'category',
- data: xAxisData,
- axisTick: {
- show: false
- },
- axisLine: {
- lineStyle: {
- color: '#e6e6e6'
- }
- },
- axisLabel: {
- show: true,
- textStyle: {
- color: '#545555',
- fontSize: '14'
- }
- }
- }
- ],
- yAxis: [
- {
- type: 'value',
- nameTextStyle: {
- color: '#545555'
- },
- axisTick: {
- show: false
- },
- splitArea: {
- show: false
- },
- splitLine: {
- lineStyle: {
- color: '#e6e6e6',
- width: 0.7
- }
- },
- axisLine: {
- lineStyle: {
- color: '#e6e6e6'
- }
- },
- axisLabel: {
- textStyle: {
- color: '#545555'
- }
- }
- }
- ],
- series: [
- {
- /* name: this.legend, */
- type: this.type,
- barWidth: '35%',
- smooth: false,
- data: seriesData,
- animationDuration,
- itemStyle: {
- normal: {
- color: '#2561ef',
- lineStyle: {
- color: '#2561ef',
- width: 2
- },
- areaStyle: {
- color: '#e9effd'
- },
- label: {
- show: true,
- position: 'top',
- textStyle: {
- color: '#545555',
- fontSize: 13
- },
- formatter: '{c}元'
- }
- }
- }
- }
- ]
- });
- }
- }
- };
- </script>
|