Flot_Core::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 7
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.4285
1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Flot (jQuery plotting plugin) Kohana integration.
4
 *
5
 * $Id: Flot.php 3769 2008-12-15 00:48:56Z zombor $
6
 *
7
 * @package    Flot
8
 * @author     Woody Gilk
9
 * @copyright  (c) 2007-2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
class Flot_Core
13
{
14
15
    // Container type and attributes
16
    protected $type = 'div';
17
    protected $attr = array();
18
19
    // Dataset and options
20
    protected $dataset;
21
    protected $options;
22
23
    public function __construct($id, $attr = array(), $type = null)
24
    {
25
        // Set the id to the attributes
26
        $attr['id'] = $id;
27
28
        // Set the attributes of the container
29
        $this->attr += $attr;
30
31
        // Set the type, if not NULL
32
        empty($type) or $this->type = $type;
33
34
        // Create the data set array
35
        $this->dataset = array();
36
37
        // Create the options object
38
        $this->options = new StdClass;
39
    }
40
41
    public function __get($key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
42
    {
43
        if (! isset($this->options->$key)) {
44
            // Create the object if it does not exist
45
            $this->options->$key = new StdClass;
46
        }
47
48
        // Return the option
49
        return $this->options->$key;
50
    }
51
52
    public function __set($key, $value)
53
    {
54
        // Set the option value
55
        $this->options->$key = $value;
56
    }
57
58
    /**
59
     * Return the rendered graph as an HTML string.
60
     *
61
     * @return string
62
     */
63
    public function __toString()
64
    {
65
        return $this->render();
66
    }
67
68
    /**
69
     * Add data to the data set.
70
     *
71
     * @chainable
72
     * @param   object   a constructed Flot_Dataset
73
     * @return  Flot_Core
74
     */
75
    public function add(Flot_Dataset $set, $label = null)
76
    {
77
        // Add the label, if requested
78
        empty($label) or $set->label = $label;
79
80
        // Add the set to the current data set
81
        $this->dataset[] = $set;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Set options.
88
     *
89
     * @chainable
90
     * @param   string  option name
91
     * @param   mixed   options value
92
     * @return  Flot_Core
93
     */
94
    public function set($key, $value)
95
    {
96
        // Set the requested value
97
        $this->__set($key, $value);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Return the rendered graph as an HTML string.
104
     *
105
     * @return string
106
     */
107
    public function render($template = 'kohana_flot')
108
    {
109
        // Load the template
110
        return View::factory($template)
111
            // Set container properties
112
            ->set('type', $this->type)
113
            ->set('attr', $this->attr)
114
            // JSON encode the dataset and options
115
            ->set('dataset', array_map('json_encode', $this->dataset))
116
            ->set('options', json_encode($this->options))
117
            // And return the rendered view
118
            ->render();
119
    }
120
} // End Flot
121