Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarChart: Animating tooltips together with bars #681

Closed
hnnngwdlch opened this issue May 22, 2021 · 1 comment
Closed

BarChart: Animating tooltips together with bars #681

hnnngwdlch opened this issue May 22, 2021 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@hnnngwdlch
Copy link

hnnngwdlch commented May 22, 2021

Hi,

I have a question related to the animation of the tooltips. Is there a way to animate the tooltips of the bar chart, so that

  • They move together with the bars in vertical direction
  • Each tooltip number is incremented/decremented to its new value

Currently, in my simple example bar chart the tooltip seems to be jumping to its new position without any animation:

Barchart_Tooltip_Animation.mov

Based on the video posted here, it seems that this is/was possible.

Code:

import 'dart:async';
import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

class BarChartSample extends StatefulWidget {

  @override
  State<StatefulWidget> createState() => BarChartSampleState();
}

class BarChartSampleState extends State<BarChartSample> {
  final Duration animDuration = const Duration(milliseconds: 700);
  final Duration animDurationTimer = const Duration(milliseconds: 800);
  List<SampleChartData> _data;
  Timer _timer;
  int _counter = 0;

  @override
  void initState() {
    _timer = Timer.periodic(animDurationTimer, (timer) {
      if (_counter >= 5) {
        timer.cancel();
      }
      _counter++;
      setState(() {
        _data = createData();
      });
    });
    _data = createData();
    super.initState();
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  List<SampleChartData> createData() {
    final random = new Random();
    final dataTmp = <SampleChartData>[];
    for (var i = 0; i <= 12; i++) {
      dataTmp.add(SampleChartData(i, random.nextDouble() * 100));
    }
    return dataTmp;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(20),
      child: BarChart(
        mainBarData(),
        swapAnimationDuration: animDuration,
      ),
    );
  }

  BarChartGroupData makeGroupData(
    int x,
    double y, {
    Color barColor = Colors.blue,
  }) {
    return BarChartGroupData(
      x: x,
      barRods: [
        BarChartRodData(
          y: y,
          colors: [barColor],
          borderRadius: const BorderRadius.all(Radius.zero),
        ),
      ],
      showingTooltipIndicators: [0],
    );
  }

  List<BarChartGroupData> showingGroups() {
    final barChartGroupDataList = <BarChartGroupData>[];
    for (var i = 0; i < _data.length; i++) {
      final barChartGroupData = makeGroupData(_data[i].x, _data[i].y);
      barChartGroupDataList.add(barChartGroupData);
    }
    return barChartGroupDataList;
  }

  BarChartData mainBarData() {
    return BarChartData(
      barTouchData: BarTouchData(
        touchTooltipData: BarTouchTooltipData(
          tooltipBgColor: Colors.transparent,
          getTooltipItem: (
            BarChartGroupData group,
            int groupIndex,
            BarChartRodData rod,
            int rodIndex,
          ) {
            return BarTooltipItem(
              rod.y.round().toString(),
              const TextStyle(
                color: Colors.black,
              ),
            );
          },
        ),
      ),
      titlesData: FlTitlesData(
        show: true,
        leftTitles: SideTitles(
          showTitles: true,
          interval: 3,
          reservedSize: 20,
        ),
      ),
      barGroups: showingGroups(),
    );
  }
}

class SampleChartData {
  final int x;
  final double y;

  SampleChartData(this.x, this.y);
}

I am using version ^0.36.1..

Thanks!

@imaNNeo
Copy link
Owner

imaNNeo commented May 27, 2021

Hi. there is a duplicate issue here #647
Please post your video there and don't forget to thumbs up.

@imaNNeo imaNNeo closed this as completed May 27, 2021
@imaNNeo imaNNeo added the duplicate This issue or pull request already exists label May 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants