Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 41 additions & 102 deletions example/lib/presentation/samples/gauge/gauge_chart_sample1.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import 'dart:math';
import 'dart:ui';

import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:flutter/material.dart';
Expand All @@ -15,6 +12,12 @@ class GaugeChartSample1 extends StatefulWidget {
class GaugeChartSample1State extends State<GaugeChartSample1> {
double _value = 0.5;

// Continuous min/max thresholds — deliberately off the 11-tick grid
// (0.0, 0.1, ..., 1.0) to show GaugeMarker carries an arbitrary
// value, not a discrete tick index.
static const _minValue = 0.23;
static const _maxValue = 0.77;

@override
Widget build(BuildContext context) {
return Padding(
Expand All @@ -40,16 +43,46 @@ class GaugeChartSample1State extends State<GaugeChartSample1> {
position: GaugeTickPosition.center,
count: 11,
offset: 0,
painter: _MyCustomGaugeMinMaxTickPainter(
painter: GaugeTickCirclePainter(
color: AppColors.contentColorWhite,
radius: 5,
minIndex: 2,
maxIndex: 8,
minMaxLineLength: 40,
minMaxLineStrokeWidth: 4,
),
checkToShowTick: GaugeTicks.hideEndpoints,
),
markers: const [
GaugeMarker(
value: _minValue,
position: GaugeTickPosition.center,
painter: GaugeMarkerLinePainter(
length: 40,
thickness: 4,
color: Colors.green,
label: 'Min',
labelStyle: TextStyle(
color: Colors.green,
fontSize: 12,
fontWeight: FontWeight.bold,
),
labelSide: GaugeMarkerLabelSide.inward,
),
),
GaugeMarker(
value: _maxValue,
position: GaugeTickPosition.center,
painter: GaugeMarkerLinePainter(
length: 40,
thickness: 4,
color: Colors.red,
label: 'Max',
labelStyle: TextStyle(
color: Colors.red,
fontSize: 12,
fontWeight: FontWeight.bold,
),
labelSide: GaugeMarkerLabelSide.inward,
),
),
],
pointers: [
// Needle extending from center toward the current value.
GaugePointer(
Expand Down Expand Up @@ -95,97 +128,3 @@ class GaugeChartSample1State extends State<GaugeChartSample1> {
);
}
}

class _MyCustomGaugeMinMaxTickPainter extends GaugeTickCirclePainter {
_MyCustomGaugeMinMaxTickPainter({
super.color,
super.radius,
required this.minIndex,
required this.maxIndex,
required this.minMaxLineLength,
required this.minMaxLineStrokeWidth,
this.textOffset = 6,
this.minTextStyle = const TextStyle(
color: Colors.green,
fontSize: 12,
fontWeight: FontWeight.bold,
),
this.maxTextStyle = const TextStyle(
color: Colors.red,
fontSize: 12,
fontWeight: FontWeight.bold,
),
}) : super();

final int minIndex;
final int maxIndex;
final double minMaxLineLength;
final double minMaxLineStrokeWidth;
final double textOffset;
final TextStyle minTextStyle;
final TextStyle maxTextStyle;

final _paint = Paint();

@override
void draw(Canvas canvas, GaugeTickInfo tickInfo) {
super.draw(canvas, tickInfo);

if (tickInfo.index != minIndex && tickInfo.index != maxIndex) {
return;
}
final isMin = tickInfo.index == minIndex;
final text = isMin ? "Min" : "Max";
final textStyle = isMin ? minTextStyle : maxTextStyle;
final rotation = isMin ? pi : 0.0;
final textPainter = TextPainter(
text: TextSpan(text: text, style: textStyle),
textDirection: TextDirection.ltr,
)..layout();
// 180 degrees
canvas.rotate(rotation);
final offsetX = isMin
? 0 + textPainter.width + textOffset
: 0 - textPainter.width - minMaxLineLength / 2 - textOffset;
textPainter.paint(canvas, Offset(offsetX, 0 - textPainter.height / 2));
_paint.color = textStyle.color!;
_paint.strokeWidth = minMaxLineStrokeWidth;
canvas.drawLine(
Offset(0 - minMaxLineLength / 2, 0),
Offset(0 + minMaxLineLength / 2, 0),
_paint,
);
}

@override
GaugeTickPainter lerp(GaugeTickPainter b, double t) {
if (b is! _MyCustomGaugeMinMaxTickPainter) {
return b;
}
return _MyCustomGaugeMinMaxTickPainter(
color: Color.lerp(color, b.color, t)!,
radius: lerpDouble(radius, b.radius, t)!,
minIndex: minIndex,
maxIndex: maxIndex,
minMaxLineLength: lerpDouble(minMaxLineLength, b.minMaxLineLength, t)!,
minMaxLineStrokeWidth:
lerpDouble(minMaxLineStrokeWidth, b.minMaxLineStrokeWidth, t)!,
textOffset: lerpDouble(textOffset, b.textOffset, t)!,
minTextStyle: TextStyle.lerp(minTextStyle, b.minTextStyle, t)!,
maxTextStyle: TextStyle.lerp(maxTextStyle, b.maxTextStyle, t)!,
);
}

@override
List<Object?> get props =>
[
super.props,
minIndex,
maxIndex,
minMaxLineLength,
minMaxLineStrokeWidth,
textOffset,
minTextStyle,
maxTextStyle,
];
}
Loading
Loading