欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue+echarts實(shí)現(xiàn)可拖動節(jié)點(diǎn)的折線圖(支持拖動方向和上下限的設(shè)置)

 更新時(shí)間:2019年04月12日 09:07:53   作者:進(jìn)擊的小王子  
制作一個折線圖用于顯示當(dāng)前24小時(shí)的數(shù)據(jù),并且可以通過拖動折現(xiàn)圖設(shè)置數(shù)據(jù),接下來通過本文給大家分享vue+echarts實(shí)現(xiàn)可拖動節(jié)點(diǎn)的折線圖(支持拖動方向和上下限的設(shè)置),感興趣的朋友跟隨一起學(xué)習(xí)吧

本篇文檔主要是利用echarts實(shí)現(xiàn)可拖動節(jié)點(diǎn)的折線圖,在echarts中找到了一個demo,傳送門:https://echarts.baidu.com/examples/editor.html?c=line-draggable,但是不是用vue寫的,并且在改寫為vue組件的過程中遇到了很多問題,在百度過程中發(fā)現(xiàn)并沒有相關(guān)的文檔,所以決定自己開發(fā),并在demo的基礎(chǔ)上開發(fā)了一些實(shí)用的功能,所以把這個過程記錄下來。文檔中還有很多不夠完善的地方,歡迎討論哈!

需求:制作一個折線圖用于顯示當(dāng)前24小時(shí)的數(shù)據(jù),并且可以通過拖動折現(xiàn)圖設(shè)置數(shù)據(jù)

效果圖如下:初步看和一般的折線圖沒什么區(qū)別,但是實(shí)際如圖示,節(jié)點(diǎn)是可以上下拖動的

代碼如下:

<template>
 <div ref="dom" class="charts chart-bar"></div>
</template>
<script>
import echarts from 'echarts'
import tdTheme from '_c/charts/theme.json' // 這是我自己寫的主題文件,可以不用管
import { on, off } from '@/libs/tools' // 這是其他一些方法函數(shù),可以不管
echarts.registerTheme('tdTheme', tdTheme)
export default {
 name: 'ChartLine',
 props: {
 text: String,
 subtext: String,
 yName: String
 },
 data () {
 return {
  dom: null,
  symbolSize: 5,
    // 通過拖動是可以實(shí)時(shí)改變這里的值的
  data: [[0, 10], [1, 10], [2, 20], [3, 30], [4, 36], [5, 20], [6, 25], [7, 20], [8, 21], [9, 22],
  [10, 23], [11, 25], [12, 10], [13, 11], [14, 19], [15, 20], [16, 12], [17, 13], [18, 12], [19, 9],
  [20, 21], [21, 18], [22, 10], [23, 12]]
 }
 },
 methods: {
 resize () {
  this.dom.resize()
 }
 },
 mounted () {
 this.dom = echarts.init(this.$refs.dom, 'tdTheme')
 this.dom.setOption({
  title: {
  text: this.text,
  subtext: this.subtext,
  x: 'center'
  },
  grid: {
  left: 50,
  top: 40
  },
  tooltip: {
  trigger: 'axis'
  },
  xAxis: {
  min: 0,
  max: 23,
  type: 'value',
  axisLabel: {
   formatter (value) {
   return value + ':00' // 格式時(shí)間顯示方式
   }
  },
  axisLine: { onZero: false }
  },
  yAxis: {
  min: 4,
  max: 36,
  type: 'value',
  name: this.yName,
  axisLine: { onZero: false }
  },
  series: [
  {
   id: 'a',
   type: 'line',
   smooth: true,
   symbolSize: this.symbolSize, // 為了方便拖拽,把 symbolSize 尺寸設(shè)大了。
   data: this.data
  }
  ]
 })
 this.dom.setOption({
  graphic: echarts.util.map(this.data, (dataItem, dataIndex) => {
  const that = this // 因?yàn)閛ndrag函數(shù)必須在回調(diào)內(nèi)使用this.position獲取實(shí)時(shí)坐標(biāo),此處必須用that替換this
  return {
   // 'circle' 表示這個 graphic element 的類型是圓點(diǎn)。
   type: 'circle',
   shape: {
   // 圓點(diǎn)的半徑。
   r: this.symbolSize / 2
   },
   // 用 transform 的方式對圓點(diǎn)進(jìn)行定位。position: [x, y] 表示將圓點(diǎn)平移到 [x, y] 位置。
   // 這里使用了 convertToPixel 這個 API 來得到每個圓點(diǎn)的位置
   position: this.dom.convertToPixel('grid', dataItem),
   // 這個屬性讓圓點(diǎn)不可見(但是不影響他響應(yīng)鼠標(biāo)事件)。
   invisible: true,
   // 這個屬性讓圓點(diǎn)可以被拖拽。
   draggable: true,
   // 把 z 值設(shè)得比較大,表示這個圓點(diǎn)在最上方,能覆蓋住已有的折線圖的圓點(diǎn)。
   z: 100,
   ondrag: echarts.util.curry(function (dataIndex) { // 此處必須用匿名函數(shù),不能用箭頭函數(shù),否則拿不到拖動的坐標(biāo)
   let origin = that.dom.convertToPixel('grid', that.data[dataIndex]) // 得到每個圓點(diǎn)原始位置
   // let maxY = that.dom.convertToPixel('grid', [40, 36]) // 最高溫度為36攝氏度,暫未做封裝
   // 超過最高溫度36將不能拖動,寫死的最低點(diǎn)高度為240,最高點(diǎn)為40
   if (this.position[1] > 240) {
    this.position[1] = 240
   } else if (this.position[1] < 40) {
    this.position[1] = 40
   }
   this.position[0] = origin[0] // 控制每個點(diǎn)位只能在y軸方向移動
   // this.position[1] = origin[1] // 控制每個點(diǎn)位只能在x軸方向移動
   // 實(shí)時(shí)獲取拖動的點(diǎn)位信息并根據(jù)此信息重新畫圖
   that.data[dataIndex] = that.dom.convertFromPixel('grid', this.position)
   that.dom.setOption({
    series: [{
    id: 'a',
    data: that.data
    }]
   })
   }, dataIndex)
  }
  })
 })
 on(window, 'resize', this.dom.setOption({
  graphic: echarts.util.map(this.data, (item, dataIndex) => {
  return {
   position: this.dom.convertToPixel('grid', item)
  }
  })
 }))
 },
 beforeDestroy () {
 off(window, 'resize', this.resize)
 }
}
</script>

總結(jié)

以上所述是小編給大家介紹的vue+echarts實(shí)現(xiàn)可拖動節(jié)點(diǎn)的折線圖(支持拖動方向和上下限的設(shè)置),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論