Reputation: 105
The result of query I use display 3 column (country, date, items). My php code side
$res = db_query($sql);
$dat = array();
while($r = db_fetch_array($res)){
$dat[]= array($r['date'], $r['items'], $r['country']);
}
// Armar
$start_date = '';
if(count($dat)>0){
$s = split(' ',$dat[0][0]);
$ss = split('-',$s[0]);
}
// Cada objeto en $dats es una grafica
$dats[] = array('type'=>'line',
'name'=>$q['title'],
'pointInterval'=>24 * 3600 * 1000,
'pointStart'=>mktime(0,0,0,$ss[1],$ss[2],$ss[0])*1000,
'data'=>$dat) ;
//echo "$sql";
echo json_encode($dats,JSON_NUMERIC_CHECK);
My Javascript Code :
function loadLine(_data){
$('#line_container').highcharts({
chart: {zoomType: 'x',spacingRight: 20},
title: { text: 'Monthly Created items'},
subtitle: {text:'Updated every day'},
xAxis: {
type: 'datetime',
maxZoom: 7 * 24 * 3600000, // fourteen days
title: {text: null}
},
yAxis: {title: {text: 'Created items'}},
tooltip: {shared: true},
legend: {enabled: true},
plotOptions: {
area: {
fillColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
lineWidth: 1,
marker: {
enabled: false
},
shadow: false,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},
series: _data
});
}
and The result displayed is like this
How Could change "series 1" in the graph by the country name I receive in my query? The data I have in the Query has date until "April" (YTD) but the Graph shows Months in the future, How could I correct this? If I have more than 1 country in my Query How could I display this in multiple chart lines at the same time. Thanks in advance.
Upvotes: 1
Views: 1030
Reputation: 40663
You have only supplied a single series, which will only translate to a single line. Try something like:
$res = db_query($sql);
$dat = array();
while($r = db_fetch_array($res)){
if (!isset($dat[$r['country']]))
$dat[$r['country']] = [];
$dat[$r['country']][] = array($r['date'], $r['items'], $r['country']);
}
// Armar
$start_date = '';
if(count($dat)>0){
$s = split(' ',$dat[0][0]);
$ss = split('-',$s[0]);
}
// Cada objeto en $dats es una grafica
$dats = [];
foreach ($dat as $country => $values) {
$dats[] = array('type'=>'line',
'name'=>$q['title'],
'pointInterval'=>24 * 3600 * 1000,
'pointStart'=>mktime(0,0,0,$ss[1],$ss[2],$ss[0])*1000,
'data'=>$values) ;
}
//echo "$sql";
echo json_encode($dats,JSON_NUMERIC_CHECK);
Upvotes: 2
Reputation: 1084
Your _data
should be something like the following:
[{
name: 'USA',
data: [[Date.UTC(2013,5,1), 7.0], [Date.UTC(2013,6,1), 5.0]]
}, {
name: 'Germany',
data: [[Date.UTC(2013,5,1), 6.0], [Date.UTC(2013,6,1), 8.0]]
}, {
name: 'Japan',
data: [[Date.UTC(2013,5,1), 7.0], [Date.UTC(2013,6,1), 3.0]]
}]
So you will need to do some mapping from your _data
Upvotes: 1