# API documentation

- Overview
- Canvas and context references
- Working with events
- Working with chart coordinates
- Working with chart data
- Updating your charts dynamically
- Setting properties
- References available on OfficeExcel objects
- Scale information
- Adding text to your charts
- OfficeExcel functions
  - Arrays
  - Strings
  - Numbers
  - Miscellaneous
  - Custom OfficeExcel event related functions
  - Other
- The skeleton file

#### Overview

Working with OfficeExcel objects is purposefully easy, to make them straight forward to integrate into your own scripts if you want to. For any particular chart type there are a few files required - the common libraries and the chart specific library. Eg:

    <script src="OfficeExcel.common.core.js"></script>
    <script src="OfficeExcel.bar.js"></script>

OfficeExcel.common.core.js is a function library that contains a large set of functions that support the chart classes. Some functions, and sets of functions, have their own files. For example, the zoom functions reside in OfficeExcel.common.zoom.js, so if you don't need zoom, you don't need this file. Each of the chart libraries (OfficeExcel.*.js) contains that particular charts class. If you'd like to see a "bare bones" implementation, you can look at the basic example. There's also a skeleton example to make it easier to create new chart types.

#### Canvas and context references

Each chart object maintains references to the context and canvas as properties. So to get hold of them all you need to do is this:

    <script>
        window.onload = function ()
        {
            // 1/2 First draw the chart
            var myBar = new OfficeExcel.Bar('myCanvas', [1,5,8,4,6,3,1,5,7,8,4,6]);
            myBar.Set('chart.labels', ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
            myBar.Draw();

            // 2/2 Now get hold of the references
            var context = myBar.context; // Get hold of a reference to the context
            var canvas  = myBar.canvas;  // Get hold of a reference to the canvas
        }
    </script>

#### Working with events

When working with events, you may come across the situation where you have a reference to the canvas, but also need to access the chart object. For this reason the constructor of each object adds a reference to the object to the canvas and you can access it like this:

    <script>
        document.getElementById("myCanvas").onclick = function (e)
        {
            var src = (OfficeExcel.isIE8() ? event.srcElement) : e.target;

            // The OfficeExcel object constructors add __object__ to the canvas.
            var myBar = src.__object__;
        }
    </script>

#### Working with chart coordinates

For most chart types, the coordinates of elements (eg bars, lines etc) are to be found in the class variable obj.coords. This is usually a multi-dimensional array consisting of the coordinates of those shapes, or of points. As an example the bar chart maintains the X, Y, width and height of each bar (or sections of bars in a stacked bar chart). The coords array is usually a flat array, even when you have multiple sets of data.

By using the OfficeExcel.getMouseXY() function and this array you can determine if a bar was clicked on, or if the mouse is near a line point etc.

    var myCoords = myBar.coords;

Note: Previously the coords array values sometimes included the margin values, and sometimes didn't. As of 17th April 2010 the values have all been unified to not include the margin values, so the values are as reported.

Note: The Line chart also has an object variable myObj.coords2, where the coordinates are indexed differently - by line index.

#### Working with chart data

Another variable you may need is the data variable. For most chart types, this is where the data is stored. It is usually untouched, so it is as you supplied to the chart objects constructor. A notable exception to this is filled line charts. Here the original data is stored in the class variable original_data. The data supplied is modified to produce the stacking effect. If you need to modify a filled line charts data you will need to change this variable instead.

Not all chart types use the data variable. For some the name is different so that it makes a little more sense. The chart types and their associate data variables are listed below1.

Chart type | Data variable(s)
--- | ---
Bar | obj.data
Bi-polar | obj.left, obj.right
Donut | This is now a variant of the Pie chart
Fuel | obj.min, obj.max, obj.value
Funnel | obj.data
Gantt | See the docs
Gauge | obj.min, obj.max, obj.value
Horizontal Bar | obj.data
Horizontal Progress bar | obj.max, obj.value
LED | obj.text
Line | obj.original_data3
Meter | obj.min, obj.max, obj.value
Odometer | obj.start, obj.end, obj.value
Pie | obj.angles, obj.data
Radial Scatter chart | obj.data
Rose | obj.max, obj.data
Scatter | obj.max, obj.data2
Thermometer | obj.min, obj.max, obj.value
Traditional Radar | obj.data, obj.max
Vertical Progress bar | obj.max, obj.value
Waterfall chart | obj.max, obj.data

1. In the table, obj refers to your chart object.
2. For the Scatter chart, each data point is an array of X/Y coordinates, the color and the tooltip for that point.
3. The Line chart obj.original_data is an aggregation of all the datasets given to the Line chart, so the first dataset is held in obj.original_data[0], the second in obj.original_data[1] etc.

#### Updating your charts dynamically

Note: It is important that you're careful with types when making AJAX requests. Since the response is initially a string, and your AJAX function/library may not do conversions for you, you may need to convert these strings to numbers. To do this you can use the Number() or parseInt() functions. For example:

    <script>
        num = Number('23');
        num = parseInt('43');
    </script>

A common request is to be able to update charts dynamically. This is quite simple and consists of three steps:

1. Get the new data from the server (with an AJAX request for example).
2. Set the data in your chart object. See the above table for the appropriate property to use.
3. Call the .Draw() method again.

If you don't need to get data from your server (ie it's all client-side) then you can omit the first step. Also, it may be sufficient to simply recreate the entire object from scratch. This means that you won't have to alter and OfficeExcel internal properties - just recreate the chart object and configuration. There's a simple function you can use for AJAX purposes here.

