mirror of
https://github.com/billz/raspap-webgui.git
synced 2023-10-10 13:37:24 +02:00
Migrate morris chart to chartjs
This commit is contained in:
parent
185115561a
commit
bc07a78559
@ -2,15 +2,44 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Morris.js barchart.
|
* Create a Chart.js barchart.
|
||||||
*/
|
*/
|
||||||
function CreateBarChart(placeholder, datasizeunits) {
|
function CreateChart(ctx, labels) {
|
||||||
var barchart = new Morris.Bar({
|
var barchart = new Chart(ctx,{
|
||||||
element: placeholder,
|
type: 'line',
|
||||||
xkey: 'date',
|
options: {
|
||||||
ykeys: ['rx', 'tx'],
|
responsive: true,
|
||||||
labels: [_t['receive']+' '+datasizeunits.toUpperCase(),
|
scales: {
|
||||||
_t['send']+' '+datasizeunits.toUpperCase()]
|
xAxes: [{
|
||||||
|
scaleLabel: {
|
||||||
|
display: true,
|
||||||
|
labelString: 'date',
|
||||||
|
},
|
||||||
|
ticks: {
|
||||||
|
maxRotation: 90,
|
||||||
|
minRotation: 80
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
yAxes: [{
|
||||||
|
id: 'y-axis-1',
|
||||||
|
type: 'linear',
|
||||||
|
display: true,
|
||||||
|
position: 'left',
|
||||||
|
//},{
|
||||||
|
// id: 'y-axis-2',
|
||||||
|
// type: 'linear',
|
||||||
|
// display: true,
|
||||||
|
// position: 'right',
|
||||||
|
ticks: {
|
||||||
|
beginAtZero: true
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
labels: labels,
|
||||||
|
datasets: []
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return barchart;
|
return barchart;
|
||||||
@ -21,7 +50,7 @@
|
|||||||
*/
|
*/
|
||||||
function CreateDataTable(placeholder, timeunits) {
|
function CreateDataTable(placeholder, timeunits) {
|
||||||
$("#"+placeholder).append('<table id="tableBandwidth'+timeunits+
|
$("#"+placeholder).append('<table id="tableBandwidth'+timeunits+
|
||||||
'" class="table table-responsive table-striped container-fluid"><thead>'+
|
'" class="table table-striped"><thead>'+
|
||||||
'<tr><th>date</th><th>rx</th><th>tx</th></tr></thead><tbody></tbody></table>');
|
'<tr><th>date</th><th>rx</th><th>tx</th></tr></thead><tbody></tbody></table>');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +59,7 @@
|
|||||||
* construct the proper barchart.
|
* construct the proper barchart.
|
||||||
*/
|
*/
|
||||||
function ShowBandwidthChartHandler(e) {
|
function ShowBandwidthChartHandler(e) {
|
||||||
// Remove all morrisjs charts
|
// Remove all chartjs charts
|
||||||
$('#divChartBandwidthhourly').empty();
|
$('#divChartBandwidthhourly').empty();
|
||||||
$('#divChartBandwidthdaily').empty();
|
$('#divChartBandwidthdaily').empty();
|
||||||
$('#divChartBandwidthmonthly').empty();
|
$('#divChartBandwidthmonthly').empty();
|
||||||
@ -39,7 +68,7 @@
|
|||||||
$('#divTableBandwidthdaily').empty();
|
$('#divTableBandwidthdaily').empty();
|
||||||
$('#divTableBandwidthmonthly').empty();
|
$('#divTableBandwidthmonthly').empty();
|
||||||
// Construct ajax uri for getting the proper data.
|
// Construct ajax uri for getting the proper data.
|
||||||
var timeunit = $('ul#tabbarBandwidth li.active a').attr('href').substr(1);
|
var timeunit = $('ul#tabbarBandwidth li.nav-item a.nav-link.active').attr('href').substr(1);
|
||||||
var uri = 'ajax/bandwidth/get_bandwidth.php?';
|
var uri = 'ajax/bandwidth/get_bandwidth.php?';
|
||||||
uri += 'inet=';
|
uri += 'inet=';
|
||||||
uri += encodeURIComponent($('#cbxInterface'+timeunit+' option:selected').text());
|
uri += encodeURIComponent($('#cbxInterface'+timeunit+' option:selected').text());
|
||||||
@ -47,8 +76,6 @@
|
|||||||
uri += encodeURIComponent(timeunit.substr(0, 1));
|
uri += encodeURIComponent(timeunit.substr(0, 1));
|
||||||
var datasizeunits = 'mb';
|
var datasizeunits = 'mb';
|
||||||
uri += '&dsu='+encodeURIComponent(datasizeunits);
|
uri += '&dsu='+encodeURIComponent(datasizeunits);
|
||||||
// Init. chart
|
|
||||||
var barchart = CreateBarChart('divChartBandwidth'+timeunit, datasizeunits);
|
|
||||||
// Init. datatable html
|
// Init. datatable html
|
||||||
var datatable = CreateDataTable('divTableBandwidth'+timeunit, timeunit);
|
var datatable = CreateDataTable('divTableBandwidth'+timeunit, timeunit);
|
||||||
// Get data for chart
|
// Get data for chart
|
||||||
@ -56,11 +83,24 @@
|
|||||||
url: uri,
|
url: uri,
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
beforeSend: function() {
|
beforeSend: function() {
|
||||||
$('#divLoaderBandwidth'+timeunit).removeClass('hidden');
|
$('#divLoaderBandwidth'+timeunit).show();
|
||||||
}
|
}
|
||||||
}).done(function(jsondata) {
|
}).done(function(jsondata) {
|
||||||
$('#divLoaderBandwidth'+timeunit).addClass('hidden');
|
$('#divLoaderBandwidth'+timeunit).hide();
|
||||||
barchart.setData(jsondata);
|
// Map json values to label array
|
||||||
|
var labels = jsondata.map(function(e) {
|
||||||
|
return e.date;
|
||||||
|
});
|
||||||
|
// Init. chart with label series
|
||||||
|
var barchart = CreateChart('divChartBandwidth'+timeunit, labels);
|
||||||
|
var dataRx = jsondata.map(function(e) {
|
||||||
|
return e.rx;
|
||||||
|
});
|
||||||
|
var dataTx = jsondata.map(function(e) {
|
||||||
|
return e.tx;
|
||||||
|
});
|
||||||
|
|
||||||
|
addData(barchart, dataRx, dataTx, datasizeunits);
|
||||||
$('#tableBandwidth'+timeunit).DataTable({
|
$('#tableBandwidth'+timeunit).DataTable({
|
||||||
'searching': false,
|
'searching': false,
|
||||||
'paging': false,
|
'paging': false,
|
||||||
@ -79,6 +119,26 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Add data array to datasets of current chart.
|
||||||
|
*/
|
||||||
|
function addData(chart, dataRx, dataTx, datasizeunits) {
|
||||||
|
chart.data.datasets.push({
|
||||||
|
label: 'Receive'+' '+datasizeunits.toUpperCase(),
|
||||||
|
yAxisID: 'y-axis-1',
|
||||||
|
borderColor: 'rgba(75, 192, 192, 1)',
|
||||||
|
backgroundColor: 'rgba(75, 192, 192, 0.2)',
|
||||||
|
data: dataRx
|
||||||
|
});
|
||||||
|
chart.data.datasets.push({
|
||||||
|
label: 'Send'+' '+datasizeunits.toUpperCase(),
|
||||||
|
yAxisID: 'y-axis-1',
|
||||||
|
borderColor: 'rgba(192, 192, 192, 1)',
|
||||||
|
backgroundColor: 'rgba(192, 192, 192, 0.2)',
|
||||||
|
data: dataTx
|
||||||
|
});
|
||||||
|
chart.update();
|
||||||
|
}
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
$('#tabbarBandwidth a[data-toggle="tab"]').on('shown.bs.tab', ShowBandwidthChartHandler);
|
$('#tabbarBandwidth a[data-toggle="tab"]').on('shown.bs.tab', ShowBandwidthChartHandler);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user