1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 14:28:51 +00:00

Add flutter_web samples (#75)

This commit is contained in:
Kevin Moore
2019-05-07 13:32:08 -07:00
committed by Andrew Brogdon
parent 42f2dce01b
commit 3fe927cb29
697 changed files with 241026 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Example of a line chart with pan and zoom enabled via
/// [Charts.PanAndZoomBehavior].
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_web/material.dart';
class ScatterPlotAnimationZoomChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
ScatterPlotAnimationZoomChart(this.seriesList, {this.animate});
/// Creates a [ScatterPlotChart] with sample data and no transition.
factory ScatterPlotAnimationZoomChart.withSampleData() {
return new ScatterPlotAnimationZoomChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory ScatterPlotAnimationZoomChart.withRandomData() {
return new ScatterPlotAnimationZoomChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<LinearSales, num>> _createRandomData() {
final random = new Random();
final data = <LinearSales>[];
final makeRadius = (int value) => (random.nextInt(value) + 2).toDouble();
for (var i = 0; i < 100; i++) {
data.add(new LinearSales(i, random.nextInt(100), makeRadius(4)));
}
final maxMeasure = 100;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (LinearSales sales, _) {
// Color bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: data,
)
];
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.ScatterPlotChart(seriesList,
animate: animate,
behaviors: [
new charts.PanAndZoomBehavior(),
]);
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(0, 5, 3.0),
new LinearSales(10, 25, 5.0),
new LinearSales(12, 75, 4.0),
new LinearSales(13, 225, 5.0),
new LinearSales(16, 50, 4.0),
new LinearSales(24, 75, 3.0),
new LinearSales(25, 100, 3.0),
new LinearSales(34, 150, 5.0),
new LinearSales(37, 10, 4.5),
new LinearSales(45, 300, 8.0),
new LinearSales(52, 15, 4.0),
new LinearSales(56, 200, 7.0),
];
final maxMeasure = 300;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (LinearSales sales, _) {
// Color bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: data,
)
];
}
}
/// Sample linear data type.
class LinearSales {
final int year;
final int sales;
final double radius;
LinearSales(this.year, this.sales, this.radius);
}

View File

