Geolocation bar chart to server page

This commit is contained in:
Rsl1122 2018-07-15 20:34:05 +03:00
parent 9e9bb8097d
commit 376690acc0
9 changed files with 175 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.utilities.analysis.ServerBanDataReader;
import com.djrapitops.plan.utilities.html.graphs.ActivityStackGraph;
import com.djrapitops.plan.utilities.html.graphs.PunchCardGraph;
import com.djrapitops.plan.utilities.html.graphs.WorldMap;
import com.djrapitops.plan.utilities.html.graphs.bar.GeolocationBarGraph;
import com.djrapitops.plan.utilities.html.graphs.calendar.ServerCalendar;
import com.djrapitops.plan.utilities.html.graphs.line.*;
import com.djrapitops.plan.utilities.html.graphs.pie.ActivityPie;
@ -325,6 +326,11 @@ public class AnalysisContainer extends DataContainer {
putSupplier(AnalysisKeys.WORLD_MAP_SERIES, () ->
new WorldMap(getUnsafe(AnalysisKeys.PLAYERS_MUTATOR).getGeolocations()).toHighChartsSeries()
);
Key<GeolocationBarGraph> geolocationBarChart = new Key<>(GeolocationBarGraph.class, "GEOLOCATION_BAR_CHART");
putSupplier(geolocationBarChart, () -> new GeolocationBarGraph(getUnsafe(AnalysisKeys.PLAYERS_MUTATOR)));
putSupplier(AnalysisKeys.COUNTRY_CATEGORIES, () -> getUnsafe(geolocationBarChart).toHighChartsCategories());
putSupplier(AnalysisKeys.COUNTRY_SERIES, () -> getUnsafe(geolocationBarChart).toHighChartsSeries());
putSupplier(AnalysisKeys.CALENDAR_SERIES, () -> new ServerCalendar(
getUnsafe(AnalysisKeys.PLAYERS_MUTATOR),
getUnsafe(AnalysisKeys.UNIQUE_PLAYERS_PER_DAY),

View File

@ -129,6 +129,8 @@ public class AnalysisKeys {
public static final PlaceholderKey<String> CALENDAR_SERIES = new PlaceholderKey<>(String.class, "calendarSeries");
public static final PlaceholderKey<String> UNIQUE_PLAYERS_SERIES = new PlaceholderKey<>(String.class, "uniquePlayersSeries");
public static final PlaceholderKey<String> NEW_PLAYERS_SERIES = new PlaceholderKey<>(String.class, "newPlayersSeries");
public static final PlaceholderKey<String> COUNTRY_CATEGORIES = new PlaceholderKey<>(String.class, "countryCategories");
public static final PlaceholderKey<String> COUNTRY_SERIES = new PlaceholderKey<>(String.class, "countrySeries");
// Variables used only during analysis
public static final Key<SessionsMutator> SESSIONS_MUTATOR = CommonKeys.SESSIONS_MUTATOR;
public static final Key<TPSMutator> TPS_MUTATOR = CommonKeys.TPS_MUTATOR;

View File

@ -102,7 +102,7 @@ public class PlayersMutator {
for (PlayerContainer player : players) {
Optional<GeoInfo> mostRecent = GeoInfoMutator.forContainer(player).mostRecent();
mostRecent.ifPresent(geoInfo -> geolocations.add(geoInfo.getGeolocation()));
geolocations.add(mostRecent.map(GeoInfo::getGeolocation).orElse("Unknown"));
}
return geolocations;

View File

@ -157,7 +157,8 @@ public class AnalysisPage implements Page {
ENTITY_SERIES, CHUNK_SERIES, PUNCHCARD_SERIES,
WORLD_MAP_SERIES, ACTIVITY_STACK_SERIES, ACTIVITY_STACK_CATEGORIES,
ACTIVITY_PIE_SERIES, CALENDAR_SERIES,
UNIQUE_PLAYERS_SERIES, NEW_PLAYERS_SERIES
UNIQUE_PLAYERS_SERIES, NEW_PLAYERS_SERIES,
COUNTRY_CATEGORIES, COUNTRY_SERIES
);
Benchmark.stop(DEBUG, DEBUG + " Chart Series");
}

View File

@ -0,0 +1,25 @@
package com.djrapitops.plan.utilities.html.graphs.bar;
public class Bar implements Comparable<Bar> {
private final String label;
private final long value;
public Bar(String label, long value) {
this.label = label;
this.value = value;
}
public String getLabel() {
return label;
}
public long getValue() {
return value;
}
@Override
public int compareTo(Bar bar) {
return Long.compare(bar.value, this.value);
}
}

View File

@ -0,0 +1,47 @@
package com.djrapitops.plan.utilities.html.graphs.bar;
import com.djrapitops.plan.utilities.html.graphs.HighChart;
import java.util.List;
public class BarGraph implements HighChart {
private final List<Bar> bars;
public BarGraph(List<Bar> bars) {
this.bars = bars;
}
public String toHighChartsCategories() {
StringBuilder categories = new StringBuilder("[");
int i = 0;
int size = bars.size();
for (Bar bar : bars) {
categories.append("'").append(bar.getLabel()).append("'");
if (i < size - 1) {
categories.append(",");
}
i++;
}
return categories.append("]").toString();
}
@Override
public String toHighChartsSeries() {
StringBuilder series = new StringBuilder("[");
int i = 0;
int size = bars.size();
for (Bar bar : bars) {
series.append(bar.getValue());
if (i < size - 1) {
series.append(",");
}
i++;
}
return series.append("]").toString();
}
}

View File

@ -0,0 +1,33 @@
package com.djrapitops.plan.utilities.html.graphs.bar;
import com.djrapitops.plan.data.store.mutators.PlayersMutator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GeolocationBarGraph extends BarGraph {
public GeolocationBarGraph(PlayersMutator mutator) {
this(mutator.getGeolocations());
}
public GeolocationBarGraph(List<String> geolocations) {
super(turnToBars(geolocations));
}
private static List<Bar> turnToBars(List<String> geolocations) {
Map<String, Integer> counts = new HashMap<>();
for (String geolocation : geolocations) {
counts.put(geolocation, counts.getOrDefault(geolocation, 0) + 1);
}
return counts.entrySet().stream()
.map(entry -> new Bar(entry.getKey(), entry.getValue()))
.sorted()
.limit(20L)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,40 @@
function horizontalBarChart(id, categories, series, text) {
Highcharts.chart(id, {
chart: {
type: 'bar'
},
title: {
text: ''
},
xAxis: {
categories: categories,
title: {
text: null
}
},
yAxis: {
min: 0,
title: {
text: text,
align: 'high'
},
labels: {
overflow: 'justify'
}
},
legend: {
enabled: false
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
}
},
credits: {
enabled: true
},
series: series
});
}

View File

@ -1044,7 +1044,14 @@
</div>
</div>
<div class="body">
<div id="worldMap" class="dashboard-flot-chart"></div>
<div class="row clearfix">
<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
<div id="countryBarChart" class="dashboard-flot-chart"></div>
</div>
<div class="col-xs-12 col-sm-12 col-md-9 col-lg-9">
<div id="worldMap" class="dashboard-flot-chart"></div>
</div>
</div>
</div>
</div>
</div>
@ -1128,6 +1135,7 @@
<script src="js/charts/worldGraph.js"></script>
<script src="js/charts/worldMap.js"></script>
<script src="js/charts/onlineActivityCalendar.js"></script>
<script src="js/charts/horizontalBarGraph.js"></script>
<!-- Chart Data -->
<script>
@ -1135,7 +1143,7 @@
lang: {noData: "No Data to Display"},
time: {
timezoneOffset: ${timeZone} * 60
}
}
})
// Replaced Variables
@ -1176,7 +1184,9 @@
activityStack: ${activityStackSeries},
activityStackCategories: ${activityStackCategories},
healthIndex: ${healthIndex},
calendar: ${calendarSeries}
calendar: ${calendarSeries},
countryCategories: ${countryCategories},
country: ${countrySeries}
}
};
@ -1335,6 +1345,11 @@
color: v.colors.punchCard,
data: v.data.punchCard
},
country: {
color: '#4CAF50',
name: 'Players',
data: v.data.country
},
activityStack: v.data.activityStack,
activityStackCategories: v.data.activityStackCategories
};
@ -1379,6 +1394,7 @@
punchCard('punchCard', series.punchCard);
healthGauge('healthGauge', [v.data.healthIndex]);
onlineActivityCalendar('#calendar', v.data.calendar, v.values.firstDay);
horizontalBarChart('countryBarChart', v.data.countryCategories, [series.country], 'Players');
${sessionTabGraphViewFunctions}
/**/