Skip to content

Commit a3e9ea4

Browse files
authored
Merge pull request #2 from glemartret/feature/gauge
Feature/gauge
2 parents 9e51b11 + fffd737 commit a3e9ea4

17 files changed

+4632
-2
lines changed
Lines changed: 3 additions & 0 deletions
Loading

example/lib/presentation/resources/app_assets.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class AppAssets {
1515
return 'assets/icons/ic_radar_chart.svg';
1616
case ChartType.candlestick:
1717
return 'assets/icons/ic_candle_chart.svg';
18+
case ChartType.gauge:
19+
return 'assets/icons/ic_gauge_chart.svg';
1820
}
1921
}
2022

example/lib/presentation/samples/chart_sample.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ class CandlestickChartSample extends ChartSample {
4646
@override
4747
ChartType get type => ChartType.candlestick;
4848
}
49+
50+
class GaugeChartSample extends ChartSample {
51+
GaugeChartSample(super.number, super.builder);
52+
@override
53+
ChartType get type => ChartType.gauge;
54+
}

example/lib/presentation/samples/chart_samples.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:fl_chart_app/presentation/samples/candlestick/candlestick_chart_sample1.dart';
2+
import 'package:fl_chart_app/presentation/samples/gauge/gauge_chart_sample1.dart';
23
import 'package:fl_chart_app/util/app_helper.dart';
34

45
import 'bar/bar_chart_sample1.dart';
@@ -71,6 +72,9 @@ class ChartSamples {
7172
],
7273
ChartType.candlestick: [
7374
CandlestickChartSample(1, (context) => const CandlestickChartSample1()),
74-
]
75+
],
76+
ChartType.gauge: [
77+
GaugeChartSample(1, (context) => const GaugeChartSample1()),
78+
],
7579
};
7680
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import 'package:fl_chart/fl_chart.dart';
2+
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
3+
import 'package:flutter/material.dart';
4+
5+
class GaugeChartSample1 extends StatefulWidget {
6+
const GaugeChartSample1({super.key});
7+
8+
@override
9+
State<StatefulWidget> createState() => GaugeChartSample1State();
10+
}
11+
12+
class GaugeChartSample1State extends State {
13+
double _value = 0.7;
14+
bool _isSelected = false;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return Padding(
19+
padding: const EdgeInsets.all(28),
20+
child: Column(
21+
children: [
22+
SizedBox(
23+
width: 250,
24+
height: 250,
25+
child: GaugeChart(
26+
GaugeChartData(
27+
value: _value,
28+
valueColor: VariableGaugeColor(
29+
colors: [
30+
AppColors.contentColorYellow,
31+
AppColors.contentColorBlue,
32+
AppColors.contentColorRed
33+
],
34+
limits: [0.35, 0.5],
35+
),
36+
backgroundColor: AppColors.contentColorPurple
37+
.withValues(alpha: _isSelected ? 0.2 : 1),
38+
strokeWidth: 30,
39+
startAngle: 45,
40+
endAngle: -225,
41+
strokeCap: StrokeCap.round,
42+
ticks: const GaugeTicks(
43+
count: 11,
44+
color: AppColors.contentColorCyan,
45+
radius: 5,
46+
position: GaugeTickPosition.inner,
47+
margin: 5,
48+
),
49+
touchData: GaugeTouchData(
50+
enabled: true,
51+
touchCallback: (_, response) => setState(() {
52+
_isSelected = response?.touchedSpot != null;
53+
}),
54+
),
55+
),
56+
),
57+
),
58+
Slider(value: _value, onChanged: (v) => setState(() => _value = v)),
59+
],
60+
),
61+
);
62+
}
63+
}

example/lib/util/app_helper.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
22
import 'package:fl_chart_app/urls.dart';
33

4-
enum ChartType { line, bar, pie, scatter, radar, candlestick }
4+
enum ChartType { line, bar, pie, scatter, radar, candlestick, gauge }
55

66
extension ChartTypeExtension on ChartType {
77
String get displayName => '$simpleName Chart';
@@ -13,6 +13,7 @@ extension ChartTypeExtension on ChartType {
1313
ChartType.scatter => 'Scatter',
1414
ChartType.radar => 'Radar',
1515
ChartType.candlestick => 'Candlestick',
16+
ChartType.gauge => 'Gauge',
1617
};
1718

1819
String get documentationUrl => Urls.getChartDocumentationUrl(this);

lib/fl_chart.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export 'src/chart/base/base_chart/base_chart_data.dart';
88
export 'src/chart/base/base_chart/fl_touch_event.dart';
99
export 'src/chart/candlestick_chart/candlestick_chart.dart';
1010
export 'src/chart/candlestick_chart/candlestick_chart_data.dart';
11+
export 'src/chart/gauge_chart/gauge_chart.dart';
12+
export 'src/chart/gauge_chart/gauge_chart_data.dart';
1113
export 'src/chart/line_chart/line_chart.dart';
1214
export 'src/chart/line_chart/line_chart_data.dart';
1315
export 'src/chart/pie_chart/pie_chart.dart';
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'package:fl_chart/src/chart/gauge_chart/gauge_chart_data.dart';
2+
import 'package:fl_chart/src/chart/gauge_chart/gauge_chart_renderer.dart';
3+
import 'package:flutter/widgets.dart';
4+
5+
class GaugeChart extends ImplicitlyAnimatedWidget {
6+
/// [data] determines how the [GaugeChart] should be look like,
7+
/// when you make any change in the [GaugeChartData], it updates
8+
/// new values with animation, and duration is [swapAnimationDuration].
9+
/// also you can change the [swapAnimationCurve]
10+
/// which default is [Curves.linear].
11+
const GaugeChart(
12+
this.data, {
13+
this.chartRendererKey,
14+
super.key,
15+
super.duration = const Duration(milliseconds: 150),
16+
super.curve = Curves.linear,
17+
});
18+
19+
/// Determines how the [GaugeChartData] should be look like.
20+
final GaugeChartData data;
21+
22+
/// We pass this key to our renderers which are supposed to
23+
/// render the chart itself (without anything around the chart).
24+
final Key? chartRendererKey;
25+
26+
/// Creates a [_GaugeChartState]
27+
@override
28+
_GaugeChartState createState() => _GaugeChartState();
29+
}
30+
31+
class _GaugeChartState extends AnimatedWidgetBaseState<GaugeChart> {
32+
/// we handle under the hood animations (implicit animations) via this tween,
33+
/// it lerps between the old [GaugeChartData] to the new one.
34+
GaugeChartDataTween? _gaugeChartDataTween;
35+
36+
@override
37+
Widget build(BuildContext context) {
38+
final showingData = _getData();
39+
40+
return GaugeChartLeaf(
41+
data: _gaugeChartDataTween!.evaluate(animation),
42+
targetData: showingData,
43+
);
44+
}
45+
46+
@override
47+
void forEachTween(TweenVisitor<dynamic> visitor) {
48+
_gaugeChartDataTween = visitor(
49+
_gaugeChartDataTween,
50+
widget.data,
51+
(dynamic value) =>
52+
GaugeChartDataTween(begin: value as GaugeChartData, end: widget.data),
53+
) as GaugeChartDataTween?;
54+
}
55+
56+
GaugeChartData _getData() {
57+
return widget.data;
58+
}
59+
}

0 commit comments

Comments
 (0)