fl_chart icon indicating copy to clipboard operation
fl_chart copied to clipboard

Dynamic tooltipBgColor in LineChart [Partial idea, i need help with the final part]

Open manylittlestorms opened this issue 2 years ago • 2 comments

Hello,

I am trying to implement a dynamic tooltipBgColor, i achieved something, but i need help with the final part.

As you can see in the following code, i am able to get the "y" coordinates of the pressed spot, since it's printed correctly. The problem is that even if i change the color of color_of_pressed_spot to the one i want, the color is not displayed since i would need to call setstate, but i cannot do it in that moment or i will get an error. Any idea on how to do it?

LineChartData mainData() {
    return LineChartData(
      lineTouchData: LineTouchData(
        touchTooltipData: LineTouchTooltipData(
          tooltipBgColor: color_of_pressed_spot,
          getTooltipItems: (List<LineBarSpot> spots) {
            return spots.map((spot) {
              print("Spot y: ${spot.y}");
              color_of_pressed_spot = changeColor(list_of_colors_i_want_to_use_for_each_spot_y, spot.y);

              return LineTooltipItem(
                getName(spot.y),
                const TextStyle(color: Colors.white),
              );
            }).toList();
          },
        ),
      ),);}

manylittlestorms avatar Jul 07 '22 15:07 manylittlestorms

Sorry I couldn't understand it properly. Can you please make a simplified sample and share your full main.dart code?

imaNNeo avatar Jul 31 '22 20:07 imaNNeo

Sorry I couldn't understand it properly. Can you please make a simplified sample and share your full main.dart code?

I am away from my laptop, anyway I basically would like to display a different color for the tooltipbgColor based on Spot.y value.

For example, if spot.y == 1 then tooltipbgColor is Blue. If spot.y == 2 then tooltipbgColor is Red and so on.

manylittlestorms avatar Aug 01 '22 06:08 manylittlestorms