Hi there, any clue yet?
I have compare them, and the only difference I find is in the query file not in the js one…
file any_publicacio.js (the one that correctly shows the labels)
$(document).ready(function () {
$.getJSON("any_publicacio.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer1", {
data: [
{
dataPoints: result
}
]
});
chart.render();
});
});
File tipus_recursos.js this one does not shows the labels
$(document).ready(function () {
$.getJSON("tipus_recursos.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer2", {
data: [
{
type: "pie",
dataPoints: result,
indexLabel: "#percent"
}
]
});
chart.render();
});
});
query of the first one, the one that works, file any_publicacio.php
$data_points = array();
$result = mysqli_query($con, "SELECT <code>Any_publicacio</code>, COUNT(*) AS 'Publicacions' FROM <code>values</code> GROUP BY <code>Any_publicacio</code> DESC");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['Any_publicacio'] , "y" => $row['Publicacions']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
query of the second one, the one that does not shows de labels: tipus_recursos.php
$data_points = array();
$result = mysqli_query($con, "SELECT <code>Tipus_1</code> AS Tipus, COUNT(*) AS 'Recursos' FROM <code>values</code> GROUP BY <code>Tipus_1</code> DESC");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['Tipus_1'] , "y" => $row['Recursos']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
The difference in the query is that in the second one I have to put “AS Tipus” otherwise graphic does not renders
Just in case, i’m adding the table structure
— phpMyAdmin SQL Dump
— version 4.2.6deb1
— http://www.phpmyadmin.net
—
— Servidor: localhost
— Tiempo de generación: 03-11-2014 a las 12:23:46
— Versión del servidor: 5.5.40-0ubuntu1
— Versión de PHP: 5.5.12-2ubuntu4.1
SET SQL_MODE = “NO_AUTO_VALUE_ON_ZERO”;
SET time_zone = “+00:00”;
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
—
— Base de datos: database
—
— ——————————————————–
—
— Estructura de tabla para la tabla values
—
CREATE TABLE IF NOT EXISTS values
(
Id
int(6) NOT NULL,
Titol
varchar(500) DEFAULT NULL,
Autor
varchar(200) DEFAULT NULL,
Any_publicacio
int(4) NOT NULL,
Pub_prod
varchar(200) DEFAULT NULL,
Lloc_publicacio
varchar(200) DEFAULT NULL,
Suport
varchar(200) DEFAULT NULL,
Consulta_linia
varchar(10) NOT NULL,
D_actualitzacio
varchar(10) NOT NULL,
Url
varchar(500) NOT NULL,
Tipus_1
set(‘lingüístic’,’jurídic’) NOT NULL,
Tipus_2
varchar(1000) NOT NULL,
Tipus_3
varchar(1000) NOT NULL,
P_clau
varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=132 ;
—
— Índices para tablas volcadas
—
—
— Indices de la tabla values
—
ALTER TABLE values
ADD PRIMARY KEY (Id
), ADD UNIQUE KEY id
(Id
);
—
— AUTO_INCREMENT de las tablas volcadas
—
—
— AUTO_INCREMENT de la tabla values
—
ALTER TABLE values
MODIFY Id
int(6) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=132;
/*!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 */;
Thanks Anjali,
I have tried before with another graphic generator http://parles.upf.edu/llocs/adljc/grafics.old/ and if you check those you will see that x axis labels are generated, not pretty well, but the labels are there because the data is actually there.
At the pie graphic you will see the labels have encoding issues, but they are there, could this be the reason for canvas not showing labels, my data is in utf-8 and words have accents…
Queries are the same, it’s only the way of generating the graphic that is different, it’s a php library: http://www.ebrueggeman.com/phpgraphlib
Actually, both graphics (phplib and canvasjs) are currently generated form the exact same data, I preffer canvas because it is visually better than the other.
So why could be json showing as null when the fields are actually not.
No luck, it renders in the same way, without the lables…
here is the query:
$data_points = array();
$result = mysqli_query($con, "SELECT Tipus_1 AS Tipus, COUNT(*) AS 'Count' FROM values GROUP BY Tipus_1 ORDER BY Count");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['Tipus_1'] , "y" => $row['Count']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
here is the js file:
var dataPoints = [];
$.getJSON("tipus_recursos.php", function (result) {
for(var i = 0; i <= result.length-1; i++) {
dataPoints.push({label: result[i].Tipus_1, y: parseInt(result[i].y)});
}
var chart = new CanvasJS.Chart("chartContainer2", {
data: [
{
type: "pie",
dataPoints: dataPoints
}
]
});
chart.render();
});
Thanks for answering, but I only see parameters for static data, how do I apply this to dynamic data, I mean, retrieved from mysql dtabase.
and thanks again man.