Search code examples
iosobjective-ccalayercore-plot

How can I access the layer so I can flip it?


I implemented corePlot into my xcode project. I'm trying to flip the legend of the graph.

enter image description here

Here is the code:

CPTGraph *graph = self.hostView.hostedGraph;
CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];
graph.legend = theLegend;

This is what I tried:

theLegend.sublayerTransform = CATransform3DMakeScale( CPTFloat(1.0), CPTFloat(-1.0), CPTFloat(1.0) );

That didn't do anything. So I tryied replacing theLegend.sublayerTransform with theLegend.transform and that didn't do anything either. How can I access the legend layer so I can flip it?

Update

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
[cell.layer addSublayer:graph.legend];

Solution

  • The short answer to your question is that you need to flip the layer of the view which contains your legend layer.

    [[viewWithLegendSublayer layer] setTransform:CATransform3DMakeScale(1.0f, -1.0f, 1.0f)];
    

    This will do the trick, but if you add any other subviews to that view, they'll be upside down.

    N.B.: There may be better ways to address this issue, which work within the intended design of the API (e.g., CPTLegend expects to be a sublayer of a CPTGraph).

    Here's the approach we discussed in chat. Since you want to put your legend into a UITableView, why not just skip the CPTLegend completely, and use several table view cells as your legend items? You'd probably have to set up your own colors, but once that's done, you can just create standard table view cells, and set their text using the titles from your legend. Then create a small square UIView (you can round its layer's corners for visual appeal), set its background color to match, and set it as your cell's accessoryView (or do the same with a UIImageView and the cell's imageView).