admin
2024-05-27 816ba1e56476f0e23f51711aae4b34507a04a59c
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
import * as d3 from '@/utils/svg/d3.js';
 
export const ycslSvgObj = {
  type: 'svg',
  svg: null,
  svgG: null,
  viewBox: null,
  pageSvg: null,//svg实例
  pageG: null, // svg g实例
  isKQYDYSF: false,//是否开启移动与缩放
  isKQSF: true,
  svgZoom: null, // svg 放大缩小zoom限制
  svgScaleExtent: [0.5, 8], // svg放缩限制
  width: '1920',
  height: '1000',
  beginValue: null,
  endValue: null,
  tooltip: null,
  init: function (config) {//对象初始化
    var _this = this;
 
    _this.tooltip = d3.select("body").append("div")
      .style('position', 'absolute')
      .style('z-index', '10')
      .style('color', '#f00')
      .style('visibility', 'hidden')   // 是否可见(一开始设置为隐藏)
      .style('font-size', '18px')
      .style('font-weight', 'bold')
      .text('');
 
    //检查参数
    if (config.svg == null) {
      alert("请求参数格式缺少svg的id值:参数名svg");
      //alert("请求参数格式存储错误,正确的请求参数为:/n");
    } else {
      _this.svg = config.svg;
    }
 
    if (config.svgG == null) {
      alert("请求参数格式缺少svgG的id值:参数名svgG");
    } else {
      _this.svgG = config.svgG;
    }
 
    if (config.width != null) {
      _this.width = config.width;
    }
 
    if (config.height != null) {
      _this.height = config.height;
    }
 
    if (config.svgScaleExtent != null) {
      _this.svgScaleExtent = config.svgScaleExtent;
    }
 
    if (config.isKQYDYSF != null) {
      _this.isKQYDYSF = config.isKQYDYSF;
    }
 
    _this.pageSvg = d3.select("#" + _this.svg);
    _this.pageG = d3.select("#" + _this.svgG);
    // 允许移动放缩
    if(_this.isKQYDYSF){
      _this.svgZoom = d3.zoom().scaleExtent(_this.svgScaleExtent).on("zoom", function () {
        if (_this.isKQSF) {
          var transforms = d3.event.transform;
          _this.pageG.attr("transform", transforms);
          //调整中心位置
          //_this.pageG.attr("transform", "translate(0,0) scale(0.1)");
        }
      });
    }
 
 
    d3.selectAll('#' + _this.svg)//d3操作的svg对象和jq对象不一样
      .attr('width', _this.width)//d3操作的对象px属性都不能带单位
      .attr('height', _this.height)
      .attr("display", "inline-block");
    // 允许移动放缩
    if(_this.isKQYDYSF){
      d3.selectAll('#' + _this.svg)
        .call(_this.svgZoom)
        .on("wheel", function () {
          d3.event.preventDefault(); // 放大倍数达到上下限时svg图形不随鼠标滚动上下移动
        })
        .on("dblclick.zoom", null);
    }
 
    if (_this.viewBox) {
      d3.selectAll('#' + _this.svg)
        .attr('viewBox', _this.viewBox);
    } else {
      if (_this.width.indexOf("%") != -1) {
        d3.selectAll('#' + _this.svg)
          .attr('viewBox', '-' + (_this.width.replace("%", "") / 2) + '% -' + (_this.height.replace("%", "") / 2) + '% ' + _this.width.replace("%", "") + '% ' + _this.height.replace("%", "") + '%');//通过png-svg转换的把实际坐标放大了1倍'
      } else {
        d3.selectAll('#' + _this.svg)
          .attr('viewBox', '-' + (_this.width / 2) + ' -' + (_this.height / 2) + ' ' + _this.width + ' ' + _this.height);//通过png-svg转换的把实际坐标放大了1倍'
      }
    }
 
    //调整中心位置
    _this.pageG.attr("transform", "translate(0,0) scale(1)");
  },
 
  addOrUpdateCameraImg:function(imgUrl,imgWidth,imgHeight){
    var _this = this;
    d3.selectAll("#cameraImgSvg").remove();
    d3.selectAll('#' + _this.svgG)
      .append("image")
      .attr("width", imgWidth)
      .attr("height", imgHeight)
      .attr("x", 0)
      .attr("y", 0)
      .attr("id", "cameraImgSvg")
      .attr("class", "cameraImgSvg")
      .attr("cursor", "pointer")
      .attr("xlink:href", imgUrl)
    _this.width=imgWidth+"";
    _this.height=imgHeight+"";
    d3.selectAll('#' + _this.svg)
      .attr('width', _this.width)
      .attr('height', _this.height)
    _this.pageG.attr("transform", "translate("+(-imgWidth/2)+","+(-imgHeight/2)+") scale(1)");
  },
  addOrUpdateCameraRedraw :function (imgTrailLine){
    var _this = this;
    d3.selectAll("#cameraImgRedrawSvg").remove();
 
    if(imgTrailLine.length==0){
      return
    }else if(imgTrailLine.length==1){
      //第一个画圆
      d3.selectAll('#' + _this.svgG)
        .append("circle")
        .attr("id", "cameraImgRedrawSvg")
        .attr("cx",(imgTrailLine[0].x/3)+"px")
        .attr("cy",(imgTrailLine[0].y/3)+"px")
        .attr("r","10px")
        .attr("fill","yellow");
    }else{
      d3.selectAll('#' + _this.svgG)
        .append('path')//插入路径
        .attr("id", "cameraImgRedrawSvg")
        .attr("class", "motionPath")
        .attr("stroke", "rgba(255, 140, 0)")
        .attr("fill", "rgba(255, 140, 0,0.2)")
        .attr("stroke-miterlimit", "0")
        .attr("stroke-width", 2)
        .attr("d", _this.getPathD(imgTrailLine));
 
      if(imgTrailLine.length>2){
        var newImgTrailLine=[];
        newImgTrailLine.push(imgTrailLine[0]);
        newImgTrailLine.push(imgTrailLine[imgTrailLine.length-1]);
        d3.selectAll('#' + _this.svgG)
          .append('path')//插入路径
          .attr("id", "cameraImgRedrawSvg")
          .attr("class", "motionPath")
          .attr("stroke", "rgba(255, 0, 0)")
          .attr("fill", "none")
          .attr("stroke-miterlimit", "0")
          .attr("stroke-width", 2)
          .attr("stroke-dasharray","2 3")
          .attr("d", _this.getPathD(newImgTrailLine));
      }
    }
  },
  getPathD: function (imgTrailLine){
    var pathD="";
    for (let i = 0; i < imgTrailLine.length; i++) {
      if(i==0){
        pathD+="M "+(imgTrailLine[i].x/3)+ " " + (imgTrailLine[i].y/3) + " ";
      }else{
        pathD+="L "+(imgTrailLine[i].x/3)+ " " + (imgTrailLine[i].y/3) + " ";
      }
    }
    return pathD;
  },
  setIsKQSF: function (isKQSF) {
    var _this = this;
    if (isKQSF) {
      _this.isKQSF = true;
    } else {
      _this.isKQSF = false;
      _this.pageG.attr("transform", "translate(0,0) scale(1)");
    }
  },
}