1 {% extends "basex.html" %}
3 {% block pageid %}code{% endblock %}
5 <link href="doc.css" media="screen" rel="stylesheet" type="text/css" />
7 {% block title %}PlotKit.Renderer{% endblock %}
10 <div class="page doc api">
12 [PlotKit Home](PlotKit.html) | [<<](PlotKit.Layout.html) | [>>](PlotKit.Canvas.html)
17 A Renderer is responsible for translating the layout calculated by PlotKit.Layout and draw it on to a HTML Canvas, SVG object or any other way. One way to use the renderer is to allow theming of graphs by tweaking the layout.
19 PlotKit includes some common basic renderers, so you do not need to customise anything if you just plan to change the spacing, colors, fonts, or layout.
21 PlotKit Renderers should follow an informal protocol to allow users to plug and play different renderers. Below is the informal protocol:
23 PlotKit Renderer Protocol
24 -------------------------
25 * Constructor: ``new Renderer(element, layout, options = {})``
27 ``element`` is the element which this renderer will perform on, ``layout`` is the PlotKit.Layout object and ``options`` is an associative dictionary described below.
29 * class function: ``isSupported()``
31 Optional check that returns ``true`` if the renderer is supported in the current browser.
33 * object method: ``render()``
35 Renders to canvas, can be called multiple times, but ``clear()`` must be called between invokations.
37 * object method: ``clear()``
41 PlotKit Renderer Options
42 ------------------------
43 To allow some basic flexibility of the output, a renderer should
44 accept and act on the following options passed in the constructor.
47 <table cellpadding="0" cellspacing="0">
49 <tr><td>Option name</td><td>Description</td><td>Type</td><td>Default</td></tr>
53 <th>backgroundColor</th>
54 <td>color to use for background</td>
55 <td>MochiKit.Color.Color</td>
56 <td>Color.whiteColor()</td>
60 <td>Color scheme used</td>
61 <td>Array of MochiKit.Color.Color</td>
62 <td>output of PlotKit.Base.colorScheme()</td>
66 <td>Color used stroking. If set to null, the renderer will
67 attempt to use strokeColorTransform</td>
68 <td>MochiKit.Color.Color or null</td>
72 <th>strokeColorTransform</th>
73 <td>Name of the method to call to transform Color into stroke color.</td>
74 <td>string (name of a function that accepts no arguments)</td>
75 <td>"asStrokeColor"</td>
78 <th>drawBackground</th>
79 <td>Whether the background should be drawn</td>
85 <td>Should fill in area under chart</td>
91 <td>Should stroke the borders of shapes in chart</td>
97 <td>Width of stroke used (if shouldStroke is set)</td>
103 <td>Padding of the graph drawn (excluding labels)</td>
104 <td>Object with properties: top, bottom, left, right.</td>
105 <td>{left: 30, right:20, top: 10, bottom: 10}</td>
120 <th>axisLineColor</th>
121 <td>Color of axes line.</td>
122 <td>MochiKit.Color.Color</td>
123 <td>Color.blackColor()</td>
126 <th>axisLineWidth</th>
127 <td>axis line width</td>
132 <th>axisTickSize</th>
133 <td>length or height of a tick on the y and x axis respectively, in pixels</td>
138 <th>axisLabelColor</th>
139 <td>color of text label on axis.</td>
140 <td>MochiKit.Color.Color</td>
141 <td>Color.blackColor()</td>
144 <th>axisLabelFontSize</th>
145 <td>Font size of labels in pixels </td>
150 <th>axisLabelWidth</th>
151 <td>Width of labels on ticks, in pixels</td>
156 <th>enableEvents</th>
157 <td>Enable events (if supported)</td>
164 Internal Renderer Methods and Style
165 ===================================
167 The default renderers that are available follow a rough structure. If
168 you plan to write a new renderer, you should think about using a
171 Also, it is important that you follow an Object Orientated style and
172 split up the rendering methods as much as logically possible to allow
173 other developers to extend the work by using a "psuedo subclassing"
174 method described below.
179 PlotKit Renderers should adopt a Javascript subclassing structure to
180 allow developers/themers to customise certain aspects of the
181 rendering. Here is an example of what is expected:
183 MyRenderer = function(element, layout, options) {
184 if (arguments.length > 0)
185 this.__init__(element, layout, options);
188 MyRenderer.prototype.__init__ = function(element, layout, options) {
192 In this case, the default javascript constructor acts only when passed
193 arguments. ``MyRenderer.prototype.__init__`` is the real
194 constructor. It is named in similar vein to Python's constructor.
196 For users who would like to subclass, they will need to use the
197 following snippet of code:
199 MyAlternateRenderer = function(element, layout. options) {
200 if (arguments.length > 0)
201 this.__init__(element, layout, options);
203 MyAlternateRenderer.prototype = new MyRenderer();
204 MyAlternateRenderer.prototype.constructor = MyAlternateRenderer;
205 MyAlternateRenderer.__super__ = MyRenderer.prototype;
207 MyAlternateRenderer.prototype.__init__ = function(element, layout, options) {
208 MyAlternateRenderer.__super__.__init__.call(this, element, layout, options);
212 For subclasses, they will need the following magic in order to
213 initialise their subclass. But after that, you can either override
214 ``MyAlternateRenderer.prototype.__init__`` with your own
215 implementation or just leave the superclass to deal with the
218 A more thorough example can be found in the PlotKit source for
219 ``Canvas.js`` and ``SweetCanvas.js`` respectively.
221 Internal Renderer Properties
222 ----------------------------
224 The bundled renderers are have the following common properties to
225 allow standard access by all subclasses:
229 The PlotKit.Layout object passed by the user.
233 The HTML element to use, either a Canvas Element or SVG Element depending
234 on whether a Canvas Renderer or SVG Renderer is in use.
238 A dictionary of options that are applicable to the rendering style.
242 A list of elements that represent the axis. Should be cleared whenever
243 ``clear()`` is executed.
247 A list of elements that represent the axis. Should be cleared whenever
248 ``clear()`` is executed.
250 Internal Renderer Methods
251 -------------------------
253 * ``_renderBarChart()``
255 Renders only the bars of a bar chart on the element by looking at
256 ``this.layout.bars`` for the bars to render. Will only be called if
257 ``this.layout.style == "bars"``
259 * ``_renderLineChart()``
261 Renders only the lines of a line chart on the element by looking at
262 ``this.layout.points`` for the points to render. Will only be called if
263 ``this.layout.style == "line"``
265 * ``_renderPieChart()``
267 Renders only the slices of the pie in ``this.layout.slices``.
268 Will only be called if ``this.layout.style == "pie"``
270 * ``_renderBarAxis()``
272 Renders the axis for a bar chart by looking at the
273 ``this.layout.xticks`` and ``this.layout.yticks``.
275 * ``_renderLineAxis()``
277 Renders the axis for a line chart by looking at the
278 ``this.layout.xticks`` and ``this.layout.yticks``.
280 * ``_renderPieAxis()``
282 Renders the labels for a pie chart by looking at
283 ``this.layout.xticks`` only.
285 * ``_renderBackground()``
287 Called to render the background of the chart. Should check whether
288 ``this.options.drawsBackground`` is set before proceeding.
291 Events from the Chart
292 =====================
294 There is preliminary support for events from the chart for the Canvas
295 Renderer but the API is not stablised and subject to change. __(TODO)__.