import 'dart:convert'; import 'dart:async'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; import 'package:charts_flutter/flutter.dart' as charts; class GraphData { List data = new List(); Future> getGraphData(String graphType) async { /// Graph types: /// - total_last_week /// - avg_spend_last_week /// - total_last_month /// - avg_spend_last_month /// /// HTTP POST request sample: /// {"graph":"total_last_week","session_key":"blahblahblah"} charts.Series dataSeries = new charts.Series(); final url = "https://dev.peartrade.org/api/v1/customer/graphs"; SharedPreferences preferences = await SharedPreferences.getInstance(); Map body = { 'graph': graphType, 'session_key': preferences.get('LastToken'), }; final response = await http.post( url, body: json.encode(body), ); if (response.statusCode == 200) { final responseJson = jsonDecode(response.body); final List labels = responseJson['graph']['labels']; final List data = responseJson['graph']['data']; // final List bounds = responseJson['graph']['bounds']; // why is this even returned? /* final data = [ new TimeSeriesSales(new DateTime(2017, 9, 19), 5), new TimeSeriesSales(new DateTime(2017, 9, 26), 25), new TimeSeriesSales(new DateTime(2017, 10, 3), 100), new TimeSeriesSales(new DateTime(2017, 10, 10), 75), ]; */ List timeSeriesSpendList = new List(); for (int i = 0; i < labels.length; i++) { print(DateTime.parse(labels[i])); timeSeriesSpendList.add(new TimeSeriesSpend(i, DateTime(i))); // timeSeriesSpendList.add(new TimeSeriesSpend(data[i], DateTime.parse(labels[i]))); } return [ new charts.Series( id: 'Spend', colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, domainFn: (TimeSeriesSpend spend, _) => spend.time, measureFn: (TimeSeriesSpend spend, _) => spend.spend, data: timeSeriesSpendList, ) ]; /* new charts.Series( id: 'Sales', colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, domainFn: (TimeSeriesSales sales, _) => sales.time, measureFn: (TimeSeriesSales sales, _) => sales.sales, data: data, )*/ // print(labels[5]); // print(data[5]); } else { final errorMessage = json.decode(response.body)['message']; print("Error: " + errorMessage); return null; } } } class TimeSeriesSpend { final DateTime time; final int spend; TimeSeriesSpend(this.spend, this.time); }