| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Returns a rendered Google Chart of just the URL from CSV data
|
| 5 |
*
|
| 6 |
* @param array $csvdata
|
| 7 |
*
|
| 8 |
* @param bool $url_only
|
| 9 |
*/
|
| 10 |
|
| 11 |
function csvchart_create_graph(&$csvdata, $url_only = FALSE) {
|
| 12 |
|
| 13 |
$ydata = array();
|
| 14 |
$legends = array();
|
| 15 |
$id = $title = $csvdata['title'][0];
|
| 16 |
$chart_type = $csvdata['type'][0];
|
| 17 |
$xdata = $csvdata['xdata'];
|
| 18 |
$xlabel = $csvdata['labels'][0];
|
| 19 |
$ylabel = $csvdata['labels'][1];
|
| 20 |
|
| 21 |
$no_data = array('title', 'type', 'labels', 'xdata');
|
| 22 |
foreach($csvdata as $key => $val) {
|
| 23 |
if (!in_array($key, $no_data)) {
|
| 24 |
array_push($ydata, $csvdata[$key]);
|
| 25 |
array_push($legends, $key);
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
| 29 |
$chart = array(
|
| 30 |
'#chart_id' => $id,
|
| 31 |
'#title' => chart_title(t($title), '0000ee', 15),
|
| 32 |
'#type' => $chart_type,
|
| 33 |
'#size' => chart_size(600, 350),
|
| 34 |
'#grid_lines' => chart_grid_lines(30, 15),
|
| 35 |
'#adjust_resolution' => TRUE,
|
| 36 |
'#bar_size' => chart_bar_size(35, 0),
|
| 37 |
);
|
| 38 |
|
| 39 |
// Load the data into the chart
|
| 40 |
$chart['#data'] = $ydata;
|
| 41 |
|
| 42 |
$ymax = 0;
|
| 43 |
foreach($ydata as $yset) {
|
| 44 |
$ymax_set = max($yset);
|
| 45 |
$ymax = round(max($ymax, $ymax_set));
|
| 46 |
}
|
| 47 |
|
| 48 |
// set up legends
|
| 49 |
$chart['#legends'] = $legends;
|
| 50 |
|
| 51 |
// Set up axes
|
| 52 |
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $ymax);
|
| 53 |
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][1][] = chart_mixed_axis_label($ylabel, 95);
|
| 54 |
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label($xdata);
|
| 55 |
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][2][] = chart_mixed_axis_label($xlabel, 50);
|
| 56 |
|
| 57 |
// Set up colors here
|
| 58 |
$chart['#data_colors'][] = '00ef00';
|
| 59 |
$chart['#data_colors'][] = '0000ef';
|
| 60 |
$chart['#data_colors'][] = 'ef0000';
|
| 61 |
$chart['#data_colors'][] = 'efef00';
|
| 62 |
$chart['#data_colors'][] = 'ef00ef';
|
| 63 |
$chart['#data_colors'][] = '00efef';
|
| 64 |
$chart['#data_colors'][] = '00af00';
|
| 65 |
$chart['#data_colors'][] = '0000af';
|
| 66 |
$chart['#data_colors'][] = 'af0000';
|
| 67 |
|
| 68 |
|
| 69 |
// return "<pre>".print_r($chart, TRUE)."</pre";
|
| 70 |
if ($url_only == TRUE) {
|
| 71 |
return chart_url($chart);
|
| 72 |
} else {
|
| 73 |
return chart_render($chart);
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
function csvchart_get_csv_values($string,$separator=",")
|
| 78 |
{
|
| 79 |
$elements = array();
|
| 80 |
if ($string != "") {
|
| 81 |
$lines = explode("\n", trim($string));
|
| 82 |
|
| 83 |
foreach ($lines as $l) {
|
| 84 |
$data = array();
|
| 85 |
$la = array_values(explode($separator, $l));
|
| 86 |
for ($i = 1; $i < count($la); $i++) {
|
| 87 |
array_push($data, trim($la[$i]));
|
| 88 |
}
|
| 89 |
$elements[$la[0]] = $data;
|
| 90 |
}
|
| 91 |
}
|
| 92 |
return $elements;
|
| 93 |
}
|