#### Setting properties

To set OfficeExcel properties, ideally you should use each objects setter (there's also a corresponding getter). These functions accomodate some backwards compatibility changes, so by not using them you run the risk of your charts not working entirely as expected.

    myObj.Set('chart.gutter.left', 25);
    myObj.Get('chart.gutter.left');

#### References available on OfficeExcel objects

OfficeExcel stores various references to objects on the canvas (typically) to make getting hold of them easier. There's also a Registry object in which references are stored. Typically the objects are named with the format __xxx__, and you can inspect the objects by using a console(eg the inspector that's part of Google Chrome - CTRL+SHIFT+J). Some examples are:

- __object__ on the canvas - a reference to the chart object
- OfficeExcel.Registry.Get('chart.tooltip') - a reference to the currently visible tooltip. This in turn has the following available:
  - __text__ - Since setting .innerHTML can cause changes in HTML, this is the original tooltip text.
  - __index__ - This is the numerical index corresponding to the tooltips array that you set.
  - __dataset__ - Used in the Scatter chart and corresponding to the dataset.
  - __canvas__ - A reference to the original canvas that triggered the tooltip.
- OfficeExcel.Registry.Get('chart.mousedown') - Used in annotating, and stipulates whether the mouse button is currently pressed.
- OfficeExcel.Registry.Get('chart.contextmenu') - The currently shown context menu, if any. This in turn has the following properties:
  - __canvas__ - The original canvas object.

These are just a sample of what's available, to find more you should use an introspection tool or look at the source.

#### Scale information

For the Bar, Bipolar, HBar, Line and Scatter charts the scale that is used is stored in the scale class variable. Eg:

    <script>
        var myBar = new OfficeExcel.Bar('cvs', [56,43,52,54,51,38,46,42,52]);
        myBar.Draw();

        var myScale = myBar.scale
    </script>

#### Adding text to your charts

If you want to add arbitrary text to your chart(s) you can use the API function OfficeExcel.Text().

    <script>
        // Draw a basic chart
        var myObj = new OfficeExcel.Bar('myCanvas', [4,5,5,8,7,6,4,8,5,9]);
        myObj.Draw();

        // Draw some text
        myObj.context.beginPath();
            size = 12;
            font = 'Verdana';
            text = 'Some text!;
            x    = 10;
            y    = 10;

            OfficeExcel.Text(myObj.context, font, size, x, y, text);
        myObj.context.fill();
    </script>

