root/trunk/modules/flot/libraries/Flot.php

Revision 3700, 2.3 kB (checked in by Shadowhand, 12 days ago)

Updates to trunk:

  • Removed all SYSPATH file checks
  • Deleted some modules: code_coverage (3.0), shoutbox (defunct), user_guide (defunct), kobot (3.0), object_db (3.0)
  • Updated ORM and ORM_Iterator to match 3.0, enabling the new HABTM stuff, see r3636 and r3640
  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2008 Kohana Team
  • Property svn:keywords set to Id
Line 
1<?php
2/**
3 * Flot (jQuery plotting plugin) Kohana integration.
4 *
5 * $Id$
6 *
7 * @package    Flot
8 * @author     Woody Gilk
9 * @copyright  (c) 2007-2008 Kohana Team
10 * @license    http://kohanaphp.com/license.html
11 */
12class Flot_Core {
13
14    // Container type and attributes
15    protected $type = 'div';
16    protected $attr = array();
17
18    // Dataset and options
19    protected $dataset;
20    protected $options;
21
22    public function __construct($id, $attr = array(), $type = NULL)
23    {
24        // Set the id to the attributes
25        $attr['id'] = $id;
26
27        // Set the attributes of the container
28        $this->attr += $attr;
29
30        // Set the type, if not NULL
31        empty($type) or $this->type = $type;
32
33        // Create the data set array
34        $this->dataset = array();
35
36        // Create the options object
37        $this->options = new StdClass;
38    }
39
40    public function __get($key)
41    {
42        if ( ! isset($this->options->$key))
43        {
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  object
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  object
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
121} // End Flot
Note: See TracBrowser for help on using the browser.