Server Preference Pie creation

This commit is contained in:
Risto Lahtela 2017-09-25 10:46:37 +03:00
parent 403c17e681
commit 786b8045ef
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package main.java.com.djrapitops.plan.utilities.html.graphs;
import java.util.Map;
public class ServerPreferencePieCreator {
private ServerPreferencePieCreator() {
throw new IllegalStateException("Utility Class");
}
public static String createSeriesData(Map<String, Long> serverPlaytimes) {
StringBuilder seriesBuilder = new StringBuilder("[");
int i = 0;
int size = serverPlaytimes.size();
for (Map.Entry<String, Long> server : serverPlaytimes.entrySet()) {
String serverName = server.getKey();
seriesBuilder.append("{name:'").append(serverName)
.append("',y:").append(server.getValue()).append("'");
seriesBuilder.append("}");
if (i < size - 1) {
seriesBuilder.append(",");
}
i++;
}
seriesBuilder.append("]");
return seriesBuilder.toString();
}
}

View File

@ -0,0 +1,53 @@
function serverPie(id, serverSeries) {
Highcharts.chart(id, {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {text: ''},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
tooltip: {
formatter: function() {
return '<b>'+this.point.name+':</b> ' + formatTimeAmount(this.y) + ' ('+this.percentage.toFixed(2)+'%)';
}
},
series: [serverSeries]
});
}
function formatTimeAmount(ms) {
var out = "";
var seconds = Math.floor(ms / 1000);
var dd = Math.floor(seconds / 86400);
seconds -= (dd * 86400);
var dh = Math.floor(seconds / 3600);
seconds -= (dh * 3600);
var dm = Math.floor(seconds / 60);
seconds -= (dm * 60);
seconds = Math.floor(seconds);
if (dd !== 0) {
out += dd.toString() + "d ";
}
if (dh !== 0) {
out += dh.toString() + "h ";
}
if (dm !== 0) {
out += dm.toString() + "m ";
}
out += seconds.toString() + "s ";
return out;
}