#### OfficeExcel functions

This is a list of some of the functions available to you in the OfficeExcel common libraries. It's not every single one that's available, but is a list of the common ones that you're likely to want to use. Arguments in square brackets are optional.

    <script src="OfficeExcel.common.core.js"></script>

    <script>
        // Returns 9
        myArray = [3,2,5,4,9,7,8];
        max = OfficeExcel.array_max(myArray);
    </script>

#### Arrays

- (number) OfficeExcel.array_max(array)
  Returns the maximum value in the array.
  File: OfficeExcel.common.core.js

- (number) OfficeExcel.array_sum(array)
  Returns the sum of all values in the array. You can also pass a single integer, in which case it is simply returned as-is.
  File: OfficeExcel.common.core.js

- (array) OfficeExcel.array_clone(array)
  Creates a copy/clone of the given array. Only numerically indexed arrays are supported.
  File: OfficeExcel.common.core.js

- (array) OfficeExcel.array_reverse(array)
  Returns a reversal of the given array.
  File: OfficeExcel.common.core.js

- (boolean) OfficeExcel.is_array(obj)
  Returns true or false as to whether the given object is an array or not.
  File: OfficeExcel.common.core.js

- (array) OfficeExcel.array_pad(array, length[, value])
  Pads the given array to the specified length (if the length is less, if not the array is returned as is). The third, optional, argument is the value which is used as the pad value. If not specified, null is used.
  File: OfficeExcel.common.core.js

- (array) OfficeExcel.array_shift(array)
  Shifts an element off of the start of the given array and returns the new array.
  File: OfficeExcel.common.core.js

#### Strings

- (string) OfficeExcel.rtrim(string)
  Strips the right hand white space from the string that you pass it.
  File: OfficeExcel.common.core.js

