-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex03.html
1147 lines (949 loc) · 41 KB
/
ex03.html
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html lang="en">
<head>
<title>Across U.S. Companies, Tax Rates Vary Greatly - Interactive Feature - NYTimes.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body id="interactiveABC" class="interactiveABC">
<a name="top"></a>
<div id="shell">
<ul id="memberTools">
<style>
body {
padding: 0;
margin: 0;
}
.NYT5Style .masthead-tools #bar1-button {
display: inline;
vertical-align: top;
}
#bar1-button {
border:none;
position: relative;
}
.bar1_hover {
opacity: 0;
}
.sub_small_hide {
display: none;
}
#bar1-button > a {
-moz-box-sizing: border-box;
background-color: #6288A5;
border: 1px solid #4D7B9F;
border-radius: 3px;
color: #fff !important;
display: inline-block;
font-size: 1em;
font-weight: bold;
font-family: nyt-franklin,nyt-franklin-1,'Helvetica Neue',Arial,sans-serif;
padding: 7px 10px 6px;
text-transform: uppercase;
text-decoration: none;
}
#bar1-button > a:hover {
background-color: #326891;
border: 1px solid #265E8B;
text-decoration: none;
}
/* EXCLUDING NYT4 INTERACTIVES */
#interactiveABC #bar1-button {
display: none;
}
</style>
</ul>
<div id="interactiveFreeFormMain">
<style type="text/css">
#interactiveFooter {
border-top: 1px solid #ddd;
margin-top: 10px;
padding-top: 12px;
}
.storyHeader h1,
.storySummary {
margin: auto;
padding: 0;
position: relative;
width: 720px;
}
.storyHeader h1 {
font-size: 28px;
margin: 1em auto .3em auto;
}
</style>
<style>
.g-content {
position: relative;
font: 11px sans-serif;
width: 970px;
/*height: 1600px;*/
}
.g-annotations {
font-size: 12px;
line-height: 1.2em;
position: absolute;
top: 350px;
}
.g-annotation {
color: #555;
position: absolute;
width: 140px;
}
.g-annotation .g-button {
background: #004276;
color: #fff;
font-weight: bold;
width: 140px;
}
.g-annotation .g-button:hover {
background: #064d84;
}
.g-annotation .g-button:active {
background: #002e5f;
}
.g-axis {
pointer-events: none;
}
.g-axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.g-axis path {
display: none;
}
.g-y.g-axis text {
font: bold 14px sans-serif;
}
.g-y.g-axis line {
stroke: #ccc;
stroke-dasharray: 1,1;
}
.g-overall {
pointer-events: none;
}
.g-overall line {
stroke: #000;
stroke-width: 2px;
}
.g-overall text {
text-anchor: middle;
}
.g-sector circle {
fill: #ddd;
stroke: #000;
stroke-width: .5px;
}
.g-sector circle.g-active {
stroke-width: 2px;
}
.g-searching .g-sector .g-match {
stroke-width: 1px;
}
.g-searching .g-sector circle:not(.g-match) {
fill-opacity: .2;
}
.g-voronoi {
fill: none;
pointer-events: all;
}
.g-buttons {
margin: 40px 0 20px 125px;
}
.g-buttons .g-button {
width: 13em;
border: solid 1px #ddd;
box-shadow: 0 1px 2px rgba(0,0,0,0.08);
outline: none;
}
.g-buttons .g-button:hover {
border-color: #ccc;
border-bottom-color: #bbb;
box-shadow: 0 1px 2px rgba(0,0,0,.15);
color: #666;
}
.g-button {
background: #fff;
border: none;
border-radius: 3px;
border-bottom-color: #ccc;
color: #888;
cursor: pointer;
font: inherit;
font-size: 12px;
line-height: 18px;
margin: 0 -1px 0 0;
padding: 4px 8px;
white-space: nowrap;
}
.g-buttons .g-button.g-active,
.g-buttons .g-button:active {
background: #f0f0f0;
border-color: #ccc;
border-top-color: #bbb;
box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
color: #222;
font-weight: 600;
}
.g-buttons .g-button:first-child {
border-radius: 5px 0 0 5px;
}
.g-buttons .g-button:last-child {
border-radius: 0 5px 5px 0;
margin-left: -5px;
}
.g-buttons .g-button:focus {
z-index: 3;
}
.g-buttons .g-button.g-active {
z-index: 2;
}
.g-search {
float: right;
position: relative;
margin-right: 154px;
}
.g-search input {
width: 240px;
height: 26px;
font: 12px sans-serif;
padding: 0 8px;
border: solid 1px #ccc;
border-radius: 4px;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.08);
}
.g-search .g-search-clear {
position: absolute;
right: 5px;
top: 5px;
background: #bbb;
border: none;
border-radius: 9px;
color: #fff;
font: 10px sans-serif;
padding: 0;
width: 18px;
height: 18px;
}
.g-search .g-search-clear:hover {
background: #aaa;
}
.g-key-color path {
display: none;
}
.g-key-color line {
stroke: #000;
shape-rendering: crispEdges;
}
.g-arrows {
position: absolute;
pointer-events: none;
top: 0;
height: 0;
width: 100%;
}
.g-arrow {
position: absolute;
bottom: 5px;
}
.g-arrow-vertical {
width: 0;
border-right: solid 1px #ccc;
}
.g-arrow-horizontal {
height: 0;
border-top: solid 1px #ccc;
}
.g-tip {
position: absolute;
pointer-events: none;
}
.g-tip-shadow {
position: absolute;
box-shadow: 0 4px 8px rgba(0,0,0,.2);
width: 100%;
height: 100%;
}
.g-tip-content {
position: absolute;
padding: 6px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
height: 100%;
}
.g-tip-box {
position: absolute;
fill: #fff;
stroke: #000;
stroke-opacity: .2;
}
.g-tip-title,
.g-tip-metric-value {
font-weight: bold;
}
.g-tip-title {
margin-bottom: 4px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.g-tip-metric {
clear: right;
padding: 2px 0;
}
.g-tip-metric + .g-tip-metric {
border-top: solid 1px #eee;
}
.g-tip-metric-value {
float: right;
}
.g-tip-metric-name {
color: #777;
}
.g-sector-notes {
color: #777;
position: absolute;
font-size: 11px;
top: 20px;
left: 10px;
margin-top: 65px;
width: 240px;
}
.g-sector-note {
position: absolute;
}
.g-annotation b,
.g-sector-note b {
color: #222;
cursor: default;
}
</style>
<div class="g-content">
<div class="g-annotations">
<div class="g-annotations-overall">
<div class="g-annotation" style="left:100px;">
<div class="g-arrows">
<div class="g-arrow g-arrow-vertical" style="left:50%;height:90px;"></div>
<div class="g-arrow g-arrow-horizontal" style="left:10%;width:80%;bottom:95px;"></div>
</div>
每7家公司中就有一家的有效税率低于10%,其中<b>Amazon</b>6%,<b>Verizon</b>9%。九家公司根本没有缴税
</div>
<div class="g-annotation" style="left:260px;">
<div class="g-arrows">
<div class="g-arrow g-arrow-vertical" style="left:17px;height:153px;"></div>
</div>
每个圆圈代表一家公司,按市值计算。最大的是<b>Apple</b>,超过4000亿美元,有效税率为14%
</div>
<div class="g-annotation" style="left:440px;">
将所有的收入和税收结合起来。标准普尔500的公司的实际税率为29.1%。但不同行业的税率差别很大
<button class="g-button" data-view="sector" style="margin-top:20px;">行业展示</button>
</div>
<div class="g-annotation" style="left:600px;">
<div class="g-arrows">
<div class="g-arrow g-arrow-vertical" style="bottom:55px;left:-50px;height:20px;"></div>
<div class="g-arrow g-arrow-vertical" style="bottom:55px;left:-14px;height:23px;"></div>
<div class="g-arrow g-arrow-vertical" style="bottom:55px;left:215px;height:46px;"></div>
<div class="g-arrow g-arrow-horizontal" style="bottom:54px;left:-50px;width:266px;"></div>
<div class="g-arrow g-arrow-vertical" style="left:50%;height:49px;"></div>
</div>
三大能源公司支付绝对最高的税收:埃克森美孚1460亿美元;雪佛龙850亿美元;和康菲石油公司58亿美元
</div>
<div class="g-annotation" style="left:780px;">
<div class="g-arrows">
<div class="g-arrow g-arrow-vertical" style="left:92px;height:87px;"></div>
</div>
有几十家公司的有效的税率无法计算,因为他们在6年的时间里亏了钱。例如,aig损失了83亿美元,同时还支付了80亿美元的税款。这些公司仍被纳入总体税率计算中。
</div>
</div>
</div>
<div class="g-graphic">
<div class="g-search">
<input type="text" placeholder="Find a company or industry…">
<button style="display:none;" class="g-search-clear">X</button>
</div>
<div class="g-buttons">
<button class="g-button g-active" data-view="overall">总体视图</button>
<button class="g-button" data-view="sector">根据行业划分</button>
</div>
</div>
<div class="g-sector-notes"></div>
<div class="g-tip" style="width:150px;height:80px;display:none;">
<div class="g-tip-shadow"></div>
<svg class="g-tip-box" width="150" height="87">
<path transform="translate(75,91)" d="M0.5,-6.5l5,-5H74.5v-79H-74.5v79H-5Z"></path>
</svg>
<div class="g-tip-content">
<div class="g-tip-title">Apple</div>
<div class="g-tip-metric" data-name="rate">
<span class="g-tip-metric-name">有效税率</span>
<span class="g-tip-metric-value">14%</span>
</div>
<div class="g-tip-metric" data-name="taxes">
<span class="g-tip-metric-name">已交税金</span>
<span class="g-tip-metric-value">$4.3B</span>
</div>
<div class="g-tip-metric" data-name="earnings">
<span class="g-tip-metric-name">收入</span>
<span class="g-tip-metric-value">$16.2B</span>
</div>
</div>
</div>
</div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
// 原图地址:https://codepen.io/ANTINEA/pen/gwbRrd
let margin = {top: 20, right: 95, bottom: 10, left: 125},
width = 970 - margin.left - margin.right,
height,
tickExtension = 20; // extend grid lines beyond scale range
const formatPercent = d3.format(".0%"),
formatTenthPercent = d3.format(".1%"),
formatNumber = d3.format(",.3s"),
formatDollars = function(d) { return (d < 0 ? "-" : "") + "$" + formatNumber(Math.abs(d)).replace(/G$/, "B"); }
const nameAll = '标普500公司'
// 顶部轴比例尺
const x = d3.scale.linear()
.domain([0, 0.6])
.rangeRound([0, width - 60])
.clamp(true)
.nice()
const y = d3.scale.ordinal()
const y0 = d3.scale.ordinal()
.domain([nameAll])
.range([150])
// d3.scaleSqrt - 创建一个幂比例尺,指数是0.5
const r = d3.scale.sqrt()
.domain([0, 1e9])
.range([0, 1])
// d3.scaleThreshold - 创建一个任意的量化的线性比例尺
const z = d3.scale.threshold()
.domain([.1, .2, .3, .4, .5])
.range(["#b35806", "#f1a340", "#fee0b6", "#d8daeb", "#998ec3", "#542788"].reverse())
// 顶部轴
const xAxis = d3.svg.axis()
.scale(x)
.orient("top")
.ticks(5)
.tickFormat(formatPercent)
const yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(-width + 60 - tickExtension * 2, 0)
.tickPadding(6)
// 柱图部分的svg
const svg = d3.select('.g-graphic')
.append('svg')
.attr('height', 420 + margin.top + margin.bottom)
.attr('width', width + margin.left + margin.right)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
// 底部图说明图例部分的svg
d3.select('.g-graphic')
.append('svg')
.style('margin-top', '20px')
.attr('height', 80)
.attr('width', width + margin.left + margin.right)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
.call(renderChartKey)
// 顶部的x轴
const gx = svg.append("g")
.attr("class", "g-x g-axis")
.call(xAxis)
// 选中所有x轴的文本部分
const tickLast = gx.selectAll(".g-x .tick:last-of-type")
// 这里的this是x轴中的单个tick, 这copy一个然后改变文字为N.A
tickLast.select(function() { return this.parentNode.appendChild(this.cloneNode(true)) })
.attr("transform", `translate(${width}, 0)`)
.select("text")
.text("N.A.")
// x轴最左边添加说明文字
const titleX = gx.append('text')
.attr('class', 'g-title')
.attr('y', -9)
.style('text-anchor', 'end')
titleX.append('tspan')
.attr('x', -20)
.style('font-weight', 'bold')
.text('有效税率')
titleX.append('tspan')
.attr('x', -20)
.attr('dy', '1em')
.text('2007-12')
/** -------------------------------------------------**/
d3.tsv("companies.tsv", type, function(error, companies) {
console.log('--------', companies)
// 根据行业进行分区, 一共12个行业
/**
[
{
key: 'Pharma',
values: [...],
rate: 0.123..
},
]
*/
const sectors = d3.nest()
.key(d => d.sector)
.entries(companies)
// 计算所有数据的平均税率
const overallRate = rate(d3.sum(companies, taxes), d3.sum(companies, earnings))
// 计算整个行业的平均税率 taxes / earnings (税收 / 收入)
sectors.forEach(d => {
d.rate = rate(d3.sum(d.values, taxes), d3.sum(d.values, earnings))
})
// 根据每个行业的税率做一个升序
sectors.sort((a, b) => a.rate - b.rate)
console.log('-----sectors', sectors)
// 计算每一个公司的税率
companies.forEach(d => {
d.rate = rate(d.taxes, d.earnings)
})
height = 120 * sectors.length
y.domain(sectors.map(d => d.key))
.rangePoints([10, height], 1)
// x轴, 12个x轴, 根据行业划分的时候预览
svg.append("g")
.attr("class", "g-y g-axis g-y-axis-sector")
.attr("transform", `translate(-${tickExtension}, 0)`)
.call(yAxis.scale(y)) // 生成y轴
.call(yAxisWrap)
.style("stroke-opacity", 0)
.style("fill-opacity", 0)
.selectAll(".tick text,.tick tspan")
.attr("x", -95)
.style("text-anchor", "start")
// 总体视图的时候展示的x轴
svg.append("g")
.attr("class", "g-y g-axis g-y-axis-overall")
.attr("transform", `translate(-${tickExtension}, 0)`)
.call(yAxis.scale(y0))
.call(yAxisWrap) // 为了将过长的text切成两个tspan, call的时候会将当前g传入
// 有多少公司就有多少clipPath(剪切路径,超出部分不显示)
const companyClip = svg.append("defs").selectAll("clipPath")
.data(companies)
.enter().append("clipPath")
.attr("id", (d, i) => "g-clip-company-" + i)
.append("circle")
.attr("cx", d => d.cx)
.attr("cy", d => d.cy - y0(nameAll))
.attr("r", d => r(d.capitalization) + 20)
// 泰森多边形, 感觉将这个节点删除有没问题啊
const gVoronoi = svg.append("g")
.attr("class", "g-voronoi")
// 没看懂这里
gVoronoi.selectAll("path")
.data(companies)
.enter().append("path")
.attr("clip-path", (d, i) => `url(#g-clip-company-${i})`)
.on("mouseover", mouseover)
.on("mouseout", mouseout)
gVoronoi.call(updateVoronoi,
function(d) { return d.cx; },
function(d) { return d.cy + y0(nameAll); },
420);
/** 12组行业的circle ------------------------------------------- **/
// 这里好像也是12组小图的逻辑
const sector = svg.append("g")
.attr("class", "g-sector")
.selectAll("g")
.data(sectors)
.enter().append("g")
.attr("transform", d => `translate(0, ${y(d.key)})`)
// 12个行业sector中插入对应公司的circle
const sectorCompany = sector.append("g")
.attr("class", "g-sector-company")
.selectAll("circle")
.data(d => d.values)
.enter().append("circle")
.attr("cx", d => d.cx)
.attr("cy", d => d.cy - y(d.sector) + y0(nameAll))
.attr("r", d => r(d.capitalization))
.style("fill", d => isNaN(d.rate) ? null : z(d.rate))
.on("mouseover", mouseover)
.on("mouseout", mouseout)
/** 12段行业描述 ------------------------------------------- **/
// 12段各行业的描述文本, 这里操作html节点
d3.select(".g-sector-notes")
.style("opacity", 0)
.style("display", "none")
.selectAll("div")
.data(sectors)
.enter().append("div")
.attr("class", "g-sector-note")
.style("top", d => y(d.key) + "px")
.html(d => sectorNoteByName[d.key])
/** 12个分视图的均值线 -------------------------------------- **/
// 在sector中插入代表平均值的竖线和数值
const sectorOverall = sector.append("g")
.attr("class", "g-overall")
.attr("transform", d => `translate(${x(d.rate)}, ${(y0(nameAll) - y(d.key))})`)
.style("stroke-opacity", 0)
.style("fill-opacity", 0)
sectorOverall.append("line")
// .attr("y1", -100)
// .attr("y2", +127)
const sectorOverallText = sectorOverall.append("text")
.attr("y", -106)
// 这里其实可以不要tspan, 直接用text就可以了
sectorOverallText.append("tspan")
.attr("x", 0)
.text(d => formatPercent(d.rate))
// 12行业的第一个额外增加一个文本描述overall
sectorOverallText.filter((d, i) => !i).append("tspan")
.attr("x", 0)
.attr("dy", "-11")
.style("font-size", "8px")
.text("OVERALL")
/** 总体视图的均值线 -------------------------------------- **/
const overall = svg.append("g")
.attr("class", "g-overall g-overall-all")
.attr("transform", `translate(${x(overallRate)}, ${y0(nameAll)})`)
overall.append("line")
.attr("y1", -100)
.attr("y2", +127)
const overallText = overall.append("text")
.attr("y", -106)
.style("font-weight", "bold")
overallText.append("tspan")
.attr("x", 0)
.style("font-size", "13px")
.text(formatTenthPercent(overallRate))
overallText.append("tspan")
.attr("x", 0)
.attr("dy", "-14")
.style("font-size", "8px")
.text("OVERALL")
/** -------------------------------------- **/
let currentView = "overall"
// 三个按钮绑定方法,用于两种视图切换
d3.selectAll(".g-content button[data-view]")
.datum(function(d) { return this.getAttribute("data-view") })
.on("click", transitionView)
// 监听输入
const searchInput = d3.select(".g-search input")
.on("keyup", keyuped)
// 输入清理
const searchClear = d3.select(".g-search .g-search-clear")
.on("click", function() {
searchInput.property("value", "").node().blur();
search();
})
var tip = d3.select(".g-tip");
var tipMetric = tip.selectAll(".g-tip-metric")
.datum(function() { return this.getAttribute("data-name"); });
d3.selectAll(".g-annotations b,.g-sector-notes b")
.datum(function() { return new RegExp("\\b" + d3.requote(this.textContent), "i"); })
.on("mouseover", mouseoverAnnotation)
.on("mouseout", mouseout);
// 键盘输入事件
function keyuped() {
// 27是ESC键
if (d3.event.keyCode === 27) {
this.value = ""
this.blur()
}
search(this.value.trim())
}
// 匹配元素, 然后添加 .g-match的class
// 如果只有一个匹配元素,则展示tooltip
function search(value) {
if (value) {
var re = new RegExp("\\b" + d3.requote(value), "i");
svg.classed("g-searching", true);
sectorCompany.classed("g-match", function(d) { return re.test(d.name) || re.test(d.sector) || (d.symbol && re.test(d.symbol)) || (d.alias && re.test(d.alias)); });
var matches = d3.selectAll(".g-match");
if (matches[0].length === 1) mouseover(matches.datum());
else mouseout();
searchClear.style("display", null);
} else {
mouseout();
svg.classed("g-searching", false);
sectorCompany.classed("g-match", false);
searchClear.style("display", "none");
}
}
// 这里void的作用立即执行后面的方法后统一返回undefined
function transitionView(view) {
if (currentView === view) view = view === "overall" ? "sector" : "overall";
d3.selectAll(".g-buttons button[data-view]").classed("g-active", function(v) { return v === view; })
switch (currentView = view) {
case "overall": return void transitionOverall();
case "sector": return void transitionSector();
}
}
// 视图切换成总视图
function transitionOverall() {
// gVoronoi.style("display", "none");
const transition = d3.transition()
.duration(750);
transition.select("svg")
.delay(720)
.attr("height", 420 + margin.top + margin.bottom)
.each("end", function() {
gVoronoi.call(updateVoronoi,
function(d) { return d.cx; },
function(d) { return d.cy + y0(nameAll); },
420);
});
transition.select(".g-annotations-overall")
.each("start", function() { this.style.display = "block"; })
.style("opacity", 1);
transition.select(".g-sector-notes")
.style("opacity", 0)
.each("end", function() { this.style.display = "none"; });
transition.selectAll(".g-y-axis-sector")
.style("stroke-opacity", 0)
.style("fill-opacity", 0);
transition.selectAll(".g-y-axis-overall")
.style("stroke-opacity", 1)
.style("fill-opacity", 1);
var transitionOverall = transition.select(".g-overall-all")
.delay(x(overallRate))
.style("stroke-opacity", 1)
.style("fill-opacity", 1);
transitionOverall.select("line")
.attr("y2", +127);
transitionOverall.select("text")
.attr("y", -106);
var transitionSectorOverall = transition.selectAll(".g-sector .g-overall")
.delay(function(d) { return x(d.rate); })
.attr("transform", function(d) { return "translate(" + x(d.rate) + "," + (y0(nameAll) - y(d.key)) + ")"; })
.style("stroke-opacity", 0)
.style("fill-opacity", 0);
transitionSectorOverall.select("line")
.attr("y1", -100)
.attr("y2", +127);
transitionSectorOverall.select("text")
.attr("y", -106);
transition.selectAll(".g-sector-company circle")
.delay(function(d) { return d.cx; })
.attr("cx", function(d) { return d.cx; })
.attr("cy", function(d) { return d.cy - y(d.sector) + y0(nameAll); });
}
// 视图切换成12个分视图
function transitionSector() {
// gVoronoi.style("display", "none");
var transition = d3.transition()
.duration(750);
transition.select("svg")
.attr("height", height + margin.top + margin.bottom)
.transition()
.delay(720)
.each("end", function() {
gVoronoi.call(updateVoronoi,
function(d) { return d.x; },
function(d) { return y(d.sector) + d.y; },
height);
});
transition.select(".g-annotations-overall")
.style("opacity", 0)
.each("end", function() { this.style.display = "none"; });
transition.select(".g-sector-notes")
.delay(250)
.each("start", function() { this.style.display = "block"; })
.style("opacity", 1);
transition.selectAll(".g-y-axis-sector,.g-sector-note")
.delay(250)
.style("stroke-opacity", 1)
.style("fill-opacity", 1);
transition.selectAll(".g-y-axis-overall")
.style("stroke-opacity", 0)
.style("fill-opacity", 0);
var transitionOverall = transition.select(".g-overall-all")
.delay(x(overallRate))
.style("stroke-opacity", 0)
.style("fill-opacity", 0);
transitionOverall.select("line")
.attr("y2", height - y0(nameAll));
var transitionSectorOverall = transition.selectAll(".g-sector .g-overall")
.delay(function(d) { return x(d.rate); })
.attr("transform", function(d) { return "translate(" + x(d.rate) + ",0)"; })
.style("stroke-opacity", 1)
.style("fill-opacity", 1);
transitionSectorOverall.select("line")
.attr("y1", -25)
.attr("y2", +25);
transitionSectorOverall.select("text")
.attr("y", -31);
transition.selectAll(".g-sector-company circle")
.delay(function(d) { return d.x; })
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
function updateVoronoi(gVoronoi, x, y, height) {
companyClip
.attr("cx", x)
.attr("cy", y);
gVoronoi
.style("display", null)
.selectAll("path")
.data(d3.geom.voronoi().x(x).y(y)(companies))
.attr("d", function(d) { return "M" + d.join("L") + "Z"; })
.datum(function(d) { return d.point; });
}
function mouseoverAnnotation(re) {
var matches = sectorCompany.filter(function(d) { return re.test(d.name) || re.test(d.alias); }).classed("g-active", true);
if (d3.sum(matches, function(d) { return d.length; }) === 1) mouseover(matches.datum());
else tip.style("display", "none");
}
function mouseover(d) {
sectorCompany.filter(function(c) { return c === d; }).classed("g-active", true);
var dx, dy;
if (currentView === "overall") dx = d.cx, dy = d.cy + y0(nameAll);
else dx = d.x, dy = d.y + y(d.sector);
dy -= 19, dx += 50; // margin fudge factors
tip.style("display", null)
.style("top", (dy - r(d.capitalization)) + "px")
.style("left", dx + "px");
tip.select(".g-tip-title")
.text(d.alias || d.name);
tipMetric.select(".g-tip-metric-value").text(function(name) {
switch (name) {
case "rate": return isNaN(d.rate) ? "N.A." : formatPercent(d.rate);
case "taxes": return formatDollars(d.taxes);
case "earnings": return formatDollars(d.earnings);
}
});
}
function mouseout() {
tip.style("display", "none");
sectorCompany.filter(".g-active").classed("g-active", false);
}
});
// 绘制底部图说明文字和图例等
function renderChartKey(g) {
const formatPercent = d3.format('.0%'),
formatNumber = d3.format('.0f')
// A position encoding for the key only.
const x = d3.scale.linear()
.domain([0, 0.6])
.range([0, 240])
const xAxis = d3.svg.axis()
.scale(x)
.orient('bottom')
.tickSize(13)
.tickValues(z.domain())
.tickFormat(d => d === 0.5 ? formatPercent(d) : formatNumber(100 * d))
g.append('text')
.attr('x', -25)
.style('text-anchor', 'end')