Home forums Using CanvasJS Issues retrievng data from MySql

This topic contains 27 replies, has 3 voices, and was last updated by  andres.chandia 4 years ago.

Viewing 13 posts - 16 through 28 (of 28 total)
  • Author
    Posts
  • #7478

    andres.chandia
    Participant

    Sorry, I’m lost now, I don’t know what you mean I should do, you mean PHP echo statements…?

    #7484

    andres.chandia
    Participant

    Could you please give me an exemple of what you mean to see if I can folow you… thanks…

    #7488

    andres.chandia
    Participant

    I’m really sorry my friend, I’m going round in circles, I have been searching and searching but I can not know what you mean with your answer, so I don’t know where to head me to, would you have a little mercy on me and give me a hand, I would thank you for ever.

    #7490

    Sunil Urs
    Keymaster

    Andres,

    Yes, you need to use php echo statements for the same. Currently we are occupied with some other tasks. If you can give us a day or two we can create a simple example for you.


    Sunil Urs

    #7497

    andres.chandia
    Participant

    Of course my friend, take your time, I’ll be waiting for your help….

    #7499

    Anjali
    Participant

    Andres,

    Here is a modified version of our previous example where we generate JSON data using echo statements.

    Below is the sample code

    1. PHP Service to return JSON Data using echo statements:

    <?php
    
    	$conn = new mysqli("127.0.0.1","user","password", "chartDb");
    
    	// Check connection
    	if ($conn->connect_error) {
    		die("Connection failed: " . $conn->connect_error);
    	} else
    	{
    		$data_points = array();
    		
    		$result = mysqli_query($conn, "SELECT * FROM pie_chart");
    
    		while($row = mysqli_fetch_array($result))
    		{        
    			$point = array("label" => $row['Support'] , "y" => $row['yValues']);
    			array_push($data_points, $point);        
    		}
    
    		//echo json_encode($data_points, JSON_NUMERIC_CHECK);
    
    		// JSON data using echo statements
    		
    		$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 "]";	
    	}
    ?>
    

    2. HTML Page to Fetch Data and render Chart

    <html>
    <head>
    <script src="jquery-1.11.1.min.js"></script>
    <script src="canvasjs.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    	var dataPoints = [];
            $.getJSON("data.php", function (result) {
                var chart = new CanvasJS.Chart("chartContainer", {
                    data: [
                        {
                            type: "pie",
                            dataPoints: result
                        }
                    ]
                });
                chart.render();
            });
        });
    </script>
    </head>
    <body>
           <div id="chartContainer" style="width:500px; height: 300px"></div>
    </body>
    </html>

    __
    Anjali

    • This reply was modified 4 years, 1 month ago by  Anjali.
    #7510

    andres.chandia
    Participant

    Sorry for this but I’ve been testing the code and no success, no graphic comes out, here are my files, I think I’ve adapted them well, but maybe I’m doing something wrong…

    tipus_recursos.php

    <?php
    $conn = new mysqli("localhost","dbuser","dbpassword","database");
    
            // Check connection
            if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
            } else
            {
                    $data_points = array();
                    $result = mysqli_query($conn, "SELECT <code>Tipus_1</code> AS 'Tipus', COUNT(*) AS 'Recursos' FROM <code>values</code>");
    
                    while($row = mysqli_fetch_array($result))
                    {
                            $point = array("label" => $row['Recursos'] , "y" => $row['Tipus']);
                            array_push($data_points, $point);
                    }
    
                    //echo json_encode($data_points, JSON_NUMERIC_CHECK);
                    // JSON data using echo statements
                    $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 "]";
            }
    
    ?>

    tipus_recursos.js

    $(document).ready(function () {
            var dataPoints = [];
            $.getJSON("tipus_recursos.php", function (result) {
                var chart = new CanvasJS.Chart("chartContainer2", {
                    data: [
                        {
                            type: "pie",
                            dataPoints: result
                            indexLabel: "#percent"
                        }
                    ]
                });
                chart.render();
            });
        });

    graf_tipus_recursos.php

    <head>
           <script src="jquery.js"></script>
            <script src="canvasjs.js"></script>
            <script src="tipus_recursos.js"></script>
    </head>
    <body>
    <h2>Gràfic per tipus de recursos</h2>
    <div id="chartContainer2" style="width: 1200px; height: 700px;" ></div>
    </body>
    #7511

    andres.chandia
    Participant

    Sorry again, I could do it, I did a merge between original file and the one proposed by you, and I could get it, but I have the encoding issues anyway you can check it here: http://parles.upf.edu/llocs/adljc/grafics/graf_tipus_recursos.php, this is the resulting file:

    tipus_recursos.php

    <?php
    header('Content-Type: application/json');
    $con = mysqli_connect("localhost","dbuser","dbpassword","database");
    // 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>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'] , "y" => $row['Recursos']);
            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);
    ?>
    #7513

    andres.chandia
    Participant

    I have tried:
    mysql_set_charset('utf8',$con);
    and
    header('Content-Type: text/html; charset=utf-8');
    but no success…

    #7535

    andres.chandia
    Participant

    So, any ideas about the encoding stuff?

    #7550

    andres.chandia
    Participant

    It seems there is no possible solution for this, isn’t it?

    #7558

    Anjali
    Participant

    Andres,

    I just got it working by using $conn->set_charset(‘utf8’); before fetching the data. Please try with the below code.

    $con = mysqli_connect("localhost","dbuser","dbpassword","database");
    $conn->set_charset('utf8'); 

    __
    Anjali

    #7569

    andres.chandia
    Participant

    Yes, thanks a lot, that did it….

Viewing 13 posts - 16 through 28 (of 28 total)

You must be logged in to reply to this topic.