CZT
2023-10-20 3631219246be48aec1e745e94e5b28ff4e8799c8
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*==========仓库类型为圆筒仓==========*/
//定义数据
var listPoints = [
    {'z': 0, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 1, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 2, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 3, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 4, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 5, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 6, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 7, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 8, 'x': 0, 'y': 0, 'temp': 0.0},
    {'z': 9, 'x': 0, 'y': 0, 'temp': 0.0}];
var listLays = [{z: 1, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}, {
    z: 2,
    tempMin: 0.0,
    tempMax: 0.0,
    tempAve: 0.0
}, {z: 3, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}, {z: 4, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}, {
    z: 5,
    tempMin: 0.0,
    tempMax: 0.0,
    tempAve: 0.0
}, {z: 6, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}, {z: 7, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}, {
    z: 8,
    tempMin: 0.0,
    tempMax: 0.0,
    tempAve: 0.0
}, {z: 9, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}, {z: 10, tempMin: 0.0, tempMax: 0.0, tempAve: 0.0}];
 
var distrList = [1];
var bottomList = [10];
 
// 定义数据显示容器
var layPoints = [];
 
var cirNum = distrList.length; // 圈数
var layNum = bottomList[bottomList.length - 1]; // 层数
var longAxis = 120; // 第一内圈半长轴
var shortAxis = 20; // 第一内圈半短轴
var spaceCir = 35; // 两圈间距
var intervalTop = 20; // 距离顶端距离
var spaceLay = 50; // 层间距
var maxSpace = cirNum * spaceCir; // 内外圈最大差值
var curVal = 1; // 层数控制变量
var edge_distance = 0;
var initAngle = 0; // 角度控制
var clockwise = true; // 时针控制
var displayMode = 'all'; // 内外显示
var textSize = 15;// 文本框大小
var arr = [];// 定义绑定事件数组
 
// 色块颜色常量
var color = null;
var R = 0;
var G = 0;
var B = 0;
 
var timer;
var t = -2;
 
// 初始化参数
function initCanvasParam() {
    cirNum = distrList.length; // 圈数
    layNum = bottomList[bottomList.length - 1]; // 层数
    longAxis = 100; // 第一内圈半长轴
    shortAxis = 20; // 第一内圈半短轴
    spaceCir = 30; // 两圈间距
    intervalTop = 20; // 距离顶端距离
    spaceLay = 60; // 层间距
    maxSpace = cirNum * spaceCir; // 内外圈最大差值
    curVal = 1; // 层数控制变量
    edge_distance = 0;
    initAngle = 0; // 角度控制
    clockwise = true; // 时针控制
    displayMode = 'all'; // 内外显示
    textSize = 15;// 文本框大小
    arr = [];// 定义绑定事件数组
};
 
// 睡眠
function sleep() {
    t++;
    if (t >= 0) {
        // initAngle += 2 * Math.PI / 360;
        flash();
    }
    if (timer) {
        clearInterval(timer);
    }
    timer = setTimeout("sleep()", 400);
}
 
// 闪烁程序
function flash() {
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    canvas.height = canvas.height;
    layPoints = [];
    arr = [];
 
    make_ellipse(ctx);
    gainData(ctx);
    entityBlock(ctx);
    make_top_ellipse(ctx);
    title(ctx)
}
 
// 绘制主程序
var center_x, center_y;
 
function draw(ctx) {
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    center_x = canvas.width / 3; // 仓心X坐标
    center_y = shortAxis / 3 + maxSpace + intervalTop; // 仓心y坐标
 
    ctx.strokeStyle = "black";
    ctx.lineWidth = 1;
    autoAdjust(canvas);
    make_ellipse(ctx);
    gainData(ctx);
    entityBlock(ctx);
    make_top_ellipse(ctx);
    title(ctx);
    showIndex(canvas, ctx);
}
 
// 绘制顶部椭圆
function make_top_ellipse(ctx) {
    // 绘制顶部椭圆
    ctx.beginPath();
    ctx.strokeStyle = "rgba(0,0,0,0.5)";
    ctx.ellipse(center_x, center_y, longAxis + maxSpace, shortAxis + maxSpace,
        0, 0, Math.PI * 2);
    ctx.stroke();
    ctx.closePath();
}
 
// 高度自调整
var caseHeight, sum_long;
 
function autoAdjust(ctx) {
    // 设置仓高
    caseHeight = spaceLay * (layNum - 1);
    // 计算仓库占画布总高
    sum_long = caseHeight + 2 * (maxSpace + shortAxis + intervalTop);
    // 总高越界进行调整层间距
    if (eval(canvas.height) < eval(sum_long)) {
        // 仓管设置为最高
        caseHeight = canvas.height - 2 * (maxSpace + shortAxis + intervalTop);
        // 调整层间距
        spaceLay = caseHeight / layNum;
    }
}
 
