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 { var chartType; List> graph; GraphData( this.chartType, ); List cachedData; bool loaded = false; Future setGraphData() async { if (loaded == true) { this.graph = [ new charts.Series( id: 'Spend', colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, domainFn: (TimeSeriesSpend spend, _) => spend.time, measureFn: (TimeSeriesSpend spend, _) => spend.spend, data: cachedData, ) ]; } final url = "https://dev.peartrade.org/api/v1/customer/graphs"; SharedPreferences preferences = await SharedPreferences.getInstance(); Map body = { 'graph': this.chartType, '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']; List timeSeriesSpendList = new List(); for (int i = 0; i < labels.length; i++) { timeSeriesSpendList.add(new TimeSeriesSpend(data[i]*1.00, DateTime.parse(labels[i]))); // print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); } cachedData = timeSeriesSpendList; loaded = true; this.graph = [ new charts.Series( id: 'Spend', colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, domainFn: (TimeSeriesSpend spend, _) => spend.time, measureFn: (TimeSeriesSpend spend, _) => spend.spend, data: timeSeriesSpendList, ) ]; } else { final errorMessage = json.decode(response.body)['message']; print("Error: " + errorMessage); this.graph = null; } } Future>> getGraphData() async { if (loaded == true) { return [ new charts.Series( id: 'Spend', colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, domainFn: (TimeSeriesSpend spend, _) => spend.time, measureFn: (TimeSeriesSpend spend, _) => spend.spend, data: cachedData, ) ]; } /// 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"} final url = "https://dev.peartrade.org/api/v1/customer/graphs"; SharedPreferences preferences = await SharedPreferences.getInstance(); Map body = { 'graph': this.chartType, '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']; List timeSeriesSpendList = new List(); for (int i = 0; i < labels.length; i++) { timeSeriesSpendList.add(new TimeSeriesSpend(data[i]*1.00, DateTime.parse(labels[i]))); // print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); } cachedData = timeSeriesSpendList; loaded = true; return [ new charts.Series( id: 'Spend', colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, domainFn: (TimeSeriesSpend spend, _) => spend.time, measureFn: (TimeSeriesSpend spend, _) => spend.spend, data: timeSeriesSpendList, ) ]; } else { final errorMessage = json.decode(response.body)['message']; print("Error: " + errorMessage); return null; } } } class TimeSeriesSpend { final DateTime time; final double spend; TimeSeriesSpend(this.spend, this.time); }