Tony
Tony

Reputation: 83

"Loading Data" Stuck on AMChart

Hello,

I am using the AMCharts framework to make charts from data in MySQL database. I get stuck with "Loading Data" instead of an actual chart. (http://gyazo.com/b72693484ab39e2635c0a0ab21c889a5)

And no, it's not actually loading the data. I went to launch and came back an hour later and it's yet to be loaded. When I used the data the AMCharts website provided, it worked just fine, but with my own data, no such luck.

Also, I have checked this link and it isn't answering my question. So this question shouldn't be a duplicate.

The Data :

For my data, I am using a section of Starbucks stock close prices for 100 dates in 2007. It's basically test data before I start the real part of the project. Just to get things rolling. Originally I started with 2100 rows, but when I first got the "Loading Data" message, I cut down my data to a simple 100 rows. But still, no such luck.

If you'd like to get the data I used, the way I got it, here is the R code I used.

require('quantmod')
getSymbols("SBUX")
starbucks <- data.frame(SBUX)
starbucks[,7] <- row.names(starbucks)
starbucks <- data.frame(starbucks[,c(7,6)])
row.names(starbucks) <- NULL
colnames(starbucks) <- c("Dates","Values")
starbucks <- data.frame(starbucks[1:100,])
write.table(starbucks, file="path\\to\\file\\starbucks.csv", sep=",")

Upload:

I created a new database called "charts" and under it made a table named "starbucks". There were two columns under starbucks named "Dates" (set as Date) and "Values" (set as float) each given a length of 10.

I then went to import and uploaded the CSV to this table and all imported well.

PHP

This is the code I used for the PHP side of things.

<?php
// Connect to MySQL
$link = mysql_connect( 'localhost', 'root', '' );
if ( !$link ) {
  die( 'Could not connect: ' . mysql_error() );
}

// Select the data base
$db = mysql_select_db( 'charts', $link );
if ( !$db ) {
  die ( 'Error selecting database \'test\' : ' . mysql_error() );
}

// Fetch the data
$query = "
  SELECT *
  FROM starbucks";
$result = mysql_query( $query );

// All good?
if ( !$result ) {
  // Nope
  $message  = 'Invalid query: ' . mysql_error() . "\n";
  $message .= 'Whole query: ' . $query;
  die( $message );
}

// Print out rows

$data = array();
while ( $row = mysql_fetch_assoc( $result ) ) {
  $data[] = $row;
}
echo json_encode( $data );
// Close the connection
mysql_close($link);

?>

Javascript

Then there is the Javascript side of things.

var chart = AmCharts.makeChart( "chartdiv", {
"type": "serial",
"dataLoader": {
"url": "../scripts/data.php"
},
"pathToImages": "http://www.amcharts.com/lib/images/",
"categoryField": "category",
"dataDateFormat": "YYYY-MM-DD",
"startDuration": 1,
"rotate": false,
"animationDuration": 0,
"minSelectedTime": 100,
"categoryAxis": {
"parseDates": true
},
"graphs": [ {
"valueField": "value1",
"bullet": "square",
"bulletBorderColor": "#FFFFFF",
"bulletBorderThickness": 2,
"lineThickness ": 2,
"lineAlpha": 0.5
} ]

} );

HTML

Theres is of course, HTML.

<div id="chartdiv" style="width:100%; height:400px;"></div>

So back to the problem

After copy/pasting all that code, back to the actual question of this post. Why am I getting "Loading Data" instead of an actual chart?

IF there is anything else needed, let me know. I've done my best to not be vague in this question.

Upvotes: 2

Views: 3006

Answers (3)

范右昇
范右昇

Reputation: 1

finally I have solution
i don't know why amchart output "data loading..." but don't output real problem
if someone don't use browser(me is chrome) ctrl+shift+J to look up error message,
he would never found why always "data loading..."

two problem that cause "data loading" situation is confirmed
1.you open local html and browser(or amchart) block javascript to read local file
2.your file doesn't exist at all

you need something like apache to simulate amchart work at real enviroment put html,json,lib to httpdoc

Upvotes: 0

Claudio Torres
Claudio Torres

Reputation: 63

I tried everything, in the end I used AJAX to retrive content and insert it in dataprovider.

    jQuery.ajax({
    url: "api/chartdata,
    type: "GET",
    contentType: 'application/json; charset=utf-8',
    success: function (resultData) {
        console.log(resultData);
        lineChart.dataProvider = JSON.parse(resultData);
        lineChart.validateData();
    },
    error: function (jqXHR, textStatus, errorThrown) {
    }
});

Upvotes: 0

DanT
DanT

Reputation: 11

I'm having a similar issue. If I save the json output, remove the preceding square braces, and use this as my input, it works:

JSON that doesn't work:

[
    [],
    {"Key1":"Val1","Key2":"Val1"},
    {"Key1":"Val2","Key2":"Val2"},
    ...
]

JSON that works:

[
    {"Key1":"Val1","Key2":"Val1"},
    {"Key1":"Val2","Key2":"Val2"},
    ...
]

Modify your AmChart.makeChart bit to test json:

"dataLoader": {
      "url": "test.json",
      "format": "json"
    },

..but I don't know how to remove the preceding braces in my PHP code.. Hopefully this will get you a step closer to resolution...

Upvotes: 1

Related Questions