Skip to content

Commit b3ce63a

Browse files
add label inside bar chart (#1893)
* add label inside bar chart * fixed problems * fixed some problems * add label inside bar chart * fixed problems * fix rotation problem * fix formatting * add test for label feature
1 parent 9e51b11 commit b3ce63a

File tree

7 files changed

+393
-22
lines changed

7 files changed

+393
-22
lines changed

example/lib/presentation/samples/bar/bar_chart_sample5.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ class BarChartSample5State extends State<BarChartSample5> {
170170
0,
171171
value1,
172172
AppColors.contentColorGreen,
173-
BorderSide(
173+
label: isTouched ? 'A' : null,
174+
borderSide: BorderSide(
174175
color: Colors.white,
175176
width: isTouched ? 2 : 0,
176177
),
@@ -179,7 +180,8 @@ class BarChartSample5State extends State<BarChartSample5> {
179180
value1,
180181
value1 + value2,
181182
AppColors.contentColorYellow,
182-
BorderSide(
183+
label: isTouched ? 'B' : null,
184+
borderSide: BorderSide(
183185
color: Colors.white,
184186
width: isTouched ? 2 : 0,
185187
),
@@ -188,7 +190,8 @@ class BarChartSample5State extends State<BarChartSample5> {
188190
value1 + value2,
189191
value1 + value2 + value3,
190192
AppColors.contentColorPink,
191-
BorderSide(
193+
label: isTouched ? 'C' : null,
194+
borderSide: BorderSide(
192195
color: Colors.white,
193196
width: isTouched ? 2 : 0,
194197
),
@@ -197,7 +200,8 @@ class BarChartSample5State extends State<BarChartSample5> {
197200
value1 + value2 + value3,
198201
value1 + value2 + value3 + value4,
199202
AppColors.contentColorBlue,
200-
BorderSide(
203+
label: isTouched ? 'D' : null,
204+
borderSide: BorderSide(
201205
color: Colors.white,
202206
width: isTouched ? 2 : 0,
203207
),
@@ -223,28 +227,32 @@ class BarChartSample5State extends State<BarChartSample5> {
223227
-value1,
224228
AppColors.contentColorGreen.withValues(
225229
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
226-
const BorderSide(color: Colors.transparent),
230+
label: isTouched ? 'A' : null,
231+
borderSide: const BorderSide(color: Colors.transparent),
227232
),
228233
BarChartRodStackItem(
229234
-value1,
230235
-(value1 + value2),
231236
AppColors.contentColorYellow.withValues(
232237
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
233-
const BorderSide(color: Colors.transparent),
238+
label: isTouched ? 'B' : null,
239+
borderSide: const BorderSide(color: Colors.transparent),
234240
),
235241
BarChartRodStackItem(
236242
-(value1 + value2),
237243
-(value1 + value2 + value3),
238244
AppColors.contentColorPink.withValues(
239245
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
240-
const BorderSide(color: Colors.transparent),
246+
label: isTouched ? 'C' : null,
247+
borderSide: const BorderSide(color: Colors.transparent),
241248
),
242249
BarChartRodStackItem(
243250
-(value1 + value2 + value3),
244251
-(value1 + value2 + value3 + value4),
245252
AppColors.contentColorBlue.withValues(
246253
alpha: isTouched ? shadowOpacity * 2 : shadowOpacity),
247-
const BorderSide(color: Colors.transparent),
254+
label: isTouched ? 'D' : null,
255+
borderSide: const BorderSide(color: Colors.transparent),
248256
),
249257
],
250258
),

lib/src/chart/bar_chart/bar_chart_data.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,13 @@ class BarChartRodStackItem with EquatableMixin {
487487
BarChartRodStackItem(
488488
this.fromY,
489489
this.toY,
490-
this.color, [
490+
this.color, {
491+
this.label,
492+
this.labelStyle,
491493
this.borderSide = Utils.defaultBorderSide,
492-
]);
494+
});
495+
final String? label;
496+
final TextStyle? labelStyle;
493497

494498
/// Renders a Stacked Chart section from [fromY]
495499
final double fromY;
@@ -509,13 +513,17 @@ class BarChartRodStackItem with EquatableMixin {
509513
double? fromY,
510514
double? toY,
511515
Color? color,
516+
String? label,
517+
TextStyle? labelStyle,
512518
BorderSide? borderSide,
513519
}) =>
514520
BarChartRodStackItem(
515521
fromY ?? this.fromY,
516522
toY ?? this.toY,
517523
color ?? this.color,
518-
borderSide ?? this.borderSide,
524+
label: label ?? this.label,
525+
labelStyle: labelStyle ?? this.labelStyle,
526+
borderSide: borderSide ?? this.borderSide,
519527
);
520528

521529
/// Lerps a [BarChartRodStackItem] based on [t] value, check [Tween.lerp].
@@ -528,12 +536,14 @@ class BarChartRodStackItem with EquatableMixin {
528536
lerpDouble(a.fromY, b.fromY, t)!,
529537
lerpDouble(a.toY, b.toY, t)!,
530538
Color.lerp(a.color, b.color, t)!,
531-
BorderSide.lerp(a.borderSide, b.borderSide, t),
539+
label: b.label,
540+
labelStyle: b.labelStyle,
541+
borderSide: BorderSide.lerp(a.borderSide, b.borderSide, t),
532542
);
533543

534544
/// Used for equality check, see [EquatableMixin].
535545
@override
536-
List<Object?> get props => [fromY, toY, color, borderSide];
546+
List<Object?> get props => [fromY, toY, color, label, labelStyle, borderSide];
537547
}
538548

539549
/// Holds values to draw a rod in rear of the main rod.
@@ -713,7 +723,10 @@ class BarTouchTooltipData with EquatableMixin {
713723
/// [BarChart] shows a tooltip popup on top of rods automatically when touch happens,
714724
/// otherwise you can show it manually using [BarChartGroupData.showingTooltipIndicators].
715725
/// Tooltip shows on top of rods, with [getTooltipColor] as a background color.
716-
/// You can set the corner radius using [tooltipBorderRadius],
726+
/// You can set the corner radius using [tooltipRoundedRadius],
727+
/// or if you need a custom border, you can use [tooltipBorderRadius].
728+
/// Note that if both [tooltipRoundedRadius] and [tooltipBorderRadius] are set,
729+
/// the value from [tooltipBorderRadius] will be used.
717730
/// If you want to have a padding inside the tooltip, fill [tooltipPadding],
718731
/// or If you want to have a bottom margin, set [tooltipMargin].
719732
/// Content of the tooltip will provide using [getTooltipItem] callback, you can override it

lib/src/chart/bar_chart/bar_chart_painter.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,43 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
320320
..clipRect(rect)
321321
..drawRRect(barRRect, _barPaint)
322322
..restore();
323+
if (stackItem.label != null) {
324+
final textStyle = stackItem.labelStyle ??
325+
const TextStyle(
326+
color: Colors.white,
327+
fontSize: 12,
328+
fontWeight: FontWeight.bold,
329+
);
330+
331+
final labelText = stackItem.label!;
332+
final textSpan = TextSpan(text: labelText, style: textStyle);
333+
final textPainter = TextPainter(
334+
text: textSpan,
335+
textAlign: TextAlign.center,
336+
textDirection: TextDirection.ltr,
337+
textScaler: holder.textScaler,
338+
)..layout();
339+
340+
// Calculate rotation
341+
final rotation = -holder.data.rotationQuarterTurns * (pi / 2);
342+
final centerX = x;
343+
final centerY = (stackFromY + stackToY) / 2;
344+
345+
// Check if text fits vertically
346+
final segmentHeight = (stackFromY - stackToY).abs();
347+
if (textPainter.height < segmentHeight) {
348+
canvasWrapper
349+
..save()
350+
..translate(centerX, centerY)
351+
..rotate(rotation)
352+
..translate(
353+
-textPainter.width / 2,
354+
-textPainter.height / 2,
355+
);
356+
textPainter.paint(canvasWrapper.canvas, Offset.zero);
357+
canvasWrapper.restore();
358+
}
359+
}
323360

324361
// draw border stroke for each stack item
325362
drawStackItemBorderStroke(

repo_files/documentations/bar_chart.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ enum values {`start`, `end`, `center`, `spaceEvenly`, `spaceAround`, `spaceBetwe
7777
|fromY|draw stack item from this value|null|
7878
|toY|draw stack item to this value|null|
7979
|color|color of the stack item|null|
80+
|label|optional text label for the stack item|null|
81+
|labelStyle|optional TextStyle for the label|null|
8082
|borderSide|draw border stroke for each stack item|null|
8183

8284
### BarTouchData ([read about touch handling](handle_touches.md))

test/chart/bar_chart/bar_chart_data_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void main() {
5454
true,
5555
);
5656
expect(barChartRodStackItem1 == barChartRodStackItem2, false);
57+
expect(barChartRodStackItem1 == barChartRodStackItem3, false);
58+
expect(barChartRodStackItem2 == barChartRodStackItem4, false);
5759
});
5860

5961
test('BackgroundBarChartRodData equality test', () {

0 commit comments

Comments
 (0)