123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <template>
- <div ref="chart" style="width:100%;height:385px;" />
- </template>
- <script>
- import * as echarts from 'echarts';
- import { GetDataByName } from '@/api/common'
- import draggable from 'vuedraggable'
- import Cookies from 'js-cookie'
- import { parseTime } from '@/utils/index.js'
- import Pagination from '@/components/Pagination'
- require('echarts/theme/macarons')
- export default {
- name: 'HorizontalBarChart',
- props: {
-
- chartData: {
- type: Object,
- required: true
- },
- // chartOpt: {
- // type: Object,
- // required: false
- // },
- },
- data() {
- return {
- chart: null
- }
- },
- watch: {
- chartData: {
- deep: true,
- handler: function (chartData) {
- this.renderChart(chartData)
- }
- },
- // schema: {
- // deep: true,
- // handler: function () {
- // this.renderChart(this.data)
- // }
- // }
- },
- mounted() {
- this.renderChart(this.chartData)
- this.$on('resized', this.handleResize)
- window.addEventListener('resize', this.handleResize)
- },
- // created() {
- // this.renderChart(this.chartData)
- // },
- beforeDestroy() {
- if (this.chart) {
- this.chart.dispose()
- }
- window.removeEventListener('resize', this.handleResize)
- },
- methods: {
- handleResize() {
- if (this.chart) {
- this.chart.resize()
- }
- },
- renderChart(chartData) {
- const option = {
- title: { text: '' },
- tooltip: { trigger: 'axis' },
- legend: { data:chartData.legendArr, right: 10, show: true, type: 'scroll' },
- grid: { top: '15%', left: '8%', right: '8%', containLabel: true },
- xAxis: {type: 'value', name: 'kg'},
- yAxis: [
- { type: 'category',data:chartData.xAxisArr }
-
- ],
-
- series: function (e) {
- var serie = [];
- for (var i = 0; i < chartData.xAxisArr.length; i++) {
- console.log(i)
-
- var item = {
- name: chartData.legendArr[i],
- data: chartData.dataArr[i],
- type: 'bar',
- emphasis: { label: { show: true, position: 'inside' } },
- }
-
- serie.push(item);
- }
- console.log(serie)
- return serie;
- }()
-
- }
- setTimeout(() => {
- if (!this.chart) {
- this.chart = echarts.init(this.$refs.chart, 'macarons')
- }
- this.chart.clear()
- this.chart.setOption(option)
- // if (this.chartOpt) {
- // this.chart.setOption(this.chartOpt)
- // }
-
- this.chart.setOption(this.chartOpt)
-
- }, 0)
- }
- }
- }
- </script>
|