Chart for Xamarin.iOS: Legend
TKChart
has built-in support for legends – descriptions about the charts on the plot. The items displayed in the legend are series specific i.e. for the pie chart the data points are shown in the legend, whereas for line series only one item is shown for each series.
Configure legend
If you would like to show the legend in TKChart
, you should set its Hidden property to false. The default value is true. The legend supports showing a series title.
chart.Legend.Hidden = false;
You can alter the position and offset origin of legend by setting its Position property:
chart.Legend.Style.Position = TKChartLegendPosition.Right;
The legend can be anchored to concrete side by using the following values TKChartLegendPosition.Left
, TKChartLegendPosition.Right
, TKChartLegendPosition.Top
and TKChartLegendPosition.SBottom
.
It can float by using TKChartLegendPosition.Floating
value. In this case, you can offset its origin manually by setting its Offset
and OffsetOrigin
properties:
chart.Legend.Style.Position = TKChartLegendPosition.Floating;
chart.Legend.Style.OffsetOrigin = TKChartLegendOffsetOrigin.TopLeft;
chart.Legend.Style.Offset = new UIOffset(10, 10);
Customize legend
You can alter visibility of the legend's title by changing ShowTitle
property.
chart.Legend.TitleLabel.Text = "Companies";
chart.Legend.ShowTitle = true;
In addition, you can disable the series selection via legend by setting AllowSelection
property to false.
The legend can be customized by using its Style
object. It contains the following properties:
-
Position
- Determines where the legend should be placed. -
Offset
- Determines the offset at which to place the legend, according to the specified offsetOrigin. -
OffsetOrigin
- Determines the starting point for the offset property. -
Fill
- Gets or sets the fill color to be used as a background. -
Stroke
- Gets or sets stroke color to be used for the legend frame.
chart.Legend.Style.Fill = new TKSolidFill(UIColor.LightGray);
chart.Legend.Style.Stroke = new TKStroke(UIColor.DarkGray);
Embedding legend outside TKChart
You can use the legend outside the chart view. You should create an instance of TKChartLegendView
and add it as subview to desired view.
var legendView = new TKChartLegendView (chart);
legendView.Frame = new CGRect (20, 20, 320, 100);
this.View.AddSubview(legendView);
legendView.ReloadItems ();