@@ -432,7 +432,6 @@ void drawChart(
432
432
}
433
433
434
434
final legend = SVGRectElement ();
435
- chart.append (legend);
436
435
legend.setAttribute ('class' ,
437
436
'downloads-chart-legend ${fillColorClass (colorIndex (i ))} ${strokeColorClass (colorIndex (i ))}' );
438
437
legend.setAttribute ('height' , '$legendHeight ' );
@@ -442,33 +441,11 @@ void drawChart(
442
441
legendLabel.setAttribute ('class' , 'downloads-chart-tick-label' );
443
442
legendLabel.text = ranges[i];
444
443
chart.append (legendLabel);
444
+ chart.append (legend);
445
445
legends.add ((legend, legendLabel));
446
446
}
447
447
448
- for (var i = legends.length - 1 ; i >= 0 ; i-- ) {
449
- // We traverse the legends in reverse order so that the legend of the newest
450
- // version is placed first.
451
- final (legend, legendLabel) = legends[i];
452
-
453
- if (legendX + marginPadding + legendWidth + legendLabel.getBBox ().width >
454
- xMax) {
455
- // There is no room for the legend and label.
456
- // Make a new line and update legendX and legendY accordingly.
457
- legendX = xZero;
458
- legendY += 2 * marginPadding + legendHeight;
459
- }
460
-
461
- legend.setAttribute ('x' , '$legendX ' );
462
- legend.setAttribute ('y' , '$legendY ' );
463
- legendLabel.setAttribute ('y' , '${legendY + legendHeight }' );
464
- legendLabel.setAttribute ('x' , '${legendX + marginPadding + legendWidth }' );
465
-
466
- // Update x coordinate for next legend
467
- legendX += legendWidth +
468
- marginPadding +
469
- legendLabel.getBBox ().width +
470
- labelPadding;
471
- }
448
+ // Setup cursor
472
449
473
450
final cursor = SVGLineElement ()
474
451
..setAttribute ('class' , 'downloads-chart-cursor' )
@@ -479,8 +456,6 @@ void drawChart(
479
456
..setAttribute ('y2' , '$yMax ' );
480
457
chart.append (cursor);
481
458
482
- // Setup mouse handling
483
-
484
459
void hideCursor (_) {
485
460
cursor.setAttribute ('style' , 'opacity:0' );
486
461
toolTip.setAttribute ('style' , 'opacity:0;position:absolute;' );
@@ -500,6 +475,8 @@ void drawChart(
500
475
areaPaths[i].setAttribute (
501
476
'class' , '${fillColorClass (colorIndex (i ))} downloads-chart-area' );
502
477
}
478
+ legends[i].$2.removeAttribute ('class' );
479
+ legends[i].$2.setAttribute ('class' , 'downloads-chart-tick-label' );
503
480
}
504
481
}
505
482
@@ -513,6 +490,7 @@ void drawChart(
513
490
if (displayAreas) {
514
491
areaPaths[i].removeAttribute ('class' );
515
492
}
493
+ legends[i].$2.removeAttribute ('class' );
516
494
517
495
if (highlightRangeIndices.contains (i)) {
518
496
linePaths[i].setAttribute (
@@ -521,24 +499,78 @@ void drawChart(
521
499
areaPaths[i].setAttribute (
522
500
'class' , '${fillColorClass (colorIndex (i ))} downloads-chart-area' );
523
501
}
502
+ legends[i]
503
+ .$2
504
+ .setAttribute ('class' , 'downloads-chart-legend-label-highlight' );
524
505
} else if (highlightRangeIndices.isNotEmpty) {
525
506
linePaths[i].setAttribute ('class' ,
526
507
'${strokeColorClass (colorIndex (i ))} downloads-chart-line-faded' );
527
508
if (displayAreas) {
528
509
areaPaths[i].setAttribute ('class' ,
529
510
'${fillColorClass (colorIndex (i ))} downloads-chart-area-faded' );
530
511
}
512
+
513
+ legends[i].$2.setAttribute ('class' , 'downloads-chart-tick-label' );
531
514
} else {
532
515
linePaths[i].setAttribute (
533
516
'class' , '${strokeColorClass (colorIndex (i ))} downloads-chart-line' );
534
517
if (displayAreas) {
535
518
areaPaths[i].setAttribute ('class' ,
536
519
'${fillColorClass (colorIndex (i ))} downloads-chart-area ' );
537
520
}
521
+ legends[i].$2.setAttribute ('class' , 'downloads-chart-tick-label' );
538
522
}
539
523
}
540
524
}
541
525
526
+ // Place legends and set highlight on hover
527
+
528
+ for (var i = legends.length - 1 ; i >= 0 ; i-- ) {
529
+ // We traverse the legends in reverse order so that the legend of the newest
530
+ // version is placed first.
531
+ final (legend, legendLabel) = legends[i];
532
+
533
+ if (legendX + marginPadding + legendWidth + legendLabel.getBBox ().width >
534
+ xMax) {
535
+ // There is no room for the legend and label.
536
+ // Make a new line and update legendX and legendY accordingly.
537
+ legendX = xZero;
538
+ legendY += 2 * marginPadding + legendHeight;
539
+ }
540
+
541
+ legend.setAttribute ('x' , '$legendX ' );
542
+ legend.setAttribute ('y' , '$legendY ' );
543
+
544
+ legendLabel.setAttribute ('y' , '${legendY + legendHeight }' );
545
+ legendLabel.setAttribute ('x' , '${legendX + marginPadding + legendWidth }' );
546
+
547
+ // Update x coordinate for next legend
548
+ legendX += legendWidth +
549
+ marginPadding +
550
+ legendLabel.getBBox ().width +
551
+ labelPadding;
552
+
553
+ legend.onMouseMove.listen ((e) {
554
+ setHighlights ({i});
555
+ e.stopImmediatePropagation ();
556
+ });
557
+ legendLabel.onMouseMove.listen ((e) {
558
+ setHighlights ({i});
559
+ e.stopImmediatePropagation ();
560
+ });
561
+
562
+ legend.onMouseLeave.listen ((e) {
563
+ resetHighlights ();
564
+ e.stopImmediatePropagation ();
565
+ });
566
+ legendLabel.onMouseLeave.listen ((e) {
567
+ resetHighlights ();
568
+ e.stopImmediatePropagation ();
569
+ });
570
+ }
571
+
572
+ // Setup mouse handling on chart: show cursor, tooltip and highlights
573
+
542
574
svg.onMouseMove.listen ((e) {
543
575
final boundingRect = svg.getBoundingClientRect ();
544
576
final yPosition = e.y - boundingRect.y;
0 commit comments