Skip to content

Commit

Permalink
Add groupVertically option (default is false) to allow have barRods…
Browse files Browse the repository at this point in the history
… below each other, #334, #875.
  • Loading branch information
imaNNeo committed Mar 4, 2022
1 parent d11ebd3 commit cb95d0e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:fl_chart/src/utils/lerp.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:fl_chart/src/extensions/color_extension.dart';
import 'dart:math';

/// [BarChart] needs this class to render itself.
///
Expand Down Expand Up @@ -180,10 +181,14 @@ class BarChartGroupData with EquatableMixin {
@required
final int x;

/// If set true, it will show bars below/above each other.
/// Otherwise, it will show bars beside each other.
final bool groupVertically;

/// [BarChart] renders [barRods] that represents a rod (or a bar) in the bar chart.
final List<BarChartRodData> barRods;

/// [BarChart] applies [barsSpace] between [barRods].
/// [BarChart] applies [barsSpace] between [barRods] if [groupVertically] is false.
final double barsSpace;

/// you can show some tooltipIndicators (a popup with an information)
Expand All @@ -201,10 +206,12 @@ class BarChartGroupData with EquatableMixin {
/// just put indices you want to show it on top of them.
BarChartGroupData({
required int x,
bool? groupVertically,
List<BarChartRodData>? barRods,
double? barsSpace,
List<int>? showingTooltipIndicators,
}) : x = x,
groupVertically = groupVertically ?? false,
barRods = barRods ?? const [],
barsSpace = barsSpace ?? 2,
showingTooltipIndicators = showingTooltipIndicators ?? const [];
Expand All @@ -215,24 +222,30 @@ class BarChartGroupData with EquatableMixin {
return 0;
}

final sumWidth = barRods
.map((rodData) => rodData.width)
.reduce((first, second) => first + second);
final spaces = (barRods.length - 1) * barsSpace;
if (groupVertically) {
return barRods.map((rodData) => rodData.width).reduce(max);
} else {
final sumWidth = barRods
.map((rodData) => rodData.width)
.reduce((first, second) => first + second);
final spaces = (barRods.length - 1) * barsSpace;

return sumWidth + spaces;
return sumWidth + spaces;
}
}

/// Copies current [BarChartGroupData] to a new [BarChartGroupData],
/// and replaces provided values.
BarChartGroupData copyWith({
int? x,
bool? groupVertically,
List<BarChartRodData>? barRods,
double? barsSpace,
List<int>? showingTooltipIndicators,
}) {
return BarChartGroupData(
x: x ?? this.x,
groupVertically: groupVertically ?? this.groupVertically,
barRods: barRods ?? this.barRods,
barsSpace: barsSpace ?? this.barsSpace,
showingTooltipIndicators:
Expand All @@ -245,6 +258,7 @@ class BarChartGroupData with EquatableMixin {
BarChartGroupData a, BarChartGroupData b, double t) {
return BarChartGroupData(
x: (a.x + (b.x - a.x) * t).round(),
groupVertically: b.groupVertically,
barRods: lerpBarChartRodDataList(a.barRods, b.barRods, t),
barsSpace: lerpDouble(a.barsSpace, b.barsSpace, t),
showingTooltipIndicators: lerpIntList(
Expand All @@ -256,6 +270,7 @@ class BarChartGroupData with EquatableMixin {
@override
List<Object?> get props => [
x,
groupVertically,
barRods,
barsSpace,
showingTooltipIndicators,
Expand Down
8 changes: 8 additions & 0 deletions lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
for (var i = 0; i < barGroups.length; i++) {
final barGroup = barGroups[i];
final groupX = groupsX[i];
if (barGroup.groupVertically) {
groupBarsPosition.add(
GroupBarsPosition(
groupX, List.generate(barGroup.barRods.length, (index) => groupX)
),
);
continue;
}

var tempX = 0.0;
final barsX = <double>[];
Expand Down

0 comments on commit cb95d0e

Please sign in to comment.