@@ -0,0 +1,264 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Example of a scatter plot chart with a bucketing measure axis and a legend.
///
/// A bucketing measure axis positions all values beneath a certain threshold
/// into a reserved space on the axis range. The label for the bucket line will
/// be drawn in the middle of the bucket range, rather than aligned with the
/// gridline for that value's position on the scale.
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_web/material.dart';
class BucketingAxisScatterPlotChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
BucketingAxisScatterPlotChart(this.seriesList, {this.animate});
/// Creates a [ScatterPlotChart] with sample data and no transition.
factory BucketingAxisScatterPlotChart.withSampleData() {
return new BucketingAxisScatterPlotChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory BucketingAxisScatterPlotChart.withRandomData() {
return new BucketingAxisScatterPlotChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<LinearSales, num>> _createRandomData() {
final random = new Random();
final makeRadius = (int value) => (random.nextInt(value) + 6).toDouble();
// Make sure that the measure values for the first five series are well
// above the threshold. This simulates the grouping of the small values into
// the "Other" series.
final myFakeDesktopData = [
new LinearSales(
random.nextInt(100), (random.nextInt(50) + 50) / 100, makeRadius(6)),
];
final myFakeTabletData = [
new LinearSales(
random.nextInt(100), (random.nextInt(50) + 50) / 100, makeRadius(6)),
];
final myFakeMobileData = [
new LinearSales(
random.nextInt(100), (random.nextInt(50) + 50) / 100, makeRadius(6)),
];
final myFakeChromebookData = [
new LinearSales(
random.nextInt(100), (random.nextInt(50) + 50) / 100, makeRadius(6)),
];
final myFakeHomeData = [
new LinearSales(
random.nextInt(100), (random.nextInt(50) + 50) / 100, makeRadius(6)),
];
// Make sure that the "Other" series values are smaller.
final myFakeOtherData = [
new LinearSales(
random.nextInt(100), random.nextInt(50) / 100, makeRadius(6)),
new LinearSales(
random.nextInt(100), random.nextInt(50) / 100, makeRadius(6)),
new LinearSales(
random.nextInt(100), random.nextInt(50) / 100, makeRadius(6)),
new LinearSales(
random.nextInt(100), random.nextInt(50) / 100, makeRadius(6)),
new LinearSales(
random.nextInt(100), random.nextInt(50) / 100, makeRadius(6)),
new LinearSales(
random.nextInt(100), random.nextInt(50) / 100, makeRadius(6)),
];
return [
new charts.Series<LinearSales, int>(
id: 'Desktop',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.blue.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeDesktopData),
new charts.Series<LinearSales, int>(
id: 'Tablet',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.red.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeTabletData),
new charts.Series<LinearSales, int>(
id: 'Mobile',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.green.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeMobileData),
new charts.Series<LinearSales, int>(
id: 'Chromebook',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.purple.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeChromebookData),
new charts.Series<LinearSales, int>(
id: 'Home',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.indigo.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeHomeData),
new charts.Series<LinearSales, int>(
id: 'Other',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.gray.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeOtherData),
];
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.ScatterPlotChart(seriesList,
// Set up a bucketing axis that will place all values below 0.1 (10%)
// into a bucket at the bottom of the chart.
//
// Configure a tick count of 3 so that we get 100%, 50%, and the
// threshold.
primaryMeasureAxis: new charts.BucketingAxisSpec(
threshold: 0.1,
tickProviderSpec: new charts.BucketingNumericTickProviderSpec(
desiredTickCount: 3)),
// Add a series legend to display the series names.
behaviors: [
new charts.SeriesLegend(position: charts.BehaviorPosition.end),
],
animate: animate);
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final myFakeDesktopData = [
new LinearSales(52, 0.75, 14.0),
];
final myFakeTabletData = [
new LinearSales(45, 0.3, 18.0),
];
final myFakeMobileData = [
new LinearSales(56, 0.8, 17.0),
];
final myFakeChromebookData = [
new LinearSales(25, 0.6, 13.0),
];
final myFakeHomeData = [
new LinearSales(34, 0.5, 15.0),
];
final myFakeOtherData = [
new LinearSales(10, 0.25, 15.0),
new LinearSales(12, 0.075, 14.0),
new LinearSales(13, 0.225, 15.0),
new LinearSales(16, 0.03, 14.0),
new LinearSales(24, 0.04, 13.0),
new LinearSales(37, 0.1, 14.5),
];
return [
new charts.Series<LinearSales, int>(
id: 'Desktop',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.blue.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeDesktopData),
new charts.Series<LinearSales, int>(
id: 'Tablet',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.red.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeTabletData),
new charts.Series<LinearSales, int>(
id: 'Mobile',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.green.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeMobileData),
new charts.Series<LinearSales, int>(
id: 'Chromebook',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.purple.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeChromebookData),
new charts.Series<LinearSales, int>(
id: 'Home',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.indigo.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeHomeData),
new charts.Series<LinearSales, int>(
id: 'Other',
colorFn: (LinearSales sales, _) =>
charts.MaterialPalette.gray.shadeDefault,
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.revenueShare,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: myFakeOtherData),
];
}
}
/// Sample linear data type.
class LinearSales {
final int year;
final double revenueShare;
final double radius;
LinearSales(this.year, this.revenueShare, this.radius);
}

View File

@@ -0,0 +1,169 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Line chart example
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_web/material.dart';
class ComparisonPointsScatterPlotChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
ComparisonPointsScatterPlotChart(this.seriesList, {this.animate});
/// Creates a [ScatterPlotChart] with sample data and no transition.
factory ComparisonPointsScatterPlotChart.withSampleData() {
return new ComparisonPointsScatterPlotChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory ComparisonPointsScatterPlotChart.withRandomData() {
return new ComparisonPointsScatterPlotChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<LinearSales, num>> _createRandomData() {
final random = new Random();
final maxMeasure = 100;
final data = [
_makeRandomDatum(maxMeasure, random),
_makeRandomDatum(maxMeasure, random),
_makeRandomDatum(maxMeasure, random),
_makeRandomDatum(maxMeasure, random),
_makeRandomDatum(maxMeasure, random),
_makeRandomDatum(maxMeasure, random),
];
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (LinearSales sales, _) {
// Color bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
domainLowerBoundFn: (LinearSales sales, _) => sales.yearLower,
domainUpperBoundFn: (LinearSales sales, _) => sales.yearUpper,
measureFn: (LinearSales sales, _) => sales.sales,
measureLowerBoundFn: (LinearSales sales, _) => sales.salesLower,
measureUpperBoundFn: (LinearSales sales, _) => sales.salesUpper,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: data,
)
];
}
static LinearSales _makeRandomDatum(int max, Random random) {
final makeRadius = (int value) => (random.nextInt(value) + 6).toDouble();
final year = random.nextInt(max);
final yearLower = (year * 0.8).round();
final yearUpper = year;
final sales = random.nextInt(max);
final salesLower = (sales * 0.8).round();
final salesUpper = sales;
return new LinearSales(year, yearLower, yearUpper, sales, salesLower,
salesUpper, makeRadius(4));
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.ScatterPlotChart(seriesList,
animate: animate,
defaultRenderer:
new charts.PointRendererConfig(pointRendererDecorators: [
new charts.ComparisonPointsDecorator(
symbolRenderer: new charts.CylinderSymbolRenderer())
]));
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(10, 7, 10, 25, 20, 25, 5.0),
new LinearSales(13, 11, 13, 225, 205, 225, 5.0),
new LinearSales(34, 34, 24, 150, 150, 130, 5.0),
new LinearSales(37, 37, 57, 10, 10, 12, 6.5),
new LinearSales(45, 35, 45, 260, 300, 260, 8.0),
new LinearSales(56, 46, 56, 200, 170, 200, 7.0),
];
final maxMeasure = 300;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
// Providing a color function is optional.
colorFn: (LinearSales sales, _) {
// Bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
domainLowerBoundFn: (LinearSales sales, _) => sales.yearLower,
domainUpperBoundFn: (LinearSales sales, _) => sales.yearUpper,
measureFn: (LinearSales sales, _) => sales.sales,
measureLowerBoundFn: (LinearSales sales, _) => sales.salesLower,
measureUpperBoundFn: (LinearSales sales, _) => sales.salesUpper,
// Providing a radius function is optional.
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: data,
)
];
}
}
/// Sample linear data type.
class LinearSales {
final int year;
final int yearLower;
final int yearUpper;
final int sales;
final int salesLower;
final int salesUpper;
final double radius;
LinearSales(this.year, this.yearLower, this.yearUpper, this.sales,
this.salesLower, this.salesUpper, this.radius);
}

