BarChart constructor

BarChart({
  1. Key? key,
  2. double maxValue = 100,
  3. double minValue = 0,
  4. BGDefaultConfig defaultConfig = const BGDefaultConfig(),
  5. int? totalGroups,
  6. int? totalBarsPerGroup,
  7. List<Group>? groups,
  8. double interGroupSpace = 10,
  9. double axisBarSpace = 5,
  10. AxisLabelFn? axisLabel,
  11. PopUpFn? defaultPopUp,
  12. int axisTotalMiddleLabels = 0,
  13. Bar? emptyBar,
  14. Group? emptyGroup,
  15. int? maxBarsInGroup,
  16. int? maxGroups,
  17. bool showMinOnAxis = true,
  18. bool showMaxOnAxis = true,
  19. Direction direction = Direction.right,
  20. bool popUpCanOverflow = false,
  21. bool expandableBarThickness = false,
})

Creates Animated Horizontal Bar Chart.

If groups is null, then the value of totalGroups and totalBarsPerGroup is used for generating random values for the graph.

If groups is null, then, totalGroups and totalBarsPerGroup cannot be null.

axisLabel can be used to define how to generate a widget to be displayed according to the corresponding value on the axis.

emptyBar is used to define the default Bar, which will be displayed before animating to the actual bar. Recommended: Not to Change.

emptyGroup is used to define the default Group, which will be displayed before animating to the actual group. Recommended: Not to Change.

maxBarsInGroup defines the maximum possible bars a group can have. If null then the value will be calculated as the max bars in a group. This value is useful if want smooth animations for bars incoming and outgoing.

maxGroups defines the maximum possible groups can be. If null then the value will be calculated as the length of the groups or totalGroups accordingly. This value is useful if want smooth animations for groups which might have initially no bars or emptyBars, but can change with time.

if expandableBarThickness is set to true, then BarConfig.width will be ignored.

Note:- If maxBarsInGroup or maxGroups is constant for the life-time of the BarChart, then it is highly recommended to enter these values, instead of letting it be null.

Important:- This Widget should be built inside a constrained width.

Implementation

BarChart({
  super.key,
  this.maxValue = 100,
  this.minValue = 0,
  this.defaultConfig = const BGDefaultConfig(),
  int? totalGroups,
  int? totalBarsPerGroup,
  List<Group>? groups,
  this.interGroupSpace = 10,
  this.axisBarSpace = 5,
  AxisLabelFn? axisLabel,
  PopUpFn? defaultPopUp,
  this.axisTotalMiddleLabels = 0,
  Bar? emptyBar,
  Group? emptyGroup,
  int? maxBarsInGroup,
  int? maxGroups,
  this.showMinOnAxis = true,
  this.showMaxOnAxis = true,
  this.direction = Direction.right,
  this.popUpCanOverflow = false,
  this.expandableBarThickness = false,
})  : assert(
          groups != null ||
              (totalGroups != null && totalBarsPerGroup != null),
          'If [groups] is null, then, [totalGroups] and [totalBarsPerGroup] cannot be null.'),
      assert(axisTotalMiddleLabels >= 0, 'Cannot be negative.'),
      axisLabel = axisLabel ?? ((value) => Text('${value.truncate()}')),
      emptyBar = FBar(
        minValue - 1,
        defaultConfig.bar.copyWith(
          const BarConfig(color: Colors.transparent),
        ),
      ),
      emptyGroup = FGroup(bars: [], config: defaultConfig.group) {
  this.groups = groups ?? _genGroups(totalGroups!, totalBarsPerGroup!);
  this.maxBarsInGroup = maxBarsInGroup ?? _maxBarsInGroup;
  this.maxGroups = maxGroups ?? this.groups.length;
}