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, imaNNeo#334, imaNNeo#875.
  • Loading branch information
imaNNeo authored and prajwal27 committed Mar 9, 2022
1 parent b77abc8 commit 4526e39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 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
19 changes: 15 additions & 4 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,15 @@ 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 Expand Up @@ -290,7 +299,8 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
if (barRod.toY != barRod.fromY) {
if (barRod.toY > barRod.fromY) {
// positive
final bottom = getPixelY(max(data.minY, barRod.fromY), drawSize, holder);
final bottom =
getPixelY(max(data.minY, barRod.fromY), drawSize, holder);
final top = min(
getPixelY(barRod.toY, drawSize, holder), bottom - cornerHeight);

Expand All @@ -301,9 +311,10 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
bottomRight: borderRadius.bottomRight);
} else {
// negative
final top = getPixelY(min(data.maxY, barRod.fromY), drawSize, holder);
final bottom =
max(getPixelY(barRod.toY, drawSize, holder), top + cornerHeight);
final top =
getPixelY(min(data.maxY, barRod.fromY), drawSize, holder);
final bottom = max(
getPixelY(barRod.toY, drawSize, holder), top + cornerHeight);

barRRect = RRect.fromLTRBAndCorners(left, top, right, bottom,
topLeft: borderRadius.topLeft,
Expand Down

0 comments on commit 4526e39

Please sign in to comment.