The YUI Charts Control visualizes tabular data on a web page in several possible formats including vertical columns, horizontal bars, lines, and pies. Notable features include support for the DataSource Utility, customizable series and axes, a customizable mouse-over datatip, combination charts, and skinning.
Important:
To use the Charts Control, include the following source files in your web page:
By default, the file charts.swf is expected in the assets directory relative to the location of the HTML page in which a Charts Control instance will appear. If you wish use charts.swf from yui.yahooapis.com, or if you'd like to place charts.swf in a different location on your own server, you may specify a path (relative to the page or absolute) by setting the following variable:
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is charts. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
The Charts Control includes six different chart types:
YAHOO.widget.LineChartYAHOO.widget.BarChartYAHOO.widget.ColumnChartYAHOO.widget.PieChartYAHOO.widget.StackedBarChartYAHOO.widget.StackedColumnChartFor the following example, we'll be using YAHOO.widget.ColumnChart.
Just like the YUI DataTable Control, we use a DataSource object to structure the data that will populate the chart. In this example, the data for this chart lives in local memory within a JavaScript array:
Pass in the array reference YAHOO.example.puppies to the DataSource constructor and set the responseType to be JavaScript array. Next, define the known data fields for the puppy objects — these fields will map to the keys you define for the axes and optionally to the series that you define.
Now you are ready to instantiate your ColumnChart by passing in
Make sure the container DIV element is available in the DOM before instantiating your ColumnChart, either by placing the script in the HTML body after the markup has been rendered, or by using the Event Utility's onAvailable method to programmatically create your ColumnChart as soon as the container DIV element is available.
The Charts Control provides many optional configuration parameters that help control how the your chart looks and behaves.
The first example above displayed a single series. Take a look at a more complex DataSource where we can extract more than one series to show in a chart.
By creating a set of series definitions, you can display multiple series in a single chart; moreover, each series can be individually customized. In particular, the series type, display name, styles, and the keys it uses to access data from the DataSource can be modified.
To pass a series definition to the chart, use the series attribute. Attributes may be set using the configuration hash in the constructor or by using the set() function.
NumericAxis BoundsBy default, charts with x and y axes automatically determine the minimum and maximum values based on the data that is being displayed. However, at times you might desire specific bounds on an axis. An axis object can be created and passed to the chart using either the xAxis or yAxis attributes, repectively.
An axis exposes the ability to customize the text that appears in each label that appears along its length. This is exposed through the labelFunction property.
Note: This function may be passed by name as a String or as a function reference.
By default, the mouse-over data tip displays the name of the series the item appears within (if present), and the item data for each axis. Data tips on PieCharts also includes percentage information. If an axis has a labelFunction, the same function will be called to receive formatting.
With a dataTipFunction, developers have full control over the text that appears within the data tip.
Note: This function may be passed by name as a String or as a function reference.
Because the Charts Control is powered by Flash Player, standard browser CSS is not available for styling individual subcomponents of a chart. Instead, styleable properties are exposed through a style attribute on the chart itself. This hash accepts a standardized set of style values for things like border, background, fonts, padding, axis styles, and datatip styles.
The following section describes each of the top-level chart styles along with any substyles that may be available.
A numeric value that specifies the spacing around the edge of the chart's contents. Unlike CSS padding in HTML, the chart's padding does not increase the dimensions of the chart.
A Boolean value that specifies whether marker animations are enabled or not. The default value is true, meaning that markers will animate when data changes.
One may declare a font style to customize the default axis text, including the font name, size, color and more. It is represented as an Object value that contains several substyles.
namecolor"ff0000" or 0xff0000.sizebolditalicunderlineThe border style allows a developer to add a colored border around the chart. The chart itself will decrease in dimensions to accomodate the border. It is represented as an Object value that contains several substyles.
color"ff0000" or 0xff0000.sizeThe background style allows one to customize the background color or image. It is represented as an Object value that contains several substyles.
colorcolor styles described above.alpha0.0 to 1.0 that refers to the transparency of the background color. This is most useful when used on the data tip background.imagemode"repeat" (default), "repeat-x", "repeat-y", "no-repeat", or "stretch".The mode value will be covered in more detail below in the Advanced Skinning section.
The legend style lets a developer customize the appearance of the legend. It is represented as an Object value that contains several substyles.
display"none", "left", "right", "top", and "bottom". The default value is "none".spacingpaddingpadding style described above.borderborder style described above.backgroundbackground style described above.fontfont style described above.The dataTip style lets a developer customize the appearance of the data tip. It is represented as an Object value that contains several substyles.
paddingpadding style described above.borderborder style described above.backgroundbackground style described above.fontfont style described above.The xAxis and yAxis styles allow one to customize the appearance of either axis. They are represented as Object values that contain several substyles.
colorcolor styles described above.size0 will hide the axis (but not the labels).showLabelstrue, the labels are displayed. If false, they are hidden.majorGridLinesminorGridLinesmajorTicksminorTicksThe majorGridLines and minorGridLines styles have a couple of substyles that need extra explanation. As shown above, majorGridLines and minorGridLines are substyles of the xAxis and yAxis styles.
colorcolor styles described above.sizesize substyle to 0 (zero). If the grid lines are hidden by default, a thickness greater than zero must be specified to show them.The majorTicks and minorTicks styles have a couple of substyles that need extra explanation. As shown above, majorTicks and minorTicks are substyles of the xAxis and yAxis styles.
colorcolor styles described above.sizelengthdisplay"none", "inside", "outside", and "cross". In many cases, "none" is the default.Note: Under certain conditions, even if the display substyle for minorTicks is a value other than "none", it is possible that no minor ticks will be drawn. The algorithm that calculates the minor divisions may determine that no minor divisions is the most ideal way to draw the chart.
Each individual series defined in the chart's series definition may be customized with a style attribute. The following example code expands the series definition created previously.
The styles defined above sets unique colors for each series, and the size of their markers.
The following styles are available for series definitions.
colorcolor styles described above.colorscolor style. Used by the PieChartsizelineSize"line".imageimagesimage style. Used by the PieChartmode"repeat" (default), "repeat-x", "repeat-y", "no-repeat", or "stretch".connectPoints"line".connectDiscontinuousPointsnull or NaN values). Available only on a series of type "line".discontinuousDashLength"line".For more information about using the image-related styles, see the Advanced Skinning section below.
The Charts Control accepts image URLs for background image styles (supported filetypes include JPG, PNG, GIF, and SWF). In an effort to mimic browser background image behavior, several image modes are available, including repeat, repeat-x, repeat-y, and no-repeat. An additional mode named stretch will increase the image dimensions to fill the entire background area.
Note: Due to the cross-domain restrictions enforced by Adobe Flash Player, and the methods used to tile and layout background images, if you are loading the Charts Control from yui.yahooapis.com, the domain from which you load image assets must provide a crossdomain.xml to allow yui.yahooapis.com to load and manipulate image files. If no crossdomain.xml file is provided, your images will not be displayed. Again, this only applies if you are loading the Charts Control from yui.yahooapis.com. If the Charts Control is loaded from the same subdomain as the HTML page from which it is being used, cross-domain restrictions will not affect you.
The following is an example of the minimal crossdomain.xml file required to allow charts.swf to load images from your domain.
The following modes are accepted for background images used by the Charts control.
repeatrepeat-xrepeat-yno-repeatstretchstretch, the image may distort if the aspect ratio of the Charts Control is different than that of the image.If the image mode is set to repeat-x or repeat-y, or the background image contains transparency, the background color will be displayed through the areas that are not covered by the image.
For futher exploration of skinning the Charts control, please review the Charts Skinning Example.
When other page content (such as Menu controls) overlap the Charts control, the Charts control may always appear on top in IE and Gecko-based browsers. To fix this problem, set the "wmode" of the Flash movie to either "transparent" or "opaque" using the wmode initialization attribute:
Due to a bug in Adobe Flash Player's ExternalInterface, fields retrieved from the DataSource used by the chart may not contain hyphens. In the following example, the "dateofbirth" field is valid, but the "first-name" and "last-name" fields will cause errors.
If this bug is fixed in a future version of Flash Player, no modifications will be needed to the Charts Control to begin working with hyphenated field names, except to possibly change the minimum required version of Flash Player.
SWFs embedded in HTML forms will not be initialized correctly when the page is viewed in Internet Explorer. When trying to embed a YUI chart into any descendant of a <form> tag, you may see an error that says yuigen0 not found and the chart will not be drawn. This problem has been fixed in Adobe Flash Player starting with version 9.0.115. If you must display a chart in an HTML form, you should override the version attribute to require a newer version of Flash Player:
In Opera, The chart reinitializes improperly when the display of a its parent element is changed to "none" and back to "block". This results in a blank chart.
The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.







Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings