google / charts

https://pub.dev/packages/charts_flutter
Apache License 2.0
2.81k stars 1.22k forks source link

Werid examples #564

Open ollyde opened 3 years ago

ollyde commented 3 years ago

In the chart examples, there's a lot of useless code and strange examples that break the traditional Flutter examples recommendations. For example, the use of Factory here is useless and it's confusing to use statics/additional funcitons and layers.

/// Gauge chart example, where the data does not cover a full revolution in the
/// chart.
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

class GaugeChart extends StatelessWidget {
  final List<charts.Series> seriesList;
  final bool animate;

  GaugeChart(this.seriesList, {this.animate});

  /// Creates a [PieChart] with sample data and no transition.
  factory GaugeChart.withSampleData() {
    return new GaugeChart(
      _createSampleData(),
      // Disable animations for image tests.
      animate: false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return new charts.PieChart(seriesList,
        animate: animate,
        // Configure the width of the pie slices to 30px. The remaining space in
        // the chart will be left as a hole in the center. Adjust the start
        // angle and the arc length of the pie so it resembles a gauge.
        defaultRenderer: new charts.ArcRendererConfig(
            arcWidth: 30, startAngle: 4 / 5 * pi, arcLength: 7 / 5 * pi));
  }

  /// Create one series with sample hard coded data.
  static List<charts.Series<GaugeSegment, String>> _createSampleData() {
    final data = [
      new GaugeSegment('Low', 75),
      new GaugeSegment('Acceptable', 100),
      new GaugeSegment('High', 50),
      new GaugeSegment('Highly Unusual', 5),
    ];

    return [
      new charts.Series<GaugeSegment, String>(
        id: 'Segments',
        domainFn: (GaugeSegment segment, _) => segment.segment,
        measureFn: (GaugeSegment segment, _) => segment.size,
        data: data,
      )
    ];
  }
}

/// Sample data type.
class GaugeSegment {
  final String segment;
  final int size;

  GaugeSegment(this.segment, this.size);
}

This could be reduced to something a lot more readable, less code. Example

    final data = [
      new SignalBlock('Low', 75),
      new SignalBlock('Acceptable', 100),
      new SignalBlock('High', 50),
      new SignalBlock('Highly Unusual', 5),
    ];

    final sampleData = [
      new charts.Series<SignalBlock, String>(
        id: 'Segments',
        domainFn: (SignalBlock segment, _) => segment.name,
        measureFn: (SignalBlock segment, _) => segment.percentage,
        data: data,
      )
    ];

    return charts.PieChart(
      sampleData,
      animate: true,
    );
muratozbayraktar commented 3 years ago

Thanks. That i was thinking. Worst documentation ever seen.

Some working easy sample here BarChart

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

class HorizontalBarChart extends StatelessWidget {
  HorizontalBarChart();

  @override
  Widget build(BuildContext context) {
    return new charts.BarChart(
      _createSampleData(),
      animate: true,
      vertical: false,
    );
  }

  static List<charts.Series<OrdinalSales, String>> _createSampleData() {
    final data = [
      new OrdinalSales('2014', 5),
      new OrdinalSales('2015', 25),
      new OrdinalSales('2016', 100),
      new OrdinalSales('2017', 75),
    ];

    return [
      new charts.Series<OrdinalSales, String>(
        id: 'Sales',
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

class OrdinalSales {
  final String year;
  final int sales;

  OrdinalSales(this.year, this.sales);
}