pixelToPoint method

CartesianChartPoint<D> pixelToPoint(
  1. Offset position
)

Converts logical pixel value to the data point value.

The pixelToPoint method takes logical pixel value as input and returns a chart data point.

Since this method is in the series controller, x and y-axis associated with this particular series will be considering for conversion value.

Widget build(BuildContext context) {
  ChartSeriesController? _chartSeriesController;
  return SfCartesianChart(
    series: <LineSeries<SalesData, num>>[
      LineSeries<SalesData, num>(
        onRendererCreated: (ChartSeriesController controller) {
          _chartSeriesController = controller;
        },
      ),
    ],
    onChartTouchInteractionUp: (ChartTouchInteractionArgs args) {
      final Offset value = Offset(args.position.dx, args.position.dy);
      final CartesianChartPoint? chartPoint =
        _chartSeriesController?.pixelToPoint(value);
      print('X point: ${chartPoint?.x}');
      print('Y point: ${chartPoint?.y}');
    },
  );
}

Implementation

CartesianChartPoint<D> pixelToPoint(Offset position) {
  if (seriesRenderer.parent == null ||
      seriesRenderer.parent!.parentData == null ||
      seriesRenderer.xAxis == null) {
    return CartesianChartPoint<D>();
  }

  final BoxParentData parentData =
      seriesRenderer.parent!.parentData! as BoxParentData;
  final Rect seriesBounds = seriesRenderer.paintBounds;
  position -= parentData.offset;
  double xValue = seriesRenderer.xAxis!
      .pixelToPoint(seriesBounds, position.dx, position.dy);
  final num yValue = seriesRenderer.yAxis!
      .pixelToPoint(seriesBounds, position.dx, position.dy);

  if (seriesRenderer.xAxis is RenderCategoryAxis ||
      seriesRenderer.xAxis is RenderDateTimeCategoryAxis) {
    xValue = xValue.round().toDouble();
  }
  final dynamic rawX = _rawXValue(seriesRenderer, xValue) ?? xValue;
  return CartesianChartPoint<D>(x: rawX, xValue: xValue, y: yValue);
}