View File

@@ -0,0 +1,58 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'package:flutter_web/material.dart';
import '../gallery_scaffold.dart';
import 'animation_zoom.dart';
import 'bucketing_axis.dart';
import 'comparison_points.dart';
import 'shapes.dart';
import 'simple.dart';
List<GalleryScaffold> buildGallery() {
return [
new GalleryScaffold(
listTileIcon: new Icon(Icons.scatter_plot),
title: 'Simple Scatter Plot Chart',
subtitle: 'With a single series',
childBuilder: () => new SimpleScatterPlotChart.withRandomData(),
),
new GalleryScaffold(
listTileIcon: new Icon(Icons.scatter_plot),
title: 'Shapes Scatter Plot Chart',
subtitle: 'With custom shapes',
childBuilder: () => new ShapesScatterPlotChart.withRandomData(),
),
new GalleryScaffold(
listTileIcon: new Icon(Icons.scatter_plot),
title: 'Comparison Points Scatter Plot Chart',
subtitle: 'Scatter plot chart with comparison points',
childBuilder: () => new ComparisonPointsScatterPlotChart.withRandomData(),
),
new GalleryScaffold(
listTileIcon: new Icon(Icons.scatter_plot),
title: 'Pan and Zoom Scatter Plot Chart',
subtitle: 'Simple scatter plot chart pan and zoom behaviors enabled',
childBuilder: () => new ScatterPlotAnimationZoomChart.withRandomData(),
),
new GalleryScaffold(
listTileIcon: new Icon(Icons.scatter_plot),
title: 'Bucketing Axis Scatter Plot Chart',
subtitle: 'Scatter plot with a measure axis that buckets values less ' +
'than 10% into a single region below the draw area',
childBuilder: () => new BucketingAxisScatterPlotChart.withRandomData(),
),
];
}

