imaNNeo / fl_chart

FL Chart is a highly customizable Flutter chart library that supports Line Chart, Bar Chart, Pie Chart, Scatter Chart, and Radar Chart.
https://flchart.dev
MIT License
6.77k stars 1.74k forks source link

Programmatically display the tooltip for a given X value #1613

Closed lamarios closed 6 months ago

lamarios commented 6 months ago

I have a dashboard with multiple graphs and when the user overs one graph (showing the tooltips of the hovered dots) I want to show the tool tips on the other graphs if there are any dots at the same X value.

I'm trying to use the showTooltipIndicators but the tools tips are not showing. I can properly broadcast to all the charts as I also display a vertical line and it works fine.

Here is the code I use to populate teh showTooltipIndicators:


     List<LineChartBarData> lines = [];

    // populate the lines

    List<LineBarSpot> forcedToolTips = [];
    if (dateHighlight != null) { // dateHighlight is the X value i want to show the tooltips for
      // we give a buffer so it doesn't have to be the exact date but a nearby
      final buffer = const Duration(minutes: 5).inMilliseconds;
      // we go through each line
      for (int i = 0; i < lines.length; i++) {
        final lineData = lines[i];

        // find the spots that are within the buffer
        lineData.spots.where((element) {
          var highlight = dateHighlight!.toDouble();
          return highlight > element.x - buffer &&
              highlight < element.x + buffer;
        }).forEach((spot) {
          // adding to the force tool tips
          print('$spot');
          forcedToolTips.add(LineBarSpot(lineData, i, spot));
        });
      }
    }

    LineChartData(
      showingTooltipIndicators: [ShowingTooltipIndicators(forcedToolTips)],
   // ...

Here's a video showing that I can effectively broacast the X value properly:

Screencast from 2024-03-20 15-50-54.webm

lamarios commented 6 months ago

Found the culprit, I had to disable the default behavior or touch events:

          lineTouchData: LineTouchData(
              handleBuiltInTouches: false,
imaNNeo commented 5 months ago

Yes, you're right. We need to disable the handleBuiltInTouches to use showingTooltipIndicators. I've mentioned it here:

I just updated the documentation. Thanks for reporting it.