pixelToPoint method
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.
Note: This method is only applicable for cartesian chart, not for the circular, pyramid, and funnel charts.
Widget build(BuildContext context) {
ChartSeriesController seriesController;
return Container(
child: SfCartesianChart(
series: <CartesianSeries<ChartSampleData, num>>[
LineSeries<ChartSampleData, num>(
onRendererCreated: (ChartSeriesController controller) {
seriesController = controller;
},
)
],
onChartTouchInteractionUp: (ChartTouchInteractionArgs args) {
final Offset value = Offset(args.position.dx, args.position.dy);
CartesianChartPoint<dynamic> chartpoint =
seriesController.pixelToPoint(value);
print('X point: ${chartpoint.x}');
print('Y point: ${chartpoint.y}');
}
)
);
}
Implementation
CartesianChartPoint<dynamic> pixelToPoint(Offset position) {
ChartAxisRenderer xAxisRenderer = seriesRenderer._xAxisRenderer!;
ChartAxisRenderer yAxisRenderer = seriesRenderer._yAxisRenderer!;
final ChartAxis xAxis = xAxisRenderer._axis;
final ChartAxis yAxis = yAxisRenderer._axis;
final CartesianSeries<dynamic, dynamic> series = seriesRenderer._series;
final Rect rect = seriesRenderer._chartState!._chartAxis._axisClipRect;
if (series.xAxisName != null || series.yAxisName != null) {
for (final ChartAxisRenderer axisRenderer
in seriesRenderer._chartState!._chartAxis._axisRenderersCollection) {
if (xAxis.name == series.xAxisName) {
xAxisRenderer = axisRenderer;
} else if (yAxis.name == series.yAxisName) {
yAxisRenderer = axisRenderer;
}
}
} else {
xAxisRenderer = xAxisRenderer;
yAxisRenderer = yAxisRenderer;
}
num xValue = _pointToXValue(
seriesRenderer._chartState!._requireInvertedAxis,
xAxisRenderer,
rect,
position.dx - (rect.left + xAxis.plotOffset),
position.dy - (rect.top + yAxis.plotOffset));
num yValue = _pointToYValue(
seriesRenderer._chartState!._requireInvertedAxis,
yAxisRenderer,
rect,
position.dx - (rect.left + xAxis.plotOffset),
position.dy - (rect.top + yAxis.plotOffset));
if (xAxisRenderer is LogarithmicAxisRenderer) {
final LogarithmicAxis axis = xAxis as LogarithmicAxis;
xValue = math.pow(xValue, _calculateLogBaseValue(xValue, axis.logBase));
} else {
xValue = xValue;
}
if (yAxisRenderer is LogarithmicAxisRenderer) {
final LogarithmicAxis axis = yAxis as LogarithmicAxis;
yValue = math.pow(yValue, _calculateLogBaseValue(yValue, axis.logBase));
} else {
yValue = yValue;
}
return CartesianChartPoint<dynamic>(xValue, yValue);
}