开发者

iPhone: cannot see any labels on my core plot graph

I'm building a graph with core plot, that works fine except that I do not see any labels on the x and y axis. I can only see major and minor ticks but no value next to them. There must be something I'm missing but I do not know what. Also, is it possible to add a label for each axis, for instance: x (hours), y (value).

The graph I am working on is part of the first row of a listview.

The code is:

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GraphCustomViewCell" owner:nil options:nil];

            for(id currentObject in topLevelObjects)
            {
                if([currentObject isKindOfClass:[GraphCustomViewCell class]])
                {
                    cell = (GraphCustomViewCell *)currentObject;
                    break;
                }
            }

            CGRect frame = cell.contentView.bounds;

            CPLayerHostingView *hostingView = [[CPLayerHostingView alloc] initWithFrame: frame];
            [cell addSubview:hostingView];

            // Create graph
            CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
            graph = (CPXYGraph *)[theme newGraph];  

            // Add graph to view
            hostingView.hostedLayer = graph;
            graph.paddingLeft = 50.0;
       开发者_如何学C     graph.paddingTop = 5.0;
            graph.paddingRight = 5.0;
            graph.paddingBottom = 25.0;

            // Adjust graph
            float xmax = [[[self arrayFromData] objectForKey:@"data"] count];
            CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
            plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                                           length:CPDecimalFromFloat(xmax)];
            plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                                           length:CPDecimalFromFloat(3000)];

            CPTextStyle *blackText = [[CPTextStyle textStyle] color:[CPColor blackColor]];
            CPLineStyle *lineStyle = [CPLineStyle lineStyle];
            lineStyle.lineColor = [CPColor blackColor];
            lineStyle.lineWidth = 1.0f;

            axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"5"] decimalValue];
            axisSet.xAxis.minorTicksPerInterval = 4;
            axisSet.xAxis.majorTickLineStyle = lineStyle;
            axisSet.xAxis.minorTickLineStyle = lineStyle;
            axisSet.xAxis.axisLineStyle = lineStyle;
            axisSet.xAxis.minorTickLength = 4.0f;
            axisSet.xAxis.majorTickLength = 8.0f;
            axisSet.xAxis.labelOffset = 3.0f;
            axisSet.xAxis.labelTextStyle = blackText;


            axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"500"] decimalValue];
            axisSet.yAxis.minorTicksPerInterval = 4;
            axisSet.yAxis.majorTickLineStyle = lineStyle;
            axisSet.yAxis.minorTickLineStyle = lineStyle;
            axisSet.yAxis.axisLineStyle = lineStyle;
            axisSet.yAxis.minorTickLength = 4.0f;
            axisSet.yAxis.majorTickLength = 8.0f;
            axisSet.yAxis.labelOffset = 1.0f;

            CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
            dataSourceLinePlot.identifier = @"MyGraph";
            dataSourceLinePlot.dataLineStyle.lineWidth = 1.f;
            dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
            dataSourceLinePlot.dataSource = self;
            [graph addPlot:dataSourceLinePlot];

            // Put an area gradient under the plot above
            CPColor *areaColor = [CPColor colorWithComponentRed:0.3 
                                                          green:1.0
                                                           blue:0.3
                                                          alpha:0.3];
            CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                                                                  endingColor:[CPColor clearColor]];
            areaGradient.angle = -90.0f;
            CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
            dataSourceLinePlot.areaFill = areaGradientFill;
            dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"1.75");

Thanks a lot, Regards, Luc

ps: quick edit, I have checked with graph.name = "my graph", but nothing is displayed on the graph


OK, this was due to the fact there were too many labels to display and also because the axis did not went far enough in the negative area => no place to display the labels. But, graph.name does not display a name of the graph...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