0

The following set of codes are used to generate a chart using fusionchart javascript library! This is the php script

   <?php
//address of the server where db is installed
$servername = "localhost";

//username to connect to the db
//the default value is root
$username = "chart";

//password to connect to the db
//this is the value you would have specified during installation of WAMP stack
$password = "L12345d";

//name of the db under which the table is created
$dbName = "test";

//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);

//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

//the SQL query to be executed
$query = "SELECT * FROM top_odi_wicket_takers";

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
  //Converting the results into an associative array
  while($row = $result->fetch_assoc()) {
    $jsonArrayItem = array();
    $jsonArrayItem['label'] = $row['player'];
    $jsonArrayItem['value'] = $row['wickets'];
    //append the above created object into the main array.
    array_push($jsonArray, $jsonArrayItem);
  }
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function. 
echo json_encode($jsonArray);
?>

This is the json script

$(function() {
    $.ajax({

       url: 'http://localhost/GP/Charts/chart_data.php',
        type: 'GET',
        success: function(data) {
            chartData = data;
            var chartProperties = {
                "caption": "Top 10 wicket takes ODI Cricket in 2015",
                "xAxisName": "Player",
                "yAxisName": "Wickets Taken",
                "rotatevalues": "1",
                "theme": "zune"
            };

            apiChart = new FusionCharts({
                type: 'column2d',
                renderAt: 'chart-container',
                width: '550',
                height: '350',
                dataFormat: 'json',
                dataSource: {
                    "chart": chartProperties,
                    "data": chartData
                }
            });
            apiChart.render();
        }
    });
});

The follwoing code gives the html code

<!DOCTYPE html>
<html>
<head>
  <title>Fusion Charts Column 2D Sample</title>
</head>
<body>
  <div id="chart-container">FusionCharts will render here</div>
  <script src="js/jquery-2.1.4.js"></script>
  <script src="js/fusioncharts.js"></script>
  <script src="js/fusioncharts.charts.js"></script>
  <script src="js/themes/fusioncharts.theme.zune.js"></script>
  <script src="js/app.js"></script>
</body>
</html>

According to the tutorial i followed it should generate the chart when the html code is executed! But when its executed no chart apperas but only a text stating "FusionCharts will render here" appears how can i correct the code inorder to generate the chart? i followed this tutorial exaclty http://www.fusioncharts.com/dev/using-with-server-side-languages/tutorials/php-mysql-charts.html

3 Answers 3

1

I guess you have not installed the jquery properly. Click here to download jquery and copy that under the above created js folder.

Sign up to request clarification or add additional context in comments.

Comments

0
require('http://localhost/GP/Charts/chart_data.php'')

should be

require('http://localhost/GP/Charts/chart_data.php')

6 Comments

sorry according to the tutorial it should be url: 'localhost/GP/Charts/chart_data.php' but still is not working!
what do you see when you go to localhost/GP/Charts/chart_data.php page with your browser?
a text "fusion charts will render here" appears on the top
I am not asking about your main html file. I am asking about your chart_data.php. Is it giving you json?
yep! [{"label":"CJ Anderson","value":"25"},{"label":"Imran Tahir","value":"25"},{"label":"JH Davey","value":"21"},{"label":"M Morkel","value":"21"},{"label":"MA Starc","value":"34"},{"label":"ST Finn","value":"27"},{"label":"TA Boult","value":"36"},{"label":"TG Southee","value":"28"},{"label":"UT Yadav","value":"22"},{"label":"Wahab Riaz","value":"25"}]
|
0

i think u should keep all the script files inside the head tag so that they load before the dom(div) loads..give it a try

2 Comments

are you sure that the http call is returning some data..? try to console the reponse and check.. in this way we can find out that is it a backend problem or a frontend
no data! only the "FusionCharts will render here" text appears

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.