- (string) OfficeExcel.number_format(obj, number[, prepend[, append]])
  This formats the given number (which can be either an integer or a float. The prepend and append variables are strings which are added to the string (eg 5%).
  Note:As of 5th September 2010 this functions argument list has been changed to include the chart object, as shown above.
  File: OfficeExcel.common.core.js

#### Numbers

- (number) OfficeExcel.random(min, max)
  Returns a random number between the minimum and maximum that you give.
  File: OfficeExcel.common.core.js
- (string) OfficeExcel.number_format(number[, prepend[, append]])
  See above.
  File: OfficeExcel.common.core.js

#### Miscellaneous

- (object) OfficeExcel.Effects.ReplaceCanvasWithDIV(canvas)
  This function is used by some animation functions to stop the page layout changing when the canvas is removed from the document. It removes the canvas from the document, replaces it with a DIV and then adds the canvas as a child node of the DIV. The canvas is placed in the center of the DIV.
  File: OfficeExcel.common.effects.js

- (object) OfficeExcel.FixEventObject(event)
  Pass this function an event object and it will attempt to "fill in" the missing properties (depending on the browser). It tries to add:
  - pageX (MSIE)
  - pageY (MSIE)
  - target (MSIE)
  - stopPropagation() (MSIE)
  - offsetX (FF)
  - offsetY (FF)
  File: OfficeExcel.common.core.js

- (null) OfficeExcel.OldBrowserCompat(context)
  This function "fills in" any required functions that some browsers don't have. At the moment this consists of adding the measureText() and fillText() functions to the context when they're missing (usually this means older versions of Opera).
  File: OfficeExcel.common.core.js

- (number) OfficeExcel.GetDays(date)
  This returns the number of days in the given month. The argument should be a Date object.
  File: OfficeExcel.common.core.js

- (null) OfficeExcel.pr(mixed)
  Emulates the PHP function print_r() by recursively printing the structure of whatever you pass to it. It handles numbers, strings, arrays, booleans, functions and objects.
  File: OfficeExcel.common.core.js

- (null) p(mixed)
  An alias of the above albeit far shorter to type.
  File: OfficeExcel.common.core.js

- (null) cl(mixed)
  A shortcut for the Firebug and Google Chrome debug function console.log().
  File: OfficeExcel.common.core.js

- (null) OfficeExcel.Clear(canvas[, color])
  Clears the canvas by drawing a white (or the optional color you give) rectangle over it. As of 23rd January 2011 you can now specify transparent as the color and the canvas will be reset back to transparency instead of to white. This may become the default at a later date.
  File: OfficeExcel.common.core.js

- (null) OfficeExcel.ClearAnnotations(id)
  This function clears the annotation data associated with the given canvas ID (but DOES NOT redraw the chart). If you want to clear the visible annotations too, you will need to redraw the canvas. You could do this using the following code:

      function ClearAndRedraw (obj)
      {
          OfficeExcel.Clear(obj.canvas); // This function also calls the OfficeExcel.ClearAnnotations() function
          obj.Draw();
      }

  File: OfficeExcel.common.annotatate.js

- (null) OfficeExcel.ReplayAnnotations(object)
  This function redraws any annotations which have previously been drawn on the canvas. You would use this function when annotations are turned off and you want previous annotations to be displayed.
  File: OfficeExcel.common.annotate.js

- (array) OfficeExcel.getMouseXY(event)
  When passed your event object, it returns the X and Y coordinates (in relation to the canvas) of the mouse pointer. (0,0) is in the top left corner, with the X value increasing going right and the Y value increasing going down.
  File: OfficeExcel.common.core.js

- (array) OfficeExcel.getCanvasXY(canvas)
  Similar to the above function but returns the X/Y coordinates of the canvas in relation to the page.
  File: OfficeExcel.common.core.js

- (number) OfficeExcel.degrees2Radians(number)
  Converts and returns the given number of degrees to radians. 1 radian = 57.3 degrees.
  File: OfficeExcel.common.core.js

- (array) OfficeExcel.getScale(max, obj)
  Given the maximum value this will return an appropriate scale. The second argument is the graph object.
  File: OfficeExcel.common.core.js

- (mixed) OfficeExcel.Registry.Get(name)
  In conjunction with the next function, this is an implementation of the Registry pattern which can be used for storing settings.
  File: OfficeExcel.common.core.js
- (mixed) OfficeExcel.Registry.Set(name, value)
  In conjunction with the previous function, this is an implementation of the Registry pattern which can be used for storing settings.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.Register(object)
  This function is used in conjunction with the next to register a canvas for redrawing. Canvases are redrawn (for example) when tooltips or crosshairs are being used.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.Redraw([id, [color]])
  This function is used in conjunction with the previous to redraw a canvas. Canvases are redrawn (for example) when tooltips or crosshairs are being used.
  Note: All canvases that are registered are redrawn. However the optional first argument can be an ID (a string) of the canvas that is NOT to be redrawn. The optional second argument is the color to use when clearing canvases.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.SetShadow(object, color, offetX, offsetY, blur)
  This function is a shortcut function used to set the four shadow properties. The arguments are: your chart object, the shadow color, the X offset, the Y offset, the shadow blur.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.NoShadow(object)
  This function is a shortcut function used to "turn off" the shadow. The argument is your chart object.
  File: OfficeExcel.common.core.js
- (number) OfficeExcel.Async(mixed[, delay])
  This is a simple function but has significant implications on your pages performance. You can pass this either a function pointer, or a string containing the code to run and it will run the code asynchronously (ie in parallel to the page loading). In doing so it can mean that your page is displayed faster, as the chart is created seperate to the page loading. As such, the placement of your canvas tag becomes more important. What you can do is define the canvas tag and then the code to produce the chart immediately after it. This is how the front page is coded, (but not using the OfficeExcel.Async() function). There's an example of it here. The optional delay argument is measured in milliseconds (1 second = 1000 milliseconds) and defaults to 1. What you can do is specify something like 1000 to make the effect more pronounced to ensure it's working as expected.

  Note: Since a dev release of version 4, Google Chrome versions 4 and 5 have an issue with rendering text when using the OfficeExcel.Async() function. The solution here is to simply not use the OfficeExcel.Async() function.

  File: OfficeExcel.common.core.js
- (null) OfficeExcel.filledCurvyRect(context, x, y, width, height[, radius[, curvy top left[, curvy top right[, curvy bottom right[, curvy bottom left]]]]])
  This draws a rectangle with curvy corners. Similar to the built in rectangle functions, and you can control individual corners. The radius controls the severity of the corner curvature and defaults to 3. By default all the corners are curvy.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.strokedCurvyRect(context, x, y, width, height[, radius[, curvy top left[, curvy top right[, curvy bottom right[, curvy bottom left]]]]])
  This draws a rectangle with curvy corners. Similar to the built in rectangle functions, and you can control individual corners. The radius controls the severity of the corner curvature and defaults to 3. By default all the corners are curvy.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.Reset(canvas)
  This function resets the canvas. At first this function may appear similar to the OfficeExcel.Clear() function, however this function will reset any translate() that has been performed, and so can stop them accumulating.
  File: OfficeExcel.common.core.js

#### Custom OfficeExcel event related functions

- (null) OfficeExcel.FireCustomEvent(object, event)
  This fires a custom event. The object is your chart object, and the name is a string specifying the name of the event.
  File: OfficeExcel.common.core.js
- (number) OfficeExcel.AddCustomEventListener(object, event, callback)
  This attaches a function to the specified event. The object argument is your chart object, the event argument should be the name of the event, eg ontooltip, and the function argument is your function which handles the event. The return value is an ID which can be used with the OfficeExcel.RemoveCustomEventListener() function.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.RemoveCustomEventListener(object, id)
  This removes the custom event listener that corresponds to the given ID. The arguments are the chart object and the ID of the event handler to remove (as returned by OfficeExcel.AddCustomEventListener()).
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.RemoveAllCustomEventListeners([id])
  To easily remove all custom event listeners you can use this function. It also can optionally take one argument - a canvas ID - to limit the clearing to.
  File: OfficeExcel.common.core.js

#### Other

These are functions which are less generic, and used to build the charts. You may still wish to use them however.

- (null) OfficeExcel.lineByAngle(context, x, y, angle, length)
  This function draws a line from the given X and Y coordinates at the specified angle.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.Text(context, font, size, x, y, text[, valign[, halign[, border[, angle[, background[, bold[, indicator]]]]]]])
  This function acts as a wrapper around the canvas text functionality. The arguments are:
  - The context is the canvases 2D context.
  - The font is the name of the font you want to use (eg Arial).
  - The size is an integer specifying the size of the text, (measured in points).
  - The x and y arguments are the X/Y coordinates at which to draw the text.
  - The text argument is the text to draw.
  - The optional valign argument is the vertical alignment and can be either top, center or bottom.
  - The optional halign argument is the horizontal alignment and can be left, center or right.
  - The optional border argument is a boolean (true or false) controlling whether the text has a border.
  - The optional angle argument specifies the angle at which the text is drawn (rotated around the X/Y coordinates and measured in degrees).
  - The optional background argument is the color of the background, (eg #fff).
  - The optional bold argument is boolean which controls whether the text is bold or not.
  - And the optional indicator argument is a boolean which controls whether a placement indicator is shown.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.DrawTitle(canvas, text, gutter[, centerx[, size]])
  This function draws the title. The centerx argument is the center point to use. If not given the center of the canvas is used.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.Tooltip(canvas, text, x, y, idx)
  This function shows a tooltip. The idx value corresponds to the tooltip array that you give.
  File: OfficeExcel.common.tooltips.js
- (null) OfficeExcel.DrawKey(object, key, colors)
  This function draws the key. The first argument is the chart object, the second is an array of key information and the last is an array of the colors to use.

  Note: As of the November 2011 the key has been seperated out into its own file.

  File: OfficeExcel.common.key.js
- (null) OfficeExcel.DrawBars(object)
  This draws the horizontal background bars. The argument is the chart object.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.DrawInGraphLabels(object)
  This draws the in-graph labels. The argument is the chart object.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.DrawCrosshairs(object)
  This function draws the crosshairs. The argument is the chart object.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.HideContext()
  This function clears the context menu. OfficeExcel uses it to hide the context menu, but only AFTER your function/callback is run. You may wish to hide the context menu before your own function, in which case you can call this function.
  File: OfficeExcel.common.context.js
- (null) OfficeExcel.showPNG([canvas[, event]])
  This function allows you to easily facilitate getting a PNG image representation of your chart. You can use it like this:

      myBar.Set('chart.contextmenu', [
                                      ['Get PNG', OfficeExcel.showPNG],
                                      null,
                                      ['Cancel', function () {}]
                                     ]);

  Optionally, you can pass in the canvas as an argument which will be used, meaning now you do not have to use a context menu (there's an example here). It WAS originally designed to be used with a context menu, hence it's part of the OfficeExcel.core.context.js file.
  File: OfficeExcel.common.context.js
- (number) OfficeExcel.getGutterSuggest(obj, data)
  This function returns a suggested gutter setting based on the vertical labels. If the bottom labels are larger, this setting may be inappropriate. The data variable is a simple single dimension array, eg [1,2,3,4,5].

      var size = OfficeExcel.getGutterSuggest(obj, data);
      obj.Set('chart.gutter.left', size);

  File: OfficeExcel.common.core.js
- (boolean) OfficeExcel.isOld()
  This function tests the useragent for MSIE 7 OR 8.
  File: OfficeExcel.common.core.js
- (boolean) OfficeExcel.isIE8()
  This function tests the useragent for MSIE8.
  File: OfficeExcel.common.core.js
- (boolean) OfficeExcel.isIE8up()
  This function tests the useragent for MSIE8 or higher.
  File: OfficeExcel.common.core.js
- (boolean) OfficeExcel.isIE9()
  This function tests the useragent for MSIE9.
  File: OfficeExcel.common.core.js
- (boolean) OfficeExcel.isIE9up()
  This function tests the useragent for MSIE9 or higher.
  File: OfficeExcel.common.core.js
- (null) OfficeExcel.background.Draw(obj)
  This function is used by the Bar, Gantt, Line and Scatter chart to draw the chart background (but not the axes). It is passed the chart object which it uses to get the properties it uses:
  - chart.gutter
  - chart.variant
  - chart.text.size
  - chart.text.font
  - chart.title
  - chart.title.xaxis
  - chart.title.xaxis.pos
  - chart.title.yaxis
  - chart.title.yaxis.pos
  - chart.background.barcolor1
  - chart.background.grid
  - chart.background.grid.width
  - chart.background.grid.autofit
  - chart.background.grid.autofit.numhlines
  - chart.background.grid.autofit.numvlines
  - chart.background.grid.autofit.align
  - chart.background.grid.hlines
  - chart.background.grid.vlines
  - chart.background.grid.hsize
  - chart.background.grid.vsize
  - chart.background.grid.color
  - chart.background.grid.border
  - chart.background.barcolor2

  Note that this function also calls OfficeExcel.DrawTitle() in order to draw the title.
  File: OfficeExcel.common.core.js

#### The skeleton file

The skeleton object (OfficeExcel.skeleton.js) gives you a base for creating new chart types that match the OfficeExcel object system (eg getters, setters, OfficeExcel names etc). Example usage of the skeleton object would be:

    <script>
        var myObj = new OfficeExcel.Skeleton('cvs', [3,3,4,5,3,8,3,2,1,9]);
        myObj.Set('chart.colors', ['red']);
        myObj.Draw();

        var colors = myObj.Get('chart.colors');
    </script>