1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><script type="text/javascript" src="../ext-all.js"></script><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../scrollbars.css" type="text/css"><link rel="stylesheet" href="../docs.css" type="text/css"><link id="styleCss" rel="stylesheet" href="../style.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script><link rel="stylesheet" href="../prettify.css" type="text/css"><!-- link(rel: 'stylesheet', href: req.baseURL + '/css/ext4.css', type: 'text/css')--><link rel="shortcut icon" type="image/ico" href="../favicon.ico"><!--[if IE]>
2 <style type="text/css">.head-band { display: none; }
3 .header { border: 0; top: 0; left: 0px; background: url(../header.gif) repeat-x; }
4 .doc-tab .members .member a.more { background-color: #efefef; }
5 </style><link rel="stylesheet" href="/new/css/ie.css" type="text/css"><![endif]-->
6 </head><body id="ext-body" class="iScroll"><div id="notice" class="notice">For up to date documentation and features, visit
7 <a href="http://docs.sencha.com/ext-js/4-0">http://docs.sencha.com/ext-js/4-0</a></div><div class="wrapper"><div class="head-band"></div><div class="header"><h2><a href="../index.html">Sencha Documentation</a></h2></div><div id="search"><form><input type="text" placeholder="Search" id="search-field" autocomplete="off" name="q"></form><div id="search-box"></div></div><div id="treePanel"></div><div id="container"><script type="text/javascript">
12 origDocClass: 'undefined',
13 docClass: 'undefined',
27 <script type="text/javascript" src="../search.js"></script>
28 <!--script type="text/javascript" src="/new/javascripts/app/examples.js"></script-->
29 <script type="text/javascript" src="../class_tree.js"></script>
30 <script type="text/javascript" src="../class_doc.js"></script>
31 <div id="top-block" class="top-block"></div><div id="docContent"><div class="guide"><h1>Drawing and Charting</h1>
35 <p><iframe src="http://player.vimeo.com/video/17673342?byline=0&portrait=0" width="500" height="281" frameborder="0"></iframe></p>
37 <p>This document is intended to guide you through the overall design and implementation details
38 of the Drawing and Charting packages. The drawing and charting packages enable you to create
39 cross browser and cross device graphics in a versatile way.</p>
41 <p>The structure of this document will cover three main topics:</p>
43 <ul><li>Section I: <a href="drawing_and_charting.html#">"Draw"</a> a versatile cross-browser/device package to draw general purpose
44 graphics and animations.</li><li>Section II: <a href="drawing_and_charting.html#">"Chart"</a> A high level presentation of the charting package and how classes are
45 organized in it.</li><li>Section III: <a href="drawing_and_charting.html#">"Series"</a> A presentation of the available series and their use.</li></ul>
47 <h2>I. The Draw Package</h2>
51 <p>The design choices in the graphics team concerning drawing were not just contrained to charting:
52 we needed a versatile tool that would enable us to create custom graphics in a cross-browser/device manner and also perform rich animations with them.</p>
54 <p>The Draw package contains a <a href="drawing_and_charting.html#">Surface</a> class that abstracts the underlying graphics implementation
55 and enables the developer to create arbitrarily shaped <a href="drawing_and_charting.html#">Sprites</a> or <a href="drawing_and_charting.html#">SpriteGroups</a> that respond to
56 interactions like mouse events and also provide rich animations on all attributes like shape, color, size,
59 <p>The underlying/concrete implementations for the <a href="drawing_and_charting.html#">Surface</a> class are <a href="drawing_and_charting.html#">SVG</a> (for SVG capable browsers) and
60 <a href="drawing_and_charting.html#">VML</a> (for the Internet Explorer family - < 9). <a href="drawing_and_charting.html#">Surface</a> can be considered as an interface for
61 the <a href="drawing_and_charting.html#">SVG</a> and <a href="drawing_and_charting.html#">VML</a> rendering engines. Surface is agnostic to its underlying implementations. Most of the methods and ways
62 to create sprites are heavily inspired by the <a href="http://www.w3.org/TR/SVG/">SVG standard</a>. </p>
64 <h3>Creating a Drawing Surface</h3>
66 <p>You can create a simple drawing surface without loading the Charting package at all. This can be useful
67 to create arbitrary graphics that work on all browsers/devices and animate well. For example, you could
68 create an interactive map of the United States where each state is a sprite, or also an infographic where
69 each element is also a sprite. What's interesting about making sprites and not images is that the document
70 acquires a new level of interactivity but also that being VML and SVG based the images will never loose quality
71 and can be printed correctly.</p>
73 <p>In order to use the Draw package directly you can create a <a href="drawing_and_charting.html#">Draw Component</a> and (for example) append it to an <code>Ext.Window</code>:</p>
75 <pre class="prettyprint"><code>var drawComponent = Ext.create('Ext.draw.Component', {
86 Ext.create('Ext.Window', {
90 items: [drawComponent]
91 }).show();</code></pre>
93 <p>In this case we created a draw component and added a sprite to it. The <em>type</em> of the sprite is <em>circle</em> so if you run this code
94 you'll see a yellow-ish circle in a Window. When setting <code>viewBox</code> to <code>false</code> we are responsible for setting the object's position and
95 dimensions accordingly.</p>
97 <p>Sprites can have different types. Some of them are:</p>
99 <ul><li><em>circle</em> - To draw circles. You can set the radius by using the <em>radius</em> parameter in the sprite configuration.</li><li><em>rect</em> - To render rectangles. You can set the width and height of the rectangle by using the <em>width</em> and <em>height</em> parameters
100 in the sprite configuration.</li><li><em>text</em> - To render text as a sprite. You can set the font/font-size by using the <em>font</em> parameter.</li><li><em>path</em> - The most powerful sprite type. With it you can create arbitrary shapes by using the <a href="http://www.w3.org/TR/SVG/paths.html">SVG path syntax</a>.
101 You can find a quick tutorial on to how to get started with
102 the path syntax <a href="https://developer.mozilla.org/en/SVG/Tutorial/Paths">here</a>.</li></ul>
104 <p>A Sprite is an object rendered in a Drawing surface. There are different options and types of sprites.
105 The configuration of a Sprite is an object with the following properties:</p>
109 <b>type </b> - (String) The type of the sprite. Possible options are 'circle', 'path', 'rect', 'text', 'square'.
112 <b>width </b> - (Number) Used in rectangle sprites, the width of the rectangle.
115 <b>height </b> - (Number) Used in rectangle sprites, the height of the rectangle.
118 <b>size </b> - (Number) Used in square sprites, the dimension of the square.
121 <b>radius </b> - (Number) Used in circle sprites, the radius of the circle.
124 <b>x </b> - (Number) The position along the x-axis.
127 <b>y </b> - (Number) The position along the y-axis.
130 <b>path </b> - (Array) Used in path sprites, the path of the sprite written in SVG-like path syntax.
133 <b>opacity </b> - (Number) The opacity of the sprite.
136 <b>fill </b> - (String) The fill color.
139 <b>stroke </b> - (String) The stroke color.
142 <b>stroke-width </b> - (Number) The width of the stroke.
145 <b>font </b> - (String) Used with text type sprites. The full font description. Uses the same syntax as the CSS <code>font</code> parameter.
148 <b>text </b> - (String) Used with text type sprites. The text itself.
152 <p> Additionally there are three transform objects that can be set with <code>setAttributes</code> which are <code>translate</code>, <code>rotate</code> and
153 <code>scale</code>.</p>
155 <p> For translate, the configuration object contains x and y attributes for the translation. For example:</p>
157 <p> <pre class="prettyprint"><code>
158 sprite.setAttributes({
166 <p> For rotate, the configuration object contains x and y attributes for the center of the rotation (which are optional),
167 and a degrees attribute that specifies the rotation in degrees. For example:</p>
169 <p> <pre class="prettyprint"><code>
170 sprite.setAttributes({
177 <p> For scale, the configuration object contains x and y attributes for the x-axis and y-axis scaling. For example:</p>
179 <p> <pre class="prettyprint"><code>
180 sprite.setAttributes({
188 <h3>Interacting with a Sprite</h3>
190 <p>Now that we've created a draw surface with a sprite in it, let's dive into how to interact with the sprite.
191 We can get a handle to the sprite we want to modify by adding that sprite imperatively to the surface:</p>
193 <pre class="prettyprint"><code>// Create a draw component
194 var drawComponent = Ext.create('Ext.draw.Component', {
198 // Create a window to place the draw component in
199 Ext.create('Ext.Window', {
203 items: [drawComponent]
206 // Add a circle sprite
207 var myCircle = drawComponent.surface.add({
215 // Now do stuff with the sprite, like changing its properties:
216 myCircle.setAttributes({
220 // or animate an attribute on the sprite
228 // Add a mouseup listener to the sprite
229 myCircle.addListener('mouseup', function() {
230 alert('mouse upped!');
233 <p>In this example we've seen how we can add events, set sprite attributes and animate these attributes using the
234 draw package. As you can see this package is a versatile abstraction layer over the graphics we can do. What's
235 most interesting about this class is that we aren't tied to a specific shape or structure; also all elements
236 support events, setting attributes and creating animations. Most important of all, all of this is compatible in all browsers and
241 <p>So now that we learnt about the expressive power of the draw package, let's dive into charts. The chart
242 package consists of a hierarchy of classes that define a chart container (something like a surface but more specific for
243 handling charts); axes, legends, series, labels, callouts, tips, cartesian and radial coordinates, and specific series
244 like <a href="drawing_and_charting.html#">Pie</a>, <a href="drawing_and_charting.html#">Area</a>, <a href="drawing_and_charting.html#">Bar</a>, etc.</p>
246 <p>In this section we will cover how these classes are tied together and what bits of functionality go into each of these
247 classes. We won't cover each particular series, since that is done in the next section.</p>
251 <p>The Chart class is the main drawing surface for series. It manages the rendering of each series and also how axes are
252 drawn and defined. Chart also delegates mouse events over to different areas of the Chart like Series, Axes, etc.
253 The <a href="drawing_and_charting.html#">Chart class</a> extends <a href="drawing_and_charting.html#">Draw Component</a>.</p>
255 <p>A Chart instance has access to:</p>
257 <ul><li>axes - Accessed through <code>chart.axes</code>. All the axes being defined and drawn for this visualization. This is a mixed collection.</li><li>series - Accessed through <code>chart.series</code>. All the series being drawn for the chart. This could be line, bar, scatter, etc. This is also a mixed collection.</li><li>legend - The legend box object and its legend items.</li></ul>
259 <p>The chart instance supports custom events that can be triggered right before and during the rendering of the visualization.
260 We can add handlers for these events by using:</p>
262 <pre class="prettyprint"><code>chart.on({
263 'refresh': function() {
264 alert('(re)drawing the chart');
268 <p>Chart also delegates events like <code>itemmousedown</code> and <code>itemmouseup</code> to the series so that we can append
269 listeners to those objects and get the target sprite of the event.</p>
273 <p>The chart configuration object accepts a <code>legend</code> parameter to enable legend items for each series and
274 to set the position of the legend. These options are passed into the constructor of the chart. For example:</p>
276 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
286 axes: [/*set an axis configuration*/],
289 series: [/*set series configuration*/]
292 <p>Each series object needs to have the <code>showInLegend</code> parameter set to <code>true</code> in order to be in the legend list.</p>
296 <p>The <code>axis</code> package contains an <code>Abstract</code> axis class that is extended by <code>Axis</code> and <code>Radial</code> axes. <code>Axis</code> represents
297 a <code>Cartesian</code> axis and <code>Radial</code> uses polar coordinates to represent the information for polar based visualizations like
298 Pie and Radar series. Axes are bound to the type of data we're trying to represent. There are axes for categorical
299 information (called <code>Category</code> axis) and also axis for quantitative information like <code>Numeric</code>. For time-based information
300 we have the <code>Time</code> axis that enables us to render information over a specific period of time, and to update that period of time
301 with smooth animations. If you'd like to know more about each axis please go to the axis package documentation. Also, you will find
302 configuration examples for axis in the bottom series examples.</p>
304 <p>An axis contains divisions and subdivisions of values, represented by major and minor ticks. These can be adjusted automatically
305 or manually to some specified interval, maximum and minimum values. The configuration options <code>maximum</code>, <code>minimum</code>, <code>majorTickSteps</code> and
306 <code>minorTickSteps</code> in the <code>Numeric</code> axis are used to change the configuration and placement of the major and minor ticks. For example, by
309 <pre class="prettyprint"><code> axes: [{
313 title: 'Number of Hits',
315 //one minor tick between two major ticks
321 title: 'Month of the Year'
324 <p>The following configuration will produce minor ticks in the left axis
325 for the line series:</p>
327 <p><img alt="Series Image" src="../Ticks.jpg"></img></p>
331 <p>The drawing and charting package has also the power to create
332 linear gradients. The gradients can be defined in the Chart configuration
333 object as an array of gradient configurations. For each gradient configuration
334 the following parameters are specified:</p>
336 <ul><li><strong>id</strong> - string - The unique name of the gradient.</li><li><strong>angle</strong> - number, optional - The angle of the gradient in degrees.</li><li><strong>stops</strong> - object - An object with numbers as keys (from 0 to 100) and style objects as values.</li></ul>
338 <p>Each key in the stops object represents the percentage of the fill on the specified color for
343 <pre class="prettyprint"><code> gradients: [{
370 <p>You can apply a gradient to a sprite by setting a reference to a gradient <strong>id</strong> in
371 the fill property. This reference is done via a url syntax. For example:</p>
373 <pre class="prettyprint"><code> sprite.setAttributes({
374 fill: 'url(#gradientId)'
375 }, true);</code></pre>
379 <p>A <code>Series</code> is an abstract class extended by concrete visualizations like
380 <code>Line</code> or <code>Scatter</code>. The <code>Series</code> class contains code that is common to all of these series, like event handling, animation
381 handling, shadows, gradients, common offsets, etc. The <code>Series</code> class is enhanced with a set of <em>mixins</em> that provide functionality
382 like highlighting, callouts, tips, etc. A <code>Series</code> will contain an array of <code>items</code> where each item contains information about the
383 positioning of each element, its associated <code>sprite</code> and a <code>storeItem</code>. The series also share the <code>drawSeries</code> method that updates
384 all positions for the series and then renders the series.</p>
388 <p>The Chart configuration object may have a <code>theme</code> property with a string value that references a builtin theme name.</p>
390 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
392 /* Other options... */
395 <p>A Theme defines the style of the shapes, color, font, axes and background
396 of a chart. The theming configuration can be very rich and complex:</p>
398 <pre class="prettyprint"><code>{
473 <p>We can also create a seed of colors that will be the base for the entire theme just by creating
474 a simple array of colors in the configuration object like:</p>
476 <pre class="prettyprint"><code>{
477 colors: ['#aaa', '#bcd', '#eee']
480 <p>When setting a base color the theme will generate an array of colors that match the base color:</p>
482 <pre class="prettyprint"><code>{
486 <p>You can create a custom theme by extending from the base theme. For example, to create a custom <code>Fancy</code> theme we can do:</p>
488 <pre class="prettyprint"><code>var colors = ['#555',
494 var baseColor = '#eee';
496 Ext.define('Ext.chart.theme.Fancy', {
497 extend: 'Ext.chart.theme.Base',
499 constructor: function(config) {
500 this.callParent([Ext.apply({
522 var chart = Ext.create('Ext.chart.Chart', {
525 /* Other options here... */
530 <p>The following section will go through our available series/visualizations, introduce each
531 one of them and show a complete configuration example of the series. The example will include the <code>Chart</code>,
532 <code>Axis</code> and <code>Series</code> configuration options.</p>
536 <p>Creates a Stacked Area Chart. The stacked area chart is useful when displaying multiple aggregated layers of information.
537 As with all other series, the Area Series must be appended in the <em>series</em> Chart array configuration.</p>
539 <p><img alt="Series Image" src="../Area.jpg"></img></p>
541 <p>A typical configuration object for the area series could be:</p>
543 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
544 renderTo: Ext.getBody(),
553 // Add Numeric and Category axis
558 fields: ['data1', 'data2', 'data3'],
559 title: 'Number of Hits',
569 adjustMinimumByMajorUnit: 0
574 title: 'Month of the Year',
583 // Add the Area Series
589 yField: ['data1', 'data2', 'data3'],
598 <p>Creates a Bar Chart. A Bar Chart is a useful visualization technique to display quantitative information for different
599 categories that can show some progression (or regression) in the dataset.
600 As with all other series, the Bar Series must be appended in the <em>series</em> Chart array configuration. See the Chart
601 documentation for more information.</p>
603 <p><img alt="Series Image" src="../Bar.jpg"></img></p>
605 <p>A typical configuration object for the bar series could be:</p>
607 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
608 renderTo: Ext.getBody(),
618 title: 'Number of Hits'
623 title: 'Month of the Year'
633 display: 'insideEnd',
635 renderer: Ext.util.Format.numberRenderer('0'),
636 orientation: 'horizontal',
638 'text-anchor': 'middle'
645 <p>Creates a Line Chart. A Line Chart is a useful visualization technique to display quantitative information for different
646 categories or other real values (as opposed to the bar chart), that can show some progression (or regression) in the dataset.
647 As with all other series, the Line Series must be appended in the <em>series</em> Chart array configuration. See the Chart
648 documentation for more information.</p>
650 <p><img alt="Series Image" src="../Line.jpg"></img></p>
652 <p>A typical configuration object for the line series could be:</p>
654 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
655 renderTo: Ext.getBody(),
666 fields: ['data1', 'data2', 'data3'],
667 title: 'Number of Hits'
672 title: 'Month of the Year'
675 // Add two line series
702 <p>A marker configuration object contains the same properties used to create a Sprite.
703 You can find the properties used to create a Sprite in the Sprite section above.</p>
707 <p>Creates a Pie Chart. A Pie Chart is a useful visualization technique to display quantitative information for different
708 categories that also have a meaning as a whole.
709 As with all other series, the Pie Series must be appended in the <em>series</em> Chart array configuration. See the Chart
710 documentation for more information. A typical configuration object for the pie series could be:</p>
712 <p><img alt="Series Image" src="../Pie.jpg"></img></p>
714 <p>A typical configuration object for the pie series could be:</p>
716 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
722 renderTo: Ext.getBody(),
727 theme: 'Base:gradients',
748 <p>Creates a Radar Chart. A Radar Chart is a useful visualization technique for comparing different quantitative values for
749 a constrained number of categories.
750 As with all other series, the Radar series must be appended in the <em>series</em> Chart array configuration. See the Chart
751 documentation for more information.</p>
753 <p><img alt="Series Image" src="../Radar.jpg"></img></p>
755 <p>A typical configuration object for the radar series could be:</p>
757 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
762 renderTo: Ext.getBody(),
773 // Add two series for radar.
805 <p>Creates a Scatter Chart. The scatter plot is useful when trying to display more than two variables in the same visualization.
806 These variables can be mapped into x, y coordinates and also to an element's radius/size, color, etc.
807 As with all other series, the Scatter Series must be appended in the <em>series</em> Chart array configuration. See the Chart
808 documentation for more information on creating charts.</p>
810 <p><img alt="Series Image" src="../Scatter.jpg"></img></p>
812 <p>A typical configuration object for the scatter series could be:</p>
814 <pre class="prettyprint"><code>var chart = Ext.create('Ext.chart.Chart', {
819 renderTo: Ext.getBody(),
823 fields: ['data1', 'data2', 'data3'],
824 title: 'Number of Hits'
859 <p> Creates a Gauge Chart. Gauge Charts are used to show progress in a certain variable. There are two ways of using the Gauge chart.
860 One is setting a store element into the Gauge and selecting the field to be used from that store. Another one is instanciating the
861 visualization and using the <code>setValue</code> method to adjust the value you want.</p>
863 <p> <img alt="Series Image" src="../Gauge.jpg"></img></p>
865 <p> A chart/series configuration for the Gauge visualization could look like this:</p>
867 <p> <pre class="prettyprint"><code>
883 colorSet: ['#F49D10', '#ddd']
888 <p> In this configuration we create a special Gauge axis to be used with the gauge visualization (describing half-circle markers), and also we're
889 setting a maximum, minimum and steps configuration options into the axis. The Gauge series configuration contains the store field to be bound to
890 the visual display and the color set to be used with the visualization.
891 </p></div></div></div></div></body></html>