Echarts散点图 拖拽布点的功能实现
实现需求:有一组item(设备)需要拖拽到散点图的指定位置,然后在前台展示给客户看
实现技术:主要是通过拖拽组件draggable 和echarts组件实现坐标转换的计算
散点图拖拽点的操作
-
//this.symbolSize = 20;
-
const that = this;
-
-
this.myChart.setOption({
-
graphic: that.data.map(function (item, dataIndex) {
-
return {
-
type: "circle",
-
position: that.myChart.convertToPixel("grid", item),
-
shape: {
-
cx: 0,
-
cy: 0,
-
r: that.symbolSize / 2,
-
},
-
invisible: true,
-
draggable: true,
-
ondrag: function () {
-
that.onPointDragging(dataIndex, [
-
this.x,
-
this.y,
-
item[2],
-
item[3],
-
]);
-
},
-
onmousemove: function () {
-
that.showTooltip(dataIndex);
-
},
-
onmouSEO((Search Engine Optimization))ut: function () {
-
that.hideTooltip(dataIndex);
-
},
-
z: 100,
-
};
-
}),
-
series: [
-
{
-
id: "a",
-
data: this.data,
-
},
-
],
-
});
-
//散点图拖拽点坐标处理方法
-
onPointDragging(dataIndex, pos) {
-
console.log(pos, "before");
-
let pointList = this.myChart.convertFromPixel("grid", pos);
-
console.log(pointList, "point after");
-
pointList.push(pos[2], pos[3]);
-
this.data[dataIndex] = pointList;
-
// Update data
-
this.myChart.setOption({
-
series: [
-
{
-
id: "a",
-
data: this.data,
-
},
-
],
-
});
-
this.option && this.myChart.setOption(this.option);
-
this.option.series.data = [];
-
},
-
-
//散点图上显示提示语的方法
-
showTooltip(dataIndex) {
-
this.myChart.dispatchAction({
-
type: "showTip",
-
seriesIndex: 0,
-
dataIndex: dataIndex,
-
});
-
},
-
//散点图上隐藏提示语的方法
-
hideTooltip() {
-
this.myChart.dispatchAction({
-
type: "hideTip",
-
});
-
},
最后:将散点图的外部目标拖拽入散点图内部的时候的坐标计算方法
-
//通过dragger组件
-
//vue html代码 绑定拖拽事件
-
<draggable
-
class="dragItem"
-
@end="(evt) => widgetOnDragged(evt, index, item)"
-
v-for="(item, index) in dragList"
-
:key="index"
-
>
-
<div class="tools-item">
-
<span class="tools-item-text">{{ item.deviceName }}</span>
-
</div>
-
</draggable>
-
-
//拖拽事件实现计算当前item 拖拽之后在散点图中坐标
-
widgetOnDragged(evt, index, item) {
-
var element = document.getElementById("main");//echarts的节点
-
let dragItem = document.querySelectorAll("div[class='dragItem']") //需要被拖拽的item列表节点
-
var rect = dragItem[index].getBoundingClientRect();//被拖拽的item
-
var chartRect = element.getBoundingClientRect();
-
let pos = [evt.originalEvent.offsetX rect.left - chartRect.left ,
-
evt.originalEvent.offsetY rect.top - chartRect.top];
-
let pointList = this.myChart.convertFromPixel("grid", pos);
-
pointList.push(item.deviceName, item.deviceId);
-
this.data.push(pointList);
-
//保存点位
-
let datalist = [];
-
this.data.forEach((item) => {
-
//所有坐标传给后端接口的时候保存两位小数点
-
datalist.push({
-
deviceId: item[3],
-
axisx: parseFloat(item[0].toFixed(2)),
-
axisy: parseFloat(item[1].toFixed(2)),
-
});
-
});
-
//拖拽之后调取后端接口保存当前点
-
editCanvas(datalist).then((res) => {
-
console.log(res, "dates");
-
});
-
//重新调取接口 获取未进入散点图的item
-
this.handlePoIntform();
-
//重新加载echarts散点图的options
-
const that = this;
-
this.myChart.setOption({
-
graphic: that.data.map(function (item, dataIndex) {
-
return {
-
type: "circle",
-
position: that.myChart.convertToPixel("grid", item),
-
shape: {
-
cx: 0,
-
cy: 0,
-
r: that.symbolSize / 2,
-
},
-
invisible: true,
-
draggable: true,
-
ondrag: function () {
-
that.onPointDragging(dataIndex, [
-
this.x,
-
this.y,
-
item[2],
-
item[3],
-
]);
-
},
-
onmousemove: function () {
-
that.showTooltip(dataIndex);
-
},
-
onmouSEO((Search Engine Optimization))ut: function () {
-
that.hideTooltip(dataIndex);
-
},
-
z: 100,
-
};
-
}),
-
series: [
-
{
-
id: "a",
-
data: this.data,
-
},
-
],
-
});
-
this.option && this.myChart.setOption(this.option);
-
},
请多多指教……
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhiegbia
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
excel打印预览压线压字怎么办
PHP中文网 06-22