-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNomogram.html
More file actions
867 lines (749 loc) · 33.8 KB
/
Copy pathNomogram.html
File metadata and controls
867 lines (749 loc) · 33.8 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
<title>SNUH MCN Nomogram Platform</title>
<link href="css/swiper.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/animate.css" rel="stylesheet" type="text/css" />
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="d3.v3.js" charset="utf-8"></script>
<script src="d3.scale.logit.js" charset="utf-8"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="js/layout.js"></script>
<script type="text/javascript" src="js/swiper.js"></script>
<script type="text/javascript"></script>
<script>
function wrap(text, width) {
var T = text[0];
var aBCRrect = T.map(function(v) { return v.getBoundingClientRect(); });
var rects = aBCRrect.filter(function(v) { return v.width > 0; });
var ridxs = T.map(function(v,i) { return i; }).filter(function(i) { return aBCRrect[i].width > 0; });
if (rects[1].left > rects[0].left) for (var i=1 ; i<rects.length ; i++) {
if (rects[i].left < rects[i-1].right) {
var c1 = rects[i-1].left + rects[i-1].width/2;
var c2 = rects[i].left + rects[i].width/2;
T[ridxs[i]].setAttribute("x", T[ridxs[i]].getAttribute("x") + rects[i].width/2);
T[ridxs[i-1]].setAttribute("x", T[ridxs[i-1]].getAttribute("x") - rects[i-1].width/2);
}
} else for (var i=rects.length-2 ; i>=0 ; i--) {
if (rects[i+1].right > rects[i].left) {
var c1 = rects[i].left + rects[i].width/2;
var c2 = rects[i+1].left + rects[i+1].width/2;
T[ridxs[i+1]].setAttribute("x", T[ridxs[i+1]].getAttribute("x") - rects[i+1].width/2);
T[ridxs[i]].setAttribute("x", T[ridxs[i]].getAttribute("x") + rects[i].width/2);
}
}
}
var dbg = false;
function d3nomogram(id, obs, axes, pred, width, height, margin, anim, duration) {
//Width and height
if (typeof width == "undefined") width = 900;
if (typeof height == "undefined") height = 500;
if (typeof dur == "undefined") dur = 300;
if (typeof margin == "undefined") margin = [20, 30, 30, 40];
if (typeof anim == "undefined") anim = true;
var colors = [
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
"#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf", "red", "blue",
];
var bUsePointNorm = true;
var textMax = 0;
var pointMin = 999999;
var pointMax = -999999;
var totalMin = bUsePointNorm ? 0 : axes[0];
var totalMax = bUsePointNorm ? 0 : axes[0];
var rawTotalMin = axes[0];
var rawTotalMax = axes[0];
var totalPoint = axes[0];
var points = [ axes[0] ];
var nFixedAxisT = 1;
var nFixedAxisB = 2;
var nFixedAxis = nFixedAxisT + nFixedAxisB;
var nObs = obs.length;
// Height of each axis
var axisHeight = (height-margin[0]-margin[2]) / (nObs+nFixedAxis);
for (var i in axes) {
if (i == "0") continue;
if (typeof axes[i][3] == 'object') {
for (var j in axes[i][3]) if (axes[i][3].hasOwnProperty(j)) {
pointMax = Math.max(pointMax, axes[i][3][j]);
pointMin = Math.min(pointMin, axes[i][3][j]);
}
} else {
pointMax = Math.max(pointMax, axes[i][4]);
pointMin = Math.min(pointMin, axes[i][3]);
}
}
console.log("Point range ", pointMin, " ~ ", pointMax);
d3.select(id).html("");
// Generate SVG
this.svg = d3.select(id).append("svg")
.attr("width", width)
.attr("height", height);
// Generate main group
this.dataGroup = this.svg.append("svg:g");
//
// Top-side fixed axis label
//
// Point
this.hTextPoint = this.dataGroup.append("svg:text")
.text("Point")
.attr("class", "hmRowText");
//
// Variable axes label
//
this.hTextVar = this.dataGroup.selectAll("#axes").data(axes).enter().append("svg:text")
.text(function(d,i) { return d[0]; })
.attr("class", "hmRowText")
.attr("opacity", 0)
.attr("fill", function(d,i) { return dbg?'black':colors[i]; })
.each(function(d) {
textMax = Math.max(textMax,this.getBBox().width);
});
if (anim == true)
this.hTextVar.transition()
.delay(function(d,i) { return dur * i; })
.duration(dur)
.attr("opacity", 1);
else
this.hTextVar.attr("opacity", 1);
//
// Bottom-side fixed axis label
//
// Total point
var B_showTotalPoint = true;
if (B_showTotalPoint) this.hTextTotalPt = this.dataGroup.append("svg:text")
.text("Total Point")
.attr("class", "hmRowText hmBoldText");
// Predictor
this.hTextPred = this.dataGroup.append("svg:text")
.text("Probability of MCN Advanced Neoplasia")
.attr("class", "hmRowText hmBoldText");
// Get the maximum width of axis hTextVars
if (B_showTotalPoint)
textMax = Math.max(
this.hTextPred.node().getBBox().width, Math.max(
this.hTextTotalPt.node().getBBox().width,
Math.max(
this.hTextPoint.node().getBBox().width, textMax
))) + margin[3];
else
textMax = Math.max(
this.hTextPred.node().getBBox().width,
Math.max(
this.hTextPoint.node().getBBox().width, textMax
)) + margin[3];
// Move to right all of the axis hTextVars
this.hTextPoint
.attr("transform", function(d,i){
return "translate(0,"+(axisHeight*0.5)+")";
});
this.hTextVar
.attr("transform", function(d,i){
return "translate(0,"+axisHeight*(i+0.5)+")";
});
if (B_showTotalPoint)
this.hTextTotalPt
.attr("transform", function(d,i){
return "translate(0,"+axisHeight*(nObs+1.5)+")";
});
this.hTextPred
.attr("transform", function(d,i){
return "translate(0,"+axisHeight*(nObs+2.5)+")";
});
// Generate axes group
this.axesGroup = this.svg.append("svg:g");
// Create scale function for point mapping
var fScalePoint = null;
var fScalePointNorm = null;
if (bUsePointNorm) {
fScalePoint = d3.scale.linear()
.domain( [pointMin, pointMax] )
.range( [0, 100] );
fScalePointNorm = d3.scale.linear()
.domain( [0, 100] )
.range( [textMax, width-margin[1]] );
} else
fScalePoint = d3.scale.linear()
.domain( [pointMin, pointMax] )
.range( [textMax, width-margin[1]] );
//
// Top-side fixed axis
//
this.axesGroup.append("svg:g")
.attr("stroke", "#222")
.attr("class", "axis")
.call(
d3.svg.axis()
.scale(fScalePointNorm ? fScalePointNorm : fScalePoint)
.orient("bottom")
)
.attr("transform", "translate(0,"+(axisHeight*0.5)+")");
//
// Variable axes
//
for (var i=1 ; i<axes.length ; i++) {
var aAx = axes[i];
var fScaleAxis = null;
var fScaleCalcPt = null;
var obsi = parseFloat(obs[i-1]);
var val = typeof aAx[5] == 'function' ?
Math.round(aAx[5](obsi)*1000)/1000 : obs[i-1];
if (aAx[2] == null && aAx[1].length) {
var tm = [], tm2 = [];
var adj = false;
/* if (fScalePointNorm && fScalePointNorm(fScalePoint(aAx[3][aAx[1][0]])) != textMax) {
tm.push(fScalePointNorm(0));
tm2.push(pointMin);
adj = true;
}*/
for (var j=0 ; j<aAx[1].length ; j++) {
tm.push(fScalePointNorm ? fScalePointNorm(fScalePoint(aAx[3][aAx[1][j]])) : fScalePoint(aAx[3][aAx[1][j]]));
tm2.push(aAx[3][aAx[1][j]]);
}
// Ordinal scale
fScaleAxis = d3.scale.ordinal()
.domain( adj ? [ "" ].concat(aAx[1]) : aAx[1] )
.range(tm);
fScaleCalcPt = d3.scale.ordinal()
.domain( adj ? [ "" ].concat(aAx[1]) : aAx[1] )
.range(tm2);
} else {
fScaleAxis = d3.scale.linear()
.domain( [0, aAx[2]] )
.range(
fScalePointNorm ?
[fScalePointNorm(fScalePoint(aAx[3])), fScalePointNorm(fScalePoint(aAx[4]))] :
[fScalePoint(aAx[3]), fScalePoint(aAx[4])]
);
fScaleCalcPt = d3.scale.linear()
.domain( [aAx[1], aAx[2]] )
.range( [aAx[3], aAx[4]] );
}
var posY = axisHeight*(i+0.5);
var posObs = fScaleAxis(val);
if (typeof posObs == "undefined")
posObs = fScaleAxis( fScaleAxis.domain()[val] );
var nTick = (fScaleAxis.range()[1]-fScaleAxis.range()[0])/40;
if (nTick < 2) nTick = 2;
// Drawing observation on point axis
if (!dbg) this.axesGroup.append("svg:circle")
.attr("r", 2)
.attr("transform", "translate("+posObs+","+(axisHeight*0.5)+")");
// Add axis
var hAxis = this.axesGroup.append("svg:g")
.attr("transform", function(d) {
return "translate(0,"+posY+")";
})
.attr("class", "axis")
.attr("stroke", dbg?'black':colors[i])
.attr("opacity", 0)
.call(
d3.svg.axis()
.scale(fScaleAxis)
.orient("bottom")
.ticks(nTick)
);
wrap(hAxis.selectAll(".tick text"));
// Drawing observation on axis
var hCircle = null;
if (!dbg) hCircle = this.axesGroup.append("svg:circle")
.attr("r", 3)
.attr("opacity", 0)
.attr("transform", "translate("+posObs+","+posY+")");
// Connect two circles
var hLineConnect = null;
if (!dbg) hLineConnect = this.axesGroup.append("svg:line")
.attr("x1", posObs)
.attr("x2", posObs)
.attr("y1", axisHeight*0.5)
.attr("y2", posY)
.attr("opacity", 0)
.attr("style", "stroke:rgb(255,0,0);stroke-width:0.5")
.style("stroke-dasharray", ("5, 5"));
var x_start_pixel = fScalePointNorm(fScalePoint(0));
if (isNaN(x_start_pixel)) {
x_start_pixel = textMax; // 安全回退
}
// 2. 创建 hLineValue, 无论变量是 Yes/No 还是连续型
var hLineValue = null;
if (!dbg) {
hLineValue = this.axesGroup.append("svg:line")
.attr("x1", x_start_pixel) // 始终使用 0 点作为起始
.attr("x2", x_start_pixel) // 起始长度为 0
.attr("y1", posY)
.attr("y2", posY)
.attr("style", "stroke:"+colors[i]+";stroke-width:5");
}
// Animation
if (anim == true) {
hAxis.transition().delay(dur * i)
.duration(dur)
.attr("opacity", 1);
if (!dbg) hCircle.transition().delay(dur * i)
.duration(dur)
.attr("opacity", 1);
if (!dbg) hLineConnect.transition().delay(dur * i)
.duration(dur)
.attr("opacity", 1);
if (!dbg) hLineValue.transition().delay(dur * i)
.duration(dur)
.attr("x2", posObs);
} else {
hAxis.attr("opacity", 1);
if (!dbg) hCircle.attr("opacity", 1);
if (!dbg) hLineConnect.attr("opacity", 1);
if (!dbg && hLineValue) hLineValue.attr("x2", posObs);
}
if (aAx[2] != null && !dbg) {
var obsText = this.axesGroup.append("svg:text")
.text(function(d) {return val;})
.attr("class", "obsText")
.attr("fill", function(d) {return colors[i];})
.attr("transform", "translate("+(posObs+3)+","+(posY-3)+")");
}
points[i] = fScaleCalcPt(val);
if (typeof points[i] == "undefined")
points[i] = fScaleCalcPt(fScaleAxis.domain()[val]);
else if (points[i] != points[i])
points[i] = 0;
if (typeof aAx[3] == 'object') {
var tMin = 99999999;
var tMax = -99999999;
for (var j in aAx[3]) if (aAx[3].hasOwnProperty(j)) {
var v = aAx[3][j];
if (tMin > v) tMin = v;
if (tMax < v) tMax = v;
}
totalMin += bUsePointNorm ? fScalePoint(tMin) : tMin;
totalMax += bUsePointNorm ? fScalePoint(tMax) : tMax;
rawTotalMin += tMin;
rawTotalMax += tMax;
if (bUsePointNorm)
console.log(aAx[0] + " point range " + fScalePoint(tMin) + " ~ " + fScalePoint(tMax));
else
console.log(aAx[0] + " point range " + tMin + " ~ " + tMax);
} else {
totalMin += bUsePointNorm ? fScalePoint(aAx[3]) : aAx[3];
totalMax += bUsePointNorm ? fScalePoint(aAx[4]) : aAx[4];
rawTotalMin += aAx[3];
rawTotalMax += aAx[4];
}
totalPoint += points[i];
}
var fScaleTotal = d3.scale.linear()
.domain([bUsePointNorm ? 0 : totalMin, totalMax])
.range([textMax, width-margin[1]]);
//
// Bottom-side fixed axis
//
// Add total point axis
if (B_showTotalPoint) this.axesGroup.append("svg:g")
.attr("transform", function(d) {
return "translate(0,"+axisHeight*(nObs+1.5)+")";
})
.attr("class", "axis")
.attr("stroke", "#222")
.call(
d3.svg.axis()
.scale(fScaleTotal)
.orient("bottom")
);
// Add prediction axis
var dd = [3.50, 20, 40, 70, 80, 90, 95, 98, 99, 99.97];
var rr = [0, 90, 130, 186, 212, 255, 285, 322,352, 510];
for (var i=0 ; i<rr.length ; i++) rr[i] += textMax;
console.log("Total point max " + totalPoint);
var fScalePred = d3.scale.ordinal()
.domain(dd)
.range(rr);
this.axesGroup.append("svg:g")
.attr("transform", function(d) {
return "translate(0,"+axisHeight*(nObs+2.5)+")";
})
.attr("class", "axis")
.attr("stroke", "#222")
.call(
d3.svg.axis()
.scale(fScalePred)
.orient("bottom")
);
var calcTotalScale = d3.scale.linear()
.domain([bUsePointNorm ? 0 : totalMin, totalMax])
.range([textMax, fScaleTotal(totalMax)]);
var lw = 0;
if (B_showTotalPoint) for (var i=1 ; i<points.length ; i++) {
if (dbg) continue;
var curLine = this.axesGroup.append("svg:line")
.attr("x1", function(d) {
var width = calcTotalScale(bUsePointNorm ? 0 : points[1]);
for (var j=1 ; j<i ; j++) {
width += calcTotalScale(fScalePoint(points[j]))-(textMax);
}
return width;
})
.attr("x2", function(d) {
var width = calcTotalScale(bUsePointNorm ? 0 : points[1]);
for (var j=1 ; j<i ; j++) {
width += calcTotalScale(fScalePoint(points[j]))-(textMax);
}
return width;
})
.attr("y1", axisHeight*(nObs+1.5))
.attr("y2", axisHeight*(nObs+1.5))
.attr("style", "stroke:"+colors[i]+";stroke-width:5");
if (anim == true)
curLine.transition().delay(dur * i)
.duration(dur)
.attr("x2", function(d) {
var width = calcTotalScale(bUsePointNorm ? 0 : points[1]);
for (var j=1 ; j<=i ; j++) {
width += calcTotalScale(fScalePoint(points[j]))-(textMax);
}
lw = width;
return width;
});
else curLine.attr("x2", function(d) {
var width = calcTotalScale(bUsePointNorm ? 0 : points[1]);
for (var j=1 ; j<=i ; j++) {
width += calcTotalScale(fScalePoint(points[j]))-(textMax);
}
lw = width;
return width;
});
}
// Add prediction bar
if (!dbg) {
this.axesGroup.append("svg:circle")
.attr("transform", "translate("+lw/*fScalePred(pred)*/+","+axisHeight*(nObs+2.5)+")")
.attr("r", 3);
this.axesGroup.append("svg:line")
.attr("x1", textMax)
.attr("x2", lw)//fScalePred(pred))
.attr("y1", axisHeight*(nObs+2.5))
.attr("y2", axisHeight*(nObs+2.5))
.attr("style", "stroke:red;stroke-width:5;");
}
}
function prob(n, acc, conv) {
var pred = n[0];
for (var i=1 ; i<n.length ; i++) {
var a = acc[i-1];
var val = $(a).val();
if (!val.length) return a+" is empty!";
if (typeof eval("conv[\""+a+"\"]") != 'undefined') {
val = Number(val);
eval("val = conv[\""+a+"\"](Number(val))");
}
if (n[i][4] === undefined) {
var key = (val === "1") ? "Yes" : "No";
pred += n[i][3][key];
}
else
pred += val * n[i][1];
}
// console.log("Prob " + pred);
return 1/(1+Math.exp(-pred));
}
function calc() {
// 1. 获取输入值
var age_val = parseFloat($("#age").val());
var size_val = parseFloat($("#size").val());
// 注意:JQuery 获取 radio value 返回的是 string,需要转 number
var cea_val = parseInt($("input[name=cea]:checked").val());
var ca199_val = parseInt($("input[name=ca199]:checked").val());
var loc_val = parseInt($("input[name=loc]:checked").val()); // Head=1, BT=0
var mnp_val = parseInt($("input[name=mnp]:checked").val());
// 简单的输入验证
if (isNaN(age_val) || isNaN(size_val)) {
alert("Please enter valid numbers for Age and Cyst Size.");
return;
}
// 2. 计算 Linear Predictor (LP)
// Coefficients based on your provided model:
// Intercept: -3.62869
// Age: 0.03108
// Size: 0.01997
// CEA(High): 1.46568
// CA199(High): 0.64071
// Loc(Head): 0.57169
// Mural(Yes): 1.54465
var lp = -3.62869 +
(0.03108 * age_val) +
(0.01997 * size_val) +
(1.46568 * cea_val) +
(0.64071 * ca199_val) +
(0.57169 * loc_val) +
(1.54465 * mnp_val);
// 3. 计算概率 (Sigmoid function)
var risk = 1 / (1 + Math.exp(-lp));
// 4. 显示结果
$("#invaprob").val((risk * 100).toFixed(2));
$("#comment").text("");
// 5. 准备 Nomogram 可视化数据 (invax)
// 这里的难点是将系数映射到 Nomogram 的 0-100 Point 刻度。
// 依据图片:Cyst Size (0-240mm) 大约对应 0-100 Points (最长轴)
// Scaling Factor (K) Calculation:
// Max contribution of Size = 240 * 0.01997 = 4.7928
// If 4.7928 raw logits = 100 Points, then K = 100 / 4.7928 ≈ 20.8646
var K = 20.8646;
// 连续变量格式: [Label, MinVal, MaxVal, MinPoint, MaxPoint]
// 分类变量格式: [Label, [Labels], null, {Label: PointValue}]
var invax = [
-1, // Placeholder for axis 0 (Intercept usually ignored by d3nomogram logic or handled separately)
// Age: Range 10-90.
// Points at 10 (Base) = 0.
// Points at 90 = (90-10) * 0.03108 * K = 51.88
["Age (years)", 10, 90, 0, (80 * 0.03108 * K)],
// CA19-9
["CA19-9 ≥ 37 u/mL", ["Low", "High"], null, {"Low":0, "High": (0.64071 * K)}],
// CEA
["CEA ≥ 5 ng/mL", ["Low", "High"], null, {"Low":0, "High": (1.46568 * K)}],
// Cyst Size: Range 0-240. Points 0 to 100.
["Cyst Size (mm)", 0, 240, 0, (240 * 0.01997 * K)],
// Location
["Loc", ["BT", "Head"], null, {"BT":0, "Head": (0.57169 * K)}],
// Mural Nodule
["Mural Nodule", ["N", "Y"], null, {"N":0, "Y": (1.54465 * K)}]
];
// 6. 准备观测值数组 (obs) 传递给绘图函数
// 注意:d3nomogram 对于连续变量需要数值,对于分类变量需要对应的 Label 字符串
var obs = [
age_val, // 对应 invax[1] Age
(ca199_val === 1 ? "High" : "Low"), // 对应 invax[2] CA199
(cea_val === 1 ? "High" : "Low"), // 对应 invax[3] CEA
size_val, // 对应 invax[4] Size
(loc_val === 1 ? "Head" : "BT"), // 对应 invax[5] Loc
(mnp_val === 1 ? "Y" : "N") // 对应 invax[6] Mural
];
// 7. 绘制
// 注意:最后一个参数是 Linear Predictor (log odds),d3nomogram 可能会用它画底部的 Total Point 映射
d3nomogram("#invanomo", obs, invax, lp, 800);
}
$(function() {
// 修复:注释掉这一行,因为它依赖未引入的 jQuery UI 库
// $("button").button();
});
function downvis(id, fmt) {
var svgelem = $('#' + id + " > svg");
if (svgelem.length == 0) {
alert("Error: SVG element not found.");
return;
}
var svgNode = svgelem[0];
var w = svgelem.width();
var h = svgelem.height();
var serializer = new XMLSerializer();
// 克隆节点以避免修改原始图像
var svgClone = svgNode.cloneNode(true);
// 提取页面中的 CSS 样式
var styles = "";
try {
styles = $('style[type="text/css"]').text();
} catch (e) {
console.warn("Unable to extract CSS styles", e);
}
// 创建 <style> 元素并注入 <defs>
var defs = document.createElementNS("http://www.w3.org/2000/svg", "defs");
var styleElem = document.createElementNS("http://www.w3.org/2000/svg", "style");
styleElem.setAttribute("type", "text/css");
styleElem.textContent = styles;
defs.appendChild(styleElem);
svgClone.insertBefore(defs, svgClone.firstChild);
// 设置SVG的xmlns属性(对于独立文件至关重要)
svgClone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
// 序列化包含内联样式的SVG
var svgString = serializer.serializeToString(svgClone);
// 创建 Blob 和 URL
var blob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' });
var url = URL.createObjectURL(blob);
// 创建临时下载链接
var a = document.createElement('a');
a.style.display = 'none';
document.body.appendChild(a);
if (fmt === 'svg') {
a.href = url;
a.download = 'nomogram.svg';
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} else if (fmt === 'png') {
// 使用 Canvas 转换为 PNG
var img = new Image();
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
// 绘制白色背景(因为PNG支持透明度)
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0, 0, w, h);
// 绘制 SVG 图像
ctx.drawImage(img, 0, 0);
// 获取 PNG data URL
var pngUrl = canvas.toDataURL('image/png');
// 触发下载
a.href = pngUrl;
a.download = 'nomogram.png';
a.click();
// 清理
document.body.removeChild(a);
URL.revokeObjectURL(url); // 撤销 blob URL
};
img.onerror = function(e) {
console.error("Image load error", e);
alert('无法将SVG转换为PNG。');
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
img.src = url; // 将 blob URL 设为图像源
} else if (fmt === 'tif') {
alert('TIFF 格式下载不支持浏览器端生成,需要服务器支持。请选择 PNG 或 SVG。');
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
}
</script>
<style type="text/css">
/* ... 你的 CSS 样式 ... */
.ZX {
font-weight:bold;
text-anchor:end;
font-size:11pt;
}
.axis text {
font: 11px sans-serif;
stroke-width: 0;
}
.axisValue {
stroke-width:3;
}
.axis line,
.axis path {
fill: none;
shape-rendering: crispEdges;
}
.ZU { font-weight:bold; padding-left:20px; }
.main { max-width: 960px; /* 最大宽度 960px */
margin: 0 auto; /* 居中 */
padding: 1rem; /* 四周留空 */
box-sizing: border-box; /* 防止 padding 撑开宽度 */}
.res1 { width: 100%; /* 先全宽显示 */
float: none; /* 移除左右浮动 */
margin-bottom: 1rem; /* 间距 */}
.res2 { width:480px; float:right; }
.res3 { width:980px; float:left; }
.ZY { text-align:center; font-size:90%; clear:both; }
legend { font-size:120%; font-weight:bold; }
#gnb li.sep {
color: #aa988a;
font-size: 20px;
font-weight: 500;
background: transparent;
margin: 20px 0 10px;
}
.button { position: absolute; top: 100%; right: 0; width: 153px; line-height: 45px; font-size: 16px; color: #fff; text-align: center; background: #ef9952; border-radius: 0 0 10px 10px; }
</style>
</head>
<body>
<div id="wrap">
<div id="header">
</div>
<div id="container">
<h2 class="s10"> MCN Advanced Neoplasia Nomogram </h2>
<div class="st_guide"> Nomogram-based Risk Prediction of MCN Advanced Neoplasia</div>
<div id="contents">
<div id="tab_con_c01" class="s01_01">
<div class="clinical_prediction">
<div class="data" id="positive_model_data">
<div class='main'>
<fieldset>
<legend> Patient Information </legend>
<table>
<tr height='10'></tr> <tr>
<tr>
<td class='ZU'>Age (years)</td>
<td>
<input type="number" id="age" name="age" placeholder="50" style="width:60px;">
<span style="font-size:0.9em; color:#666;">(Range: 10-90)</span>
</td>
</tr>
<tr>
<td class='ZU'>Cyst Size (mm)</td>
<td>
<input type="number" id="size" name="size" placeholder="50" style="width:60px;">
<span style="font-size:0.9em; color:#666;">(Range: 0-240)</span>
</td>
</tr>
<tr>
<td class='ZU'>Location</td>
<td>
<input type="radio" id="loc_head" name="loc" value="1">
<label for="loc_head" style="cursor: pointer;">Head</label>
<input type="radio" id="loc_bt" name="loc" value="0" checked>
<label for="loc_bt" style="cursor: pointer;">Body/Tail</label>
</td>
</tr>
<tr>
<td class='ZU'>CEA ≥ 5 ng/mL</td>
<td>
<input type='radio' id='cea_yes' name='cea' value='1' >
<label for='cea_yes' style="cursor: pointer;">Yes</label>
<input type='radio' id='cea_no' name='cea' value='0' checked >
<label for='cea_no' style="cursor: pointer;">No</label>
</td>
</tr>
<tr>
<td class='ZU'>CA19-9 ≥ 37 U/mL</td>
<td>
<input type='radio' id='ca199_yes' name='ca199' value='1' >
<label for='ca199_yes' style="cursor: pointer;">Yes</label>
<input type='radio' id='ca199_no' name='ca199' value='0' checked >
<label for='ca199_no' style="cursor: pointer;">No</label>
</td>
</tr>
<tr>
<td class='ZU'> Mural Nodule Present</td>
<td>
<input type='radio' id='mnp_yes' name='mnp' value='1' >
<label for='mnp_yes' style="cursor: pointer;">Yes</label>
<input type='radio' id='mnp_no' name='mnp' value='0' checked>
<label for='mnp_no' style="cursor: pointer;">No</label>
<br />
</td>
</tr>
<tr height='10'></tr> <tr> <td colspan='2'>
<a href="#" class="bt_submit" onclick="calc()">CALCULATE</a>
</td>
</tr>
</table>
</fieldset>
</div>
<br />
<div class='res1'>
<fieldset>
<legend> Risk Prediction Result</legend>
Probability of MCN Advanced Neoplasia: <input type='text' id='invaprob' size= readonly />% <span id="comment" style="color:red; font-weight:bold; margin-left:10px;"></span><br />
<div id="invanomo"></div>
<p style="font-style: italic; color: #555; margin-bottom: 5px;">
Download the current Nomogram visualization:
</p>
<button onclick="downvis('invanomo', 'png')">PNG</button>
<button onclick="downvis('invanomo', 'svg')">SVG</button>
</fieldset>
</div>
<div id="my"></div>
<div class='ZY'>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>