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 "]";
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
Yes, that did it, thanks a lot.
Yes, thanks a lot, that did it….
It seems there is no possible solution for this, isn’t it?
So, any ideas about the encoding stuff?
I have tried: mysql_set_charset('utf8',$con); and header('Content-Type: text/html; charset=utf-8'); but no success…
mysql_set_charset('utf8',$con);
header('Content-Type: text/html; charset=utf-8');
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); ?>
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…
<?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>
Of course my friend, take your time, I’ll be waiting for your help….
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.
Could you please give me an exemple of what you mean to see if I can folow you… thanks…
Sorry, I’m lost now, I don’t know what you mean I should do, you mean PHP echo statements…?
In case json_encode has some issue encoding the data, try creating the entire json data using echo statements and string concatination. If you can get those values to show up in JSON data chart will automatically render them.
Sorry but that sounds like chinese to me….
Ok, at this file: http://parles.upf.edu/llocs/adljc/grafics/graf_tipus_recursos.php I have put this query: SELECTTipus_1AS 'Tipus', COUNT(*) AS 'Recursos' FROMvaluesGROUP BYTipus_1DESC
SELECT
AS 'Tipus', COUNT(*) AS 'Recursos' FROM
GROUP BY
DESC
You can see the resulting table under the graphic.
The complete code for rendering the table is this one:
<?php include ("../admin/dbconnect.php"); $query= "SELECT <code>Tipus_1</code> AS 'Tipus', COUNT(*) AS 'Recursos' FROM <code>values</code> GROUP BY <code>Tipus_1</code> DESC"; $result = mysql_query($query); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<table class='sortable'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td align=center>".strtr(" {$field->name} ", $transname)."</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr onMouseOver=this.bgColor='#e0e0e7'; onMouseOut=this.bgColor='white';>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td align=center>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?>
2. Type of data: Tipus_1 set(‘lingüístic’,’jurídic’) NOT NULL,