View File

@@ -0,0 +1,206 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Example of a scatter plot chart using custom symbols for the points.
///
/// The series has been configured to draw each point as a square by default.
///
/// Some data will be drawn as a circle, indicated by defining a custom "circle"
/// value referenced by [pointSymbolRendererFnKey].
///
/// Some other data have will be drawn as a hollow circle. In addition to the
/// custom renderer key, these data also have stroke and fillColor values
/// defined. Configuring a separate fillColor will cause the center of the shape
/// to be filled in, with white in these examples. The border of the shape will
/// be color with the color of the data.
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_web/material.dart';
class ShapesScatterPlotChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
ShapesScatterPlotChart(this.seriesList, {this.animate});
/// Creates a [ScatterPlotChart] with sample data and no transition.
factory ShapesScatterPlotChart.withSampleData() {
return new ShapesScatterPlotChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory ShapesScatterPlotChart.withRandomData() {
return new ShapesScatterPlotChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<LinearSales, num>> _createRandomData() {
final random = new Random();
final makeRadius = (int value) => (random.nextInt(value) + 2).toDouble();
final data = [
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
'circle', null, null),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
// Render a hollow circle, filled in with white.
new LinearSales(random.nextInt(100), random.nextInt(100),
makeRadius(4) + 4, 'circle', charts.MaterialPalette.white, 2.0),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
'circle', null, null),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
// Render a hollow circle, filled in with white.
new LinearSales(random.nextInt(100), random.nextInt(100),
makeRadius(4) + 4, 'circle', charts.MaterialPalette.white, 2.0),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6),
null, null, null),
// Render a hollow square, filled in with white.
new LinearSales(random.nextInt(100), random.nextInt(100),
makeRadius(4) + 4, null, charts.MaterialPalette.white, 2.0),
];
final maxMeasure = 100;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (LinearSales sales, _) {
// Color bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
radiusPxFn: (LinearSales sales, _) => sales.radius,
fillColorFn: (LinearSales row, _) => row.fillColor,
strokeWidthPxFn: (LinearSales row, _) => row.strokeWidth,
data: data,
)
// Accessor function that associates each datum with a symbol renderer.
..setAttribute(
charts.pointSymbolRendererFnKey, (int index) => data[index].shape)
// Default symbol renderer ID for data that have no defined shape.
..setAttribute(charts.pointSymbolRendererIdKey, 'rect')
];
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.ScatterPlotChart(seriesList,
animate: animate,
// Configure the point renderer to have a map of custom symbol
// renderers.
defaultRenderer:
new charts.PointRendererConfig<num>(customSymbolRenderers: {
'circle': new charts.CircleSymbolRenderer(),
'rect': new charts.RectSymbolRenderer(),
}));
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(0, 5, 3.0, 'circle', null, null),
new LinearSales(10, 25, 5.0, null, null, null),
new LinearSales(12, 75, 4.0, null, null, null),
// Render a hollow circle, filled in with white.
new LinearSales(
13, 225, 5.0, 'circle', charts.MaterialPalette.white, 2.0),
new LinearSales(16, 50, 4.0, null, null, null),
new LinearSales(24, 75, 3.0, null, null, null),
new LinearSales(25, 100, 3.0, 'circle', null, null),
new LinearSales(34, 150, 5.0, null, null, null),
new LinearSales(37, 10, 4.5, null, null, null),
// Render a hollow circle, filled in with white.
new LinearSales(
45, 300, 8.0, 'circle', charts.MaterialPalette.white, 2.0),
new LinearSales(52, 15, 4.0, null, null, null),
// Render a hollow square, filled in with white.
new LinearSales(56, 200, 7.0, null, charts.MaterialPalette.white, 2.0),
];
final maxMeasure = 300;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
// Providing a color function is optional.
colorFn: (LinearSales sales, _) {
// Bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
radiusPxFn: (LinearSales sales, _) => sales.radius,
fillColorFn: (LinearSales row, _) => row.fillColor,
strokeWidthPxFn: (LinearSales row, _) => row.strokeWidth,
data: data,
)
// Accessor function that associates each datum with a symbol renderer.
..setAttribute(
charts.pointSymbolRendererFnKey, (int index) => data[index].shape)
// Default symbol renderer ID for data that have no defined shape.
..setAttribute(charts.pointSymbolRendererIdKey, 'rect')
];
}
}
/// Sample linear data type.
class LinearSales {
final int year;
final int sales;
final double radius;
final String shape;
final charts.Color fillColor;
final double strokeWidth;
LinearSales(this.year, this.sales, this.radius, this.shape, this.fillColor,
this.strokeWidth);
}

