Add randomly generated radials example

This commit is contained in:
Victor Choueiri 2017-11-03 13:45:27 +02:00
parent f4084f9d1d
commit 96f3fd172d
3 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import 'package:flutter_circular_chart/flutter_circular_chart.dart';
import 'dart:math' as Math;
import 'color_palette.dart';
void main() {
runApp(new MaterialApp(
home: new RandomizedRadialChartExample(),
));
}
class RandomizedRadialChartExample extends StatefulWidget {
@override
_RandomizedRadialChartExampleState createState() =>
new _RandomizedRadialChartExampleState();
}
class _RandomizedRadialChartExampleState
extends State<RandomizedRadialChartExample> {
final GlobalKey<AnimatedCircularChartState> _chartKey =
new GlobalKey<AnimatedCircularChartState>();
final _chartSize = const Size(300.0, 300.0);
final Math.Random random = new Math.Random();
List<CircularStackEntry> data;
@override
void initState() {
data = _generateRandomData();
}
double value = 50.0;
void _randomize() {
setState(() {
data = _generateRandomData();
_chartKey.currentState.updateData(data);
});
}
List<CircularStackEntry> _generateRandomData() {
int stackCount = random.nextInt(10);
List<CircularStackEntry> data = new List.generate(stackCount, (i) {
int segCount = random.nextInt(10);
List<CircularSegmentEntry> segments = new List.generate(segCount, (j) {
Color randomColor = ColorPalette.primary.random(random);
return new CircularSegmentEntry(random.nextDouble(), randomColor);
});
return new CircularStackEntry(segments);
});
return data;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Randomized radial data'),
),
body: new Center(
child: new AnimatedCircularChart(
key: _chartKey,
size: _chartSize,
initialChartData: data,
chartType: CircularChartType.Radial,
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _randomize,
child: const Icon(Icons.refresh),
),
);
}
}

View File

@ -0,0 +1,35 @@
import 'dart:math';
import 'package:flutter/material.dart';
class ColorPalette {
static final ColorPalette primary = new ColorPalette(<Color>[
Colors.blue[400],
Colors.blue[200],
Colors.red[400],
Colors.red[200],
Colors.green[400],
Colors.green[200],
Colors.yellow[400],
Colors.yellow[200],
Colors.purple[400],
Colors.purple[200],
Colors.orange[400],
Colors.orange[200],
Colors.teal[400],
Colors.teal[200],
Colors.black,
]);
ColorPalette(List<Color> colors) : _colors = colors {
assert(colors.isNotEmpty);
}
final List<Color> _colors;
Color operator [](int index) => _colors[index % length];
int get length => _colors.length;
Color random(Random random) => this[random.nextInt(length)];
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB