Legend in WPF Smith Chart (SfSmithChart)

18 Nov 201810 minutes to read

The legend contains the list of chart series that appear in a Smith chart. It can be defined by using the following code example.

NOTE

Add a name to the Label property of Series, which in turn mapped to the legend.

<syncfusion:SfSmithChart x:Name="SmithChart">
   <syncfusion:LineSeries Label="Transmission-1" ShowMarker="True" ResistancePath="Resistance" ReactancePath="Reactance" ItemsSource="{Binding Data1}">                
   </syncfusion:LineSeries>
   <syncfusion:LineSeries Label="Transmission-2" ShowMarker="True" ResistancePath="Resistance" ReactancePath="Reactance" ItemsSource="{Binding Data2}">
   </syncfusion:LineSeries>
   
   <!--Adding Legend to SmithChart-->
   <syncfusion:SfSmithChart.Legend>
       <syncfusion:SmithChartLegend></syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend> 
</syncfusion:SfSmithChart>
SfSmithChart chart = new SfSmithChart();

//Create line series1
 LineSeries series1 = new LineSeries();
//Display the legend text for the series.
 series1.Label = "Transmission-1";
 series1.ItemsSource = Data1;
 series1.ResistancePath = "Resistance";
 series1.ReactancePath = "Reactance";
 series1.ShowMarker = true;
 chart.Series.Add(series1);

 //Create line series2
 LineSeries series2 = new LineSeries();
//Display the legend text for the series.
 series2.Label = "Transmission-2";
 series2.ItemsSource = Data2;
 series2.ResistancePath = "Resistance";
 series2.ReactancePath = "Reactance";
 series2.ShowMarker = true;
 chart.Series.Add(series2);

//Adding legend to the SmithChart
 SmithChartLegend legend = new SmithChartLegend();
 chart.Legend = legend;

this.Grid1.Children.Add(chart);

Legend_img1

Positioning the Legend

Docking

Legends can be docked at left, right, top, or bottom around the chart area by using the DockPosition property.

By default, the Smith chart legend is docked at the Right of the chart.

To display the legend at the bottom, set the DockPosition as Bottom as in the below code snippet.

<syncfusion:SfSmithChart x:Name="SmithChart">
     <!--Adding Legend to SmithChart-->
   <syncfusion:SfSmithChart.Legend>
       <syncfusion:SmithChartLegend DockPosition="Bottom">
       </syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
//Adding legend to the SmithChart
SmithChartLegend legend = new SmithChartLegend();
legend.DockPosition = ChartDock.Bottom;
chart.Legend = legend;

Legend_img2

Floating Legends

To position the legend at any arbitrary location inside the chart, set the DockPosition as Floating and provide its relative position by using the OffsetX and OffsetY properties.

The offset specifies the x or y distance from the origin.

<syncfusion:SfSmithChart x:Name="SmithChart">
  <!--Adding legend to SmithChart-->
  <syncfusion:SfSmithChart.Legend>
    <syncfusion:SmithChartLegend DockPosition="Floating" OffsetX="200" OffsetY="120">
    </syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
//Adding legend to the SmithChart
SmithChartLegend legend = new SmithChartLegend();
legend.DockPosition = ChartDock.Floating;
legend.OffsetX = 200;
legend.OffsetY = 120;
chart.Legend = legend;

Legend_img3

Legend Icon

Represents the symbol associated with each legend item. By default, the legend icon is Circle.

The legend icon can be customized by using the LegendIcon property in the Smith chart legend as in the below code snippet.

<syncfusion:SfSmithChart x:Name="SmithChart">
  <!--Adding legend to SmithChart-->
  <syncfusion:SfSmithChart.Legend>
    <syncfusion:SmithChartLegend LegendIcon="HorizontalLine">
    </syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
//Adding legend to the SmithChart
SmithChartLegend legend = new SmithChartLegend();
legend.LegendIcon = SmithChartLegendIcon.HorizontalLine;
chart.Legend = legend;

Legend_img4

Custom Legend Icon

A custom icon for the legend can be added by using the LegendIconTemplate property in the Smith chart legend as in the below code example.

<syncfusion:SfSmithChart x:Name="SmithChart">    
 <syncfusion:SfSmithChart.Resources>
        <DataTemplate x:Key="Ellipse">
            <Ellipse Stretch="Fill" Fill="{Binding Interior}" Width="12" Height="5" />
        </DataTemplate>
    </syncfusion:SfSmithChart.Resources>
    <!--Adding custom legend icon to SmithChart-->
    <syncfusion:SfSmithChart.Legend>
        <syncfusion:SmithChartLegend LegendIcon="Custom" LegendIconTemplate="{StaticResource Ellipse}">
    </syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
SmithChartLegend legend = new SmithChartLegend();
//Adding custom legend icon to SmithChart
legend.LegendIcon = SmithChartLegendIcon.Custom;
legend.LegendIconTemplate = this.Grid1.Resources["Ellipse"] as DataTemplate;
chart.Legend = legend;

Legend_img5

Customizing Legend

The following code example illustrates the customization of the legend icon and text.

<syncfusion:SfSmithChart x:Name="SmithChart"> 
  <syncfusion:SfSmithChart.Legend>
        <syncfusion:SmithChartLegend IconHeight="15" IconWidth="15" ItemMargin="10"
                                     FontWeight="Bold" Foreground="Red" FontSize="13"
                                     Background="LightGray" BorderBrush="CadetBlue"
                                     BorderThickness="3">
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
//Adding legend to the Smith chart
 SmithChartLegend legend = new SmithChartLegend();
 
 //Customizing the Smith chart legend icon
 legend.IconHeight = 15;
 legend.IconWidth = 15;
 legend.ItemMargin = new Thickness(10);
 
 //Customizing legend text
 legend.Foreground = new SolidColorBrush(Colors.Red);
 legend.FontSize = 13;
 legend.FontWeight = FontWeights.Bold;
 
 legend.Background = new SolidColorBrush(Colors.LightSkyBlue);
 legend.BorderBrush = new SolidColorBrush(Colors.CadetBlue);
 legend.BorderThickness = new Thickness(3);
 chart.Legend = legend;

Legend_img6

VisibilityOnLegend

To limit the number of series to be displayed in the chart, use the VisibilityOnLegend property as shown in the below code example.

<syncfusion:SfSmithChart x:Name="SmithChart">
   <syncfusion:LineSeries VisibilityOnLegend="False" Label="Transmission-1" ShowMarker="True" ResistancePath="Resistance" ReactancePath="Reactance" ItemsSource="{Binding Data1}">
   </syncfusion:LineSeries>
   <syncfusion:LineSeries Label="Transmission-2" ShowMarker="True" ResistancePath="Resistance" ReactancePath="Reactance" ItemsSource="{Binding Data2}">
   </syncfusion:LineSeries>
   <syncfusion:SfSmithChart.Legend>
        <syncfusion:SmithChartLegend IconHeight="15" IconWidth="15" 
                                     FontWeight="Bold" Foreground="Red" FontSize="13"
                                     Background="LightGray" BorderBrush="CadetBlue"
                                     BorderThickness="3">
        </syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
//Create line series1
LineSeries series1 = new LineSeries();
series1.Label = "Transmission-1";
//Remove the LegendItem from the legend collections.
series1.VisibilityOnLegend = false;
chart.Series.Add(series1);

//Create line series2
LineSeries series2 = new LineSeries();
series2.Label = "Transmission-2";
chart.Series.Add(series2);

//Adding legend to the SmithChart
SmithChartLegend legend = new SmithChartLegend();
chart.Legend = legend;

Legend_img7

Series Visibility

To hide the series segment programmatically, set the IsSeriesVisible property as False for the specific series. After the property has been set, the legend item for the specific series will be displayed with a shade.

Now, you can show/hide the series segment on the chart by clicking on the particular legend item.

<syncfusion:SfSmithChart x:Name="SmithChart">
   <syncfusion:LineSeries IsSeriesVisible="False" Label="Transmission-1" ShowMarker="True" ResistancePath="Resistance" ReactancePath="Reactance" ItemsSource="{Binding Data1}">                
   </syncfusion:LineSeries>
   <syncfusion:LineSeries Label="Transmission-2" ShowMarker="True" ResistancePath="Resistance" ReactancePath="Reactance" ItemsSource="{Binding Data2}">
   </syncfusion:LineSeries>
   <syncfusion:SfSmithChart.Legend>
        <syncfusion:SmithChartLegend>
        </syncfusion:SmithChartLegend>
   </syncfusion:SfSmithChart.Legend>
</syncfusion:SfSmithChart>
//Create line series1
LineSeries series1 = new LineSeries();
series1.Label = "Transmission-1";
//Hide the series visibility in Chart.
series1.IsSeriesVisible = false;
chart.Series.Add(series1);

//Create line series2
LineSeries series2 = new LineSeries();
series2.Label = "Transmission-2";
chart.Series.Add(series2);

//Adding legend to the SmithChart
SmithChartLegend legend = new SmithChartLegend();
chart.Legend = legend;

Legend_img8