YUI Library Home

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.

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.

1var CustomGraph = function() 
2
3    this.text = "*************************************"
4    this.chart; 
5    this.dataTipFunction = CustomGraph.prototype.dataTipFunction; 
6    this.monthlyExpenses = 
7    [ 
8        { month: "January", car: 265.35, power:60.00, cable:101.45, entertainment:304.33}, 
9        { month: "February", car: 265.35, power:65.00, cable:123.87, entertainment:255.65}, 
10        { month: "March", car: 265.35, power:75.34, cable:101.45, entertainment:284.85}, 
11        { month: "April", car: 265.35, power:88.45, cable:115.64, entertainment:245.90}, 
12        { month: "May", car: 265.35, power:98.47, cable:101.45, entertainment:265.34}, 
13        { month: "June", car: 265.35, power:101.35, cable:108.64, entertainment:301.23} 
14    ];   
15    this.styleDefinition = 
16    { 
17        font:{color:0x663333}, 
18        background:{color:0xDCAD70}, 
19        border:{color:0x663333, size:2}, 
20        xAxis:{color:0x663333}, 
21        yAxis:{color:0x663333, minorTicks:{display:"none"}, majorTicks:{display:"none"}, majorGridLines:{color:0x663333}} 
22         
23    } 
24
25 
26CustomGraph.prototype.getDataSource = function() 
27
28    var myDataSource = new YAHOO.util.DataSource( this.monthlyExpenses ); 
29    myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
30    myDataSource.responseSchema = 
31    { 
32        fields: [ "month""car""power""entertainment""cable" ] 
33    }; 
34    return myDataSource;     
35}    
view plain | print | ?

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.

1CustomGraph.prototype.formatCurrencyAxisLabel = function( value ) 
2
3    return this.numberToCurrency(value); 
4
5 
6CustomGraph.prototype.numberToCurrency = function(value) 
7
8    return YAHOO.util.Number.format( value, 
9    { 
10        prefix: "$"
11        thousandsSeparator: ","
12        decimalPlaces: 2 
13    });  
14}        
15 
16CustomGraph.prototype.fixedExpensesDataTipFunction = function(item, index, series) 
17
18    var toolTipText = this.text; 
19    toolTipText += "\n" + series.displayName + " payment for " + item.month; 
20    toolTipText += "\n" + this.formatCurrencyAxisLabel( item[series.yField] ); 
21    toolTipText += "\n" + this.text; 
22    return toolTipText;  
23
24 
25CustomGraph.prototype.dataTipFunction = function( item, index, series ) 
26
27    var expense = series.yField; 
28    var num = item[expense]; 
29    return this.getDataTipText(expense, item.month, num); 
30
31 
32CustomGraph.prototype.getDataTipText = function(expense, month, amount) 
33
34    var toolTipText = this.text; 
35    toolTipText += "\nYour " + month + " " + expense; 
36    var limit; 
37    switch(expense) 
38    { 
39        case "entertainment" : 
40            toolTipText += " expenses are"
41            toolTipText += amount <= 300 ? "\n in an acceptable range: \n" : "\n  too high. \nGo out less:\n"
42        break
43        case "cable" : 
44            toolTipText += " bill is"
45            toolTipText += amount <= 110 ? "\n in an acceptable range: \n" : "\n  too high. \nOrder less movies:\n"
46        break
47        case "power" : 
48            toolTipText += " bill is"
49            toolTipText += amount <= 80 ? "\n in an acceptable range: \n" : "\n too high. \nTurn down your AC:\n"
50        break
51         
52    } 
53    toolTipText += this.formatCurrencyAxisLabel(amount); 
54    toolTipText += "\n" + this.text; 
55     
56    return toolTipText; 
57
view plain | print | ?

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.

1CustomGraph.prototype.getVerticalAxis = function() 
2
3    var currencyAxis = new YAHOO.widget.NumericAxis(); 
4    currencyAxis.alwaysShowZero = false
5    currencyAxis.labelFunction = {func:this.formatCurrencyAxisLabel, scope:this};    
6 
7    return currencyAxis; 
8}    
9 
10CustomGraph.prototype.getSeriesDefs = function() 
11
12    var seriesDef = 
13    [ 
14        {  
15            displayName: "Car",  
16            yField: "car",  
17            style:{skin:"DiamondSkin", color:0x006600}, 
18            dataTipFunction:{func:this.fixedExpensesDataTipFunction, scope:this
19         }, 
20        {    
21            displayName: "power",  
22            yField: "power",  
23            style:{color:0x8899dd} 
24        }, 
25        { 
26            displayName: "Cable"
27            yField: "cable"
28            style:{color:0xee6600} 
29        }, 
30        { 
31            displayName: "Entertainment"
32            yField: "entertainment"
33            style:{color:0x6e5000} 
34        } 
35    ];   
36 
37    return seriesDef; 
38
39 
40CustomGraph.prototype.drawChart = function(myDiv) 
41
42    var myAtts = {xField:"month", version:9.28, expressInstall: "assets/expressinstall.swf"}; 
43    myAtts.dataTipFunction = {func:this.dataTipFunction, scope:this}; 
44    myAtts.series = this.getSeriesDefs(); 
45    myAtts.yAxis = this.getVerticalAxis(); 
46    myAtts.style = this.styleDefinition; 
47    this.chart = new YAHOO.widget.LineChart(myDiv, this.getDataSource(), myAtts); 
48}    
view plain | print | ?

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.

1var myGraph = new CustomGraph(); 
2myGraph.drawChart("chart"); 
view plain | print | ?

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:

Logger Console

INFO 105ms (+105) 5:19:39 PM:

LogReader instance0

LogReader initialized

INFO 0ms (+0) 5:19:39 PM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More Charts Control Resources:

Copyright © 2010 Yahoo! Inc. All rights reserved.

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