• echarts拆成清晰化


    封装echarts组件

    1. 基本组件common\echart\index.vue
    <template>
      <div :id="id" :class="className" :style="{ height: height,  width }" />
    </template>
    
    <script>
    var echarts = require('echarts')
    export default {
      name: 'Echart',
      props: {
        className: {
          type: String,
          default: 'chart'
        },
        id: {
          type: String,
          default: 'chart'
        },
         {
          type: String,
          default: '100%'
        },
        height: {
          type: String,
          default: '100%'
        },
        options: {
          type: Object,
          default: () => ({})
        }
      },
      data () {
        return {
          chart: null
        }
      },
      watch: {
        options: {
          handler (options) {
            // 设置true清空echart缓存
            this.chart.setOption(options, true)
          },
          deep: true
        }
      },
      created () {
      },
      activated () {
        // 防止 keep-alive 之后图表变形
        if (this.chart) {
          this.chart.resize()
        }
      },
    
      mounted () {
        this.initChart()
        window.addEventListener('resize', () => {
          this.chart.resize()
        })
      },
      methods: {
        initChart () {
          // 初始化echart
          this.chart = echarts.init(this.$el) // 'tdTheme'
          this.chart.setOption(this.options, true)
        }
      }
    }
    </script>
    
    <style></style>
    
    
    2. 封装单个图标使用 components/Echarts/histogram
    2.1数据 components/Echarts/histogram/index.vue
    <template>
      <!-- 数据层 -->
      <Chart :cdata="cdata" />
    </template>
    
    <script>
    import Chart from '../histogram/charts.vue'
    export default {
      components: {
        Chart
      },
      data () {
        return {
          cdata: {
            xAsisData: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
            data: [5, 20, 36, 10, 10, 20]
          }
        }
      },
      computed: {},
      watch: {},
      created () { },
      mounted () { },
      methods: {}
    }
    </script>
    <style lang="less" scoped>
    </style>
    
    
    2.1业务 components/Echarts/histogram/charts.vue
    <template>
      <!-- 业务层 -->
      <Echart id="Chart"
              :options="options"
              height="100%"
              width="100%" />
    </template>
    
    <script>
    import Echart from '@/common/basicEcharts'
    export default {
      components: {
        Echart
      },
      props: {
        cdata: {
          type: Object,
          default: () => ({})
        }
      },
      data () {
        return {
          options: {}
        }
      },
      watch: {
        cdata: {
          handler (newData) {
            this.echartsChange()
          },
          immediate: true,
          deep: true
        }
      },
      methods: {
        echartsChange () {
          this.options = {
            title: {
              text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
              data: ['销量']
            },
            xAxis: {
              data: this.cdata.xAsisData
            },
            yAxis: {},
            series: [
              {
                name: '销量',
                type: 'bar',
                data: this.cdata.data
              }
            ]
          }
        }
      }
    }
    </script>
    
    <style lang="less" scoped>
    </style>
    
    
    3.主页面调用
    <template>
      <div>
        <Histogram style="height:40vh;30vw;" />
      </div>
    </template>
    <script>
    import Histogram from '@/components/Echarts/histogram'
    export default {
      data () {
        return {
    
        }
      },
      components: {
        Histogram
      },
      mounted () {
      },
      methods: {
      }
    }
    </script>
    <style scoped>
    
    </style>
    
    
  • 相关阅读:
    sqlite遇到database is locked问题的完美解决
    Delphi使程序的窗口出现在最前面并激活
    win10 家庭版不支持gpedit.msc的解决办法
    Delphi Record To Stream
    SQL Server 查看CPU情况
    JavaScript 获取 Url 上的参数(QueryString)值
    关于EF分页查询报错(Count must have a non-negative value.)的解决方案
    调用微信退款接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
    Javascript中“==”与“===”的区别
    IE浏览器各版本的CSS Hack
  • 原文地址:https://www.cnblogs.com/yoututu/p/15697575.html
Copyright © 2020-2023  润新知