YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Charts Control: Column Chart with Rotated Title and Labels

# Charts Control: Column Chart with Rotated Title and Labels

Axes Title and Label fields can be rotated on the YUI Charts Control by setting the titleRotation and labelRotation axis styles. This example shows you how.

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.

Monthly 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.

## Data

We will start by adding our data for the charts.

YAHOO.example.monthlyExpenses = [ { month: "January", rent: 1350.00, utilities: 941.68 }, { month: "February", rent: 1350.00, utilities: 901.35 }, { month: "March", rent: 1350.00, utilities: 789.32 }, { month: "April", rent: 1350.00, utilities: 684.71 }, { month: "May", rent: 1500.00, utilities: 779.811 }, { month: "June", rent: 1500.00, utilities: 897.95 }, { month: "July", rent: 1500.00, utilities: 919.811 }, { month: "August", rent: 1500.00, utilities: 937.95 }, { month: "September", rent: 1500.00, utilities: 779.811 }, { month: "October", rent: 1500.00, utilities: 697.95 }, { month: "November", rent: 1500.00, utilities: 679.811 }, { month: "December", rent: 1500.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" ] };

## Series and Styles

Next we'll add a series to our chart, and give it a style.

//series definition for chart var seriesDef = [ { displayName: "Rent", yField: "rent", style:{size:10} }, { displayName: "Utilities", yField: "utilities", style:{size:10} } ];

In the styles for our x-axis and y-axis, we'll set the corresponding `labelRotation` and `titleRotation` to -90. This will make the title on the y-axis and the labels on the x-axis to be rotated -90 degrees (counterclockwise). While both styles accept values between -90 and 90, it is generally recomended to only use the values -90, 90 or 0 (default), as text fields will display more clearly at these settings.

//Style object for chart var styleDef = { xAxis: { labelRotation:-90 }, yAxis: { titleRotation:-90 } }

## Chart

Add our axes and label formatting methods.

//format currency YAHOO.example.formatCurrencyAxisLabel = function( value ) { return YAHOO.util.Number.format( value, { prefix: "$", thousandsSeparator: ",", decimalPlaces: 2 }); } //DataTip function for the chart YAHOO.example.getDataTipText = function( item, index, series ) { var toolTipText = series.displayName + " for " + item.month; toolTipText += "\n" + YAHOO.example.formatCurrencyAxisLabel( item[series.yField] ); return toolTipText; } //create a Numeric Axis for displaying dollars var currencyAxis = new YAHOO.widget.NumericAxis(); currencyAxis.labelFunction = YAHOO.example.formatCurrencyAxisLabel; currencyAxis.title = "Money Spent"; //create Category Axis to specify a title for the months var categoryAxis = new YAHOO.widget.CategoryAxis(); categoryAxis.title = "Month";

Finally, we'll add the chart and call it good.

//create a Chart var mychart = new YAHOO.widget.ColumnChart( "chart", myDataSource, { series: seriesDef, xField: "month", yAxis: currencyAxis, xAxis: categoryAxis, style: styleDef, dataTipFunction: YAHOO.example.getDataTipText, //only needed for flash player express install expressInstall: "assets/expressinstall.swf" });

There are even more styles available for the axes than we've covered in this tutorial. For full details about the axes styles, read the Charts Control User's Guide.

## 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: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### 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