From fdb6a5c69bc41c963d0b42d01e2dae91b39318bd Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 16 Aug 2019 16:44:24 +0100 Subject: [PATCH] Graphs properly rendered and fetched --- lib/common/apifunctions/get_graph_data.dart | 132 ++++++++++++++------ lib/pages/stats_page.dart | 105 +++++++++++++--- 2 files changed, 182 insertions(+), 55 deletions(-) diff --git a/lib/common/apifunctions/get_graph_data.dart b/lib/common/apifunctions/get_graph_data.dart index aea686c..8012a29 100644 --- a/lib/common/apifunctions/get_graph_data.dart +++ b/lib/common/apifunctions/get_graph_data.dart @@ -5,25 +5,34 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:charts_flutter/flutter.dart' as charts; class GraphData { - List data = new List(); + var chartType; + List> graph; - Future>> getGraphData(String graphType) async { -// print("called"); - /// 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"} + 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, + ) + ]; + } - 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, + 'graph': this.chartType, 'session_key': preferences.get('LastToken'), }; @@ -37,29 +46,82 @@ class GraphData { final List labels = responseJson['graph']['labels']; final List data = responseJson['graph']['data']; - for (int i = 0; i < data.length; i++) { -// print(labels[i].toString() + " : " + data[i].toString()); - } -// final List bounds = responseJson['graph']['bounds']; // why is this even returned? + List timeSeriesSpendList = new List(); - /* - 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), + 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++) { -// print(DateTime.parse(labels[i])); - timeSeriesSpendList.add(new TimeSeriesSpend(i, DateTime(i))); -// timeSeriesSpendList.add(new TimeSeriesSpend(data[i], DateTime.parse(labels[i]))); + timeSeriesSpendList.add(new TimeSeriesSpend(data[i]*1.00, DateTime.parse(labels[i]))); +// print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); } -// print(timeSeriesSpendList); + cachedData = timeSeriesSpendList; + loaded = true; return [ new charts.Series( @@ -70,18 +132,6 @@ class GraphData { 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); @@ -95,7 +145,7 @@ class GraphData { class TimeSeriesSpend { final DateTime time; - final int spend; + final double spend; TimeSeriesSpend(this.spend, this.time); } diff --git a/lib/pages/stats_page.dart b/lib/pages/stats_page.dart index e8a470c..92fbc72 100644 --- a/lib/pages/stats_page.dart +++ b/lib/pages/stats_page.dart @@ -29,8 +29,16 @@ class StatsPage extends StatefulWidget { class StatsPageState extends State { - GraphData graphData = new GraphData(); - List> displayedGraphData; + /// Graph types: + /// - total_last_week + /// - avg_spend_last_week + /// - total_last_month + /// - avg_spend_last_month + + GraphData totalLastWeekGraph = new GraphData("total_last_week"); + GraphData avgSpendLastWeekGraph = new GraphData("avg_spend_last_week"); + GraphData totalLastMonthGraph = new GraphData("total_last_month"); + GraphData avgSpendLastMonth = new GraphData("avg_spend_last_month"); @override void initState() { @@ -50,12 +58,29 @@ class StatsPageState extends State { @override Widget build(BuildContext context) { - if (graphData.data.length == 0) { - graphData.getGraphData('total_last_week').then((fetchedData) { - displayedGraphData = (fetchedData); - for (int i = 0; i < fetchedData[0].data.length; i++) { - print(fetchedData[0].data[i].time); // this is getting very frustrating - } + + // Initializing graphs: + + if (!totalLastWeekGraph.loaded) { + totalLastWeekGraph.setGraphData().then((_) { + setState(() {}); + }); + } + + if (!avgSpendLastWeekGraph.loaded) { + avgSpendLastWeekGraph.setGraphData().then((_) { + setState(() {}); + }); + } + + if (!totalLastMonthGraph.loaded) { + totalLastMonthGraph.setGraphData().then((_) { + setState(() {}); + }); + } + + if (!avgSpendLastMonth.loaded) { + avgSpendLastMonth.setGraphData().then((_) { setState(() {}); }); } @@ -84,7 +109,7 @@ class StatsPageState extends State { Container( padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), child : Text( - "This Week's Spend", + "Last Week's Total Spend", textAlign: TextAlign.center, style: TextStyle( fontSize: 22.0, @@ -97,12 +122,64 @@ class StatsPageState extends State { Container( padding: EdgeInsets.symmetric(horizontal: 10), height: 200, -// width: 250, + child: totalLastWeekGraph.graph != null ? new charts.TimeSeriesChart(totalLastWeekGraph.graph) : Container(), //List> + ), -// child: new TimeSeries(), - child: displayedGraphData != null ? new charts.TimeSeriesChart(displayedGraphData) : Container(), //List> -// child: new charts.TimeSeriesChart(displayedGraphData), -// child: new SimpleTimeSeriesChart(),//seriesList: List + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Week's Average Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: avgSpendLastWeekGraph.graph != null ? new charts.TimeSeriesChart(avgSpendLastWeekGraph.graph) : Container(), //List> + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Month's Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: totalLastMonthGraph.graph != null ? new charts.TimeSeriesChart(totalLastMonthGraph.graph) : Container(), //List> + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Month's Average Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: avgSpendLastMonth.graph != null ? new charts.TimeSeriesChart(avgSpendLastMonth.graph) : Container(), //List> ), ],