phpgoogle-visualizationpygooglechart

Google chart - columns labels


I have following code:

$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'),
);

and wonder how to change it, to be able to change the number of columns. Display columns, depending on the variables (something like this):

$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);  
    }
}

I hope is clear what I try to do. Please help me.


Solution

  • So you can easily add an item to your $tables['col'] array. To add something to an array do this:

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

    In your case, your code would looks something like this:

    $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');
    }
    );