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

Add Touchable Circular Chart #2

Open
wants to merge 1 commit into
base: master
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
4 changes: 3 additions & 1 deletion lib/flutter_circular_chart_two.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library flutter_circular_chart_two;

export 'src/circular_chart.dart';
export 'src/animated_circular_chart.dart';
export 'src/circular_chart.dart';
export 'src/consts.dart';
export 'src/entry.dart';
export 'src/touchable_circular_chart.dart';
33 changes: 6 additions & 27 deletions lib/src/animated_circular_chart.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_circular_chart_two/src/circular_chart.dart';
import 'package:flutter_circular_chart_two/src/entry.dart';
import 'package:flutter_circular_chart_two/src/painter.dart';

// The default chart tween animation duration.
const Duration _kDuration = const Duration(milliseconds: 300);
// The default angle the chart is oriented at.
const double _kStartAngle = -90.0;

enum CircularChartType {
Pie,
Radial,
}

/// Determines how the ends of a chart's segments should be drawn.
enum SegmentEdgeStyle {
/// Segments begin and end with a flat edge.
flat,

/// Segments begin and end with a semi-circle.
round,
}
import 'consts.dart';

class AnimatedCircularChart extends StatefulWidget {
AnimatedCircularChart({
Key key,
@required this.size,
@required this.initialChartData,
this.chartType = CircularChartType.Radial,
this.duration = _kDuration,
this.duration = kDuration,
this.percentageValues = false,
this.holeRadius,
this.startAngle = _kStartAngle,
this.startAngle = kStartAngle,
this.holeLabel,
this.labelStyle,
this.edgeStyle = SegmentEdgeStyle.flat,
Expand Down Expand Up @@ -117,13 +99,11 @@ class AnimatedCircularChart extends StatefulWidget {
assert(context != null);
assert(nullOk != null);

final AnimatedCircularChartState result =
context.findAncestorStateOfType<AnimatedCircularChartState>();
final AnimatedCircularChartState result = context.findAncestorStateOfType<AnimatedCircularChartState>();

if (nullOk || result != null) return result;

throw FlutterError(
'AnimatedCircularChart.of() called with a context that does not contain a AnimatedCircularChart.\n'
throw FlutterError('AnimatedCircularChart.of() called with a context that does not contain a AnimatedCircularChart.\n'
'No AnimatedCircularChart ancestor could be found starting from the context that was passed to AnimatedCircularChart.of(). '
'This can happen when the context provided is from the same StatefulWidget that '
'built the AnimatedCircularChart.\n'
Expand All @@ -149,8 +129,7 @@ class AnimatedCircularChart extends StatefulWidget {
/// ...
/// chartKey.currentState.updateData(newData);
/// ```
class AnimatedCircularChartState extends State<AnimatedCircularChart>
with TickerProviderStateMixin {
class AnimatedCircularChartState extends State<AnimatedCircularChart> with TickerProviderStateMixin {
CircularChartTween _tween;
AnimationController _animation;
final Map<String, int> _stackRanks = <String, int>{};
Expand Down
5 changes: 2 additions & 3 deletions lib/src/circular_chart.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:flutter/animation.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_circular_chart_two/src/animated_circular_chart.dart';
import 'package:flutter_circular_chart_two/src/entry.dart';
import 'package:flutter_circular_chart_two/src/stack.dart';
import 'package:flutter_circular_chart_two/src/tween.dart';

import 'consts.dart';

class CircularChart {
static const double _kStackWidthFraction = 0.75;

Expand Down
18 changes: 18 additions & 0 deletions lib/src/consts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The default chart tween animation duration.
const Duration kDuration = const Duration(milliseconds: 300);
// The default angle the chart is oriented at.
const double kStartAngle = -90.0;

enum CircularChartType {
Pie,
Radial,
}

/// Determines how the ends of a chart's segments should be drawn.
enum SegmentEdgeStyle {
/// Segments begin and end with a flat edge.
flat,

/// Segments begin and end with a semi-circle.
round,
}
10 changes: 9 additions & 1 deletion lib/src/entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import 'dart:ui';
/// [value], what proportion of a stack this corresponds to depends on the [percentageValues]
/// property of the chart.
class CircularSegmentEntry {
const CircularSegmentEntry(this.value, this.color, {this.rankKey});
const CircularSegmentEntry(
this.value,
this.color, {
this.rankKey,
this.id,
});

/// The value of this data point, defines the sweep angle of the arc
/// that is drawn. If the chart being drawn has [percentageValues] set to false
Expand All @@ -25,6 +30,9 @@ class CircularSegmentEntry {
/// transitioning between data points.
final String rankKey;

///Segment Identifier
final String id;

String toString() {
return '$rankKey: $value $color';
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/painter.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:math' as Math;

import 'package:flutter/material.dart';
import 'package:flutter_circular_chart_two/src/animated_circular_chart.dart';
import 'package:flutter_circular_chart_two/src/circular_chart.dart';
import 'package:flutter_circular_chart_two/src/stack.dart';

import 'consts.dart';

class AnimatedCircularChartPainter extends CustomPainter {
AnimatedCircularChartPainter(this.animation, this.labelPainter) : super(repaint: animation);

Expand Down Expand Up @@ -53,8 +54,7 @@ void _paintLabel(Canvas canvas, Size size, TextPainter labelPainter) {

void _paintChart(Canvas canvas, Size size, CircularChart chart) {
final Paint segmentPaint = Paint()
..style =
chart.chartType == CircularChartType.Radial ? PaintingStyle.stroke : PaintingStyle.fill
..style = chart.chartType == CircularChartType.Radial ? PaintingStyle.stroke : PaintingStyle.fill
..strokeCap = chart.edgeStyle == SegmentEdgeStyle.round ? StrokeCap.round : StrokeCap.butt;

for (final CircularChartStack stack in chart.stacks) {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/segment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_circular_chart_two/src/tween.dart';

class CircularChartSegment extends MergeTweenable<CircularChartSegment> {
CircularChartSegment(this.rank, this.sweepAngle, this.color);
CircularChartSegment(this.rank, this.sweepAngle, this.color, {this.id});

final int rank;
final double sweepAngle;
final Color color;
final String id;

@override
CircularChartSegment get empty => CircularChartSegment(rank, 0.0, color);
Expand All @@ -17,23 +18,22 @@ class CircularChartSegment extends MergeTweenable<CircularChartSegment> {
bool operator <(CircularChartSegment other) => rank < other.rank;

@override
Tween<CircularChartSegment> tweenTo(CircularChartSegment other) =>
CircularChartSegmentTween(this, other);
Tween<CircularChartSegment> tweenTo(CircularChartSegment other) => CircularChartSegmentTween(this, other);

static CircularChartSegment lerp(CircularChartSegment begin, CircularChartSegment end, double t) {
static CircularChartSegment lerp(CircularChartSegment begin, CircularChartSegment end, double t, {String id}) {
assert(begin.rank == end.rank);

return CircularChartSegment(
begin.rank,
lerpDouble(begin.sweepAngle, end.sweepAngle, t),
Color.lerp(begin.color, end.color, t),
id: id,
);
}
}

class CircularChartSegmentTween extends Tween<CircularChartSegment> {
CircularChartSegmentTween(CircularChartSegment begin, CircularChartSegment end)
: super(begin: begin, end: end) {
CircularChartSegmentTween(CircularChartSegment begin, CircularChartSegment end) : super(begin: begin, end: end) {
assert(begin.rank == end.rank);
}

Expand Down
20 changes: 12 additions & 8 deletions lib/src/stack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ class CircularChartStack implements MergeTweenable<CircularChartStack> {
double sweepAngle = (entries[i].value / valueSum * _kMaxAngle) + previousSweepAngle;
previousSweepAngle = sweepAngle;
int rank = entryRanks[entries[i].rankKey] ?? i;
return CircularChartSegment(rank, sweepAngle, entries[i].color);
return CircularChartSegment(
rank,
sweepAngle,
entries[i].color,
id: entries[i].id,
);
});

return CircularChartStack(
Expand All @@ -56,24 +61,23 @@ class CircularChartStack implements MergeTweenable<CircularChartStack> {
}

@override
CircularChartStack get empty =>
CircularChartStack(rank, radius, 0.0, startAngle, <CircularChartSegment>[]);
CircularChartStack get empty => CircularChartStack(rank, radius, 0.0, startAngle, <CircularChartSegment>[]);

@override
bool operator <(CircularChartStack other) => rank < other.rank;

@override
Tween<CircularChartStack> tweenTo(CircularChartStack other) =>
CircularChartStackTween(this, other);
Tween<CircularChartStack> tweenTo(CircularChartStack other) => CircularChartStackTween(this, other);
}

class CircularChartStackTween extends Tween<CircularChartStack> {
CircularChartStackTween(CircularChartStack begin, CircularChartStack end)
: _circularSegmentsTween = MergeTween<CircularChartSegment>(begin.segments, end.segments),
CircularChartStackTween(
CircularChartStack begin,
CircularChartStack end,
) : _circularSegmentsTween = MergeTween<CircularChartSegment>(begin.segments, end.segments),
super(begin: begin, end: end) {
assert(begin.rank == end.rank);
}

final MergeTween<CircularChartSegment> _circularSegmentsTween;

@override
Expand Down
Loading