123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <template>
- <div :class="className" :style="{height:height,width:width}" />
- </template>
- <script>
- import echarts from 'echarts'
- import resize from './mixins/resize'
- export default {
- mixins: [resize],
- props: {
- className: {
- type: String,
- default: 'chart'
- },
- width: {
- type: String,
- default: '100%'
- },
- height: {
- type: String,
- default: '350px'
- },
- chartData: {
- type: Object,
- required: true
- }
- },
- data() {
- return {
- chart: null
- }
- },
- watch: {
- chartData: {
- deep: true,
- handler(val) {
- this.setOptions(val)
- }
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.initChart()
- })
- },
- beforeDestroy() {
- if (!this.chart) {
- return
- }
- this.chart.dispose()
- this.chart = null
- },
- methods: {
- initChart() {
- this.chart = echarts.init(this.$el)
- this.setOptions(this.chartData)
- },
- setOptions({ labels, datasets } = {}) {
- const option = {
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'shadow'
- }
- },
- legend: {
- data: ['计划量', '完成量', '完成率'],
- right: '4%',
- top: '0%'
- },
- grid: {
- left: '3%',
- right: '4%',
- bottom: '3%',
- containLabel: true
- },
- xAxis: {
- type: 'category',
- data: labels,
- axisLabel: {
- interval: 0,
- rotate: 30
- }
- },
- yAxis: [
- {
- type: 'value',
- name: '单位/个',
- position: 'left',
- axisLine: {
- show: true,
- lineStyle: {
- color: '#606266'
- }
- },
- axisLabel: {
- formatter: '{value}'
- }
- },
- {
- type: 'value',
- name: '完成率',
- position: 'right',
- max: 100,
- axisLine: {
- show: true,
- lineStyle: {
- color: '#67C23A'
- }
- },
- axisLabel: {
- formatter: '{value}%'
- }
- }
- ],
- series: [
- {
- name: '计划量',
- type: 'bar',
- data: datasets[0].data,
- itemStyle: {
- color: '#409EFF'
- },
- barMaxWidth: 30
- },
- {
- name: '完成量',
- type: 'bar',
- data: datasets[1].data,
- itemStyle: {
- color: '#FF9F43'
- },
- barMaxWidth: 30
- },
- {
- name: '完成率',
- type: 'line',
- yAxisIndex: 1,
- data: datasets[2].data,
- itemStyle: {
- color: '#67C23A'
- },
- lineStyle: {
- width: 2
- },
- symbol: 'circle',
- symbolSize: 6
- }
- ]
- }
- this.chart.setOption(option)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .chart-container {
- position: relative;
- width: 100%;
- height: 100%;
- }
- </style>
|