View source
<?php
define('QUANT_PALETTE_AMOUNT', 25);
class QuantChartChartAPI extends QuantChart {
var $output = NULL;
var $chart = NULL;
function variables() {
return array(
'quant_chartapi_palette',
);
}
function adminSettings() {
$form = array();
$palette = $this
->palette();
$form['chartapi_color'] = array(
'#type' => 'fieldset',
'#title' => t('Color settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Specify the colors that the charts will be rendered in.'),
);
for ($i = 0; $i < QUANT_PALETTE_AMOUNT; $i++) {
$form['chartapi_color']['quant_chartapi_palette_color_' . $i] = array(
'#type' => 'textfield',
'#title' => t('Color') . ' #' . ($i + 1),
'#default_value' => $palette[$i],
'#field_prefix' => '#',
'#size' => 10,
'#maxlength' => 6,
);
}
return $form;
}
function adminSettingsValidate(&$form, &$form_state) {
$colors = array();
for ($i = 0; $i < QUANT_PALETTE_AMOUNT; $i++) {
$color = $form_state['values']['quant_chartapi_palette_color_' . $i];
$colors[] = strtoupper($color);
unset($form_state['values']['quant_chartapi_palette_color_' . $i]);
}
if (empty($colors)) {
form_set_error('color', t('You need to enter at least one color.'));
}
else {
$form_state['values']['quant_chartapi_palette'] = $colors;
}
}
function build() {
switch ($this->quant->chartType) {
case 'line':
$this
->line_chart();
break;
case 'bar':
$this
->bar_chart();
break;
case 'pie':
$this
->pie_chart();
break;
}
switch ($this->quant->dataType) {
case 'single':
$this
->chart_single();
break;
case 'multiple':
$this
->chart_multiple();
break;
case 'count':
$this
->chart_count();
break;
}
$this
->color();
$this
->size();
}
function render() {
$this->output = theme('chart', array(
'chart' => $this->chart,
));
return $this->output;
}
function line_chart() {
$this->chart = array(
'#chart_id' => $this->quant->id,
'#title' => $this
->title(),
'#type' => CHART_TYPE_LINE,
'#adjust_resolution' => TRUE,
'#chart_fill' => chart_fill('c', 'fff'),
'#grid_lines' => chart_grid_lines(20, 20, 1, 5),
);
}
function bar_chart() {
$this->chart = array(
'#chart_id' => $this->quant->id,
'#title' => $this
->title(),
'#type' => CHART_TYPE_BAR_V,
'#adjust_resolution' => TRUE,
'#grid_lines' => chart_grid_lines(30, 20),
'#bar_size' => chart_bar_size(45, 15),
);
}
function pie_chart() {
$this->chart = array(
'#chart_id' => $this->quant->id,
'#title' => $this
->title(),
'#type' => CHART_TYPE_PIE,
'#adjust_resolution' => TRUE,
);
}
function title() {
$title = $this->quant->label;
if ($this->quant->labelsum) {
$sum = 0;
foreach ($this->quant->data->data as $value) {
if ($this->quant->dataType == 'multiple') {
foreach ($value as $amount) {
$sum = $sum + $amount;
}
}
else {
$sum = $sum + $value;
}
}
$title .= ' (' . t('Total: !sum', array(
'!sum' => $sum,
)) . ')';
}
return chart_title($title);
}
function color() {
$palette = $this
->palette(TRUE);
if ($this->quant->dataType == 'single') {
$this->chart['#data_colors'][] = $palette[rand(0, count($palette))];
}
else {
for ($i = 0; $i < count($palette); $i++) {
$this->chart['#data_colors'][] = $palette[$i];
}
}
}
function size() {
$this->chart['#size'] = chart_size(variable_get('quant_width', 500), variable_get('quant_height', 200));
}
function x_label($label) {
$this->chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_label($label);
}
function y_range($min, $max) {
$max = max($max, 1);
$this->chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label($min, $max);
}
function palette($random = FALSE) {
$palette = variable_get('quant_chartapi_palette', array());
if (!$palette) {
$default_palette = chart_color_schemes();
$palette = $default_palette['default'];
}
if ($random) {
shuffle($palette);
}
return $palette;
}
function chart_count() {
$max = 0;
foreach ($this->quant->data->data as $key => $value) {
$this->chart['#data'][] = $value;
if ($this->quant->chartType == 'pie') {
$this
->x_label($key . ' (' . $value . ')');
}
else {
$this
->x_label($key);
}
$max = max($max, $value);
}
$this
->y_range(0, $max);
}
function chart_single() {
$max = 0;
$interval = 0;
$period = ceil(count($this->quant->data->data) / 10);
foreach ($this->quant->data->data as $date => $value) {
if (!$interval) {
$this
->x_label($date);
$interval = $period;
}
$this->chart['#data'][] = $value;
$max = max($max, $value);
$interval--;
}
$this
->y_range(0, $max);
}
function chart_multiple() {
$max = 0;
$interval = 0;
$x = FALSE;
foreach ($this->quant->data->data as $type => $values) {
$this->chart['#legends'][] = $type;
$period = ceil(count($this->quant->data->data[$type]) / 10);
foreach ($values as $date => $value) {
$this->chart['#data'][$type][] = $value;
$max = max($max, $value);
if (!$x) {
if (!$interval) {
$this
->x_label($date);
$interval = $period;
}
$interval--;
}
}
$x = TRUE;
}
$this
->y_range(0, $max);
}
}