Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gauge Chart #1424

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions example/assets/icons/ic_gauge_chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions example/lib/presentation/resources/app_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class AppAssets {
return 'assets/icons/ic_scatter_chart.svg';
case ChartType.radar:
return 'assets/icons/ic_radar_chart.svg';
case ChartType.gauge:
return 'assets/icons/ic_gauge_chart.svg';
}
}

Expand Down
6 changes: 6 additions & 0 deletions example/lib/presentation/samples/chart_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ class RadarChartSample extends ChartSample {
@override
ChartType get type => ChartType.radar;
}

class GaugeChartSample extends ChartSample {
GaugeChartSample(super.number, super.builder);
@override
ChartType get type => ChartType.gauge;
}
4 changes: 4 additions & 0 deletions example/lib/presentation/samples/chart_samples.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fl_chart_app/presentation/samples/gauge/gauge_chart_sample1.dart';
import 'package:fl_chart_app/util/app_helper.dart';

import 'bar/bar_chart_sample1.dart';
Expand Down Expand Up @@ -62,5 +63,8 @@ class ChartSamples {
ChartType.radar: [
RadarChartSample(1, (context) => RadarChartSample1()),
],
ChartType.gauge: [
GaugeChartSample(1, (context) => const GaugeChartSample1()),
],
};
}
63 changes: 63 additions & 0 deletions example/lib/presentation/samples/gauge/gauge_chart_sample1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

class GaugeChartSample1 extends StatefulWidget {
const GaugeChartSample1({super.key});

@override
State<StatefulWidget> createState() => GaugeChartSample1State();
}

class GaugeChartSample1State extends State {
double _value = 0.7;
bool _isSelected = false;

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(28),
child: Column(
children: [
SizedBox(
width: 250,
height: 250,
child: GaugeChart(
GaugeChartData(
value: _value,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have a list of values (each value can have startAngle and endAngle).
Then we can draw them on top of each other.
This way, it is more customizable.

Look at these samples:

image image image

valueColor: VariableGaugeColor(
colors: [
AppColors.contentColorYellow,
AppColors.contentColorBlue,
AppColors.contentColorRed
],
limits: [0.35, 0.5],
),
backgroundColor: AppColors.contentColorPurple
.withOpacity(_isSelected ? 0.2 : 1),
strokeWidth: 30,
startAngle: 45,
endAngle: -225,
strokeCap: StrokeCap.round,
ticks: const GaugeTicks(
count: 11,
color: AppColors.contentColorCyan,
radius: 5,
position: GaugeTickPosition.inner,
margin: 5,
),
touchData: GaugeTouchData(
enabled: true,
touchCallback: (_, value) => setState(() {
_isSelected = value?.spot != null;
}),
),
),
),
),
Slider(value: _value, onChanged: (v) => setState(() => _value = v)),
],
),
);
}
}
3 changes: 2 additions & 1 deletion example/lib/util/app_helper.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:fl_chart_app/presentation/resources/app_resources.dart';
import 'package:fl_chart_app/urls.dart';

enum ChartType { line, bar, pie, scatter, radar }
enum ChartType { line, bar, pie, scatter, radar, gauge }

extension ChartTypeExtension on ChartType {
String get displayName => '$simpleName Chart';
Expand All @@ -12,6 +12,7 @@ extension ChartTypeExtension on ChartType {
ChartType.pie => 'Pie',
ChartType.scatter => 'Scatter',
ChartType.radar => 'Radar',
ChartType.gauge => 'Gauge',
};

String get documentationUrl => Urls.getChartDocumentationUrl(this);
Expand Down
2 changes: 2 additions & 0 deletions lib/fl_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export 'src/chart/base/axis_chart/axis_chart_data.dart';
export 'src/chart/base/axis_chart/axis_chart_widgets.dart';
export 'src/chart/base/base_chart/base_chart_data.dart';
export 'src/chart/base/base_chart/fl_touch_event.dart';
export 'src/chart/gauge_chart/gauge_chart.dart';
export 'src/chart/gauge_chart/gauge_chart_data.dart';
export 'src/chart/line_chart/line_chart.dart';
export 'src/chart/line_chart/line_chart_data.dart';
export 'src/chart/pie_chart/pie_chart.dart';
Expand Down
62 changes: 62 additions & 0 deletions lib/src/chart/gauge_chart/gauge_chart.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:fl_chart/src/chart/gauge_chart/gauge_chart_data.dart';
import 'package:fl_chart/src/chart/gauge_chart/gauge_chart_renderer.dart';
import 'package:flutter/widgets.dart';

class GaugeChart extends ImplicitlyAnimatedWidget {
/// [data] determines how the [GaugeChart] should be look like,
/// when you make any change in the [GaugeChartData], it updates
/// new values with animation, and duration is [swapAnimationDuration].
/// also you can change the [swapAnimationCurve]
/// which default is [Curves.linear].
const GaugeChart(

Check warning on line 11 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L11

Added line #L11 was not covered by tests
this.data, {
this.chartRendererKey,
super.key,
Duration swapAnimationDuration = const Duration(milliseconds: 150),
Curve swapAnimationCurve = Curves.linear,
}) : super(

Check warning on line 17 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L17

Added line #L17 was not covered by tests
duration: swapAnimationDuration,
curve: swapAnimationCurve,
);

/// Determines how the [GaugeChartData] should be look like.
final GaugeChartData data;

/// We pass this key to our renderers which are supposed to
/// render the chart itself (without anything around the chart).
final Key? chartRendererKey;

/// Creates a [_GaugeChartState]
@override
_GaugeChartState createState() => _GaugeChartState();

Check warning on line 31 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L30-L31

Added lines #L30 - L31 were not covered by tests
}

class _GaugeChartState extends AnimatedWidgetBaseState<GaugeChart> {
/// we handle under the hood animations (implicit animations) via this tween,
/// it lerps between the old [GaugeChartData] to the new one.
GaugeChartDataTween? _gaugeChartDataTween;

@override

Check warning on line 39 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L39

Added line #L39 was not covered by tests
Widget build(BuildContext context) {
final showingData = _getData();

Check warning on line 41 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L41

Added line #L41 was not covered by tests

return GaugeChartLeaf(
data: _gaugeChartDataTween!.evaluate(animation),

Check warning on line 44 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L43-L44

Added lines #L43 - L44 were not covered by tests
targetData: showingData,
);
}

GaugeChartData _getData() {
return widget.data;

Check warning on line 50 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L49-L50

Added lines #L49 - L50 were not covered by tests
}

@override

Check warning on line 53 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L53

Added line #L53 was not covered by tests
void forEachTween(TweenVisitor<dynamic> visitor) {
_gaugeChartDataTween = visitor(
_gaugeChartDataTween,
widget.data,
(dynamic value) =>
GaugeChartDataTween(begin: value as GaugeChartData, end: widget.data),

Check warning on line 59 in lib/src/chart/gauge_chart/gauge_chart.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/gauge_chart/gauge_chart.dart#L55-L59

Added lines #L55 - L59 were not covered by tests
) as GaugeChartDataTween?;
}
}
Loading