java2dev java2dev - 7 months ago 332
PHP Question

Multiple series data Highcharts line

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
enter image description here

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.

Answer

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);
Comments