vue+echarts實(shí)現(xiàn)可拖動(dòng)節(jié)點(diǎn)的折線圖(支持拖動(dòng)方向和上下限的設(shè)置)
本篇文檔主要是利用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)文章
import.meta.glob() 如何導(dǎo)入多個(gè)目錄下的資源(最新推薦)
import.meta.glob() 其實(shí)不僅能接收一個(gè)字符串,還可以接收一個(gè)字符串?dāng)?shù)組,就是匹配多個(gè)位置,本文給大家介紹import.meta.glob() 如何導(dǎo)入多個(gè)目錄下的資源,感興趣的朋友一起看看吧2023-11-11
Vue生命周期中的八個(gè)鉤子函數(shù)相機(jī)
這篇文章主要為大家介紹了Vue生命周期中的八個(gè)鉤子函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
vue-cli創(chuàng)建項(xiàng)目ERROR?in?Conflict:?Multiple?assets?emit?dif
最近vue/cli創(chuàng)建項(xiàng)目后出現(xiàn)了錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于vue-cli創(chuàng)建項(xiàng)目ERROR?in?Conflict:?Multiple?assets?emit?different?content?to?the?same?filename?index.html問(wèn)題的解決辦法,需要的朋友可以參考下2023-02-02
VUE單頁(yè)面切換動(dòng)畫(huà)代碼(全網(wǎng)最好的切換效果)
今天小編就為大家分享一篇VUE單頁(yè)面切換動(dòng)畫(huà)代碼(全網(wǎng)最好的切換效果),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Vue3中依賴(lài)注入provide、inject的使用
這篇文章主要介紹了Vue3中依賴(lài)注入provide、inject的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue.$set 失效的坑 問(wèn)題發(fā)現(xiàn)及解決方案
這篇文章主要介紹了Vue.$set 失效的坑 問(wèn)題發(fā)現(xiàn)及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
vue移動(dòng)端輕量級(jí)的輪播組件實(shí)現(xiàn)代碼
這篇文章主要介紹了vue移動(dòng)端輕量級(jí)的輪播組件實(shí)現(xiàn)代碼,一個(gè)簡(jiǎn)單的移動(dòng)端卡片滑動(dòng)輪播組件,適用于Vue2.x。本文給大家?guī)?lái)了實(shí)例代碼,需要的朋友參考下吧2018-07-07
Vue源碼之關(guān)于vm.$delete()/Vue.use()內(nèi)部原理詳解
這篇文章主要介紹了Vue源碼之關(guān)于vm.$delete()/Vue.use()內(nèi)部原理詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05

