Skip to content

Commit 600fcfa

Browse files
feat!: bar charts take a BorderRadius
The same change BarSeries had, for the four charts that draw bars: HistogramChart.barRadius, RMultipleChart.barRadius, WaterfallChart.barRadius and TradeTimelineChart.barRadius are each a BorderRadius rather than a double, so a caller picks the corners instead of getting whichever end the painter decided to round. roundedBox in corner_radius.dart is the one place that reads the radii as they are drawn and scales them down together when they do not fit; seriesBarBox now delegates to it. The default of each keeps the shape it drew before — top corners on the histogram and R-multiple bars, all four on the waterfall and the timeline. A trade that is still open keeps its square right edge whatever the radius says, since that is the chart saying the bar has not ended.
1 parent 1cde060 commit 600fcfa

10 files changed

Lines changed: 51 additions & 42 deletions

‎doc/histogram-chart.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ width are drawn at unequal widths, since bars are placed by their range.
4444
| `barColor` | Colour of a bar without one of its own |
4545
| `negativeColor` | Colour of a bar whose range lies below zero |
4646
| `barSpacing` | Pixels taken off each side of a bar |
47-
| `barRadius` | Rounding of the top corners |
47+
| `barRadius` | Corner rounding, as a `BorderRadius` |
4848
| `referenceLines` | Values marked with a vertical line — zero, the mean, a target |
4949
| `referenceColor` | The colour of those lines |
5050
| `gridColor` | Line ruled across the chart at each count tick |

