Google Line Chart. GB downloaded VS speed of download
by enagu
HTML
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
</head>
<body>
<p>
Toma la hora actual de tu navegador y cada 30 minutos calcula cuantos Gb podria descargar hasta esa hora, teniendo en cuenta que la velocidad es constante.
</p>
<!-- Speed -->
<label for="speed_input">Velocidad de descarga (kb/s)</label>
<input type="number" id="speed_input" name="Velocidad (kb/s)" min="1" value="42">
<br>
<!-- Hours -->
<label for="hours_diff">Margen de horas</label>
<input type="number" id="hours_diff" name="Margen horas" min="1" max="200" value="12">
<br>
<!-- Total GB -->
<label for="hours_diff">o, total de GB a descargar</label>
<input type="number" id="gb_total_input" name="Total GB" min="1" max="1000" value="0">
<i>si lo dejás en 0 solo se tiene en cuanta las horas ingresadas, sino solo se tiene en cuenta los GB a descargar ingresados.</i>
<br>
<!-- Gb init -->
<label for="gb_init">Gb de inicio</label>
<input type="number" id="gb_init" name="Gb" min="0" max="9999" value="0">
<br>
<!-- Buuton calcl -->
<button id="btn_calcular"> Calcular </button>
<div id="chart_div"></div>
</body>
</html>
JavaScript
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawBackgroundColor);
function drawBackgroundColor(input_speed = 42, hours_margin = 12, gb_total_input = -1, gb_init = 0) {
let time = moment();
let intialHours = time.hours();
let finalTime = moment().add(hours_margin, 'hours');
let content = [];
let totalGB = gb_init;
let td_gb = gb_total_input;
const speed = input_speed; // kb/s
const segToDownloadOneMb = (1024 / speed).toFixed(2); //
const secInHour = 3600;
const minutesToAdd = 30;
const diff = minutesToAdd * 60; // diferencia en segundos al tiempo anterior. Calcula cuanto descargo en este lapso
let indx = 0;
// si ingresó un total de gb a descargar, si no
if(td_gb != -1){
while(totalGB <= td_gb){
// segToDownloadOneMb -> MB
// diff -> x MB
if(indx != 0){
let downloaded = diff / segToDownloadOneMb;
totalGB += downloaded / 1024; // convertir a gb
}
let time_format = time.hour() + ":"+time.minutes();
content.push([time_format, totalGB]);
console.log("xd", totalGB);
time.add(minutesToAdd, 'minutes');
indx++;
}
}else{
while(finalTime.diff(time, 'hours') > -1){
// segToDownloadOneMb -> MB
// diff -> x MB
if(indx != 0){
let downloaded = diff / segToDownloadOneMb;
totalGB += downloaded / 1024;
}
let time_format = time.hour() + ":"+time.minutes();
content.push([time_format, totalGB]);
time.add(minutesToAdd, 'minutes');
indx++;
}
}
// Agregar el dia en el cual termina de de descargar
content[content.length-1][0] = time.format("MMM DD").toString() +...