grafica chartjs con ajax y mysql
Publicado por maikol (1 intervención) el 07/02/2020 15:00:34
hola compañer@s tengo un problema con mi codigo y no doy con la solucion.
estoy intentando hacer una consulta de ajax a mysql para unos datos de una grafica, pero no retorna resultados y no se que hice mal
Codigo html y ajax:
Codigo php a mysql:
Base de datos: tienda
Base de datos: `tienda`
por favor demen una manito
estoy intentando hacer una consulta de ajax a mysql para unos datos de una grafica, pero no retorna resultados y no se que hice mal
Codigo html y ajax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="../vendor/jquery/jquery-3.4.1.js"></script>
<script src="Chart.js/dist/Chart.min.js"></script>
</head>
<body>
<select id="ano" onchange="mostrarResultados();" >
<option >2014</option>
<option>2015</option>
</select>
<div style="width: 500px;height: 600px;">
<canvas height="200" id="mybarChart"></canvas>
</div>
<script >
function mostrarResultados(){
cadena="año=" + año;
$.ajax({
type:"POST",
url:"controlador/procesar.php",
data:cadena,
success:function(data){
var valores = data;
var en = valores[0];
var fe = valores[1];
var ma = valores[2];
var ab = valores[3];
var may = valores[4];
var jun = valores[5];
var ju = valores[6];
var ag = valores[7];
var se = valores[8];
var oc = valores[9];
var no = valores[10];
var di = valores[11];
}
});
return false;
}
</script>
<script >
var ctx = document.getElementById("mybarChart");
var mybarChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Jjulio"],
datasets: [{
label: '# of Votes',
backgroundColor: "#26B99A",
data: [e, 30, 40, 28, 92, 50, 45]
}, {
label: '# of Votes',
backgroundColor: "#03586A",
data: [41, 56, 25, 48, 72, 34, 12]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>
</script>
</body>
</html>
Codigo php a mysql:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
function conexion(){
$servidor="localhost";
$usuario="root";
$contrasena="";
$bd="tienda";
$conexion=mysqli_connect($servidor,$usuario,$contrasena,$bd);
return $conexion;
}
$conexion=conexion();
$año=$_POST['año'];
$sql0="select sum(monto_venta) as r from ventas where month(fecha_venta) = 1 and year(fecha_venta) = '$año'";
$sql2="select sum(monto_venta) as r from ventas where month(fecha_venta) = 2 and year(fecha_venta) = '$año'";
$sql3="select sum(monto_venta) as r from ventas where month(fecha_venta) = 3 and year(fecha_venta) = '$año'";
$sql4="select sum(monto_venta) as r from ventas where month(fecha_venta) = 4 and year(fecha_venta) = '$año'";
$sql5="select sum(monto_venta) as r from ventas where month(fecha_venta) = 5 and year(fecha_venta) = '$año'";
$sql6="select sum(monto_venta) as r from ventas where month(fecha_venta) = 6 and year(fecha_venta) = '$año'";
$sql7="select sum(monto_venta) as r from ventas where month(fecha_venta) = 7 and year(fecha_venta) = '$año'";
$sql8="select sum(monto_venta) as r from ventas where month(fecha_venta) = 8 and year(fecha_venta) = '$año'";
$sql9="select sum(monto_venta) as r from ventas where month(fecha_venta) = 9 and year(fecha_venta) = '$año'";
$sql10="select sum(monto_venta) as r from ventas where month(fecha_venta) = 10 and year(fecha_venta) = '$año'";
$sql11="select sum(monto_venta) as r from ventas where month(fecha_venta) = 11 and year(fecha_venta) = '$año'";
$sql12="select sum(monto_venta) as r from ventas where month(fecha_venta) = 12 and year(fecha_venta) = '$año'";
$result=mysqli_query($conexion,$sql0);
$result1=mysqli_query($conexion,$sql2);
$result2=mysqli_query($conexion,$sql3);
$result3=mysqli_query($conexion,$sql4);
$result4=mysqli_query($conexion,$sql5);
$result5=mysqli_query($conexion,$sql6);
$result6=mysqli_query($conexion,$sql7);
$result7=mysqli_query($conexion,$sql8);
$result8=mysqli_query($conexion,$sql9);
$result9=mysqli_query($conexion,$sql10);
$result10=mysqli_query($conexion,$sql11);
$result11=mysqli_query($conexion,$sql12);
while ($resultado=mysqli_fetch_array($result)) {
$enero=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result1)) {
$febrero=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result2)) {
$marzo=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result3)) {
$abril=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result4)) {
$mayo=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result5)) {
$junio=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result6)) {
$julio=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result7)) {
$agosto=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result8)) {
$septiembre=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result9)) {
$octubre=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result10)) {
$noviembre=$resultado['r'];
}
while ($resultado=mysqli_fetch_array($result11)) {
$diciembre=$resultado['r'];
}
$data = array(0 => round($enero,1),
1 => round($febrero,1),
2 => round($marzo,1),
3 => round($abril,1),
4 => round($mayo,1),
5 => round($junio,1),
6 => round($julio,1),
7 => round($agosto,1),
8 => round($septiembre,1),
9 => round($octubre,1),
10 => round($noviembre,1),
11 => round($diciembre,1),
);
echo $data;
?>
Base de datos: tienda
Base de datos: `tienda`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `ventas`
--
DROP TABLE IF EXISTS `ventas`;
CREATE TABLE IF NOT EXISTS `ventas` (
`id_venta` int(11) NOT NULL,
`fecha_venta` date NOT NULL,
`monto_venta` float NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `ventas`
--
INSERT INTO `ventas` (`id_venta`, `fecha_venta`, `monto_venta`) VALUES
(1, '2014-01-15', 15000),
(2, '2014-02-13', 10000),
(3, '2014-03-11', 12000),
(4, '2014-04-22', 13000),
(5, '2014-05-22', 30000),
(6, '2014-06-14', 11500),
(7, '2014-07-30', 10200),
(8, '2014-08-16', 15000),
(9, '2014-09-23', 10000),
(10, '2014-10-02', 12000),
(11, '2014-11-20', 13000),
(12, '2015-01-20', 30000),
(13, '2015-03-11', 11500),
(14, '2014-11-20', 10200),
(15, '2015-01-20', 9500);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
por favor demen una manito
Valora esta pregunta


0