Флаттер-диаграмма с mysql - PullRequest
0 голосов
/ 27 мая 2020

Я хочу показать диаграмму с данными из запроса, которые у меня есть на карте, но я не знаю, как передать ее в List seriesList;.

enter image description here

Примерно так выглядят результаты запроса JSON. Я хочу "mes" на красной стрелке и "resultado" на синей стрелке.

enter image description here

Вот полный код:

import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart' as http;



 class Grafica1 extends StatefulWidget {

   Grafica1(): super();

   final String title = "Gráfica 1";

   @override
   Grafica1State createState() => Grafica1State();
}

class Grafica1State extends State<Grafica1>{

  //TextEditingController anio = new TextEditingController();

  List listaMeses;
   Future<List> obtenerValor() async {
    var respuesta;
    var url = "http://192.168.0.4/vete/grafica1.php";
    final response = await http.post(url, body: {
      "year" : "2020",
    });
    setState(() {
      respuesta = json.decode(response.body);
      listaMeses = respuesta["data"];
    });
    print(respuesta);

    pintarMeses();
  }

  String _dropMes = null;
  Map<String, String>listarMesesM = Map();

  void pintarMeses(){

    for(var i=0; i<listaMeses.length;i++){
      listarMesesM[listaMeses[i]['mes']]=listaMeses[i]['resultado'];
    }
    _dropMes = listarMesesM[listaMeses[0]['mes']];
  }


  List<charts.Series>seriesList;

 List<charts.Series<Sales, String>> _createSampleData(){
    //final random = Random();
    final deskopSalesData =[
      Sales(listarMesesM.values.toString(), 50),
      Sales(listarMesesM.values.toString(), 40),
    ];

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

  barChart(){
    return charts.BarChart(
      seriesList,
      animate: true,
      vertical: true,
    );
  }

  List<Year> _years = Year.getYear();
  List<DropdownMenuItem<Year>> _graficaMenuItems;
  Year _selectYear;

  List data = [];

  @override
  void initState() {
    _graficaMenuItems = buildGrafica1MenuItems(_years);
    _selectYear = _graficaMenuItems[0].value;
    // TODO: implement initState
    super.initState();
    seriesList = _createSampleData();
    obtenerValor();
    //fetchData();
  }

  /*void fetchData() async{
    final response = await http.get('http://192.168.0./vete/grafica1.php');

    if (response.statusCode == 200){
      setState(() {
        data = json.decode(response.body);
      });
    }
  }*/

  List<DropdownMenuItem<Year>> buildGrafica1MenuItems(List years){
    List<DropdownMenuItem<Year>> items = List();
    for(Year year in years){
      items.add(DropdownMenuItem(value: year, child:Text(year.yearName),),);
    }
    return items;
  }

  onChangeDropdownItem(Year selectedYear){
    setState(() {
      _selectYear = selectedYear;
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title),
          ),
          body: SafeArea(child:
            SingleChildScrollView(child: Container(
            child : Center(
              child : Column(
                children : <Widget>[
           Text("Selecciona un año"),
            SizedBox(height: 10.0,),
            DropdownButton(
              value: _selectYear,
              items: _graficaMenuItems,
              onChanged: onChangeDropdownItem,
            ),
            SizedBox(height: 10.0,
            ),
            Container(
              height: 430,
              child: barChart(),
            ),
            ],
            ),
            ),
            padding: EdgeInsets.all(20.0),
            //¿child: barChart(),
          ),),),
        );
    }
}

class Sales{
  final String mes;
  final int sales;

  Sales(this.mes, this.sales);

}

class Year{
  int idY;
  String yearName;

  Year(this.idY, this.yearName);

  static List<Year>getYear(){
    return<Year>[
      Year(1,'2020'),
      //Year(2,'2019'),
      //Year(3,'2020'),
    ];
  }
}
...