initMouseEvents method

void initMouseEvents(
  1. CanvasRenderingContext2D ctx
)

Implementation

void initMouseEvents(CanvasRenderingContext2D ctx) {
  mouseCanvas!.onMouseOut.listen((e) {
    ctx.clearRect(0, 0, width, height);
  });
  mouseCanvas!.onMouseMove.listen((e) {
    ctx.clearRect(0, 0, width, height);
    final x = e.offset.x.toInt();
    final y = e.offset.y.toInt();
    ChartPoint? nearestPoint;
    var minDist = 0.0;
    if (points.isNotEmpty) {
      for (final point in points) {
        final dist = (point.x - x).abs() + (point.y - y).abs();
        if (nearestPoint == null || dist < minDist) {
          nearestPoint = point;
          minDist = dist;
        }
      }
    }
    CandlePoint? nearestCandlePoint;
    if (candlePoints.isNotEmpty) {
      for (final candlePoint in candlePoints) {
        final dist = (candlePoint.x0 - x).abs();
        if (nearestCandlePoint == null || dist < minDist) {
          nearestCandlePoint = candlePoint;
          minDist = dist;
        }
      }
    }
    if (nearestPoint != null) {
      ctx
        ..setLineDash([3, 3])
        ..strokeStyle = style.fontColor
        ..beginPath()
        ..moveTo(x, 0)
        ..lineTo(x, height)
        ..stroke()
        ..beginPath()
        ..moveTo(0, y)
        ..lineTo(width, y)
        ..stroke()
        ..setLineDash([])
        ..beginPath()
        ..arc(nearestPoint.x, nearestPoint.y, 3, 0, 2 * pi)
        ..stroke()
        ..fillStyle = style.fontColor;
      var label1 = 'date: ';
      if (axis.timeStep.endsWith('d')) {
        label1 += formatDateHum(nearestPoint.dataRow.date);
      } else {
        label1 += formatDateTimeHum(nearestPoint.dataRow.date);
      }
      final label2 = 'value: ${nearestPoint.dataRow.value}';
      if (nearestPoint.x > width / 2) {
        ctx
          ..textAlign = 'right'
          ..fillText(label1, nearestPoint.x - 3, 9)
          ..fillText(label2, nearestPoint.x - 3, 19);
      } else {
        ctx
          ..textAlign = 'left'
          ..fillText(label1, nearestPoint.x + 3, 9)
          ..fillText(label2, nearestPoint.x + 3, 19);
      }
    }
    if (nearestCandlePoint != null) {
      final x = (nearestCandlePoint.x0 + nearestCandlePoint.x1) / 2;
      ctx
        ..setLineDash([3, 3])
        ..strokeStyle = style.fontColor
        ..beginPath()
        ..moveTo(x, 0)
        ..lineTo(x, height)
        ..stroke()
        ..beginPath()
        ..moveTo(0, y)
        ..lineTo(width, y)
        ..stroke()
        ..setLineDash([])
        ..beginPath()
        ..stroke()
        ..fillStyle = style.fontColor;
      final candleRow = nearestCandlePoint.candleRow;
      var label1 = 'date: ';
      if (axis.timeStep.endsWith('d')) {
        label1 += formatDateHum(candleRow.date);
      } else {
        label1 += formatDateTimeHum(candleRow.date);
      }
      final labelO = 'o: ${candleRow.open}';
      final labelH = 'h: ${candleRow.high}';
      final labelL = 'l: ${candleRow.low}';
      final labelC = 'c: ${candleRow.close}';

      if (x > width / 2) {
        ctx
          ..textAlign = 'right'
          ..fillText(label1, x - 3, 9)
          ..fillText(labelO, x - 3, 19)
          ..fillText(labelH, x - 3, 29)
          ..fillText(labelL, x - 3, 39)
          ..fillText(labelC, x - 3, 49);
      } else {
        ctx
          ..textAlign = 'left'
          ..fillText(label1, x + 3, 9)
          ..fillText(labelO, x + 3, 19)
          ..fillText(labelH, x + 3, 29)
          ..fillText(labelL, x + 3, 39)
          ..fillText(labelC, x + 3, 49);
      }
    }
  });
}