0
I am unable to put three legend boxes on the graph.I have tried the following code


     CPTGraph *graph1 = self.hostView.hostedGraph;

        CGFloat legendPadding = -(self.view.bounds.size.width / 8);

        graph1.legendDisplacement = CGPointMake(legendPadding, 0.0);  
                        
        CPTLegend *theLegend1 = [CPTLegend legendWithGraph:graph1];

        theLegend1.numberOfColumns = 1;

        theLegend1.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];

        theLegend1.borderLineStyle = [CPTLineStyle lineStyle];

        theLegend1.cornerRadius = 5.0;       


        graph1.legend = theLegend1;     

        graph1.legendAnchor = CPTRectAnchorTop;
        

        CGFloat legendPadding1 = -(self.view.bounds.size.width / 4);


        graph1.legendDisplacement = CGPointMake(legendPadding1, 0.0);

where am i going wrong ?

Here is the code for adding plots:

     CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.delegate = self;
    // 2 - Create the three plots
    aaplPlot = [[[CPTScatterPlot alloc] init]autorelease];
    aaplPlot.dataSource = self;
    aaplPlot.identifier = @"MaxWeight";

    CPTColor *aaplColor = [CPTColor magentaColor];
    [graph addPlot:aaplPlot toPlotSpace:plotSpace];
    googPlot = [[[CPTScatterPlot alloc] init]autorelease];
    googPlot.dataSource = self;
    googPlot.identifier = @"MinWeight";
    CPTColor *googColor = [CPTColor greenColor];
    [graph addPlot:googPlot toPlotSpace:plotSpace];

    msftPlot = [[[CPTScatterPlot alloc] init]autorelease];
    msftPlot.dataSource = self;
    msftPlot.identifier = @"NormalWeight";

    CPTColor *msftColor = [CPTColor blueColor];
    [graph addPlot:msftPlot toPlotSpace:plotSpace];

    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:aaplPlot,msftPlot,googPlot,nil]];

プロットを追加するためにこのコードを追加しました。上記のコードを使用すると、凡例ボックスを 1 つだけプロットできます。私は間違っていますか?

4

1 に答える 1

0

セットアップ コードを省略しない限り、問題は、凡例を作成した時点でグラフにプロットがないことです。+legendWithGraph:プロットをグラフに追加した後にで凡例を作成するか、-addPlot:または-insertPlot:atIndex:メソッドを使用して後で凡例に追加します。

于 2013-03-21T00:27:22.610 に答える