From 303fad71d8d3fe7a30cde8dc1aec830008514d94 Mon Sep 17 00:00:00 2001 From: joethei Date: Thu, 9 May 2019 18:14:43 +0200 Subject: [PATCH] + day selector ~correct values for shopping times & week overview #SPM-18: add work 4h development --- .../spm/baudas/web/ShoppingTimesDiagram.java | 32 ++++- .../spm/baudas/web/WeekOverviewDiagram.java | 16 +-- src/main/webapp/data/overview.json | 26 ---- src/main/webapp/footer.jsp | 6 +- src/main/webapp/header.jsp | 125 +++++++++++------- src/main/webapp/index.jsp | 5 +- src/main/webapp/js/shoping_times.js | 57 -------- src/main/webapp/js/shopping_times.js | 105 +++++++++++++++ 8 files changed, 214 insertions(+), 158 deletions(-) delete mode 100644 src/main/webapp/data/overview.json delete mode 100644 src/main/webapp/js/shoping_times.js create mode 100644 src/main/webapp/js/shopping_times.js diff --git a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java index 0042832..a728d98 100644 --- a/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/ShoppingTimesDiagram.java @@ -1,6 +1,7 @@ package de.hsel.spm.baudas.web; import com.google.gson.Gson; +import de.hsel.spm.baudas.analysis.DayHour; import de.hsel.spm.baudas.analysis.ShoppingTimes; import org.jetbrains.annotations.NotNull; @@ -11,9 +12,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author Johannes Theiner @@ -31,7 +30,10 @@ public class ShoppingTimesDiagram extends HttpServlet { resp.setContentType("application/json"); PrintWriter out = resp.getWriter(); - Map> result = new HashMap<>(); + List definedOrder = Arrays.asList("<10 Uhr", "10-12 Uhr", "12-14 Uhr", "14-17 Uhr", ">17 Uhr"); + Comparator comparator = Comparator.comparingInt(definedOrder::indexOf); + + Map> result = new HashMap<>(); Gson gson = new Gson(); @@ -42,9 +44,29 @@ public class ShoppingTimesDiagram extends HttpServlet { } assert file.getUuid() != null; ShoppingTimes shoppingTimes = new ShoppingTimes(Data.get(file.getUuid())); + Map map = shoppingTimes.getResult(); - out.print(gson.toJson(shoppingTimes.getResult())); + Set days = new TreeSet<>(); + Set hours = new TreeSet<>(comparator); + for (DayHour dayHour : map.keySet()) { + days.add(dayHour.getDay()); + hours.add(dayHour.getHour()); + } + result.put("labels", hours); + for (String day : days) { + if (!result.containsKey(day)) + result.put(day, new ArrayList<>()); + for (String hour : hours) { + if (map.containsKey(new DayHour(day, hour))) { + result.get(day).add(map.get(new DayHour(day, hour)).toString()); + } else result.get(day).add("0"); + } + } + + + out.print(gson.toJson(result)); + } } \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java index 21855c4..8d56b02 100644 --- a/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java +++ b/src/main/java/de/hsel/spm/baudas/web/WeekOverviewDiagram.java @@ -11,12 +11,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * changes data from week overview into readable format for chart.js @@ -58,20 +53,15 @@ public class WeekOverviewDiagram extends HttpServlet { assert file.getUuid() != null; WeekOverview overview = new WeekOverview(Data.get(file.getUuid())); - List labels = new ArrayList<>(); - List count = new ArrayList<>(); List revenue = new ArrayList<>(); - for (Map.Entry> entry : overview.getResult().entrySet()) { - labels.add(entry.getKey()); - count.add(entry.getValue().getKey().toString()); - revenue.add(entry.getValue().getValue().toString()); - } + List labels = new ArrayList<>(overview.getResult().keySet()); Comparator comparator = Comparator.comparingInt(definedOrder::indexOf); labels.sort(comparator); + System.out.println(labels); for (String label : labels) { Map.Entry entry = overview.getResult().get(label); diff --git a/src/main/webapp/data/overview.json b/src/main/webapp/data/overview.json deleted file mode 100644 index 3f3c8db..0000000 --- a/src/main/webapp/data/overview.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "labels": [ - "1 Jan", - "2 Jan", - "3 Jan", - "4 Jan", - "5 Jan", - "6 Jan" - ], - "data1": [ - 200, - 150, - 190, - 99, - 142, - 222 - ], - "data2": [ - 434, - 300, - 324, - 198, - 349, - 353 - ] - } \ No newline at end of file diff --git a/src/main/webapp/footer.jsp b/src/main/webapp/footer.jsp index b7edde8..a358f08 100644 --- a/src/main/webapp/footer.jsp +++ b/src/main/webapp/footer.jsp @@ -10,10 +10,10 @@ - - + +<%-- - +--%> diff --git a/src/main/webapp/header.jsp b/src/main/webapp/header.jsp index f0aa07f..c7bd797 100644 --- a/src/main/webapp/header.jsp +++ b/src/main/webapp/header.jsp @@ -7,13 +7,16 @@ - - + + - + @@ -25,19 +28,23 @@ @@ -46,50 +53,66 @@
    -
  • -
    - -
    -
    -
    -
  • -
  • -
  • - -
    - -
  • -
  • - -
    - -
    -
  • -
  • -
  • -
    -
    -
    - File - -
    -
    - +
  • +
    +
    +
    +
    +
    - -
  • + +
  • +
    +
  • +
  • +
    + +
    + +
    +
  • +
  • +
    + +
    + +
    +
    +
  • +
  • +
    +
  • +
    +
  • +
    +
    +
    + File + +
    +
    + +
    +
    +
  • +
  • +
    + +
    + +
  • +
\ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 9fbc781..06451ba 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -34,16 +34,15 @@
- - - +
diff --git a/src/main/webapp/js/shoping_times.js b/src/main/webapp/js/shoping_times.js deleted file mode 100644 index 2644b26..0000000 --- a/src/main/webapp/js/shoping_times.js +++ /dev/null @@ -1,57 +0,0 @@ - -$.ajax({ - url: 'data/shoping_times.json', - dataType: 'json' -}).done(function (results) { - new Chart(document.getElementById("shoping_times_chart"), { - type: 'bar', - data: { - labels: results.labels, - datasets: [{ - label: 'Anzahl verkaufter Artikel', - data: results.data1, - backgroundColor: [ - 'rgba(143, 170, 220, 0.9)', - 'rgba(255, 50, 1, 0.9)', - 'rgba(143, 170, 220, 0.9)', - 'rgba(143, 170, 220, 0.9)', - 'rgba(103, 215, 153, 0.9)', - 'rgba(143, 170, 220, 0.9)', - ], - borderColor: [ - 'rgba(143, 170, 220, 0.9)', - 'rgba(255, 50, 1, 1)', - 'rgba(143, 170, 220, 1)', - 'rgba(143, 170, 220, 1)', - 'rgba(103, 215, 153, 1)', - 'rgba(143, 170, 220, 1)', - ], - borderWidth: 1 - }] - }, - options: { - responsive: true, - title: { - display: false, - text: 'Einkaufszeiten' - }, - legend: { - display: false - }, - scales: { - yAxes: [{ - categoryPercentage: 1.0, - barPercentage: 0.5, - ticks: { - beginAtZero: true - } - }], - xAxes: [{ - gridLines: { - display: false - }, - }] - } - } - }); -}); \ No newline at end of file diff --git a/src/main/webapp/js/shopping_times.js b/src/main/webapp/js/shopping_times.js new file mode 100644 index 0000000..97655fd --- /dev/null +++ b/src/main/webapp/js/shopping_times.js @@ -0,0 +1,105 @@ +let shopping_result; + +let shopping_chart = new Chart(document.getElementById("shoping_times_chart"), { + type: 'bar', + data: { + labels: [1, 2, 3, 4, 5], + datasets: [{ + label: 'Anzahl verkaufter Artikel', + data: [0, 0, 0, 0, 0], + backgroundColor: [ + 'rgba(143, 170, 220, 0.9)', + 'rgba(143, 170, 220, 0.9)', + 'rgba(143, 170, 220, 0.9)', + 'rgba(143, 170, 220, 0.9)', + 'rgba(143, 170, 220, 0.9)', + 'rgba(143, 170, 220, 0.9)', + ], + borderColor: [ + 'rgba(143, 170, 220, 1)', + 'rgba(143, 170, 220, 1)', + 'rgba(143, 170, 220, 1)', + 'rgba(143, 170, 220, 1)', + 'rgba(143, 170, 220, 1)', + 'rgba(143, 170, 220, 1)', + ], + borderWidth: 1 + }] + }, + options: { + responsive: true, + title: { + display: false, + text: 'Einkaufszeiten' + }, + legend: { + display: false + }, + scales: { + yAxes: [{ + categoryPercentage: 1.0, + barPercentage: 0.5, + ticks: { + beginAtZero: true + } + }], + xAxes: [{ + gridLines: { + display: false + }, + }] + } + } +}); + +$.ajax({ + url: 'shopping_times', + dataType: 'json' +}).done(function (results) { + shopping_result = results; + updateDays(); +}); + +function updateDays() { + let e = document.getElementById("day"); + let value = e.options[e.selectedIndex].text; + shopping_chart.data.labels = shopping_result.labels; + shopping_chart.data.datasets[0].data = shopping_result[value]; + + let color = shopping_result[value].map(x => 'rgba(143, 170, 220, 0.9)'); + + console.log("max"); + argMax(shopping_result[value]).slice().forEach(function (item) { + console.log(item); + color[item] = 'green'; + }); + /*console.log("min"); + argMin(shopping_result[value]).forEach(function(item) { + console.log(item); + color[item] = 'red'; + });*/ + + shopping_chart.data.datasets[0].backgroundColor = color; + + shopping_chart.update(); +} + +function argMax(array) { + let result = []; + console.log(Math.max.apply(null, array)); + array.forEach(function(item) { + console.log(item); + if(item == Math.max.apply(null, array)) { + console.log(true); + result.push(array.indexOf(item)); + } + }); + console.log(result); + return result; +} + +function argMin(array) { + let arr = array.slice().sort(); + console.log(arr[0]); + return arr +} \ No newline at end of file