// 绘制仓体
function make_ellipse(ctx) {
    // 绘制仓左右边界
    ctx.beginPath();
    ctx.moveTo(center_x + (longAxis + maxSpace), center_y);
    ctx.lineTo(center_x + (longAxis + maxSpace), center_y + caseHeight)
    ctx.moveTo(center_x - (longAxis + maxSpace), center_y);
    ctx.lineTo(center_x - (longAxis + maxSpace), center_y + caseHeight)
    ctx.stroke();
    ctx.closePath();
    // 填充背部
    var grd = ctx.createRadialGradient(center_x, center_y + caseHeight
        - longAxis, shortAxis, center_x, center_y + caseHeight - 2
        * longAxis, 5 * longAxis);
    grd.addColorStop(0, "white");
    // grd.addColorStop(1,"#c0deea");
    grd.addColorStop(1, "rgba(115,200,231,1)")
    // grd.addColorStop(1,"#b1d6e6");
 
    ctx.beginPath();
    ctx.fillStyle = grd;
    ctx.moveTo(center_x + (longAxis + maxSpace), center_y + caseHeight);
    ctx.lineTo(center_x + (longAxis + maxSpace), center_y);
    ctx.ellipse(center_x, center_y, longAxis + maxSpace, shortAxis + maxSpace,
        0, 0, Math.PI, anticlockwise = true);
    ctx.lineTo(center_x - (longAxis + maxSpace), center_y + caseHeight);
 
    ctx.fill();
    ctx.closePath();
 
    // 绘制底部椭圆
    var grd = ctx.createRadialGradient(center_x, center_y + caseHeight
        - longAxis, shortAxis, center_x, center_y + caseHeight - 2
        * longAxis, 5 * longAxis);
    grd.addColorStop(0, "white");
    grd.addColorStop(1, "rgba(115,200,231,1)");
 
    ctx.beginPath();
    ctx.fillStyle = grd;
    ctx.ellipse(center_x, center_y + caseHeight, longAxis + maxSpace, shortAxis
        + maxSpace, 0, 0, Math.PI * 2);
    ctx.stroke();
    ctx.fill();
    ctx.closePath();
}
 
// 过滤数据
function gainData() {
    for (var i = 0; i < listPoints.length; i++) {
        if (listPoints[i]['z'] + 1 === curVal) {
            // console.log(layPoints);
            layPoints.push(listPoints[i])
        }
    }
}
 
// 进行排线,绘制色块
function entityBlock(ctx) {
    var z_peak = [1, cirNum];
    var color_list = ["e9e9e9", "bbe5cc", "f8dabc", "f4c8d5", "958a9b", "9f9aa0"];
    var turns = null;
    for (i = 0; i < bottomList.length; i++) {
        if (curVal === bottomList[i]) {
            turns = cirNum - i;
            break;
        } else {
            turns = cirNum;
        }
    }
    var showMax = turns;
    // 循环生成每圈
    for (i = turns; i >= 1; i--) {
        // 内外显示逻辑判断
        if ((displayMode === 'outside') && !(i === cirNum)) {
            continue;
        } else if ((displayMode === 'inner') && i === cirNum) {
            showMax -= 1;
            continue;
        }
        var splNum = distrList[i - 1]; //每一圈根数
        //只有一列数据时候控制中心位置
        if(splNum === 1){
            point = null;
            //循环找到对应点值
            for(z = 0;z < layPoints.length; z++){
                if((layPoints[z]['x'] === i-1)&&(layPoints[z]['y'] === 0)){
                    point = layPoints[z];
                    break;
                }
            }
            point_x = center_x ;
            point_y = center_y  + curVal * spaceLay ;
            textBox(ctx,point,point_x,point_y);
            continue;
        }
 
        ctx.beginPath();
        ctx.strokeStyle = 'rgba(0,0,0,1)';
        ctx.ellipse(center_x, center_y + (curVal) * spaceLay, longAxis + i
            * spaceCir, shortAxis + i * spaceCir, 0, 0, Math.PI * 2);
        if (i === 1) {
            var grd = ctx.createRadialGradient(center_x, center_y + caseHeight
                - longAxis, shortAxis, center_x, center_y + caseHeight - 2
                * longAxis, 5 * longAxis);
            grd.addColorStop(0, "white");
            grd.addColorStop(1, "rgba(115,200,231,1)");
            ctx.fillStyle = grd;
            ctx.fill();
        } else {
            var grd = ctx.createRadialGradient(center_x, center_y + caseHeight
                - longAxis, shortAxis, center_x, center_y + caseHeight - 2
                * longAxis, 2 * longAxis);
            grd.addColorStop(0, "white");
            grd.addColorStop(1, "#" + color_list[i]);
            ctx.fillStyle = grd;
            ctx.fill();
        }
        ctx.stroke();
        ctx.closePath();
        // 设置点值为null;
        point = null;
        //获取每一圈的最大根数
        var maxGen = 0;
        for(t = 0; t < distrList.length; t++){
            if(t <= i - 1){
                maxGen += distrList[t];
            }
        }
        // 循环生成圈内点
        for (j = 0; j < maxGen; j++) {
            // 循环找到对应点值
            for (z = 0; z < layPoints.length; z++) {
                if ((layPoints[z]['x'] === i - 1) && (layPoints[z]['y'] === j)) {
                    point = layPoints[z];
                    break;
                }
            }
            // 判断显示时针
            if (clockwise) {
                angle = (2 * Math.PI / splNum) * j + initAngle;
            } else {
                angle = (2 * Math.PI / splNum) * (splNum - j) + initAngle;
            }
            point_x = center_x + (longAxis + i * spaceCir) * Math.cos(angle);
            point_y = center_y + curVal * spaceLay + (shortAxis + i * spaceCir)
                * Math.sin(angle);
            if(point){
                textBox(ctx, point, point_x, point_y)
            }
        }
    }
}
 
