| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <template>
- <canvas id="graph"></canvas>
- </template>
- <script>
- var generateTemplate = function(){
- var rand = function(){
- return Math.floor(Math.random()*256);
- }
- var color = "rgba("+rand()+","+rand()+","+rand()+",0.1)";
- // console.log(color);
- return {
- label: "Traffic",
- fill: true,
- lineTension: 0.1,
- backgroundColor: "rgba(75,192,192,0.4)",
- borderColor: color,
- borderCapStyle: 'butt',
- borderDash: [],
- borderDashOffset: 0.0,
- borderJoinStyle: 'miter',
- pointBorderColor: color, //"rgba(75,192,192,1)",
- pointBackgroundColor: "#fff",
- // pointBorderWidth: 1,
- // pointHoverRadius: 5,
- // pointHoverBackgroundColor: "rgba(75,192,192,1)",
- // pointHoverBorderColor: "rgba(220,220,220,1)",
- // pointHoverBorderWidth: 2,
- pointRadius: 1,
- pointHitRadius: 10,
- data: [],
- spanGaps: true,
- };
- };
- import Chart from "chart.js";
- export default {
- created: function () {
- this.updateData();
- },
- mounted: function () {
- //this.update();
- },
- props: {
- server: {
- type: Number,
- default:-1
- },
- maxplayers: {
- type: Number,
- default:-1
- }
- },
- data: function () {
- return {
- chart: {},
- // labels: {},
- stamp: null
- // datasetTempalte:{
- // label: "Traffic",
- // fill: true,
- // // lineTension: 0.1,
- // // backgroundColor: "rgba(75,192,192,0.4)",
- // // borderColor: "rgba(75,192,192,1)",
- // // borderCapStyle: 'butt',
- // // borderDash: [],
- // // borderDashOffset: 0.0,
- // // borderJoinStyle: 'miter',
- // // pointBorderColor: "rgba(75,192,192,1)",
- // // pointBackgroundColor: "#fff",
- // // pointBorderWidth: 1,
- // // pointHoverRadius: 5,
- // // pointHoverBackgroundColor: "rgba(75,192,192,1)",
- // // pointHoverBorderColor: "rgba(220,220,220,1)",
- // // pointHoverBorderWidth: 2,
- // // pointRadius: 1,
- // // pointHitRadius: 10,
- // data: [],
- // // spanGaps: false,
- // },
- }
- },
- methods: {
- initData: function () {
- this.params = {
- type: 'line',
- data: {},
- options: {
- responsive: true,
- legend:{
- display: false
- },
- scales: {
- yAxes: [{
- ticks: {
- beginAtZero:true
- }
- }],
- xAxes: [{
- display: true,
- type: 'time',
- time: {
- tooltipFormat: "H:mm:ss",
- displayFormat: "H:mm:ss"
- },
- position: 'bottom'
- }]
- }
- }
- };
- },
- update: function () {
- if(this.maxplayers > 0){
- this.params.options.scales.yAxes[0].ticks.max = parseInt(this.maxplayers);
- }else if(this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers){
- this.params.options.scales.yAxes[0].ticks.max = this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers;
- }
- this.params.data = this.graphdata;
- this.chart = new Chart(this.$el, this.params);
- },
- updateData: function () {
- this.initData();
- this.graphdata = {
- labels: [],
- datasets: [
- {
- label: "Traffic",
- data: null
- }
- ]
- };
- if(this.server === -1){
- return -1;
- }
- var self = this;
- var date = new Date();
- var year = date.getUTCFullYear();
- var month = date.getUTCMonth() + 1;
- var day = date.getUTCDate();
- var url = '/api/trafic/';
- url += this.server;
- url += "/";
- url += day;
- url += "/";
- url += month;
- url += "/";
- url += year;
- url += "/day";
- // console.log(url);
- $.getJSON(url)
- .done(function (obj) {
- if(obj.length <= 0){
- return -1;
- }
- console.log(self);
- self.graphdata.datasets[0].data = [];
- self.graphdata.datasets = [];
- var firstStamp = new Date(obj[0].createdAt);
- self.params.options.scales.xAxes[0].time.min = firstStamp;
- self.stamp = firstStamp;
- var dataset = {
- label: "Traffic",
- data: [],
- labels: []
- };
- // console.log(dataset);
- dataset = Object.assign({}, generateTemplate(), dataset)
- var lastStamp = firstStamp/1000;
- var gap = firstStamp/1000 - new Date(obj[1].createdAt)/1000;
- gap = Math.abs(Math.floor(gap));
- obj.forEach(function (el) {
- var stamp = new Date(el.createdAt) - firstStamp;
- stamp /= 1000;
- // console.log("Gap: ", gap);
- // console.log("Stamp: ", stamp);
- // console.log("lastStamp: ", lastStamp);
- // console.log(stamp-lastStamp);
- if(stamp-lastStamp > 2*gap){
- // console.log("Dataset ", dataset);
- self.graphdata.datasets.push(dataset);
- dataset = {
- label: "Traffic",
- data: [],
- labels: []
- };
- dataset = Object.assign({}, generateTemplate(), dataset)
- }
- lastStamp = stamp;
- stamp /= 60;
- stamp = Math.floor(stamp);
- stamp = new Date(el.createdAt);
- self.params.options.scales.xAxes[0].time.max = stamp;
- dataset.data.push({
- x: stamp,
- y: el.players
- });
- // self.graphdata.datasets[0].data.push({
- // x: stamp,
- // y: el.players
- // });
- // dataset.labels.push(new Date(el.createdAt).toLocaleTimeString());
- //self.labels[stamp] = new Date(el.createdAt).toLocaleTimeString();
- });
- // console.log("Dataset ", dataset);
- // self.graphdata.datasets.push(dataset);
- self.graphdata.datasets.push(dataset);
- self.graphdata = JSON.parse(JSON.stringify(self.graphdata));
- // console.log("Graphdata ", self.graphdata);
- self.update();
- });
- }
- }
- }
- </script>
|