Google 图表 - 列标签

Google chart - columns labels

我有以下代码:

$rows = array(); 
$table = array();
$table['cols'] = array(

    array('label' => 'Date', 'type' => 'string'),
    array('label' => 'Charging Current', 'type' => 'number'),
    array('label' => 'Battery Voltage', 'type' => 'number'),    
    array('label' => 'Charging Power', 'type' => 'number'),
    array('label' => 'Rotation', 'type' => 'number'),
);

想知道如何更改它,以便能够更改列数。根据变量显示列(类似这样):

$rows = array(); 
$table = array();
$table['cols'] = array(
   array('label' => 'Date', 'type' => 'string'),
if ($current == "true") {**
    array('label' => 'Charging Current', 'type' => 'number'),
}
if ($voltage == "true") {
    array('label' => 'Battery Voltage', 'type' => 'number'),    
}
if ($power == "true") {
    array('label' => 'Charging Power', 'type' => 'number'),
}
if ($rotation == "true") {
    array('label' => 'Rotation', 'type' => 'number'),
}
);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the chart
    $temp[] = array('v' => (string) $r['TimeStamp']); 

    // Values of each slice
    if ($current == "true") {
        $temp[] = array('v' => (int) $r['ChargingCurrent']); 
        $rows[] = array('c' => $temp);
    }
    if ($voltage == "true") {
        $temp[] = array('v' => (int) $r['BatteryVoltage']); 
        $rows[] = array('c' => $temp);  
    }
    if ($power == "true") {
        $temp[] = array('v' => (int) $r['ChargingPower']); 
        $rows[] = array('c' => $temp);
    }
    if ($rotation == "true") {
        $temp[] = array('v' => (int) $r['Rotation']); 
        $rows[] = array('c' => $temp);  
    }
}

我希望清楚我尝试做什么。请帮助我。

因此,您可以轻松地将项目添加到 $tables['col'] 数组中。要向数组添加内容,请执行以下操作:

$table['cols'][] = 'something';

在您的情况下,您的代码将如下所示:

$rows = array();
$table = array();
$table['cols'] = array(
    array('label' => 'Date', 'type' => 'string')
);

if ($current == "true") {
    $table['cols'][] = array('label' => 'Charging Current', 'type' => 'number');
}
if ($voltage == "true") {
    $table['cols'][] = array('label' => 'Battery Voltage', 'type' => 'number');
}
if ($power == "true") {
    $table['cols'][] = array('label' => 'Charging Power', 'type' => 'number');
}
if ($rotation == "true") {
    $table['cols'][] = array('label' => 'Rotation', 'type' => 'number');
}
);