JSON, HighCharts/HighStock, DBSlayer - Data display
I have a MySQL database with some price data in it I would like to display using HighCharts/HighStock yet I'm not sure how I can actually go about getting this data from MySQL (via DBslayer as the JSON layer) and display in high charts (the example I found on their site isnt helpful and searching around there are no good tuts).
So essentially the system looks like this:
MySQL <---> DBSlayer Service <---> JSON Requests to DBSlayer <---> Web page with charts - send query to DBSLayer
The MySQL View which DBSlayer queries in a nutshell looks like this:
DATE TIME | Symbol1 | Price 1 | Symbol2 | Price 2 | Price3
2011-09-01| ABC | 12.3 | XYZ | 67.8 | 0.0852
Or a better example is the returned JSON from a query to DBSlayer:
{"RESULT" : {"HEADER" : ["id" , , "authorID" , "msgDate" , "obj_obj" , "obj_subj" , "obj_diff" , "subj_pos" , "subj_neg" , "subj_diff" , "pos_lex" , "neg_lex" ] ,
"ROWS" : [["4e0f1c393bfbb6aa4b7278c2" , "27" , "2011-06-30 13:59:47" , 0.0275171 , 0.972483 , -0.944966 , 0.993814 , 0.00618577 , 0.987628 , 1 , 0 ] ,
["4e0f1c393bfbb6aa4b7278c3" , "36324" , "2011-06-30 13:59:31" , 0.364953 , 0.635047 , -0.270095 , 0.0319281 , 0.968072 , -0.936144 , 3 , 1 ] ,
["4e0f1c393bfbb6aa4b7278c4" , "12134" , "2011-06-30 13:59:28" , 0.0112589 , 0.988741 , -0.977482 , 0.857735 , 0.142265 , 0.715469 , 1 , 1 ] ] ,
"TYPES" : ["MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_DATETIME" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_LONG" } , "SERVER" : "db-webPrices"}
How should I deploy this to High Charts? Should I use Node.js to wrap the query first (there are Node.js to DBSlayer libs however they dont work with the newest version of Node.js.
How would I use JQuery to get this data and format for a HighStock chart like this one: http://www.highcharts.com/stock/demo/multiple-series/gray
The basic HighCharts Demo using a CSV file as a data source looks like this:
$(function() {
var seriesOptions = [],
yAxisOptions = [],
seriesCounter = 0,
names = ['DJI', 'SENTIMENT', 'GOOG', 'GS', 'SENTIMENT-Z', 'DJI-Z'],
colors = Highcharts.getOptions().colors;
$.each(names, function(i, name) {
$.get(name +'.csv', function(csv, state, xhr) {
// inconsistency
if (typeof csv != 'string') {
csv = xhr.responseText;
}
// parse the CSV data
var data = [], header, comment = /^#/, x;
$.each(csv.split('\n'), function(i, line){
if (!comment.test(line)) {
if (!header) {
header = line;
}
else {
var point = line.split(';'), date = point[0].split('-');
x = Date.UTC(date[2], date[1] - 1, date[0]);
if (point.length > 1) {
// use point[4], the close value
data.push([
x,
parseFloat(point[4])
]);
}
}
}
});
seriesOptions[i] = {
name: name,
data: data,
yAxis: i
};
// create one y axis for each series in order to be able to compare them
yAxisOptions[i] = {
alternateGridColor: null,
gridLineWidth: i ? 0 : 1, // only grid lines for the first series
opposite: i ? true : false,
minorGridLineWidth: 0,
title: {
text: name,
style: {
color: colors[i]
}
},
lineWidth: 2,
lineColor: colors[i]
};
// As we're loading the data asynchronously, we don't know what order it will arrive. So
// we keep a counter and create the chart when all the data is loaded.
seriesCounter++;
if (seriesCounter == names.length) {
createChart();
}
});
});
// create the chart when all data is loaded
function createChart() {
chart = new Highcharts.StockChart({
chart: {
开发者_如何学Go renderTo: 'container',
alignTicks: false
},
rangeSelector: {
selected: 1
},
title: {
text: null
},
xAxis: {
type: 'datetime',
maxZoom: 14 * 24 * 3600000, // fourteen days
title: {
text: null
}
},
yAxis: yAxisOptions,
series: seriesOptions
});
}
});
</script>
<script type="text/javascript" src="js/themes/gray.js"></script>
Any examples/code would be greatly appreciated!
Cheers!
If you view the source of the demo page you provided, you can see how it's done with CSV data. A JSON result is even easier to use.
Update: The documentation shows you more info on how your data should look.
精彩评论