-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_reworked.html
1172 lines (1029 loc) · 35.4 KB
/
index_reworked.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
<!DOCTYPE html>
<html>
<head>
<title>LLMPerf</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Open Sans', sans-serif;
background-color: #f5f5f7;
margin: 0;
padding: 0;
}
.header {
background-color: #1d1d1f;
color: white;
padding: 10px;
margin: 0px;
text-align: center;
font-size: 24px;
font-weight: 300;
letter-spacing: 2px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.header h1 {
padding: 0;
margin: 0;
}
.tab {
overflow: hidden;
background-color: #f5f5f7;
display: flex;
justify-content: center;
}
.tab button {
background-color: inherit;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
color: #1d1d1f;
border-bottom: 2px solid transparent;
font-weight: 400;
}
.tab button:hover {
color: #0070c9;
font-weight: 600;
}
.tab button.active {
color: #0070c9;
border-bottom: 2px solid #0070c9;
font-weight: 600;
}
.tabcontent {
display: none;
padding: 10px;
width: 100%;
}
dt {
font-weight: 600;
margin-top: 10px;
}
.axis path,
.axis line {
fill: none;
stroke: #1d1d1f;
shape-rendering: crispEdges;
}
.axis text {
font-size: 14px;
fill: #1d1d1f;
}
.grid line {
stroke: #ddd;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
stroke-dasharray: 3, 3;
}
.grid path {
stroke-width: 0;
}
.dot {
stroke: #1d1d1f;
cursor: pointer;
r: 10;
}
.dot-label {
font-size: 12px;
text-anchor: middle;
fill: #1d1d1f;
}
.dot-label-bg {
font-size: 12px;
text-anchor: middle;
fill: #1d1d1f;
}
.detail-panel {
position: fixed;
right: -320px;
top: 120px;
width: 300px;
padding: 20px;
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: right 0.3s;
}
.detail-panel.show {
right: 20px;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
font-size: 40px;
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #f5f5f7;
font-weight: 600;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
.legend {
margin-top: 20px;
display: flex;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 10px;
padding: 10px;
}
.legend-color {
width: 15px;
height: 15px;
margin-right: 5px;
}
.notification-bar {
background: linear-gradient(to right, #f5deb3, #d2b48c);
color: black;
padding: 10px;
text-align: center;
font-size: 14px;
border-radius: 10px;
margin: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.tooltip {
position: absolute;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 10px;
border-radius: 5px;
pointer-events: none;
font-size: 14px;
line-height: 1.4;
max-width: 250px;
word-wrap: break-word;
}
.axis-label {
font-size: 16px;
font-weight: 600;
}
#graph {
position: relative;
width: 90%;
height: calc(100vh - 150px);
transition: width 0.3s;
width: 95%;
}
#graph.sidebar-open {
width: calc(100% - 320px);
}
#graphCore {
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: right 0.3s;
margin: 10px;
width: 90%;
}
#footer {
width: 100%;
display: block;
background-color: #1d1d1f;
color: white;
text-align: center;
padding: 10px;
}
#footer a {
color: white;
text-decoration: none;
}
.dropdown {
font-size: 18px;
padding: 10px;
}
.dropdownholder {
display: flex;
justify-content: center;
margin-top: 20px;
font-size: 18px;
}
.vs {
margin-left: 20px;
margin-right: 20px;
font-size: 30px;
}
.selection-widget {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 20px;
}
.selection-widget div {
padding: 10px;
background-color: white;
border-radius: 5px;
margin: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.selection-widget div.selected {
background-color: #f0f0f0;
}
.selection-widget div:hover {
background-color: #f0f0f0;
}
.selection-widget div input[type="checkbox"] {
margin-right: 5px;
}
@media screen and (max-width: 1200px) {
.detail-panel {
position: fixed;
top: 20px;
left: 0;
width: 93%;
height: 70%;
overflow-y: auto;
z-index: 9999;
display: none;
}
.dot {
r: 15;
}
.dot-label {
display: none;
}
.dot-label-bg {
display: none;
}
.axis-label {
font-size: 18px;
}
.selection-widget {
flex-direction: column;
}
}
@media (pointer: coarse) {
.dot {
r: 30;
}
.dot-label {
display: none;
}
.dot-label-bg {
display: none;
}
}
</style>
</head>
<body>
<div class="header">
<h1>LLMPerf</h1>
</div>
<div class="notification-bar">
Updated March 31: Added Command R and Qwen, added mobile UI and FAQ
</div>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'graph')">Graph</button>
<button class="tablinks" onclick="openTab(event, 'table')">Table</button>
<button class="tablinks" onclick="openTab(event, 'faq')">FAQ</button>
</div>
<div id="graph" class="tabcontent">
<div class="dropdownholder">
<select id="xMetric" class="dropdown">
<option value="output_tokens_price_per_million">Output Tokens Price
per Million (USD)</option>
<option value="input_tokens_price_per_million">Input Tokens Price per
Million (USD)</option>
</select>
<div class="vs">vs.</div>
<select id="yMetric" class="dropdown">
<option value="score">Chatbot Arena ELO</option>
<option value="mmlu_score">MMLU Score</option>
<option value="mt_bench">MT-Bench</option>
<option value="context_length">Context</option>
<option value="gpqa">GPQA</option>
<option value="human_eval">HumanEval (Coding)</option>
</select>
</div>
<div id="detailPanel" class="detail-panel">
<span class="close-btn" onclick="closeDetailPanel()">×</span>
<h2 id="selectedLLM"></h2>
<p id="llmDescription"></p>
<p>Company: <a id="companyLink" target="_blank"></a></p>
<table id="llmStats">
<tbody>
</tbody>
</table>
</div>
<div id="graphCore"></div>
<div class="legend"></div>
</div>
<div id="table" class="tabcontent">
<table id="dataTable">
<thead>
<tr>
<th>Model/Service</th>
<th>Company</th>
<th>Input Tokens Price per Million (USD)</th>
<th>Output Tokens Price per Million (USD)</th>
<th>Context Length</th>
<th>Chatbot Arena ELO</th>
<th>MMLU Score</th>
<th>MT-Bench</th>
<th>GPQA</th>
<th>HumanEval (Coding)</th>
<th>Release Date</th>
<th>Knowledge Cutoff Date</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="legend"></div>
</div>
<div id="faq" class="tabcontent">
<h2>FAQ</h2>
<dl>
<dt>What is an LLM?</dt>
<dd>LLM stands for Large Language Model, which is a type of artificial
intelligence that understands and generates human-like text based on
the input it receives. LLMs are trained on vast datasets of human
language and can perform a wide range of language-related tasks.</dd>
<dt>Why do you display obsolete models?</dt>
<dd>We display obsolete models to provide a comprehensive historical
context and enable performance comparisons over time. This helps users
understand the evolution of LLM technology and its capabilities.</dd>
<dt>Why don't you display very old models?</dt>
<dd>Very old models are omitted to maintain the relevance and clarity of
our benchmarks. Including them might clutter the data and make it
harder for users to find useful information about current and recently
obsolete models.</dd>
<dt>How do you gather # information for open models?</dt>
<dd>For the open models, you can download and run the weights yourself
for free -- but you probably will not due to the very expensive GPUs
required. Instead, this site shows prices from together.ai, a low-cost
service that hosts open models for you.</dd>
<dt>What is context length?</dt>
<dd>Context length refers to the maximum number of tokens an LLM can
process in a single prompt. A token can be a word, part of a word, or
even punctuation, depending on the model's training data and
tokenization method. This parameter is crucial as it affects the
model's ability to understand and generate coherent and contextually
relevant responses.</dd>
<dt>What are the metrics shown?</dt>
<dd>The metrics displayed on our site include Chatbot Arena ELO, MMLU
(Massive Multitask Language Understanding), MT-Bench (Multi-turn Benchmark),
GPQA (Google-proof Question and Answer), and
HumanEval. These metrics are used to evaluate the performance of LLMs
across different tasks and capabilities.</dd>
<dt>How often is the data updated?</dt>
<dd>The data is updated approximately once a week. This frequency
ensures that our benchmarks remain up-to-date and reflect the latest
developments and improvements in LLM technology.</dd>
<dt>Why is Grok not included?</dt>
<dd>We don't yet have any benchmark data for Grok.</dd>
<dt>Which metric is best for my application?</dt>
<dd>The best metric for your application depends on the specific tasks
you intend to use the LLM for.
<ul>
<li><strong>Chatbot Arena ELO:</strong> This is a human-rated score
where the human asks two randomly-chosen LLMs the same question and chooses the superior
answer. The ChatBot
Arena is <a
href="https://huggingface.co/spaces/lmsys/chatbot-arena-leaderboard">here.</a></li>
<li><strong>MMLU:</strong> MMLU is a very large database of
school-test-like multiple choice questions, similar to what an
undergraduate student might learn. Try it <a
href="https://d.erenrich.net/are-you-smarter-than-an-llm/index.html">here</a>.</li>
<li><strong>MT-Bench:</strong> MT-Bench is a benchmark for
"multi-turn" chats (conversations involving back-and-forth). A
description is <a
href="https://klu.ai/glossary/mt-bench-eval">here</a>.</li>
<li><strong>GPQA:</strong> GPQA is an extraordinarily challenging
multiple-choice question database similar to what an advanced
graduate student or scientific expert might learn. Learn more
about it <a href="https://github.com/idavidrein/gpqa">
here</a>.</li>
<li><strong>HumanEval:</strong> HumanEval is a code generation
benchmark that pairs questions in 23 natural languages with 12
different programming languages. The benchmark asks a question in
a natural language; gets a small piece of code; and runs it to
verify functionality.</li>
</ul>
<p>For many applications, Chatbot Arena ELO is the best metric to use, since it cannot
be trained for in advance and is evaluated by humans and an independent organization.
One downside of Chatbot Arena is that humans typically favor longer responses, even if a shorter
response is equally correct. The highest-scoring models on Chatbot Arena do tend to produce long responses. </p>
<p>MMLU, MT-Bench, HumanEval, and GPQA are automatically evaluated, so they are not biased by long answers.
The risk with these four benchmarks is that correct answers might leak into training data.
This is particularly a problem for MMLU, since the questions and answers are widely available.
MT-Bench and HumanEval mitigate this by using automatically-generated questions, and GPQA mitigates this
by not publicly releasing the correct answers. However, at least in theory, a model could be excessively
fine-tuned for these benchmarks. </p>
</dd>
</dl>
</div>
<div id="footer">
<h3>
<a href="https://github.com/dfeldman/llmperf" target="_blank">
<img
src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
alt="GitHub Link" style="width: 32px; height: 32px;"> Edit on GitHub
</a>
</h3>
</div>
<script>
const data = [
{
name: "Claude 3 Haiku",
input_tokens_price_per_million: 0.25,
output_tokens_price_per_million: 1.25,
context_length: 200,
score: 1179,
mmlu_score: 75.2,
mt_bench: null,
gpqa: 33.3,
human_eval: 75,
company: "Anthropic",
description: "Claude 3 Haiku is a powerful language model developed by Anthropic, offering high-quality text generation with a context length of 200K tokens.",
companyWebsite: "https://www.anthropic.com",
release: "2024-03-14",
cutoff: "2023-08-01"
},
{
name: "Claude 3 Sonnet",
input_tokens_price_per_million: 3,
output_tokens_price_per_million: 15,
context_length: 200,
score: 1198,
mmlu_score: 79.0,
mt_bench: null,
gpqa: 40.4,
human_eval: 73,
company: "Anthropic",
description: "Claude 3 Sonnet is another impressive language model by Anthropic, providing excellent performance for various natural language tasks with a context length of 200K tokens.",
companyWebsite: "https://www.anthropic.com",
release: "2024-03-14",
cutoff: "2023-08-01"
},
{
name: "Claude 3 Opus",
input_tokens_price_per_million: 15,
output_tokens_price_per_million: 75,
context_length: 200,
score: 1253,
mmlu_score: 86.8,
mt_bench: null,
gpqa: 50.4,
human_eval: 84.9,
company: "Anthropic",
description: "Claude 3 Opus is the most advanced language model in the Claude 3 series by Anthropic, delivering top-notch performance with a context length of 200K tokens.",
companyWebsite: "https://www.anthropic.com",
release: "2024-03-14",
cutoff: "2023-08-01"
},
{
name: "GPT-4 Turbo",
input_tokens_price_per_million: 10,
output_tokens_price_per_million: 30,
context_length: 128,
score: 1251,
mmlu_score: 86.4,
mt_bench: 9.32,
gpqa: 35.7,
human_eval: 74.5,
company: "OpenAI",
description: "GPT-4 Turbo is a high-performance language model by OpenAI, offering a context length of 128K tokens for efficient and accurate text generation.",
companyWebsite: "https://www.openai.com",
cutoff: "2023-04-01",
release: "202-06-13"
},
{
name: "GPT-4",
input_tokens_price_per_million: 30,
output_tokens_price_per_million: 60,
context_length: 32,
score: 1159,
mmlu_score: 86.4,
mt_bench: 8.96,
gpqa: 35.7,
human_eval: 74.5,
company: "OpenAI",
description: "GPT-4 is the original, now-deprecated version of GPT-4. New users should switch to GPT-4 Turbo.",
companyWebsite: "https://www.openai.com",
release: "2023-03-14",
cutoff: "2021-09-01",
},
{
name: "GPT-3.5 Turbo 0613",
input_tokens_price_per_million: 0.50,
output_tokens_price_per_million: 1.50,
context_length: 16,
score: 1114,
mmlu_score: 70,
mt_bench: 8.39,
gpqa: 28.1,
human_eval: null,
company: "OpenAI",
description: "GPT-3.5 Turbo 0613 is a powerful language model by OpenAI, offering a context length of 16K tokens for efficient and high-quality text generation.",
companyWebsite: "https://www.openai.com",
release: "2023-06-13",
cutoff: "2021-09-01"
},
{
name: "Mistral Medium",
input_tokens_price_per_million: 2.7,
output_tokens_price_per_million: 8.1,
context_length: 8,
score: 1145,
mmlu_score: null,
mt_bench: null,
gpqa: null,
human_eval: null,
company: "Mistral",
description: "Mistral Medium is a language model developed by Mistral, offering a context length of 8K tokens for various natural language tasks.",
companyWebsite: "http://mistral.ai",
release: "2024-02-25",
},
{
name: "Mistral Large",
input_tokens_price_per_million: 8,
output_tokens_price_per_million: 24,
context_length: 32,
score: 1157,
mmlu_score: null,
mt_bench: null,
gpqa: null,
human_eval: null,
company: "Mistral",
description: "Mistral Large is a more advanced language model by Mistral, delivering improved performance with a context length of 32K tokens.",
companyWebsite: "http://mistral.ai",
release: "2024-02-25",
cutoff: "2022-12-01"
},
{
name: "Gemini Dev API",
input_tokens_price_per_million: 0.50,
output_tokens_price_per_million: 1.50,
context_length: 128,
score: 1125,
mmlu_score: 83.7,
mt_bench: null,
gpqa: null,
human_eval: 74.4,
company: "Google",
description: "Gemini Dev API is a language model offered by Google, providing a context length of 128K tokens for various natural language processing tasks.",
companyWebsite: "https://www.google.com",
release: "2024-02-15",
cutoff: "2023-04-01",
},
{
name: "Gemini Pro (Bard)",
input_tokens_price_per_million: null,
output_tokens_price_per_million: null,
context_length: 128,
score: 1203,
mmlu_score: null,
mt_bench: null,
gpqa: null,
human_eval: null,
company: "Google",
description: "Gemini Advanced is a high-performance language model by Google, offering a context length of 128K tokens for advanced natural language applications.",
companyWebsite: "https://www.google.com",
release: "Unknown",
cutoff: "Online"
},
{
name: "Qwen1.5-72B-Chat",
input_tokens_price_per_million: 0.90,
output_tokens_price_per_million: 0.90,
context_length: 32,
score: 1149,
mmlu_score: 77.5,
human_eval: 42.5,
mt_bench: 8.61,
company: "Alibaba (Open)",
description: "Qwen1.5-72B-Chat is a high-performance open language model by Alibaba.",
companyWebsite: "https://qwenlm.github.io/blog/qwen1.5/",
cutoff: "2024-02-01",
release: "2024-02-04"
},
{
name: "Command R",
input_tokens_price_per_million: 0.90,
output_tokens_price_per_million: 0.90,
score: 1146,
context_length: 32,
company: "Cohere (Open)",
description: "Command R is an open model intended specifically for retrieval-augemented generation (RAG) tasks.",
companyWebsite: "https://cohere.ai",
cutoff: "2024-03-11"
}
];
function formatName(internalName) {
switch (internalName) {
case "output_tokens_price_per_million":
return "Output Tokens Price per Million (USD)";
case "input_tokens_price_per_million":
return "Input Tokens Price per Million (USD)";
case "score":
return "Chatbot Arena ELO";
case "mmlu_score":
return "MMLU Score";
case "mt_bench":
return "MT-Bench";
case "context_length":
return "Context Length";
case "gpqa":
return "GPQA";
case "human_eval":
return "HumanEval (Coding)";
case "release":
return "Release Date"
case "cutoff":
return "Knowledge Cutoff Date"
default:
return internalName;
}
}
const graphContainer = d3.select("#graphCore");
const margin = { top: 40, right: 20, bottom: 60, left: 60 };
const width = 800 - margin.left - margin.right;
const height = 600 - margin.top - margin.bottom;
const svg = graphContainer.append("svg")
.attr("width", "100%")
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const xScale = d3.scaleLinear().range([0, width]);
const yScale = d3.scaleLinear().range([height, 0]);
const colorScale = d3.scaleOrdinal(d3.schemePastel1);
const xAxis = d3.axisBottom(xScale);
const yAxis = d3.axisLeft(yScale);
let labelData = [];
let lastYMetric = "";
let lastXMetric = "";
let prevPositions = {};
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function addToolTip(xMetric, yMetric) {
return function (event, d) {
if (d.data) {
d = d.data;
}
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(`
<strong>${d.name}</strong><br/>
${formatName(xMetric)}: ${d[xMetric]}<br/>
${formatName(yMetric)}: ${d[yMetric]}<br/>
Company: ${d.company}
`)
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 28) + "px");
}
}
function closeAllToolTips() {
d3.select(".tooltip").style("opacity", 0);
}
function getTextWidth(text) {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
context.font = "12px sans-serif"; // Specify the font style and size
const width = context.measureText(text).width;
return width;
}
function isTopModel(d, xMetric, yMetric) {
const selectedModel = d;
const otherModels = data.filter(model => model !== selectedModel);
return otherModels.every(model => {
return (
(model[xMetric] === null || model[xMetric] > selectedModel[xMetric]) ||
(model[xMetric] <= selectedModel[xMetric] && model[yMetric] < selectedModel[yMetric])
);
});
}
function update(xMetric, yMetric) {
newWidth = graphContainer.node().getBoundingClientRect().width - margin.left - margin.right;
newHeight = graphContainer.node().getBoundingClientRect().height - margin.top - margin.bottom;
const filteredData = data.filter(d => d[xMetric] !== null && d[yMetric] !== null && d[xMetric] !== undefined && d[yMetric] !== undefined);
svg.selectAll(".axis-layer").remove();
svg.selectAll(".dot-layer").remove();
svg.selectAll(".label-layer").remove();
svg.selectAll(".grid-layer").remove();
const axisLayer = svg.append("g").attr("class", "axis-layer");
const dotLayer = svg.append("g").attr("class", "dot-layer");
const labelLayer = svg.append("g").attr("class", "label-layer");
const gridLayer = svg.append("g").attr("class", "grid-layer");
xScaleOrig = d3.extent(filteredData, d => d[xMetric]);
xScaleOrig[1] = xScaleOrig[1] * 1.2;
xScale.domain(xScaleOrig).nice();
yScale.domain(d3.extent(filteredData, d => d[yMetric])).nice();
// Add grid lines
gridLayer.append("g")
.attr("class", "grid")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(xScale)
.tickSize(-height)
.tickFormat("")
);
gridLayer.append("g")
.attr("class", "grid")
.call(d3.axisLeft(yScale)
.tickSize(-width)
.tickFormat("")
);
axisLayer.selectAll(".x-axis").remove();
axisLayer.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(0,${height})`)
.call(xAxis)
.append("text")
.attr("class", "axis-label")
.attr("x", (width - margin.left - margin.right) / 2)
.attr("y", margin.bottom - 10)
.attr("fill", "black")
.style("text-anchor", "middle")
.text(formatName(xMetric));
axisLayer.selectAll(".y-axis").remove();
axisLayer.append("g")
.attr("class", "y-axis")
.call(yAxis)
.append("text")
.attr("class", "axis-label")
.attr("transform", "rotate(-90)")
.attr("x", -(height - margin.top - margin.bottom) / 2)
.attr("y", -margin.left + 20)
.attr("fill", "black")
.style("text-anchor", "middle")
.style("z-index", "-1000")
.text(formatName(yMetric));
dotLayer.selectAll(".dot").remove();
dotLayer.selectAll().remove();
dotLayer.selectAll(".dot")
.data(filteredData)
.join("circle")
.attr("class", "dot")
.attr("cx", d => xScale(d[xMetric]))
.attr("cy", height)
.attr("r", 6)
.style("fill", d => isTopModel(d, xMetric, yMetric) ? colorScale(d.company) : "#ddd")
.on("mouseover", addToolTip(xMetric, yMetric))
.on("mouseout", function (d) {
tooltip.transition()
.duration(1200)
.style("opacity", 0);
})
.on("click", function (event, d) {
showDetailPanel(d);
})
.transition()
.duration(1000)
.attr("cy", d => yScale(d[yMetric]));
const padding = 5;
const rectHeight = 14;
if (newWidth < 500) {
return;
}
prevPositions = labelData.map(label => ({ x: label.x, y: label.y }));
labelData = filteredData.map(d => {
const x = xScale(d[xMetric]);
const y = yScale(d[yMetric]) - 22;
const textWidth = getTextWidth(d.name);
return {
x: x,
y: y,
textWidth: textWidth,
data: d,
width: textWidth + padding,
height: rectHeight,
};
});
adjustLabels(labelData);
const labelRects = labelLayer.selectAll(".dot-label-rect.data(labelData)").join("rect")
.attr("class", "dot-label-rect")
.attr("x", d => d.x - (d.width / 2))
.attr("y", d => d.y - (d.height / 2) - (padding))
.attr("width", d => d.width)
.attr("height", d => d.height + padding)
.attr("rx", 5)
.style("fill", "#ccc");
const labelTexts = labelLayer.selectAll(".dot-label-text")
.data(labelData)
.join("text")
.attr("class", "dot-label-text")
.attr("x", d => d.x)
.attr("y", height)
.text(d => d.data.name)
.style("text-anchor", "middle")
.style("font-size", "12px")
.style("fill", "#333")
.on("mouseover", addToolTip(xMetric, yMetric))
.on("mouseout", function (d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
})
.on("click", function (event, d) {
showDetailPanel(d.data);
})
.transition()
.duration(1000)
.attr("y", d => d.y);
const labelsVisible = d3.select("#labelCheckbox").property("checked");
labelRects.style("display", labelsVisible ? "block" : "none");
labelTexts.style("display", labelsVisible ? "block" : "none");
}
function adjustLabels(labelData) {
const maxIterations = 500;
let iteration = 0;
let overlaps = true;
while (overlaps && iteration < maxIterations) {
overlaps = false;
for (let i = 0; i < labelData.length - 1; i++) {
const label1 = labelData[i];
for (let j = i + 1; j < labelData.length; j++) {
const label2 = labelData[j];
if (isOverlapping(label1, label2)) {
overlaps = true;
const dx = label2.x - label1.x;
const dy = label2.y - label1.y;
const distance = Math.sqrt(dx * dx + dy * dy);
const overlap = nanToSmall((label1.width + label2.width) / 2 - distance);
const adjustmentX = nanToSmall(dx / distance) * overlap * (0.2);
const adjustmentY = nanToSmall(dy / distance) * overlap * (0.2);
label1.x -= adjustmentX;
label1.y -= adjustmentY;
label2.x += adjustmentX;
label2.y += adjustmentY;
}
}
//console.log("first pass end",labelData[i].data.name, labelData[i].x, labelData[i].y)
}
iteration++;
}
// Update label positions based on previous positions
labelData.forEach((label, index) => {
if (prevPositions[index]) {
const distanceThreshold = 10; // Adjust this value as needed
const prevX = prevPositions[index].x;
const prevY = prevPositions[index].y;
// Calculate the distance between the current and previous positions
const distance = Math.sqrt((label.x - prevX) ** 2 + (label.y - prevY) ** 2);
// If the distance is within the threshold, use the previous position
if (distance < distanceThreshold) {
//console.log("Switch back to previous position", label.x, label.y, prevX, prevY)
label.x = prevX;
label.y = prevY;
}
} else {
//console.log("No previous position found")
}
});
}