// 绘制一个圆形文本框
function textBox(ctx, point, x, y) {
    var val = -100;
    if (point) {
        val = point.temp;
    }
    if ((t % 2 === 1)) {
        if (val == listLays[curVal - 1]['tempMax']) { // 此点为最高温
            ctx.fillStyle = "rgba(229,29,29,1)";
        } else if (val == listLays[curVal - 1]['tempMin']) { // 此点为最低温
            ctx.fillStyle = "rgba(0,255,28,1)";
        } else {
            ctx.fillStyle = getColor(val);
        }
    } else {
        ctx.fillStyle = getColor(val);
    }
 
    //处理备用和故障
    if (val <= -100) {
        val = "备用";
    }
    if (val <= -101) {
        val = "故障";
    }
 
    ctx.beginPath();
    ctx.lineWidth = 1;
    ctx.arc(x, y, textSize, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fill();
    ctx.fillStyle = "rgba(0,0,0,1)";
    ctx.textAlign = 'center'
    ctx.fillText(val, x, y + 3);
    ctx.closePath();
 
    arr.push({
        'x': x - 0.6 * textSize,
        'y': y - 0.7 * textSize,
        'width': 2.1 * textSize,
        'height': 2.2 * textSize,
        'type': 'val',
        'lay': point.z,
        'col': point.y,
        'val': val
    })
}
 
// 根据温度获取RGB
function getColor(val) {
    if (val <= -100.0) {
        color = "rgba(220,220,220,220)";
        return color;
    }
    if (val >= 50) {
        color = "rgba(255,0,0,1)";
        return color;
    } else if (val >= 20) {
        G = Math.round(255 - 255 * ((val - 20) / 30));
        color = "rgba(255," + G + ",0,1)";
        return color;
    } else if (val >= 0) {
        R = Math.round(100 + (155 / 20) * val);
        B = Math.round(30 - 1.5 * val);
        color = "rgba(" + R + ",255," + B + ",1)";
        return color;
    } else if (val >= -10) {
        R = Math.round(100 - 6.6 * (val * (-1)));
        G = Math.round(255 - 2.2 * (val * (-1)));
        B = Math.round(30 + 1.2 * (val * (-1)));
        color = "rgba(" + R + "," + G + "," + B + ",1)";
        return color;
    } else {
        color = "rgba(34,233,42,1)";
        return color;
    }
}
 
//标题栏
function title(ctx) {
    var start_x, start_y, val;
    for (i = 1; i <= layNum; i++) {
        start_x = center_x + (longAxis + maxSpace) + 20;
        start_y = center_y + spaceLay * (i - 1);
        if (curVal === i) {
            var grad = ctx.createLinearGradient(start_x, start_y, start_x + 2
                * spaceLay, start_y);
            grad.addColorStop(0, "#2f2fff");
            // grad.addColorStop(0.3, "#5858ff");
            grad.addColorStop(0.7, "#b3b3ff");
            grad.addColorStop(1, "#ffffff");
            //设置为未选择时的渐变填充
        } else {
            var grad = ctx.createLinearGradient(start_x, start_y, start_x + 2
                * spaceLay, start_y);
            grad.addColorStop(0, "#a2baef");
            grad.addColorStop(0.8, "#ffffff");
        }
        ctx.fillStyle = grad;
        ctx.lineWidth = 1;
        ctx.strokeStyle = 'rgba(214,223,232,1)';
        ctx.beginPath();
        ctx.moveTo(start_x, start_y + 1); //原点
        ctx.lineTo(start_x + 1.8 * spaceLay, start_y + 1); //右移
        ctx.lineTo(start_x + 1.8 * spaceLay, start_y + spaceLay - 1); //下移
        ctx.lineTo(start_x, start_y + spaceLay - 1); //左移
        ctx.lineTo(start_x, start_y + 1); //上移
        ctx.stroke();
        ctx.fill();
        ctx.closePath();
        //设置为选择时的字体填充
        if (curVal === i) {
            ctx.fillStyle = 'rgba(255,14,0,1)';
            //设置为未选择时的字体填充
        } else {
            ctx.fillStyle = 'rgba(30,30,30,1)';
        }
        val = "第" + i + "层";
        ctx.font = "15px Arial";
        ctx.textAlign = 'center';
        ctx.fillText(val, start_x + 0.9 * spaceLay, start_y + 0.7 * spaceLay);
        arr.push({
            'x': start_x,
            'y': start_y + 1,
            'width': 1.8 * spaceLay,
            'height': spaceLay,
            'num': i,
            'type': 'title'
        })
    }
}
 
//绑定触发的事件
function draws(x, y, canvas, ctx) {
    //创建点击事件的画布(画布2)需要考虑父框的位置
    var parent = $("#canvas_parent");
    var parentLeft = parent.offset().left, parentTop = parent.offset().top;
 
    ctx.rect(0, 0, canvas.width, canvas.height);
    arr.forEach(function (v, i) {
        ctx.beginPath();
        //绘制每一个对象的区域
        ctx.rect(v.x + parentLeft, v.y + parentTop, v.width, v.height);
        ctx.closePath();
        //判断点是否在区域内
        if (ctx.isPointInPath(x, y)) {
            //点的类型为值
            if (v.type === 'val') {
                //类型为val,则弹出点信息
                // alert("层=" + v.lay + "\n列=" + v.col + "\n温度=" + v.val);
                layer.msg("层=" + (v.lay + 1) + "\n列=" + (v.col + 1) + "\n温度=" + v.val);
            } else { //其他类型则切换显示色块
                curVal = v['num'];
                flash();
            }
        }
    })
}
 
//绑定事件
function showIndex(canvas, ctx) {
    //整个画布对象添加点击事件
    canvas.addEventListener('click', function location(e) {
        x = e.clientX;
        y = e.clientY;
        draws(x, y, canvas, ctx);
    }, false);
}
 
// --------------- 按钮点击事件---------------------
//上一行
function last() {
    if (curVal === 1) {
        curVal = Number(layNum);
    } else {
        curVal -= 1;
    }
    flash();
    renderGrainByLay(curVal);
}
 
//下一行
function next() {
    if (curVal === Number(layNum)) {
        curVal = 1;
    } else {
        curVal += 1;
    }
    flash();
 
    renderGrainByLay(curVal);
}
 
//时针切换
function clockSwitch() {
    if (clockwise) {
        clockwise = false;
    } else {
        clockwise = true;
    }
    flash();
}
 
//角度旋转
function rotate() {
    initAngle += (document.getElementById("rotate").value) * 2 * Math.PI / 360;
    flash();
}
 
//全部显示点击事件
function display_all() {
    displayMode = 'all';
    flash();
}
 
//外部显示点击事件
function display_outside() {
    displayMode = 'outside';
    flash();
}
 
//内部显示点击事件
function display_inside() {
    displayMode = 'inner';
    flash();
}
 
// 初始化三维粮情
function initGrainChar3d() {
    draw();
};
 
function clearGrainChart3d() {
    initCanvasParam();
    draw();
};
 
//重新渲染
function resetGrainChart3d() {
 
    //console.log(grainData);
 
    listPoints = grainData.listPoints;
    listLays = grainData.listLays;
    distrList = grainData.cable.split("-").map(Number);
    bottomList = grainData.cableCir.split("-").map(Number);
 
    initCanvasParam();
    draw();
    sleep();//开始闪烁
    curType = "z";
    curVal = 1;
    renderGrainByLay(curVal);
}
 
// 根据用户选择的层,渲染数据,从1开始
function renderGrainByLay(num) {
    var lay;
    $.each(listLays, function (index, data) {
        if (num == data.z) {
            lay = data;
            // 更新层信息
            $("#dynamicAve").text(lay.tempAve);
            $("#dynamicMax").text(lay.tempMax);
            $("#dynamicMin").text(lay.tempMin);
            $("#dynamicAveName").text("层均温");
            $("#dynamicMaxName").text("层高温");
            $("#dynamicMinName").text("层低温");
            return;
        }
    });
};