You must be logged in to post your query.
Home › Forums › Chart Support › Legends showing only DataPoint
Tagged: legend names db
Hi there again, I have a pie graphic to which I have added legends but on legends I can only get “DataPoint” and not the proper legend name, see graphic, thanks.
js code:
$(document).ready(function () {
$.getJSON("materies.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer3", {
data: [
{
type: "pie",
dataPoints: result,
indexLabel: "#percent %",
showInLegend: true
}
]
});
chart.render();
});
});
php code:
<?php
header('Content-Type: application/json');
header('Content-Type: text/html; charset=utf-8');
$con = mysqli_connect("localhost","user","password","database");
$con->set_charset('utf8');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}else
{
$data_points = array();
$result = mysqli_query($con, "SELECT <code>Descriptors</code>, COUNT(*) AS 'Count' FROM <code>values</code> GROUP BY <code>Descriptors</code>");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['Descriptors'] , "y" => $row['Count']);
array_push($data_points, $point);
}
// echo json_encode($data_points, JSON_NUMERIC_CHECK);
$length = sizeof($data_points);
echo "[";
for ( $i = 0; $i <= $length-1; $i++) {
echo "{ \"label\": \"" , $data_points[$i]['label'],"\", \"y\": " , $data_points[$i]['y'], "}";
if( $i < $length-1)
echo ",";
}
echo "]";
}
mysqli_close($con);
?>
Thanks Anjali, but again I have the same problem, the exemples show how to do it with static data, and not with data retreived from databases, I’ve been trying to reuse this code at the php file:
{
$point = array("legend" => $row['Descriptors'] , "y" => $row['Count']);
array_push($data_points, $point);
}
$length = sizeof($data_points);
echo "[";
for ( $i = 0; $i <= $length-1; $i++) {
echo "{ \"legend\": \"" , $data_points[$i]['legend'],"\", \"y\": " , $data_points[$i]['y'], "}";
if( $i < $length-1)
echo ",";
}
echo "]";
but besides that I’m not sure that this is the way, I have no success with it…, thanks again
andres,
While echoing a JSON data you are using \"legend\": \"" , $data_points[$i]['legend'],
but the property is legendText so it will be \"legendText\": \"" , $data_points[$i]['legend'],
.
__
Anjali
Sorry Anjali, I can not make it work, I only get a blank page, here is the code as I adapted:
//labels
{
$point = array("label" => $row['Descriptors'] , "y" => $row['Count']);
array_push($data_points, $point);
}
$length = sizeof($data_points);
echo "[";
for ( $i = 0; $i <= $length-1; $i++) {
echo "{ \"label\": \"" , $data_points[$i]['label'],"\", \"y\": " , $data_points[$i]['y'], "}";
if( $i < $length-1)
echo ",";
}
echo "]";
//legends
{
$pointB = array("legend" => $row['Descriptors'] , "y" => $row['Count']);
array_push($data_points, $pointB);
}
$lengthB = sizeof($data_points);
echo "[";
for ( $i = 0; $i <= $lengthB-1; $i++) {
echo "{ \"legendText\": \"" , $data_points[$i]['legend'],"\", \"y\": " , $data_points[$i]['y'], "}";
if( $i < $lengthB-1)
echo ",";
}
echo "]";
andres,
You are echoing data twice for creating JSON, instead of this echo at once and include all required properties within it. For doing the same please follow the below code:
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['Support'] , "y" => $row['yValues'], "legendText" => $row['Descriptors']);
array_push($data_points, $point);
}
$length = sizeof($data_points);
echo "[";
for ( $i = 0; $i <= $length-1; $i++) {
echo "{ \"label\": \"" , $data_points[$i]['label'],"\", \"y\": ", $data_points[$i]['y'], ",
\"legendText\": \"" , $data_points[$i]['legendText'], "\" }";
if( $i < $length-1)
echo ",";
}
echo "]";
__
Anjali
Tagged: legend names db
You must be logged in to reply to this topic.