View File

@@ -0,0 +1,150 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Scatter plot chart example
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_web/material.dart';
class SimpleScatterPlotChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
SimpleScatterPlotChart(this.seriesList, {this.animate});
/// Creates a [ScatterPlotChart] with sample data and no transition.
factory SimpleScatterPlotChart.withSampleData() {
return new SimpleScatterPlotChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory SimpleScatterPlotChart.withRandomData() {
return new SimpleScatterPlotChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<LinearSales, num>> _createRandomData() {
final random = new Random();
final makeRadius = (int value) => (random.nextInt(value) + 2).toDouble();
final data = [
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
new LinearSales(random.nextInt(100), random.nextInt(100), makeRadius(6)),
];
final maxMeasure = 100;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (LinearSales sales, _) {
// Color bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: data,
)
];
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.ScatterPlotChart(seriesList, animate: animate);
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(0, 5, 3.0),
new LinearSales(10, 25, 5.0),
new LinearSales(12, 75, 4.0),
new LinearSales(13, 225, 5.0),
new LinearSales(16, 50, 4.0),
new LinearSales(24, 75, 3.0),
new LinearSales(25, 100, 3.0),
new LinearSales(34, 150, 5.0),
new LinearSales(37, 10, 4.5),
new LinearSales(45, 300, 8.0),
new LinearSales(52, 15, 4.0),
new LinearSales(56, 200, 7.0),
];
final maxMeasure = 300;
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
// Providing a color function is optional.
colorFn: (LinearSales sales, _) {
// Bucket the measure column value into 3 distinct colors.
final bucket = sales.sales / maxMeasure;
if (bucket < 1 / 3) {
return charts.MaterialPalette.blue.shadeDefault;
} else if (bucket < 2 / 3) {
return charts.MaterialPalette.red.shadeDefault;
} else {
return charts.MaterialPalette.green.shadeDefault;
}
},
domainFn: (LinearSales sales, _) => sales.year,
measureFn: (LinearSales sales, _) => sales.sales,
// Providing a radius function is optional.
radiusPxFn: (LinearSales sales, _) => sales.radius,
data: data,
)
];
}
}
/// Sample linear data type.
class LinearSales {
final int year;
final int sales;
final double radius;
LinearSales(this.year, this.sales, this.radius);
}