mzimmerm / flutter_charts

Charts Library for Flutter, written in Dart with Flutter.
Other
250 stars 42 forks source link

Area line chart string vs int index issue #28

Closed duindain closed 2 years ago

duindain commented 4 years ago

I've made a Line area chart and copied the code from a bar chart I am made however I can't seem to get it to render, I think I am making a mistake in the data I provide (I find it very confusing the differences between charts, some can accept dates others only numbers others again can take strings or numbers, it seems quite random)

This is the chart

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

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

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

  factory AChart.withData() {
    return new AChart(
       _createSampleData() ,
      // Disable animations for image tests.
      animate: false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return new charts.LineChart(seriesList,
        defaultRenderer: new charts.LineRendererConfig(includeArea: true, stacked: true),
        behaviors: [new charts.SeriesLegend()],
        animate: animate);
  }

  /// Create one series with sample hard coded data.
  static List<charts.Series<Efficiency, String>> _createSampleData() {
    var data = new Map<String,List<Efficiency>>();
    data["98.0"] = [
      new Efficiency(1, 8.2),
      new Efficiency(2, 8.1),
      new Efficiency(3, 8.4),
      new Efficiency(4, 8.3),];
    data["95.0"] = [
      new Efficiency(1, 7.9),
      new Efficiency(2, 7.8),
      new Efficiency(3, 7.9),
      new Efficiency(4, 8.0),];
    data["92.0"] = [
      new Efficiency(1, 8.4),
      new Efficiency(2, 8.6),
      new Efficiency(3, 8.5),
      new Efficiency(4, 8.7),
      new Efficiency(5, 8.6),
      new Efficiency(6, 8.7),];

    return createSeries(data);
  }

  static List<charts.Series<Efficiency, String>> createSeries(Map<String,List<Efficiency>> data)
  {
    var series = new List<charts.Series<Efficiency, String>>();
    data.forEach((k,v) =>
    {
      series.add(new charts.Series<Efficiency, String>(
        id: k,
        domainFn: (Efficiency refill, _) => '$refill.index',
        measureFn: (Efficiency refill, _) => refill.efficiency,
        data: v,
      ))
    });
    return series;
  }
}

/// Sample linear data type.
class Efficiency {
  final int index;
  final double efficiency;

  Efficiency(this.index, this.efficiency);
}

I've tried changing index in Efficiency to be a string or int, when its an int it has a compile error saying it should be a string when its a string it compiles but fails at runtime to render with the following error

type 'List<Series<Efficiency, String>>' is not a subtype of type 'List<Series<dynamic, num>>'

Thanks for any help

mzimmerm commented 2 years ago

@duindain Looks like you are refering to another library, charts_flutter. This is a different charting library, flutter_charts. Please check with the other library help.