原文地址:
http://support.dundas.com/Dashboard3.Legend_Usage.ashx
lements of a legend¶
The figure below shows the main elements of a legend control. By default, the background of a legend is transparent, and no border is visible.![]() Elements of a legend. |
Usage¶
Adding a legend and connecting it to a data visualization control is similar for all controls. The chart is used below as an example.Adding a legend to a chart¶
In this example, assume that you have already created a dashboard to display two series ( i.e. KPIs) on a line chart:![]() A line chart displaying two KPIs. |
Follow these steps to add a legend to the dashboard:
- Go to the toolbox in the Dashboard Designer and expand the Data Controls folder.
- Drag the Legend item from the toolbox and drop it onto the dashboard canvas.
- The legend appears on the canvas with a default appearance (which indicates that it is not hooked up to any data visualization control yet).
Next, connect the chart to the legend:
- Select the chart control on the canvas, then go to its Properties grid.
- Set the chart's Legend property to Legend1. The appearance of the legend on the canvas is automatically updated to reflect the KPIs and other elements displayed in the chart.
- Save, then preview your dashboard.
![]() Chart with corresponding legend. |
Checkbox legend items¶
It's easy to set up an interactive legend that displays a checkbox beside each legend item. Users can toggle a checkbox in order to show or hide the corresponding series ( e.g. KPI) in the chart:- Drag a legend control from the toolbox to the canvas.
- Set the chart's Legend property to Legend1.
- Set the legend's Item Checkbox Visible property to True.
- Save, then preview your dashboard.
Toggle a legend item checkbox to show or hide the corresponding series (KPI) in the chart:
![]() Legend with checkbox items. |
Item Check Changed interaction¶
When you add a legend control to your dashboard, a script is automatically added to the advanced collection property Item Check Changed Interactions :![]() Item Check Changed Interactions |
For reference, here is the script that is generated:
if (e.ParentControlElements.Count > 0)
{
for(int i=0;i<e.ParentControlElements.Count;i++)
{
if (e.IsChecked)
{
e.ParentControlElements[i].Visibility = Visibility.Visible;
}
else
{
e.ParentControlElements[i].Visibility = Visibility.Collapsed;
}
}
}
Category headings¶
Multiple data visualization controls can be connected to a single legend control on the canvas, and they may display different types of items. For example, two charts can have the Legend property point to the same legend. In this case, some of the items in the legend correspond to one chart, while the remaining items correspond to the second chart. To help group or separate legend items, you can use category headings as follows:- Select the legend control and go to its Properties grid.
- Select a data visualization control and go to its Properties grid.
- Set the Legend Category property to your desired heading text.
- Repeat steps 2 and 3 as needed.
- Save, then preview your dashboard.
The resulting legend displays category headings above each group of legend items with the same category:
![]() Legend with category headings. |
Some data visualization controls have separate legend settings for different elements, such as the series or strip lines of a chart. To categorize those elements separately from each other, locate and set their Legend Category .
Note: If there is only one legend category, its header will not be displayed by default. To display the category header when there is only one category, set the legend's
Category Visibilityproperty to
True. (It is normally
Auto.)
![Category Visibility property.](http://support.dundas.com/GetFile.aspx?File=/doc-images/controls/legend/legend30-category-visibility.png)
Horizontal alignment¶
Use the legend's Item Horizontal Alignment property if you want to change the alignment of the legend items. For example, you can align the legend items to the right, or have them centered.If you want to control the horizontal alignment of a single legend item, you can do this from script by accessing the legend's LegendItems property, which gives you access to all of the legend items (available for scripting only). The example code below centers the first legend item:
Legend1.LegendItems[0].HorizontalAlignment = HorizontalAlignment.Center;