Skip to content

Commit

Permalink
Adding grouped bars
Browse files Browse the repository at this point in the history
  • Loading branch information
isabia committed Feb 16, 2020
1 parent 9a58c85 commit 63cd0b3
Showing 1 changed file with 152 additions and 19 deletions.
171 changes: 152 additions & 19 deletions charts/lib/bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,185 @@ import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

import 'color_palette.dart';
import 'tween.dart';

class BarChart {
BarChart(this.bars);
BarChart(this.groups);

final List<Bar> bars;
factory BarChart.empty(Size size) {
return BarChart(<BarGroup>[]);
}

static BarChart lerp(BarChart begin, BarChart end, double t) {
final barCount = max(begin.bars.length, end.bars.length);
final bars = List.generate(
barCount,
(i) => Bar.lerp(begin._barOrNull(i), end._barOrNull(i), t),
factory BarChart.random(Size size, Random random) {
const groupWidthFraction = 0.75;
const stackWidthFraction = 0.9;
final groupRanks = _selectRanks(random, 5);
final groupCount = groupRanks.length;
final groupDistance = size.width / (1 + groupCount);
final groupWidth = groupDistance * groupWidthFraction;
final startX = groupDistance - groupWidth / 2;
final stackRanks = _selectRanks(random, ColorPalette.primary.length ~/ 2);
final stackCount = stackRanks.length;
final stackDistance = groupWidth / stackCount;
final stackWidth = stackDistance * stackWidthFraction;
final groups = List.generate(
groupCount,
(i) {
final stacks = List.generate(
stackCount,
(j) {
final baseColor = ColorPalette.primary[stackRanks[j]];
final barRanks = _selectRanks(random, 4);
final monochrome = ColorPalette.monochrome(baseColor, 4);
final bars = List.generate(
barRanks.length,
(k) => Bar(
barRanks[k],
random.nextDouble() * size.height / 2,
monochrome[barRanks[k]],
),
);
return BarStack(
stackRanks[j],
baseColor,
startX + i * groupDistance + j * stackDistance,
stackWidth,
bars,
);
},
);
return BarGroup(
groupRanks[i],
stacks,
);
},
);
return BarChart(bars);
return BarChart(groups);
}

static List<int> _selectRanks(Random random, int cap) {
final ranks = <int>[];
var rank = 0;
while (true) {
rank += random.nextInt(2);
if (cap <= rank) break;
ranks.add(rank);
rank++;
}
return ranks;
}

Bar _barOrNull(int index) => (index < bars.length ? bars[index] : null);
final List<BarGroup> groups;
}

class BarChartTween extends Tween<BarChart> {
BarChartTween(BarChart begin, BarChart end) : super(begin: begin, end: end);
BarChartTween(BarChart begin, BarChart end)
: _groupsTween = MergeTween<BarGroup>(begin.groups, end.groups),
super(begin: begin, end: end);

final MergeTween<BarGroup> _groupsTween;

@override
BarChart lerp(double t) => BarChart(_groupsTween.lerp(t));
}

class BarGroup implements MergeTweenable<BarGroup> {
BarGroup(this.rank, this.stacks);

final int rank;
final List<BarStack> stacks;

@override
BarGroup get empty => BarGroup(rank, <BarStack>[]);

@override
bool operator <(BarGroup other) => rank < other.rank;

@override
BarChart lerp(double t) => BarChart.lerp(begin, end, t);
Tween<BarGroup> tweenTo(BarGroup other) => BarGroupTween(this, other);
}

class Bar {
Bar(this.x, this.width, this.height, this.color);
class BarGroupTween extends Tween<BarGroup> {
BarGroupTween(BarGroup begin, BarGroup end)
: _stacksTween = MergeTween<BarStack>(begin.stacks, end.stacks),
super(begin: begin, end: end) {
assert(begin.rank == end.rank);
}

final MergeTween<BarStack> _stacksTween;

@override
BarGroup lerp(double t) => BarGroup(begin.rank, _stacksTween.lerp(t));
}

class BarStack implements MergeTweenable<BarStack> {
BarStack(this.rank, this.baseColor, this.x, this.width, this.bars);

final int rank;
final Color baseColor;
final double x;
final double width;
final List<Bar> bars;

@override
BarStack get empty => BarStack(rank, baseColor, x, 0.0, <Bar>[]);

@override
bool operator <(BarStack other) => rank < other.rank;

@override
Tween<BarStack> tweenTo(BarStack other) => BarStackTween(this, other);
}

class BarStackTween extends Tween<BarStack> {
BarStackTween(BarStack begin, BarStack end)
: _barsTween = MergeTween<Bar>(begin.bars, end.bars),
super(begin: begin, end: end) {
assert(begin.rank == end.rank);
}

final MergeTween<Bar> _barsTween;

@override
BarStack lerp(double t) => BarStack(
begin.rank,
Color.lerp(begin.baseColor, end.baseColor, t),
lerpDouble(begin.x, end.x, t),
lerpDouble(begin.width, end.width, t),
_barsTween.lerp(t),
);
}

class Bar extends MergeTweenable<Bar> {
Bar(this.rank, this.height, this.color);

final int rank;
final double height;
final Color color;

@override
Bar get empty => Bar(rank, 0.0, color);

@override
bool operator <(Bar other) => rank < other.rank;

@override
Tween<Bar> tweenTo(Bar other) => BarTween(this, other);

static Bar lerp(Bar begin, Bar end, double t) {
if (begin == null && end == null) return null;
assert(begin.rank == end.rank);
return Bar(
lerpDouble((begin ?? end).x, (end ?? begin).x, t),
lerpDouble(begin?.width, end?.width, t),
lerpDouble(begin?.height, end?.height, t),
Color.lerp((begin ?? end).color, (end ?? begin).color, t),
begin.rank,
lerpDouble(begin.height, end.height, t),
Color.lerp(begin.color, end.color, t),
);
}
}

class BarTween extends Tween<Bar> {
BarTween(Bar begin, Bar end) : super(begin: begin, end: end);
BarTween(Bar begin, Bar end) : super(begin: begin, end: end) {
assert(begin.rank == end.rank);
}

@override
Bar lerp(double t) => Bar.lerp(begin, end, t);
Expand Down

0 comments on commit 63cd0b3

Please sign in to comment.