David PHAM-VAN

Add RadialGrid charts

@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 4
5 - Fix some linting issues 5 - Fix some linting issues
6 - Add PdfPage.rotate attribute 6 - Add PdfPage.rotate attribute
  7 +- Add RadialGrid for charts with polar coordinates
7 8
8 ## 3.0.1 9 ## 3.0.1
9 10
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +import 'dart:math' as math;
  18 +
  19 +import 'package:meta/meta.dart';
  20 +import 'package:pdf/pdf.dart';
  21 +
  22 +import '../geometry.dart';
  23 +import '../widget.dart';
  24 +import 'chart.dart';
  25 +
  26 +@experimental
  27 +class RadialGrid extends ChartGrid {
  28 + RadialGrid();
  29 +
  30 + late PdfRect gridBox;
  31 +
  32 + @override
  33 + void layout(Context context, BoxConstraints constraints,
  34 + {bool parentUsesSize = false}) {
  35 + super.layout(context, constraints, parentUsesSize: parentUsesSize);
  36 +
  37 + final datasets = Chart.of(context).datasets;
  38 + final size = constraints.biggest;
  39 +
  40 + gridBox = PdfRect(0, 0, size.x, size.y);
  41 +
  42 + for (final dataset in datasets) {
  43 + dataset.layout(context, BoxConstraints.tight(gridBox.size));
  44 + }
  45 + }
  46 +
  47 + @override
  48 + PdfPoint toChart(PdfPoint p) {
  49 + const z = 3.0;
  50 + return PdfPoint(
  51 + z * p.y * math.cos(p.x / 7 * math.pi * 2) + gridBox.width / 2,
  52 + z * p.y * math.sin(p.x / 7 * math.pi * 2) + gridBox.height / 2,
  53 + );
  54 + }
  55 +
  56 + void paintBackground(Context context) {}
  57 +
  58 + void clip(Context context, PdfPoint size) {
  59 + context.canvas
  60 + ..saveContext()
  61 + ..drawBox(gridBox)
  62 + ..clipPath();
  63 + }
  64 +
  65 + @override
  66 + void paint(Context context) {
  67 + super.paint(context);
  68 +
  69 + final datasets = Chart.of(context).datasets;
  70 +
  71 + clip(context, box!.size);
  72 + for (var dataSet in datasets) {
  73 + dataSet.paintBackground(context);
  74 + }
  75 + context.canvas.restoreContext();
  76 + paintBackground(context);
  77 + clip(context, box!.size);
  78 + for (var dataSet in datasets) {
  79 + dataSet.paint(context);
  80 + }
  81 + context.canvas.restoreContext();
  82 + }
  83 +}
@@ -25,6 +25,7 @@ export 'src/widgets/chart/bar_chart.dart'; @@ -25,6 +25,7 @@ export 'src/widgets/chart/bar_chart.dart';
25 export 'src/widgets/chart/chart.dart'; 25 export 'src/widgets/chart/chart.dart';
26 export 'src/widgets/chart/grid_axis.dart'; 26 export 'src/widgets/chart/grid_axis.dart';
27 export 'src/widgets/chart/grid_cartesian.dart'; 27 export 'src/widgets/chart/grid_cartesian.dart';
  28 +export 'src/widgets/chart/grid_radial.dart';
28 export 'src/widgets/chart/legend.dart'; 29 export 'src/widgets/chart/legend.dart';
29 export 'src/widgets/chart/line_chart.dart'; 30 export 'src/widgets/chart/line_chart.dart';
30 export 'src/widgets/clip.dart'; 31 export 'src/widgets/clip.dart';