general update
This commit is contained in:
1 parent
6755e96f06
commit
43a1d9b47d
13 files changed
+158
-191
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart/src/circular_chart.dart';
|
||||
import 'package:flutter_circular_chart/src/entry.dart';
|
||||
import 'package:flutter_circular_chart/src/painter.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);
|
||||
@@ -113,17 +113,16 @@ class AnimatedCircularChart extends StatefulWidget {
|
||||
/// ```dart
|
||||
/// AnimatedCircularChartState animatedCircularChart = AnimatedCircularChart.of(context);
|
||||
/// ```
|
||||
static AnimatedCircularChartState of(BuildContext context,
|
||||
{bool nullOk: false}) {
|
||||
static AnimatedCircularChartState of(BuildContext context, {bool nullOk: false}) {
|
||||
assert(context != null);
|
||||
assert(nullOk != null);
|
||||
|
||||
final AnimatedCircularChartState result = context
|
||||
.ancestorStateOfType(const TypeMatcher<AnimatedCircularChartState>());
|
||||
final AnimatedCircularChartState result =
|
||||
context.findAncestorStateOfType<AnimatedCircularChartState>();
|
||||
|
||||
if (nullOk || result != null) return result;
|
||||
|
||||
throw new FlutterError(
|
||||
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 '
|
||||
@@ -133,7 +132,7 @@ class AnimatedCircularChart extends StatefulWidget {
|
||||
}
|
||||
|
||||
@override
|
||||
AnimatedCircularChartState createState() => new AnimatedCircularChartState();
|
||||
AnimatedCircularChartState createState() => AnimatedCircularChartState();
|
||||
}
|
||||
|
||||
/// The state for a circular chart that animates when its data is updated.
|
||||
@@ -144,9 +143,9 @@ class AnimatedCircularChart extends StatefulWidget {
|
||||
/// can refer to the [AnimatedCircularChart]'s state with a global key:
|
||||
///
|
||||
/// ```dart
|
||||
/// GlobalKey<AnimatedCircularChartState> chartKey = new GlobalKey<AnimatedCircularChartState>();
|
||||
/// GlobalKey<AnimatedCircularChartState> chartKey = GlobalKey<AnimatedCircularChartState>();
|
||||
/// ...
|
||||
/// new AnimatedCircularChart(key: chartKey, ...);
|
||||
/// AnimatedCircularChart(key: chartKey, ...);
|
||||
/// ...
|
||||
/// chartKey.currentState.updateData(newData);
|
||||
/// ```
|
||||
@@ -156,21 +155,21 @@ class AnimatedCircularChartState extends State<AnimatedCircularChart>
|
||||
AnimationController _animation;
|
||||
final Map<String, int> _stackRanks = <String, int>{};
|
||||
final Map<String, int> _entryRanks = <String, int>{};
|
||||
final TextPainter _labelPainter = new TextPainter();
|
||||
final TextPainter _labelPainter = TextPainter();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_animation = new AnimationController(
|
||||
_animation = AnimationController(
|
||||
duration: widget.duration,
|
||||
vsync: this,
|
||||
);
|
||||
|
||||
_assignRanks(widget.initialChartData);
|
||||
|
||||
_tween = new CircularChartTween(
|
||||
new CircularChart.empty(chartType: widget.chartType),
|
||||
new CircularChart.fromData(
|
||||
_tween = CircularChartTween(
|
||||
CircularChart.empty(chartType: widget.chartType),
|
||||
CircularChart.fromData(
|
||||
size: widget.size,
|
||||
data: widget.initialChartData,
|
||||
chartType: widget.chartType,
|
||||
@@ -188,8 +187,7 @@ class AnimatedCircularChartState extends State<AnimatedCircularChart>
|
||||
@override
|
||||
void didUpdateWidget(AnimatedCircularChart oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.holeLabel != widget.holeLabel ||
|
||||
oldWidget.labelStyle != widget.labelStyle) {
|
||||
if (oldWidget.holeLabel != widget.holeLabel || oldWidget.labelStyle != widget.labelStyle) {
|
||||
_updateLabelPainter();
|
||||
}
|
||||
}
|
||||
@@ -217,10 +215,9 @@ class AnimatedCircularChartState extends State<AnimatedCircularChart>
|
||||
|
||||
void _updateLabelPainter() {
|
||||
if (widget.holeLabel != null) {
|
||||
TextStyle _labelStyle =
|
||||
widget.labelStyle ?? Theme.of(context).textTheme.body2;
|
||||
TextStyle _labelStyle = widget.labelStyle ?? Theme.of(context).textTheme.bodyText1;
|
||||
_labelPainter
|
||||
..text = new TextSpan(style: _labelStyle, text: widget.holeLabel)
|
||||
..text = TextSpan(style: _labelStyle, text: widget.holeLabel)
|
||||
..textDirection = Directionality.of(context)
|
||||
..textScaleFactor = MediaQuery.of(context).textScaleFactor
|
||||
..layout();
|
||||
@@ -235,9 +232,9 @@ class AnimatedCircularChartState extends State<AnimatedCircularChart>
|
||||
_assignRanks(data);
|
||||
|
||||
setState(() {
|
||||
_tween = new CircularChartTween(
|
||||
_tween = CircularChartTween(
|
||||
_tween.evaluate(_animation),
|
||||
new CircularChart.fromData(
|
||||
CircularChart.fromData(
|
||||
size: widget.size,
|
||||
data: data,
|
||||
chartType: widget.chartType,
|
||||
@@ -255,9 +252,9 @@ class AnimatedCircularChartState extends State<AnimatedCircularChart>
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new CustomPaint(
|
||||
return CustomPaint(
|
||||
size: widget.size,
|
||||
painter: new AnimatedCircularChartPainter(
|
||||
painter: AnimatedCircularChartPainter(
|
||||
_tween.animate(_animation),
|
||||
widget.holeLabel != null ? _labelPainter : null,
|
||||
),
|
||||
|
||||
+19
-21
@@ -1,10 +1,10 @@
|
||||
import 'package:flutter/animation.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart/src/animated_circular_chart.dart';
|
||||
import 'package:flutter_circular_chart/src/entry.dart';
|
||||
import 'package:flutter_circular_chart/src/stack.dart';
|
||||
import 'package:flutter_circular_chart/src/tween.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';
|
||||
|
||||
class CircularChart {
|
||||
static const double _kStackWidthFraction = 0.75;
|
||||
@@ -20,7 +20,7 @@ class CircularChart {
|
||||
final SegmentEdgeStyle edgeStyle;
|
||||
|
||||
factory CircularChart.empty({@required CircularChartType chartType}) {
|
||||
return new CircularChart(<CircularChartStack>[], chartType);
|
||||
return CircularChart(<CircularChartStack>[], chartType);
|
||||
}
|
||||
|
||||
factory CircularChart.fromData({
|
||||
@@ -35,38 +35,36 @@ class CircularChart {
|
||||
SegmentEdgeStyle edgeStyle,
|
||||
}) {
|
||||
final double _holeRadius = holeRadius ?? size.width / (2 + data.length);
|
||||
final double stackDistance =
|
||||
(size.width / 2 - _holeRadius) / (2 + data.length);
|
||||
final double stackDistance = (size.width / 2 - _holeRadius) / (2 + data.length);
|
||||
final double stackWidth = stackDistance * _kStackWidthFraction;
|
||||
final double startRadius = stackDistance + _holeRadius;
|
||||
|
||||
List<CircularChartStack> stacks = new List<CircularChartStack>.generate(
|
||||
List<CircularChartStack> stacks = List<CircularChartStack>.generate(
|
||||
data.length,
|
||||
(i) => new CircularChartStack.fromData(
|
||||
stackRanks[data[i].rankKey] ?? i,
|
||||
data[i].entries,
|
||||
entryRanks,
|
||||
percentageValues,
|
||||
startRadius + i * stackDistance,
|
||||
stackWidth,
|
||||
startAngle,
|
||||
),
|
||||
(i) => CircularChartStack.fromData(
|
||||
stackRanks[data[i].rankKey] ?? i,
|
||||
data[i].entries,
|
||||
entryRanks,
|
||||
percentageValues,
|
||||
startRadius + i * stackDistance,
|
||||
stackWidth,
|
||||
startAngle,
|
||||
),
|
||||
);
|
||||
|
||||
return new CircularChart(stacks, chartType, edgeStyle: edgeStyle);
|
||||
return CircularChart(stacks, chartType, edgeStyle: edgeStyle);
|
||||
}
|
||||
}
|
||||
|
||||
class CircularChartTween extends Tween<CircularChart> {
|
||||
CircularChartTween(CircularChart begin, CircularChart end)
|
||||
: _stacksTween =
|
||||
new MergeTween<CircularChartStack>(begin.stacks, end.stacks),
|
||||
: _stacksTween = MergeTween<CircularChartStack>(begin.stacks, end.stacks),
|
||||
super(begin: begin, end: end);
|
||||
|
||||
final MergeTween<CircularChartStack> _stacksTween;
|
||||
|
||||
@override
|
||||
CircularChart lerp(double t) => new CircularChart(
|
||||
CircularChart lerp(double t) => CircularChart(
|
||||
_stacksTween.lerp(t),
|
||||
begin.chartType,
|
||||
edgeStyle: end.edgeStyle,
|
||||
|
||||
+11
-15
@@ -1,13 +1,12 @@
|
||||
import 'dart:math' as Math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart/src/animated_circular_chart.dart';
|
||||
import 'package:flutter_circular_chart/src/circular_chart.dart';
|
||||
import 'package:flutter_circular_chart/src/stack.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';
|
||||
|
||||
class AnimatedCircularChartPainter extends CustomPainter {
|
||||
AnimatedCircularChartPainter(this.animation, this.labelPainter)
|
||||
: super(repaint: animation);
|
||||
AnimatedCircularChartPainter(this.animation, this.labelPainter) : super(repaint: animation);
|
||||
|
||||
final Animation<CircularChart> animation;
|
||||
final TextPainter labelPainter;
|
||||
@@ -44,7 +43,7 @@ void _paintLabel(Canvas canvas, Size size, TextPainter labelPainter) {
|
||||
if (labelPainter != null) {
|
||||
labelPainter.paint(
|
||||
canvas,
|
||||
new Offset(
|
||||
Offset(
|
||||
size.width / 2 - labelPainter.width / 2,
|
||||
size.height / 2 - labelPainter.height / 2,
|
||||
),
|
||||
@@ -53,13 +52,10 @@ void _paintLabel(Canvas canvas, Size size, TextPainter labelPainter) {
|
||||
}
|
||||
|
||||
void _paintChart(Canvas canvas, Size size, CircularChart chart) {
|
||||
final Paint segmentPaint = new Paint()
|
||||
..style = chart.chartType == CircularChartType.Radial
|
||||
? PaintingStyle.stroke
|
||||
: PaintingStyle.fill
|
||||
..strokeCap = chart.edgeStyle == SegmentEdgeStyle.round
|
||||
? StrokeCap.round
|
||||
: StrokeCap.butt;
|
||||
final Paint segmentPaint = Paint()
|
||||
..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) {
|
||||
for (final segment in stack.segments) {
|
||||
@@ -67,8 +63,8 @@ void _paintChart(Canvas canvas, Size size, CircularChart chart) {
|
||||
segmentPaint.strokeWidth = stack.width;
|
||||
|
||||
canvas.drawArc(
|
||||
new Rect.fromCircle(
|
||||
center: new Offset(size.width / 2, size.height / 2),
|
||||
Rect.fromCircle(
|
||||
center: Offset(size.width / 2, size.height / 2),
|
||||
radius: stack.radius,
|
||||
),
|
||||
stack.startAngle * _kRadiansPerDegree,
|
||||
|
||||
+7
-10
@@ -1,7 +1,7 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart/src/tween.dart';
|
||||
import 'package:flutter_circular_chart_two/src/tween.dart';
|
||||
|
||||
class CircularChartSegment extends MergeTweenable<CircularChartSegment> {
|
||||
CircularChartSegment(this.rank, this.sweepAngle, this.color);
|
||||
@@ -11,20 +11,19 @@ class CircularChartSegment extends MergeTweenable<CircularChartSegment> {
|
||||
final Color color;
|
||||
|
||||
@override
|
||||
CircularChartSegment get empty => new CircularChartSegment(rank, 0.0, color);
|
||||
CircularChartSegment get empty => CircularChartSegment(rank, 0.0, color);
|
||||
|
||||
@override
|
||||
bool operator <(CircularChartSegment other) => rank < other.rank;
|
||||
|
||||
@override
|
||||
Tween<CircularChartSegment> tweenTo(CircularChartSegment other) =>
|
||||
new CircularChartSegmentTween(this, other);
|
||||
CircularChartSegmentTween(this, other);
|
||||
|
||||
static CircularChartSegment lerp(
|
||||
CircularChartSegment begin, CircularChartSegment end, double t) {
|
||||
static CircularChartSegment lerp(CircularChartSegment begin, CircularChartSegment end, double t) {
|
||||
assert(begin.rank == end.rank);
|
||||
|
||||
return new CircularChartSegment(
|
||||
return CircularChartSegment(
|
||||
begin.rank,
|
||||
lerpDouble(begin.sweepAngle, end.sweepAngle, t),
|
||||
Color.lerp(begin.color, end.color, t),
|
||||
@@ -33,13 +32,11 @@ class CircularChartSegment extends MergeTweenable<CircularChartSegment> {
|
||||
}
|
||||
|
||||
class CircularChartSegmentTween extends Tween<CircularChartSegment> {
|
||||
CircularChartSegmentTween(
|
||||
CircularChartSegment begin, CircularChartSegment end)
|
||||
CircularChartSegmentTween(CircularChartSegment begin, CircularChartSegment end)
|
||||
: super(begin: begin, end: end) {
|
||||
assert(begin.rank == end.rank);
|
||||
}
|
||||
|
||||
@override
|
||||
CircularChartSegment lerp(double t) =>
|
||||
CircularChartSegment.lerp(begin, end, t);
|
||||
CircularChartSegment lerp(double t) => CircularChartSegment.lerp(begin, end, t);
|
||||
}
|
||||
+12
-15
@@ -1,9 +1,9 @@
|
||||
import 'dart:ui' show lerpDouble;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart/src/entry.dart';
|
||||
import 'package:flutter_circular_chart/src/segment.dart';
|
||||
import 'package:flutter_circular_chart/src/tween.dart';
|
||||
import 'package:flutter_circular_chart_two/src/entry.dart';
|
||||
import 'package:flutter_circular_chart_two/src/segment.dart';
|
||||
import 'package:flutter_circular_chart_two/src/tween.dart';
|
||||
|
||||
const double _kMaxAngle = 360.0;
|
||||
|
||||
@@ -39,16 +39,14 @@ class CircularChartStack implements MergeTweenable<CircularChartStack> {
|
||||
);
|
||||
|
||||
double previousSweepAngle = 0.0;
|
||||
List<CircularChartSegment> segments =
|
||||
new List<CircularChartSegment>.generate(entries.length, (i) {
|
||||
double sweepAngle =
|
||||
(entries[i].value / valueSum * _kMaxAngle) + previousSweepAngle;
|
||||
List<CircularChartSegment> segments = List<CircularChartSegment>.generate(entries.length, (i) {
|
||||
double sweepAngle = (entries[i].value / valueSum * _kMaxAngle) + previousSweepAngle;
|
||||
previousSweepAngle = sweepAngle;
|
||||
int rank = entryRanks[entries[i].rankKey] ?? i;
|
||||
return new CircularChartSegment(rank, sweepAngle, entries[i].color);
|
||||
return CircularChartSegment(rank, sweepAngle, entries[i].color);
|
||||
});
|
||||
|
||||
return new CircularChartStack(
|
||||
return CircularChartStack(
|
||||
stackRank,
|
||||
startRadius,
|
||||
stackWidth,
|
||||
@@ -58,21 +56,20 @@ class CircularChartStack implements MergeTweenable<CircularChartStack> {
|
||||
}
|
||||
|
||||
@override
|
||||
CircularChartStack get empty => new 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) =>
|
||||
new CircularChartStackTween(this, other);
|
||||
CircularChartStackTween(this, other);
|
||||
}
|
||||
|
||||
class CircularChartStackTween extends Tween<CircularChartStack> {
|
||||
CircularChartStackTween(CircularChartStack begin, CircularChartStack end)
|
||||
: _circularSegmentsTween =
|
||||
new MergeTween<CircularChartSegment>(begin.segments, end.segments),
|
||||
: _circularSegmentsTween = MergeTween<CircularChartSegment>(begin.segments, end.segments),
|
||||
super(begin: begin, end: end) {
|
||||
assert(begin.rank == end.rank);
|
||||
}
|
||||
@@ -80,7 +77,7 @@ class CircularChartStackTween extends Tween<CircularChartStack> {
|
||||
final MergeTween<CircularChartSegment> _circularSegmentsTween;
|
||||
|
||||
@override
|
||||
CircularChartStack lerp(double t) => new CircularChartStack(
|
||||
CircularChartStack lerp(double t) => CircularChartStack(
|
||||
begin.rank,
|
||||
lerpDouble(begin.radius, end.radius, t),
|
||||
lerpDouble(begin.width, end.width, t),
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ class MergeTween<T extends MergeTweenable<T>> extends Tween<List<T>> {
|
||||
final _tweens = <Tween<T>>[];
|
||||
|
||||
@override
|
||||
List<T> lerp(double t) => new List.generate(
|
||||
List<T> lerp(double t) => List.generate(
|
||||
_tweens.length,
|
||||
(i) => _tweens[i].lerp(t),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user