YUI Library Examples: Charts Control (experimental): Chart with Legend Example

Charts Control (experimental): Chart with Legend Example

Though the YUI Charts Control doesn't support legends out of the box, you can easily create a legend using a simple unordered list. 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.

Survey: What is your favorite season?
Unable to load Flash content. The YUI Charts Control requires Flash Player 9.0.45 or higher. You can install the latest version at the Adobe Flash Player Download Center.

  • Summer
  • Fall
  • Spring
  • Winter
  • Undecided

Build and HTML Legend

The Charts Control doesn't have a legend by default. However, one can build a simple legend using a standard HTML unordered list.

1<ul id="legend"
2    <li class="summer"><span class="categoryName">Summer</span></li
3    <li class="fall"><span class="categoryName">Fall</span></li
4    <li class="spring"><span class="categoryName">Spring</span></li
5    <li class="winter"><span class="categoryName">Winter</span></li
6    <li class="undecided"><span class="categoryName">Undecided</span></li
7</ul> 
view plain | print | ?

Each list item has its own class to allow CSS to give it the correct background color. A span element surrounds the text in the list item to allow styling, and a descriptive CSS class may be provided for extra semantic meaning.

Adding Styles

The legend itself is given a list-style value of none to hide the bullets. Each list item is given left padding to allow a background-color to be displayed.

1#legend 
2{ 
3    floatleft
4    list-stylenone
5    padding5px
6    margin-left10px
7    margin-top90px
8    background-color: #ffffff; 
9} 
10 
11#legend li 
12{ 
13    height1.2em
14    padding-left15px
15    margin-bottom4px
16} 
view plain | print | ?

Each of the PieChart's categories has its own class that is used to set the background-color of the list item.

1.summer 
2{ 
3    background-color: #00b8bf; 
4} 
5.fall 
6{ 
7    background-color: #8dd5e7
8} 
9.spring 
10{ 
11    background-color: #c0fff6
12} 
13.winter 
14{ 
15    background-color: #ffa928
16} 
17.undecided 
18{ 
19    background-color: #edff9f; 
20} 
view plain | print | ?

The span element inside the list items is given a background-color to match the legend's main background and to hide the category color under the text. Setting display:block conceals the colored background on the right side.

1.categoryName 
2{ 
3    displayblock
4    padding-left4px
5    background-color: #ffffff; 
6} 
view plain | print | ?

YUI Logger Output:

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

Reload with logging
and debugging enabled.

More Charts Control (experimental) Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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