From 2216b96c6c893fe86049fc56b3a8ed2e4b440914 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Mon, 20 Nov 2017 15:14:13 +0100 Subject: [PATCH] Added a limit for the number of displayed item in piechart --- static/js/trendings.js | 22 +++++++++++++++++----- trendings_helper.py | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/static/js/trendings.js b/static/js/trendings.js index f0a020b..5a9c72f 100644 --- a/static/js/trendings.js +++ b/static/js/trendings.js @@ -144,6 +144,13 @@ function generateEmptyAndFillData(data) { return toPlot; } +function compareObj(a,b) { + if (a.data < b.data) + return -1; + if (a.data > b.data) + return 1; + return 0; +} /* UPDATES */ function updatePie(pie, data) { @@ -166,14 +173,19 @@ function updatePie(pie, data) { toPlot_obj[itemStr] += count; } } - toPlot = []; - for (var itemStr in toPlot_obj) { - if (toPlot_obj.hasOwnProperty(itemStr)) { - toPlot.push({label: itemStr, data: toPlot_obj[itemStr], color: itemMapping[itemStr].colour}) + if (Object.keys(toPlot_obj).length == 0) { // no data + toPlot = [{ label: 'No data', data: 100 }]; + } else { + toPlot = []; + for (var itemStr in toPlot_obj) { + if (toPlot_obj.hasOwnProperty(itemStr)) { + toPlot.push({label: itemStr, data: toPlot_obj[itemStr], color: itemMapping[itemStr].colour}) + } } } + toPlot.sort(compareObj).reverse(); + toPlot = toPlot.slice(0,15); // take at max 12 elements } - if (!(pieWidget === undefined)) { pieWidget.setData(toPlot); pieWidget.setupGrid(); diff --git a/trendings_helper.py b/trendings_helper.py index ac81ca8..2fe4fdf 100644 --- a/trendings_helper.py +++ b/trendings_helper.py @@ -54,7 +54,7 @@ class Trendings_helper: ''' GETTER ''' - def getGenericTrending(self, trendingType, dateS, dateE, topNum=12): + def getGenericTrending(self, trendingType, dateS, dateE, topNum=0): to_ret = [] prev_days = (dateE - dateS).days for curDate in util.getXPrevDaysSpan(dateE, prev_days):