‎doc/r-multiple-chart.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ RMultipleChart(
7070
| `binWidth` | How many R each bin spans (default `0.5`) |
7171
| `profitColor`, `lossColor` | Bins above and below zero |
7272
| `showZeroLine`, `showExpectancyLine`, `referenceColor` | The reference lines |
73-
| `barSpacing`, `barRadius` | Bar shape |
73+
| `barSpacing`, `barRadius` | Bar shape; `barRadius` is a `BorderRadius` |
7474
| `tickCount`, `valueFormatter`, `axisLabelStyle`, `gridColor` | Axes; values default to `1.5R` |
7575
| `padding`, `backgroundColor` | Around and behind the chart |
7676

‎doc/trade-timeline-chart.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Trades wholly outside `start`–`end` are left out; the rest are cut to it.
8181
| --- | --- |
8282
| `profitColor`, `lossColor`, `flatColor` | Bar colours by result |
8383
| `shadeByPnl` | Draws bigger wins and losses stronger |
84-
| `barRadius` | Corner radius |
84+
| `barRadius` | Corner rounding, as a `BorderRadius`; an open trade keeps a square right edge |
8585
| `showSideMarkers`, `markerColor` | The long or short arrow |
8686
| `showPnl`, `pnlFormatter`, `pnlStyle` | The result, written in bars wide enough |
8787
| `laneBandColor` | The band behind every other lane |

‎doc/waterfall-chart.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ range the chart would pick: every total and zero, with a little room.
4646
| `min`, `max` | Ends of the value axis; `null` uses `waterfallRange` |
4747
| `barWidthFraction` | How much of its column a bar takes (default `0.6`) |
4848
| `maxBarWidth` | Upper limit on a bar's width |
49-
| `barRadius` | Corner radius |
49+
| `barRadius` | Corner rounding, as a `BorderRadius` |
5050
| `labelHeight`, `showLabels` | The label row under the bars |
5151
| `axisWidth`, `tickCount`, `showValueAxis` | The value axis |
5252
| `padding` | Space around the chart |

‎lib/src/corner_radius.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter/painting.dart';
2+
3+
/// The shape of [rect] with [radius] on its corners.
4+
///
5+
/// The radii are read as they are drawn on the screen — `topLeft` is the
6+
/// rectangle's top-left corner however the shape it belongs to was grown — and
7+
/// scaled down together when they are too big for [rect], so a corner can
8+
/// never bow past the middle of an edge.
9+
RRect roundedBox(Rect rect, BorderRadius radius) {
10+
if (radius == BorderRadius.zero) {
11+
return RRect.fromRectAndRadius(rect, Radius.zero);
12+
}
13+
return radius.toRRect(rect).scaleRadii();
14+
}

‎lib/src/distribution/histogram_chart.dart‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:math' as math;
22

33
import 'package:flutter/widgets.dart';
44

5+
import '../corner_radius.dart';
56
import '../renderer/text_painter_cache.dart';
67
import '../series/series_axis.dart';
78
import '../utils/axis_ticks.dart';
@@ -215,7 +216,7 @@ class HistogramChart extends StatefulWidget {
215216
this.barColor = const Color(0xFF4C86CD),
216217
this.negativeColor,
217218
this.barSpacing = 1,
218-
this.barRadius = 1,
219+
this.barRadius = const BorderRadius.vertical(top: Radius.circular(1)),
219220
this.showValueAxis = true,
220221
this.showCountAxis = true,
221222
this.countAxisWidth = 40,
@@ -256,8 +257,9 @@ class HistogramChart extends StatefulWidget {
256257
/// How many pixels are taken off each side of a bar.
257258
final double barSpacing;
258259

259-
/// How rounded the top corners of a bar are.
260-
final double barRadius;
260+
/// The rounding of each bar's corners, as drawn on the screen. Bars grow
261+
/// up from the bottom, so `top` is the end away from the axis.
262+
final BorderRadius barRadius;
261263

262264
/// Whether the value axis is written along the bottom.
263265
final bool showValueAxis;
@@ -553,7 +555,6 @@ class HistogramChartPainter extends CustomPainter {
553555

554556
final t = animation.clamp(0.0, 1.0);
555557
final fill = Paint()..isAntiAlias = true;
556-
final radius = Radius.circular(math.max(0, chart.barRadius));
557558
for (final bar in bars) {
558559
if (bar.rect.width <= 0) continue;
559560
fill.color = _colorOf(bar);
@@ -563,10 +564,7 @@ class HistogramChartPainter extends CustomPainter {
563564
bar.rect.right,
564565
bar.rect.bottom,
565566
);
566-
canvas.drawRRect(
567-
RRect.fromRectAndCorners(rect, topLeft: radius, topRight: radius),
568-
fill,
569-
);
567+
canvas.drawRRect(roundedBox(rect, chart.barRadius), fill);
570568
}
571569

572570
_paintReferenceLines(canvas);

‎lib/src/distribution/r_multiple_chart.dart‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class RMultipleChart extends StatelessWidget {
228228
this.showExpectancyLine = true,
229229
this.referenceColor = const Color(0x99FFFFFF),
230230
this.barSpacing = 1,
231-
this.barRadius = 1,
231+
this.barRadius = const BorderRadius.vertical(top: Radius.circular(1)),
232232
this.tickCount = 5,
233233
this.valueFormatter,
234234
this.axisLabelStyle,
@@ -287,8 +287,9 @@ class RMultipleChart extends StatelessWidget {
287287
/// How many pixels are taken off each side of a bar.
288288
final double barSpacing;
289289

290-
/// The corner radius of a bar.
291-
final double barRadius;
290+
/// The rounding of each bar's corners, as drawn on the screen, passed
291+
/// through to the histogram underneath.
292+
final BorderRadius barRadius;
292293

293294
/// About how many ticks to write on each axis.
294295
final int tickCount;

‎lib/src/series/series_paths.dart‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:typed_data';
33

44
import 'package:flutter/painting.dart';
55

6+
import '../corner_radius.dart';
67
import 'series_data.dart';
78

89
/// Appends one unbroken run of [points], in pixels and left to right, to
@@ -229,8 +230,5 @@ Path seriesRunPath(
229230
/// Returns null for a bar with no width or no length.
230231
RRect? seriesBarBox({required Rect rect, required BorderRadius radius}) {
231232
if (rect.width <= 0 || rect.height <= 0) return null;
232-
if (radius == BorderRadius.zero) {
233-
return RRect.fromRectAndRadius(rect, Radius.zero);
234-
}
235-
return radius.toRRect(rect).scaleRadii();
233+
return roundedBox(rect, radius);
236234
}

‎lib/src/timeline/trade_timeline_chart.dart‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:math' as math;
22

33
import 'package:flutter/widgets.dart';
44

5+
import '../corner_radius.dart';
56
import '../renderer/text_painter_cache.dart';
67
import '../series/series_axis.dart';
78
import '../trading.dart';
@@ -464,7 +465,7 @@ class TradeTimelineChart extends StatefulWidget {
464465
this.rowGap = 2,
465466
this.maxRowHeight = 22,
466467
this.minBarWidth = 2,
467-
this.barRadius = 3,
468+
this.barRadius = const BorderRadius.all(Radius.circular(3)),
468469
this.profitColor = const Color(0xFF2F9E44),
469470
this.lossColor = const Color(0xFFE03131),
470471
this.flatColor = const Color(0xFF868E96),
@@ -524,8 +525,10 @@ class TradeTimelineChart extends StatefulWidget {
524525
/// The narrowest a bar is drawn, so a quick trade still shows.
525526
final double minBarWidth;
526527

527-
/// The corner radius of a bar.
528-
final double barRadius;
528+
/// The rounding of each bar's corners, as drawn on the screen. A bar runs
529+
/// left to right, and an open trade has not ended, so its right-hand
530+
/// corners are left square whatever this says.
531+
final BorderRadius barRadius;
529532

530533
/// The colour of a trade that made money.
531534
final Color profitColor;
@@ -997,26 +1000,20 @@ class TradeTimelineChartPainter extends CustomPainter {
9971000
),
9981001
bar.rect.height,
9991002
);
1000-
final radius = Radius.circular(
1001-
math.min(chart.barRadius, math.min(rect.width, rect.height) / 2),
1002-
);
10031003
final open = bar.trade.isOpen;
1004+
// An open trade has not ended, so its right edge is left square.
1005+
final corners = open
1006+
? chart.barRadius.copyWith(
1007+
topRight: Radius.zero,
1008+
bottomRight: Radius.zero,
1009+
)
1010+
: chart.barRadius;
10041011
fill.color = _colorOf(bar.trade, largest);
1005-
canvas.drawRRect(
1006-
RRect.fromRectAndCorners(
1007-
rect,
1008-
topLeft: radius,
1009-
bottomLeft: radius,
1010-
// An open trade has not ended, so its right edge is left square.
1011-
topRight: open ? Radius.zero : radius,
1012-
bottomRight: open ? Radius.zero : radius,
1013-
),
1014-
fill,
1015-
);
1012+
canvas.drawRRect(roundedBox(rect, corners), fill);
10161013

10171014
if (bar.index == touched) {
10181015
canvas.drawRRect(
1019-
RRect.fromRectAndRadius(rect.inflate(1), radius),
1016+
roundedBox(rect.inflate(1), corners),
10201017
Paint()
10211018
..style = PaintingStyle.stroke
10221019
..strokeWidth = 1.5

‎lib/src/waterfall/waterfall_chart.dart‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:math' as math;
22

33
import 'package:flutter/widgets.dart';
44

5+
import '../corner_radius.dart';
56
import '../renderer/text_painter_cache.dart';
67
import '../series/series_axis.dart';
78
import '../utils/axis_ticks.dart';
@@ -250,7 +251,7 @@ class WaterfallChart extends StatefulWidget {
250251
this.riseColor = const Color(0xFF2F9E44),
251252
this.fallColor = const Color(0xFFE03131),
252253
this.totalColor = const Color(0xFF4C86CD),
253-
this.barRadius = 2,
254+
this.barRadius = const BorderRadius.all(Radius.circular(2)),
254255
this.showConnectors = true,
255256
this.connectorColor = const Color(0x55FFFFFF),
256257
this.showValues = true,
@@ -302,8 +303,9 @@ class WaterfallChart extends StatefulWidget {
302303
/// The colour of a total or subtotal.
303304
final Color totalColor;
304305

305-
/// How rounded a bar is.
306-
final double barRadius;
306+
/// The rounding of each bar's corners, as drawn on the screen. A bar
307+
/// floats between two totals, so all four corners are rounded by default.
308+
final BorderRadius barRadius;
307309

308310
/// Whether a line joins each bar to the next.
309311
final bool showConnectors;
@@ -611,7 +613,6 @@ class WaterfallChartPainter extends CustomPainter {
611613
}
612614

613615
final t = animation.clamp(0.0, 1.0);
614-
final radius = Radius.circular(math.max(0, chart.barRadius));
615616
final fill = Paint()..isAntiAlias = true;
616617

617618
for (final bar in bars) {
@@ -624,7 +625,7 @@ class WaterfallChartPainter extends CustomPainter {
624625
anchor + (bar.rect.bottom - anchor) * t,
625626
);
626627
fill.color = _colorOf(bar);
627-
canvas.drawRRect(RRect.fromRectAndRadius(rect, radius), fill);
628+
canvas.drawRRect(roundedBox(rect, chart.barRadius), fill);
628629
if (t >= 1) _paintValue(canvas, bar);
629630
if (chart.showLabels) _paintLabel(canvas, bar);
630631
}

0 commit comments

Comments
 (0)