I've been using amCharts stocks charts to display data from Yahoo Finance .csv file which displays data in this format: (which works)
Date,Open,High,Low,Close,Volume,Adj Close
2011-07-27,617.18,620.95,604.75,607.22,3934400,607.22
2011-07-26,618.05,627.50,617.22,622.52,2342900,622.52
2011-07-25,613.36,625.41,613.00,618.98,3131600,618.98
However now I need to get data from Google finance and they format they're data in this format (which isn't working).
Date,Open,High,Low,Close,Volume
28-Jul-11,3开发者_如何学Python6.02,36.84,36.01,36.42,8870180
27-Jul-11,36.71,36.86,35.98,36.06,10395443
26-Jul-11,37.26,37.27,36.80,36.86,6366097
I believe the data format different between Yahoo and Google is what's causing the amChart to not display any figures because it can't read the file.
How do I format the date to get it to read the values properly?
I'll assume that you are using amstock charts 1.4.0.1. In your amstock_settings.xml you need to edit the date format settings. By default it is using YYYY-MM-DD which I assume based from your example is Yahoo Finance's default date format in their CSV. Google finance's date format is DD-Mon-YY which based on AmCharts 'Mon' is not a valid symbol. You can try to change the settings to DD-Mon-YY and see if it works if not you may need to use a program(e.g. java) to change the date values in csv into YYYY-MM-DD.
<!-- [YYYY-MM-DD] (date format) The valid symbols are: YYYY, MM, DD, hh, mm, ss, fff. Any order
and separators can be used, for example: DD-MM-YYYY, YYYY-MM, YYYY-DD-MM hh,
DD-MM-YY hh:mm:ss, etc. fff means milliseconds. In case you use miliseconds, you must provide the
3 digit number -->
<date_format>YYYY-MM-DD</date_format>
amStock does not support dates in 28-Jul-11 format. You will need to build a server-side proxy script that converts all dates to some supported format like 2011-07-28.
Here's one in PHP:
<?php
// get input data (replace with the actual data)
// i.e.: $input = file_get_contents('http://...........');
$input = "Date,Open,High,Low,Close,Volume
28-Jul-11,36.02,36.84,36.01,36.42,8870180
27-Jul-11,36.71,36.86,35.98,36.06,10395443
26-Jul-11,37.26,37.27,36.80,36.86,6366097";
// parse line by line
$lines = preg_split("/\r\n|\r|\n/", $input);
$cnt = sizeof($lines);
for ($i = 0; $i < $cnt; $i++) {
$cols = explode(',', $lines[$i]);
if ($date = strtotime($cols[0])) {
$cols[0] = date('Y-m-d', $date);
$lines[$i] = implode(',', $cols);
}
}
// output
header('Content-Type: text/csv');
echo implode("\r\n", $lines);
?>
精彩评论