yf_charts class

Fileplugins/charts/classes/yf_charts.class.php
Use_class('charts')

Public properties

A site sets them in its config with $PROJECT_CONF['charts']['NAME'] = ...;

PropertyDefaultDescription
$TICK_COUNT 10
$_ids []

Public methods 8

chart

public function chart($data, $params)

Data array format for chart. Examples: array('{DATE1}' => '{VALUE1}' '{DATE2}' => '{VALUE2}') array('{LINE_NAME1}' => Array('DATE1' => 'VAL1', 'DATE2' => 'VAL2'), '{LINE_NAME2}' => Array('DATE1' => 'VAL1', 'DATE2' => 'VAL2')}.

    public function chart($data, $params)
    {
        $width = isset($params['width']) ? $params['width'] : '90%';
        $height = isset($params['height']) ? $params['height'] : '90%';
        $count = null;
        foreach ((array) $data as $data_item) {
            $count = count((array) $data_item);
            break;
        }
        if ($count == 1) {
            $data = [
                '1' => $data,
            ];
        }
        $items = null;
        foreach ((array) $data as $name => $data_items) {
            foreach ((array) $data_items as $date => $val) {
                $items[] = [
                    'date' => $date,
                    'name' => $name,
                    'val' => $val,
                ];
            }
        }
        $incude_js = null;
        if ( ! $params['not_include_js']) {
            $incude_js = $this->_include_js();
        }
        return $incude_js . tpl()->parse('charts/chart', [
            'items' => $items,
            'id' => 'chart' . rand(),
            'width' => is_numeric($width) ? $width . 'px' : $width,
            'height' => is_numeric($height) ? $height . 'px' : $height,
        ]);
    }

chart_bar

public function chart_bar($data, $params)

Data array format for bar chart. Examples: array('{NAME}' => array('val' => '{VALUE}', 'link' => '{LINK}')}.

    public function chart_bar($data, $params)
    {
        $direction = isset($params['direction']) ? $params['direction'] : 'horizontal';
        $width = isset($params['width']) ? $params['width'] : '90%';
        foreach ((array) $data as $key => $value) {
            if (is_array($value)) {
                break;
            }
            $data[$key] = [
                'val' => $value,
                'link' => '',
            ];
        }
        $incude_js = null;
        if ( ! $params['not_include_js']) {
            $incude_js = $this->_include_js();
        }
        return $incude_js . tpl()->parse('charts/chart_bar', [
            'data' => $data,
            'ticks' => $this->_get_ticks($data),
            'direction' => $direction,
            'id' => 'bar' . rand(),
            'width' => is_numeric($width) ? $width . 'px' : $width,
        ]);
    }

chart_pie

public function chart_pie($data, $params)

Data array format for pie chart. Examples: array('{NAME}' => array('val' => '{VALUE}', 'link' => '{LINK}')}.

    public function chart_pie($data, $params)
    {
        $width = isset($params['width']) ? $params['width'] : '90%';
        $height = isset($params['height']) ? $params['height'] : '90%';
        $incude_js = null;
        if ( ! $params['not_include_js']) {
            $incude_js = $this->_include_js();
        }
        return $incude_js . tpl()->parse('charts/chart_pie', [
            'data' => $data,
            'id' => 'pie' . rand(),
            'width' => is_numeric($width) ? $width . 'px' : $width,
            'height' => is_numeric($height) ? $height . 'px' : $height,
        ]);
    }

chartjs

public function chartjs($type, $data, $extra = [])

Chart.js chart (driver charts/yf_charts_plugin_chartjs): line, bar, pie, doughnut, radar or polarArea. Examples: chartjs('bar', ['Mon' => 3, 'Tue' => 5]), chartjs('line', ['Visits' => ['Mon' => 3, 'Tue' => 5], 'Orders' => ['Mon' => 1]], ['title' => 'Week']).

  • $type string
  • $data array one series [label => value] or several [series name => [label => value]]
  • $extra array title, height, id, stacked, horizontal, fill, colors, options
    public function chartjs($type, $data, $extra = [])
    {
        return _class('charts_plugin_chartjs', 'classes/charts/')->render((string) $type, (array) $data, (array) $extra);
    }
public function jquery_sparklines($data, $extra = [])

http://omnipotent.net/jquery.sparkline.

    public function jquery_sparklines($data, $extra = [])
    {
        $extra['id'] = ($extra['id'] ?? null) ?: __FUNCTION__ . '_' . ++$this->_ids[__FUNCTION__];
        if ( ! is_array($data) || ! $data) {
            return false;
        }
        $extra['trim'] = isset($extra['trim']) ? $extra['trim'] : true;
        // Trim empty data left and right
        if ($extra['trim']) {
            foreach ((array) $data as $k => $v) {
                if (is_array($v)) {
                    if ( ! array_sum($v)) {
                        unset($data[$k]);
                        continue;
                    }
                } elseif ( ! $v) {
                    unset($data[$k]);
                    continue;
                }
                break;
            }
            foreach (array_reverse($data, $preserve_keys = true) as $k => $v) {
                if (is_array($v)) {
                    if (array_sum($v)) {
                        break;
                    }
                } elseif ($v) {
                    break;
                }
                unset($data[$k]);
            }
            if ( ! $data) {
                return false;
            }
        }
        $extra['type'] = ($extra['type'] ?? null) ?: 'bar';

        asset('jquery-sparklines');
        jquery('
			$("#' . $extra['id'] . '").sparkline(' . json_encode(array_values($data)) . ', {
				type: "' . $extra['type'] . '",
				enableTagOptions: true,
				tooltipFormat: "{{offset:key}}: {{value}}",
				tooltipValueLookups: { key: ' . json_encode(array_keys($data)) . ' }
			});
		');
        return '<span id="' . $extra['id'] . '" class="sparkline"></span>';
    }

Methods starting with _ hooks and helpers; not routable as page actions

__call

public function __call($name, $args)

Catch missing method call.

    public function __call($name, $args)
    {
        return main()->extend_call($this, $name, $args);
    }

_get_ticks

public function _get_ticks($data)
    public function _get_ticks($data)
    {
        $max = 0;
        foreach ((array) $data as $val) {
            if ($val['val'] > $max) {
                $max = $val['val'];
            }
        }
        $max_real = $max;
        $max = ceil($max);
        if ($max < 10) {
            $max = 10;
        }
        $len = strlen($max);
        $max = round($max, -1 * ($len - 1));
        $step = $max / ($this->TICK_COUNT - 1);
        if ($max <= $max_real || ($max - $max_real) < $step) {
            $max = $max + $step;
        }
        $result = [];
        for ($i = 0; $i <= $max; $i = $i + $step) {
            $tick = round($i, -1 * ($len - 2));
            $result[$tick] = $tick;
        }
        return $result;
    }

_include_js

public function _include_js()
    public function _include_js()
    {
        $content = null;
        if ( ! $GLOBALS['chart']['include_js']) {
            $GLOBALS['chart']['include_js'] = true;
            $content = tpl()->parse('charts/chart_include_js');
        }
        return $content;
    }