Search code examples
graphzedgraph

Positioning ZedGraph legend at middle right not top right?


Is there some way to position the legend using ZedGraph so that it is on the right-hand side, but vertically centred. Using:

output.Legend.Position = ZedGraph.LegendPos.Right

positions the legend at the top right, but beside a pie chart this looks misaligned. Is there a way to get the legend to vertically centre? Changing output.Legend.Location.Y seems to have no effect, nor does trying output.Legend.Location.AlignV = ZedGraph.AlignV.Center

Added in response to first answer below...

Floating, reducing the chart size and positioning the label does centre vertically, and is better than I had managed previously. Unfortunately it has a side-effect, in that the legend switches to multi-column and tries to occupy half the width of the total chart area, thus usually overlapping the chart (see picture). Looking at the ZedGraph source, this wider mode is used for all layouts except Left and Right.

Multi-column legend


Solution

  • Location is only enabled when Legend.Position is set to Float.

    You could do like this (C#):

    output.Legend.Position = LegendPos.Float;
    output.Legend.Location = 
       new Location(1.05f, 0.5f, CoordType.ChartFraction, AlignH.Right, AlignV.Center);
    

    with the probably undesired "feature" that the legend is drawn partially inside the chart:

    Legend in chart

    To workaround this issue I believe you also have to manually resize your chart:

    output.Chart.Rect = new RectangleF(xstart, ystart, xsize, ysize);