Setting the font weight on the MX Legend control in Flex 4

In a previous example, “Setting the font weight on a charting Legend control in Flex”, we saw how you could set the font weight of a Flex Charting Legend control by setting the fontWeight style on the LegendItem CSS selector.

The following example shows how you can set the font weight of an MX Legend control by setting the fontWeight style on the LegendItem CSS selector in Flex 4.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/23/setting-the-font-weight-on-the-mx-legend-control-in-flex-4/ -->
<s:Application name="MX_LegendItem_fontWeight_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        mx|LegendItem {
            fontWeight: normal;
        }
    </fx:Style>
 
    <fx:Declarations>
        <s:XMLListCollection id="dp">
            <s:source>
                <fx:XMLList>
                    <product label="Product 1" data="3" />
                    <product label="Product 2" data="1" />
                    <product label="Product 3" data="4" />
                    <product label="Product 4" data="1" />
                    <product label="Product 5" data="5" />
                    <product label="Product 6" data="9" />
                </fx:XMLList>
            </s:source>
        </s:XMLListCollection>
    </fx:Declarations>
 
    <s:Panel left="20" right="20" top="20" bottom="20">
        <s:controlBarContent>
            <mx:Legend dataProvider="{pieChart}"
                    direction="horizontal"
                    horizontalGap="100"
                    width="100%" />
        </s:controlBarContent>
        <mx:PieChart id="pieChart"
                dataProvider="{dp}"
                showDataTips="true"
                height="100%"
                width="100%">
            <mx:series>
                <mx:PieSeries id="pieSeries"
                        field="@data"
                        nameField="@label"
                        filters="[]" />
            </mx:series>
        </mx:PieChart>
    </s:Panel>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

Or, if you wanted to use an embedded font, you could use the following code:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/23/setting-the-font-weight-on-the-mx-legend-control-in-flex-4/ -->
<s:Application name="MX_LegendItem_fontWeight_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        @font-face {
            src: url("C:/Windows/Fonts/comic.ttf");
            fontFamily: myEmbeddedFont;
            fontWeight: normal;
            embedAsCFF: false;
        }
 
        mx|LegendItem {
            color: white;
            fontFamily: myEmbeddedFont;
            fontSize: 16;
            fontWeight: normal;
        }
    </fx:Style>
 
    <fx:Declarations>
        <s:XMLListCollection id="dp">
            <s:source>
                <fx:XMLList>
                    <product label="Product 1" data="3" />
                    <product label="Product 2" data="1" />
                    <product label="Product 3" data="4" />
                    <product label="Product 4" data="1" />
                    <product label="Product 5" data="5" />
                    <product label="Product 6" data="9" />
                </fx:XMLList>
            </s:source>
        </s:XMLListCollection>
    </fx:Declarations>
 
    <s:Panel left="20" right="20" top="20" bottom="20">
        <s:controlBarContent>
            <mx:Legend dataProvider="{pieChart}"
                    direction="horizontal"
                    horizontalGap="100"
                    width="100%" />
        </s:controlBarContent>
        <mx:PieChart id="pieChart"
                dataProvider="{dp}"
                showDataTips="true"
                height="100%"
                width="100%">
            <mx:series>
                <mx:PieSeries id="pieSeries"
                        field="@data"
                        nameField="@label"
                        filters="[]" />
            </mx:series>
        </mx:PieChart>
    </s:Panel>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.