YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Charts Control: Apply scope to label functions

# Charts Control: Apply scope to label functions

A YUI Charts Control and a DataTable Control may share a DataSource to display the same data.

Please note: The YUI Charts Control requires Flash Player 9.0.45 or higher. The latest version of Flash Player is available at the Adobe Flash Player Download Center.

View example in new window.

Personal Expenses
Unable to load Flash content. The YUI Charts Control requires Flash Player 9.0.45 or higher. You can download the latest version of Flash Player from the Adobe Flash Player Download Center.

## Create Custom Graph Class

We will start by creating a custom class to contain all the necessary elements required to render a chart.

var CustomGraph = function() { this.text = "*************************************"; this.chart; this.dataTipFunction = CustomGraph.prototype.dataTipFunction; this.monthlyExpenses = [ { month: "January", car: 265.35, power:60.00, cable:101.45, entertainment:304.33}, { month: "February", car: 265.35, power:65.00, cable:123.87, entertainment:255.65}, { month: "March", car: 265.35, power:75.34, cable:101.45, entertainment:284.85}, { month: "April", car: 265.35, power:88.45, cable:115.64, entertainment:245.90}, { month: "May", car: 265.35, power:98.47, cable:101.45, entertainment:265.34}, { month: "June", car: 265.35, power:101.35, cable:108.64, entertainment:301.23} ]; this.styleDefinition = { font:{color:0x663333}, background:{color:0xDCAD70}, border:{color:0x663333, size:2}, xAxis:{color:0x663333}, yAxis:{color:0x663333, minorTicks:{display:"none"}, majorTicks:{display:"none"}, majorGridLines:{color:0x663333}} } } CustomGraph.prototype.getDataSource = function() { var myDataSource = new YAHOO.util.DataSource( this.monthlyExpenses ); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; myDataSource.responseSchema = { fields: [ "month", "car", "power", "entertainment", "cable" ] }; return myDataSource; }

Our class contains a reference to a dataTip function, data, chart, styles and some custom text to be used in our data tips.

## Add Tips Formatting Methods

We will now add all the methods necessary for formatting axis labels and data tips. It is important to note that the dataTip and axis label methods make use of other class level methods, so the ability to add socpe to these methods will prove useful.

CustomGraph.prototype.formatCurrencyAxisLabel = function( value ) { return this.numberToCurrency(value); } CustomGraph.prototype.numberToCurrency = function(value) { return YAHOO.util.Number.format( value, { prefix: "$", thousandsSeparator: ",", decimalPlaces: 2 }); } CustomGraph.prototype.fixedExpensesDataTipFunction = function(item, index, series) { var toolTipText = this.text; toolTipText += "\n" + series.displayName + " payment for " + item.month; toolTipText += "\n" + this.formatCurrencyAxisLabel( item[series.yField] ); toolTipText += "\n" + this.text; return toolTipText; } CustomGraph.prototype.dataTipFunction = function( item, index, series ) { var expense = series.yField; var num = item[expense]; return this.getDataTipText(expense, item.month, num); } CustomGraph.prototype.getDataTipText = function(expense, month, amount) { var toolTipText = this.text; toolTipText += "\nYour " + month + " " + expense; var limit; switch(expense) { case "entertainment" : toolTipText += " expenses are"; toolTipText += amount <= 300 ? "\n in an acceptable range: \n" : "\n too high. \nGo out less:\n"; break; case "cable" : toolTipText += " bill is"; toolTipText += amount <= 110 ? "\n in an acceptable range: \n" : "\n too high. \nOrder less movies:\n"; break; case "power" : toolTipText += " bill is"; toolTipText += amount <= 80 ? "\n in an acceptable range: \n" : "\n too high. \nTurn down your AC:\n"; break; } toolTipText += this.formatCurrencyAxisLabel(amount); toolTipText += "\n" + this.text; return toolTipText; }

## Set Axis and DataTip Functions

Next, we will add methods that return an axis and a series definition. We will also add a method that draws our chart. Each of these methods will create references to text formatting functions to pass to our YUI Chart instance. Our vertical axis method sets the axis label function, our series definition sets a data tip function for our car payment series and our chart method sets the data tip function for all other series.

CustomGraph.prototype.getVerticalAxis = function() { var currencyAxis = new YAHOO.widget.NumericAxis(); currencyAxis.alwaysShowZero = false; currencyAxis.labelFunction = {func:this.formatCurrencyAxisLabel, scope:this}; return currencyAxis; } CustomGraph.prototype.getSeriesDefs = function() { var seriesDef = [ { displayName: "Car", yField: "car", style:{skin:"DiamondSkin", color:0x006600}, dataTipFunction:{func:this.fixedExpensesDataTipFunction, scope:this} }, { displayName: "power", yField: "power", style:{color:0x8899dd} }, { displayName: "Cable", yField: "cable", style:{color:0xee6600} }, { displayName: "Entertainment", yField: "entertainment", style:{color:0x6e5000} } ]; return seriesDef; } CustomGraph.prototype.drawChart = function(myDiv) { var myAtts = {xField:"month", version:9.28, expressInstall: "assets/expressinstall.swf"}; myAtts.dataTipFunction = {func:this.dataTipFunction, scope:this}; myAtts.series = this.getSeriesDefs(); myAtts.yAxis = this.getVerticalAxis(); myAtts.style = this.styleDefinition; this.chart = new YAHOO.widget.LineChart(myDiv, this.getDataSource(), myAtts); }

Normally, when setting a formatting function for the chart, a function is passed either directly or as a string reference. When you want to include scope, however, you instead pass an object with two properties, `func`, a reference to the function, and `scope`, a reference to the scope. `dataTipFunction:{func:myDataTipFunction, scope:this}`

## Create the Chart

Finally, we'll instantiate our class and draw the chart.

var myGraph = new CustomGraph(); myGraph.drawChart("chart");

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Charts Control Examples:

- Charts Quickstart Example
- Chart with Legend Example
- Skinning a Chart Example
- Chart and DataTable Example
- Chart with DataSource Polling
- Stacked Bar Chart
- Charts in a TabView
- Column Chart with Rotated Title and Labels
- Styling Lines, Borders and Fills
- Chart with 2 Numeric Axes
- Hide series in a chart.
- Customize series items
- Apply scope to label functions

### More Charts Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings