最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

代碼如下:

<template>
 <div ref="dom" class="charts chart-bar"></div>
</template>
<script>
import echarts from 'echarts'
import tdTheme from '_c/charts/theme.json' // 這是我自己寫(xiě)的主題文件,可以不用管
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,
    // 通過(guò)拖動(dòng)是可以實(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' 表示這個(gè) graphic element 的類(lèi)型是圓點(diǎn)。
   type: 'circle',
   shape: {
   // 圓點(diǎn)的半徑。
   r: this.symbolSize / 2
   },
   // 用 transform 的方式對(duì)圓點(diǎn)進(jìn)行定位。position: [x, y] 表示將圓點(diǎn)平移到 [x, y] 位置。
   // 這里使用了 convertToPixel 這個(gè) API 來(lái)得到每個(gè)圓點(diǎn)的位置
   position: this.dom.convertToPixel('grid', dataItem),
   // 這個(gè)屬性讓圓點(diǎn)不可見(jiàn)(但是不影響他響應(yīng)鼠標(biāo)事件)。
   invisible: true,
   // 這個(gè)屬性讓圓點(diǎn)可以被拖拽。
   draggable: true,
   // 把 z 值設(shè)得比較大,表示這個(gè)圓點(diǎn)在最上方,能覆蓋住已有的折線圖的圓點(diǎn)。
   z: 100,
   ondrag: echarts.util.curry(function (dataIndex) { // 此處必須用匿名函數(shù),不能用箭頭函數(shù),否則拿不到拖動(dòng)的坐標(biāo)
   let origin = that.dom.convertToPixel('grid', that.data[dataIndex]) // 得到每個(gè)圓點(diǎn)原始位置
   // let maxY = that.dom.convertToPixel('grid', [40, 36]) // 最高溫度為36攝氏度,暫未做封裝
   // 超過(guò)最高溫度36將不能拖動(dòng),寫(xiě)死的最低點(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] // 控制每個(gè)點(diǎn)位只能在y軸方向移動(dòng)
   // this.position[1] = origin[1] // 控制每個(gè)點(diǎn)位只能在x軸方向移動(dòng)
   // 實(shí)時(shí)獲取拖動(dòng)的點(diǎn)位信息并根據(jù)此信息重新畫(huà)圖
   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)可拖動(dòng)節(jié)點(diǎn)的折線圖(支持拖動(dòng)方向和上下限的設(shè)置),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論

新化县| 和静县| 饶平县| 鄂托克旗| 中牟县| 淮滨县| 恩施市| 临邑县| 旌德县| 崇州市| 弋阳县| 三门峡市| 高青县| 谷城县| 都昌县| 徐州市| 连江县| 五大连池市| 班戈县| 竹山县| 故城县| 美姑县| 思南县| 东平县| 长治县| 安岳县| 黑河市| 开阳县| 图们市| 新闻| 应城市| 突泉县| 白银市| 翁源县| 金平| 儋州市| 吴桥县| 诸城市| 贡山| 东山县| 米林县|