Home forums Using CanvasJS Legends showing only DataPoint

This topic contains 5 replies, has 2 voices, and was last updated by  Anjali 1 year, 11 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #7621

    andres.chandia
    Participant

    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);
    ?>
    #7623

    Anjali
    Participant

    andres,

    Please use legendText property of dataSeries/ dataPoints.

    __
    Anjali

    #7624

    andres.chandia
    Participant

    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

    #7631

    Anjali
    Participant

    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

    #7633

    andres.chandia
    Participant

    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 "]";
    #7635

    Anjali
    Participant

    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

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.