YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
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:
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/element/element-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/datasource/datasource-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/json/json-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/swf/swf-min.js"></script> <!-- OPTIONAL: Connection (enables XHR) --> <script src="http://yui.yahooapis.com/2.9.0/build/connection/connection-min.js"></script> <!-- Source files --> <script src="http://yui.yahooapis.com/2.9.0/build/charts/charts-min.js"></script>
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/element/element-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/datasource/datasource-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/json/json-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/swf/swf-min.js"></script> <!-- OPTIONAL: Connection (enables XHR) --> <script src="http://yui.yahooapis.com/2.9.0/build/connection/connection-min.js"></script> <!-- Source files --> <script src="http://yui.yahooapis.com/2.9.0/build/charts/charts-min.js"></script>
Note: YUI 2.8.0 introduces a new dependency for the Charts. YUI SWF Utility is required to load the chart control.
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:
Note: The charts.swf is a component of Charts Control and is not intended to be used independently. If you do use the charts.swf separately from the Charts Control, check the Release Notes when upgrading for changes that may affect your application.
YAHOO.widget.Chart.SWFURL = "http://yui.yahooapis.com/2.9.0/build/charts/assets/charts.swf";
YAHOO.widget.Chart.SWFURL = "http://yui.yahooapis.com/2.9.0/build/charts/assets/charts.swf";
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 to 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. 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:
YAHOO.example.puppies = [ { name: "Ashley", breed: "German Shepherd", age: 12 }, { name: "Dirty Harry", breed: "Norwich Terrier", age: 5 }, { name: "Emma", breed: "Labrador Retriever", age: 9 }, { name: "Oscar", breed: "Yorkshire Terrier", age: 6 }, { name: "Riley", breed: "Golden Retriever", age: 6 }, { name: "Shannon", breed: "Greyhound", age: 12 }, { name: "Washington" ,breed: "English Bulldog", age: 8 }, { name: "Zoe", breed: "Labrador Retriever", age: 3 } ];
YAHOO.example.puppies = [ { name: "Ashley", breed: "German Shepherd", age: 12 }, { name: "Dirty Harry", breed: "Norwich Terrier", age: 5 }, { name: "Emma", breed: "Labrador Retriever", age: 9 }, { name: "Oscar", breed: "Yorkshire Terrier", age: 6 }, { name: "Riley", breed: "Golden Retriever", age: 6 }, { name: "Shannon", breed: "Greyhound", age: 12 }, { name: "Washington" ,breed: "English Bulldog", age: 8 }, { name: "Zoe", breed: "Labrador Retriever", age: 3 } ];
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.
var myDataSource = new YAHOO.util.DataSource(YAHOO.example.puppies); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "name","breed","age" ] };
var myDataSource = new YAHOO.util.DataSource(YAHOO.example.puppies); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "name","breed","age" ] };
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.
var myChart = new YAHOO.widget.ColumnChart( "myContainer", myDataSource, { xField: "name", yField: "age" });
var myChart = new YAHOO.widget.ColumnChart( "myContainer", myDataSource, { xField: "name", yField: "age" });
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.
YAHOO.example.monthlyExpenses = [ { month: "January", rent: 880.00, utilities: 894.68 }, { month: "February", rent: 880.00, utilities: 901.35 }, { month: "March", rent: 880.00, utilities: 889.32 }, { month: "April", rent: 880.00, utilities: 884.71 }, { month: "May", rent: 910.00, utilities: 879.811 }, { month: "June", rent: 910.00, utilities: 897.95 } ]; var myDataSource = new YAHOO.util.DataSource(YAHOO.example.monthlyExpenses); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "month", "rent", "utilities" ] };
YAHOO.example.monthlyExpenses = [ { month: "January", rent: 880.00, utilities: 894.68 }, { month: "February", rent: 880.00, utilities: 901.35 }, { month: "March", rent: 880.00, utilities: 889.32 }, { month: "April", rent: 880.00, utilities: 884.71 }, { month: "May", rent: 910.00, utilities: 879.811 }, { month: "June", rent: 910.00, utilities: 897.95 } ]; var myDataSource = new YAHOO.util.DataSource(YAHOO.example.monthlyExpenses); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "month", "rent", "utilities" ] };
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.
var seriesDef = [ { displayName: "Rent", yField: "rent" }, { displayName: "Utilities", yField: "utilities" } ];
var seriesDef = [ { displayName: "Rent", yField: "rent" }, { displayName: "Utilities", yField: "utilities" } ];
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.
new YAHOO.widget.LineChart( "myContainer", myDataSource, { xField: "month", series: seriesDef });
new YAHOO.widget.LineChart( "myContainer", myDataSource, { xField: "month", series: seriesDef });
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.
var axisWithMinimum = new YAHOO.widget.NumericAxis(); axisWithMinimum.minimum = 800; myChart.set( "yAxis", axisWithMinimum );
var axisWithMinimum = new YAHOO.widget.NumericAxis(); axisWithMinimum.minimum = 800; myChart.set( "yAxis", axisWithMinimum );
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.
var currencyAxis = new YAHOO.widget.NumericAxis(); currencyAxis.labelFunction = "formatCurrencyAxisLabel"; function formatCurrencyAxisLabel(value) { return YAHOO.util.Number.format(value, { prefix: "$", thousandsSeparator: ",", decimalPlaces: 2 }); } myChart.set("yAxis", currencyAxis);
var currencyAxis = new YAHOO.widget.NumericAxis(); currencyAxis.labelFunction = "formatCurrencyAxisLabel"; function formatCurrencyAxisLabel(value) { return YAHOO.util.Number.format(value, { prefix: "$", thousandsSeparator: ",", decimalPlaces: 2 }); } myChart.set("yAxis", currencyAxis);
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.
function getDataTipText( item, index, series ) { var toolTipText = series.displayName + " for " + item.month; toolTipText += "\n" + formatCurrencyAxisLabel( item[series.yField] ); return toolTipText; } chart.set( "dataTipFunction", getDataTipText );
function getDataTipText( item, index, series ) { var toolTipText = series.displayName + " for " + item.month; toolTipText += "\n" + formatCurrencyAxisLabel( item[series.yField] ); return toolTipText; } chart.set( "dataTipFunction", getDataTipText );
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.
new YAHOO.widget.LineChart("myContainer", myDataSource, { xField: "month", series: seriesDef, style: { padding: 20, animationEnabled: false, border: { color: 0x995566, size: 2 }, font: { name: "Verdana", color: 0x995566, size: 12 } } });
new YAHOO.widget.LineChart("myContainer", myDataSource, { xField: "month", series: seriesDef, style: { padding: 20, animationEnabled: false, border: { color: 0x995566, size: 2 }, font: { name: "Verdana", color: 0x995566, size: 12 } } });
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", "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", "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.
color"#ff0000", "ff0000" or 0xff0000.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".spacingpaddinglegend's contents. Unlike CSS padding in HTML, the legend's padding does not increase the dimensions of the legend.borderborder style described above.legend. Contains the same substyles as the border style in the Basic Styling Section.
backgroundlegend. Contains the same substyles as the background style in the Basic Styling Section.
fontlegend text. Contains the same substyles as the font style in the Basic Styling Section.
Available only on a series of type "pie".The dataTip style lets a developer customize the appearance of the data tip. It is represented as an Object value that contains several substyles.
paddingdataTip's contents. Unlike CSS padding in HTML, the dataTip's padding does not increase the dimensions of the dataTip.borderdataTip. Contains the same substyles as the border style in the Basic Styling Section.
backgrounddataTip. Contains the same substyles as the background style in the Basic Styling Section.
fontdataTip text. Contains the same substyles as the font style in the Basic Styling Section.
The xAxis and yAxis styles allow one to customize the appearance of either axis. They are represented as Object values that contain several substyles.
color"#ff0000", "ff0000" or 0xff0000.size0 will hide the axis (but not the labels).showLabelstrue, the labels are displayed. If false, they are hidden.hideOverlappingLabelscalculateCategoryCount is false. The style will be used on the TimeAxis and NumericAxis when the user specifies the majorUnit. Otherwise, the axes will place the labels so that they do not overlap.labelRotation-90 through 90. Labels will display most clearly when set to 90, -90 or 0. The default value is 0.labelSpacing2.labelDistance2.titleRotationtitleDistance2.majorGridLinesminorGridLineszeroGridLinemajorTicksminorTicksThe 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.
color"#ff0000", "ff0000" or 0xff0000.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 zeroGridLine style allows for emphasis on the zero grid line when it falls beyond the origin of the axis. The zeroGridLine style has the following substyles:
color"#ff0000", "ff0000" or 0xff0000.sizesize substyle to 0.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.
color"#ff0000", "ff0000" or 0xff0000.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.
var seriesDef = [ { displayName: "Rent", yField: "rent", style: { color: 0xff0000, size: 20 } }, { displayName: "Utilities", yField: "utilities", style: { color: 0x0000ff, size: 30 } } ];
var seriesDef = [ { displayName: "Rent", yField: "rent", style: { color: 0xff0000, size: 20 } }, { displayName: "Utilities", yField: "utilities", style: { color: 0x0000ff, size: 30 } } ];
The styles defined above sets unique colors for each series, and the size of their markers.
The following styles are available for series definitions.
color"#ff0000", "ff0000" or 0xff0000.colorscolor style. Used by the PieChartsizealpha0.0 to 1.0, that represents the alpha of the markers in the series.borderColor"#ff0000", "ff0000" or 0xff0000. If not specified, the marker border color is determined by the color style.borderAlpha0.0 to 1.0, that represents the border alpha of the markers in the series.fillColor"#ff0000", "ff0000" or 0xff0000. If not specified, the marker fill color is determined by the color style.fillAlpha0.0 to 1.0, that represents the fill alpha of the markers in the series.lineSize"line".lineColor"#ff0000", "ff0000" or 0xff0000. Available only on a series of type "line".lineAlpha0.0 to 1.0, that represents the alpha value of lines in a line series. Available only on a series of type "line".
showAreaFill"line".areaFillAlpha0.0 to 1.0, that represents the alpha value of the area fill in a line series when showAreaFill is set to true.
Available only on a series of type "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".skinCircleSkin, DiamondSkin, TriangleSkin and RectangleSkin. Available only on a series of type "line".visibilityhidden and visible.showInLegendtrue. This style is not available for a series of type pie.showLabelstrue, value labels will appear in the wedges of a pie series. Available only on a series of type "pie".font
showLabels is set to true. Contains the same substyles as the font style in the Basic Styling Section.
Available only on a series of type "pie".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.
style: { background: { image: "images/tile.png", mode: "repeat", color: 0xcc9900 } }
style: { background: { image: "images/tile.png", mode: "repeat", color: 0xcc9900 } }
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.
<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-access-from domain="yui.yahooapis.com"/> </cross-domain-policy>
<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <allow-access-from domain="yui.yahooapis.com"/> </cross-domain-policy>
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.
The underlying charts swf needs a global object to communicate with the DOM. As a result, the YAHOO global object needs to be declared outside of the YUI3 use statement.
var YAHOO; YUI().use('node', 'yui2-utilities', 'yui2-datasource', 'yui2-json', 'yui2-swf', 'yui2-charts', function (Y) { YAHOO = Y.YUI2; ... });
var YAHOO; YUI().use('node', 'yui2-utilities', 'yui2-datasource', 'yui2-json', 'yui2-swf', 'yui2-charts', function (Y) { YAHOO = Y.YUI2; ... });
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:
var myChart = new YAHOO.widget.ColumnChart("myContainer", myDataSource, { wmode: "opaque" });
var myChart = new YAHOO.widget.ColumnChart("myContainer", myDataSource, { wmode: "opaque" });
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.
var myDataSource = new YAHOO.util.DataSource( YAHOO.example.employees ); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "first-name", "last-name", "dateofbirth" ] };
var myDataSource = new YAHOO.util.DataSource( YAHOO.example.employees ); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "first-name", "last-name", "dateofbirth" ] };
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:
var myChart = new YAHOO.widget.ColumnChart("myContainer", myDataSource, { version: "9.0.115" });
var myChart = new YAHOO.widget.ColumnChart("myContainer", myDataSource, { version: "9.0.115" });
There have been some issues in previous releases with charts that use a logarithmic scale. Changes in the 2.7.0 made these issues more apparent in some use cases.
The YUI Library and related topics are discussed on the on the YUILibrary.com forums.
Also be sure to check out 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 YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.

Copyright © 2013 Yahoo! Inc. All rights reserved.