您可以使用fl_chart提供的LineTouchData来实现这个功能。以下是一个示例代码:

LineChartData(
  // 省略其他配置
  lineTouchData: LineTouchData(
    touchCallback: (LineTouchResponse touchResponse) {
      if (touchResponse.lineBarSpots.length > 0 &&
          touchResponse.lineBarSpots.first != null) {
        final spot = touchResponse.lineBarSpots.first;
        // 在这里获取选中点的数据,并将其显示出来
        print('Selected spot: ${spot.x}, ${spot.y}');
      }
    },
  ),
  // 省略其他配置
)

在这个示例中,我们使用LineTouchDatatouchCallback回调来处理触摸事件。如果lineBarSpots不为空,则表示有一个点被选中。我们在回调函数中获取第一个点,并将其数据显示出来。您可以根据自己的需求修改回调函数的逻辑。

注意,在使用LineTouchData时,您需要在LineChartData中设置lineTouchData属性才能生效。

用fl_chart绘制的折线图我想点击折线后数据就显示出来

原文地址: http://www.cveoy.top/t/topic/Zdt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录