2

I have created a CPTXYGraph using Core Plot in Swift. The graph works just fine. Now I am trying to add a legend. I have looked at several examples and come up with the following to create the legend:

    var theLegend=CPTLegend(graph: graph)
    var legendFill=CPTFill(color: CPTColor.blueColor())
    theLegend.fill = legendFill
    var legendLineStyle = CPTMutableLineStyle()
    legendLineStyle.lineColor = CPTColor.whiteColor()
    theLegend.borderLineStyle = legendLineStyle
    theLegend.cornerRadius = 2.0
    theLegend.swatchSize = CGSizeMake(15.0, 15.0)
    graph.legend = theLegend
    graph.legendAnchor = CPTRectAnchor.TopLeft
    graph.legendDisplacement = CGPointMake(100.0, -100.0)

When I run this, I get a blue square with slightly rounded corners that appears to be about 10 X 10 pixels. I tried setting the frame just to see if it would make a difference:

    theLegend.frame = CGRectMake(0, 0, 200, 200)

This did not change anything.

Has anyone had any success with this in Swift?

docwelch
  • 75
  • 6

3 Answers3

0

Make sure all of your plots are already in the graph before using that legend initializer. You can also initialize a legend with an array of individual plots.

Eric Skroch
  • 27,381
  • 3
  • 29
  • 36
  • Eric, yes, all plots are already in the graph. I did also try to initialize the legend with an array of plots but it still did not work. – docwelch Dec 10 '14 at 01:49
0

I found a solution here : Problems with legend in Core-Plot (pieChart)

You need to add this method

func legendTitleForPieChart(pieChart:CPTPieChart,recordIndex index:Int) -> NSString{
    return "the title"
}
Community
  • 1
  • 1
LukeYU
  • 1
0

You need 3 things:

this delegate:

CPTPieChartDelegate

You need set the delegate to self on piechart:

pieChart.delegate = self

and feed the legend:

func legendTitleForPieChart(pieChart: CPTPieChart!, recordIndex idx: UInt) -> String! {
    return "titulo"
}
ricardo
  • 1,221
  • 2
  • 21
  • 39
  • I have all of that set, but still the method is not getting called. Could I be missing anything else? – userx Jun 26 '15 at 04:37