index.vue 7.75 KB
Newer Older
何虹's avatar
何虹 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
<template>
  <div style='width:100%;height:100%;'>
    <div :id="elId"
         :style='style'></div>
      <jsoneditor
                  v-model='jsoneditorVisible'
                  :elInfo='elInfo'
                  :complatData='complatData'
                  :initStyle='initStyle'
                  :layout='layout'
                  :jsoneditorCloseAfter='jsoneditorCloseAfter'
                  :jsoneditorOpenAfter='jsoneditorOpenAfter'
                  >
      </jsoneditor>

  </div>

</template>
<script>
import uuidv1 from 'uuid/v1'
import jsoneditor from '../common/jsoneditor'
import mockData from '../common/initDbConfigDataJson'
import ucComponent from '../ucClass/uc_component'
import echartResize from './echartResize'
export default {
  mixins: [ucComponent, echartResize],
  components: {
    jsoneditor
  },
  name: 'hsGantt',
  props: {
    elInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    height: {// 图表高度
      type: String,
      default: '100%'
    },
    width: {// 图表宽度
      type: String,
      default: '100%'
    },
    allSourceData: {
      type: Object,
      default() {
        return {}
      }
    },

    initStyle: {
      type: Object,
      default() {
        return {}
      }
    },
    complatData: {
      type: Array,
      default() {
        return []
      }
    },
    mock: {
      type: Boolean,
      default: false
    },
    echartType: {
      type: String,
      default: 'hsLineBar'
    },
    layout: {},
    jsoneditorCloseAfter: {
      type: Function
    },
    jsoneditorOpenAfter: {
      type: Function
    }
  },
  watch: {
    allSourceData: {
      handler: function(newVal, oldVal) {
        const { config, sourceData } = newVal
        if (config && sourceData) {
          this.drowEchart(newVal)
        } else {
          this.initMockData()
        }
      },
      deep: true
    },
    editData: {
      handler: function(newVal, oldVal) {
        this.jsoneditorData = newVal
      },
      deep: true
    }
  },
  computed: {
    style() {
      return {
        height: this.height,
        width: this.width
      }
    }
  },
  data() {
    return {
      elId: '',
      chart: null,
      jsoneditorVisible: false, // 是否显示动态配置的弹框
      itemLabel: {
        color: 'white',
        fontSize: 14,
        position: 'top',
        show: true,
        formatter: '{a}:{c}'
      },
      editData: {
        config: {},
        sourceData: [],
        sql: ''
      },
      jsoneditorData: {}
    }
  },
  created() {
    this.elId = uuidv1() // 获取随机id
  },

  mounted() {
    this.initMockData()
  },
  methods: {
    initMockData() {
      if (this.mock) {
        switch (this.echartType) {
          case 'hsLineBar':
            this.initMockLineBar()
            break
          // 加载lineBar的模拟数据
          case 'hsPie':
            break
          default:
            this.initMockLineBar()
        }
      } else {
        this.initMockLineBar(this.elInfo.el)
      }
    },
    // 折线图的模拟数据
    initMockLineBar(type) {
      const mockData_ = mockData[type]
      const data = {
        config: mockData_.config,
        sourceData: mockData_.sourceData
      }
      this.editData.config = mockData_.config
      this.editData.sourceData = mockData_.sourceData
      this.editData.sql = mockData_.sql
      this.drowEchart(data)
    },
    drowEchart(data) {
      const source = JSON.parse(JSON.stringify(data))
      if (!source.sourceData.length) return
      this.editData.config = source.config
      this.editData.sourceData = source.sourceData
      this.editData.sql = source.sql
      const { config, sourceData } = JSON.parse(JSON.stringify(data))
      if (!Object.keys(config).length) return
      this.dealWithSource(config, sourceData)
      this.draw_complate_options(config)
    },
    // 传入完整的配置
    draw_complate_options(options) {
      // 重新绘制的情况
      if (this.chart) {
        this.chart.setOption(options, true)
        return
      }
      this.chart = this.drawEcharts(this.elId, options)
      // 新增双击事件
      this.chart.on('dblclick', (parm) => {
        this.$emit('echartDbClick', parm)
      })
      // 单击事件
      this.chart.on('click', (parm) => {
        this.$emit('echartClick', parm)
      })
    },
    // 处理配置和原始数据
    dealWithSource(config, sourceData) {
      if (this.elInfo.el === 'hsLineBar') {
        return this.dealWithSource_lineBar(config, sourceData)
      } else if (this.elInfo.el === 'hsPie') {
        return this.dealWithSource_pie(config, sourceData)
      } else if (this.elInfo.el === 'hsRadar') {
        return this.dealWithSource_radar(config, sourceData)
      }
    },
    // 处理配置和原始数据
    dealWithSource_lineBar(config, sourceData) {
      const isTransverse = config.isTransverse
      const series = config.series
      if (!series) {
        return
      }
      series.forEach(item => {
        const targetList = sourceData.filter(jtem => {
          return jtem.sSeries === item.name
        })
        const seriesData = []
        const xData = []
        targetList.forEach(ftem => {
          seriesData.push(ftem.dValue)
          xData.push(ftem.sName)
        })
        item.data = seriesData
        item.xData = xData
        // 支持渐变色
        if (item.itemStyle && item.itemStyle.hsColor) {
          var flage = false
          if (isTransverse) {
            flage = true
          }
          item.itemStyle.color = new this.$echarts.graphic.LinearGradient(
            flage ? 1 : 0, 0, 0, flage ? 0 : 1,
            [
              { offset: 0, color: item.itemStyle.hsColor[0] },
              { offset: 1, color: item.itemStyle.hsColor[1] }
            ]
          )
        }
        // 支持区域填充样式。
        if (item.areaStyle && item.areaStyle.hsColor) {
          item.areaStyle.color = new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: item.areaStyle.hsColor[0] },
            { offset: 1, color: item.areaStyle.hsColor[1] }
          ])
        }
        // 这里显示showValue
        if (!item.label) {
          item.label = this.itemLabel
        }
        item.label.formatter = function(params) {
          const { seriesName, data, name } = params
          const target = sourceData.find(jtem => {
            return jtem.sSeries === seriesName && jtem.dValue === data && jtem.sName === name
          })
          return target.sShowValue
        }
      })
      // 如果要横向显示的话 就得设置yAxis
      if (config.isTransverse) {
        config.yAxis[0].data = series[0].xData
      } else {
        config.xAxis.data = series[0].xData
      }
      return config
    },
    // 处理配置和原始数据
    dealWithSource_pie(config, sourceData) {
      const series = config.series
      if (!series) {
        return
      }
      const legendDatas = []
      series.forEach(item => {
        const targetList = sourceData.filter(jtem => {
          return jtem.sSeries === item.name
        })

        const seriesData = []
        targetList.forEach(ftem => {
          seriesData.push({ value: ftem.dValue, name: ftem.sName })
          legendDatas.push(ftem.sName)
        })
        item.data = seriesData
      })
      config.legend.data = legendDatas
      return config
    },
    dealWithSource_radar(config, sourceData) {
      const series = config.series
      if (!series) {
        return
      }
      series.forEach(item => {
        const targetList = sourceData.filter(jtem => {
          return jtem.sSeries === item.name
        })
        const seriesData = []
        targetList.forEach(ftem => {
          seriesData.push(ftem.dValue)
        })
        item.data = seriesData
      })
      return config
    }

  }
}
</script>