<canvas width="600" height="400"></canvas>
<script>
let serie2009 = [6, 47, 41, 3, 3];
let serie2019 = [81, 9, 3, 3, 4];
let colores = ["blue", "green", "yellow", "red", "gray"];
dibujarBarra(50, 50, serie2009, colores, "2009");
dibujarBarra(150, 50, serie2019, colores, "2019");
//FUNCIONES
function dibujarBarra(x, y, altura, color, texto) {
let pantalla = document.querySelector("canvas");
let pincel = pantalla.getContext("2d");
let acomulableY = 0;
for (let i = 0; i < altura.length; i++) {
pincel.fillStyle = color[i];
pincel.fillRect(x, y + acomulableY, 40, altura[i]);
pincel.strokeStyle = "black";
pincel.strokeRect(x, y + acomulableY, 40, altura[i]);
acomulableY = acomulableY + altura[i];
}
escribirTexto((x+5), y-5, texto);
}
function escribirTexto(x, y, texto) {
let pantalla = document.querySelector("canvas");
let pincel = pantalla.getContext("2d");
pincel.font = "15px Georgia";
pincel.fillStyle = "black";
pincel.fillText(texto, x, y);
}
</script>