Legend position and customization


#1

Hi all,

I have a line chart with 3 series and I need to put an horizontal legend on the top left corner of the chart, outside the plot area, with a custom label in front of the series’ description (please see what I need to achieve at this url: https://www.dropbox.com/s/r2ik2sg9alwdoxw/grafico_L.JPG?dl=0 ).

I tried to add the custom label as a subview of the chart and then change, using sChartRenderFinished, the x origin of the legend (rendered with a top middle position), but a pan gesture on the chart makes the legend invisible with error “Specified legend height is too small to fit the number/layout of series”.

Here is my code:

In viewDidLoad, before adding the chart to the controller’s view:

self.chart.legend.position = SChartLegendPositionTopMiddle
self.chart.legend.placement = SChartLegendPlacementOutsidePlotArea
self.chart.legend.style.symbolAlignment = SChartSeriesLegendAlignSymbolsLeft
self.chart.legend.style.orientation = SChartLegendOrientationHorizontal

var custom_label = UILabel(frame: CGRectMake(5, 9, 0, 0))
custom_label.text = "LABEL"
custom_label.sizeToFit()

self.chart.addSubview(custom_label)

and then:

func sChartRenderFinished(chart: ShinobiChart!) {
        chart.legend.frame.origin.x = 75
        chart.legend.setNeedsDisplay()
    }

Can you give me a hint?

Thanks,
Vale


#2

Hi Vr_ants,

The chart usually handles the lay out of the legend. However, if you set your legends frame, the chart will give up its responsibility for positioning and sizing, leaving it completely up to you.

I’m not exactly sure why the legend would disappear on a pan gesture - that is quite odd. Do you think you could recreate this in a small sample application and send it over to info@shinobicontrols.com? Then we’d be able to debug this against source and hopefully find you a solution.

As a workaround, ditching all of the legend positioning code and simple set your legends frame after you’ve created your chart. You can even add it as a subview of your view controller’s view, rather than your chart.

Best regards,

Jan Akerman