使用Core Plot时未显示数据点

时间:2012-03-10 18:43:26

标签: objective-c cocoa-touch uiview graph core-plot

我正在使用Core Plot 1.0和iOS 5.1为iOS应用制作图表。

我已经浏览了所有可以找到核心情节的教程,除了散点图数据本身外,我的图表上都显示了所有内容...

由于声誉无法发布截图,因此以下链接指向:http://francoismaillet.com/coreplot_problem.png

我正在使用SimpleScatterPlot.m example中的numberOfRecordsForPlot和numberForPlot方法。 generateData方法生成0到2之间的数字。

我已经转了一段时间,我们将非常感谢任何帮助。

这是我的班级界面:

interface PriceChartViewController : UIViewController <CPTPlotDataSource, CPTScatterPlotDelegate>{
    NSArray *plotData;
}

这是我的viewDidLoad方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self generateData];

    CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];

    CPTXYGraph *graph = [[[CPTXYGraph alloc] initWithFrame:self.view.bounds] autorelease];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
    [graph applyTheme:theme];

    hostingView.hostedGraph = graph;
    //hostingView.collapsesLayers = YES;

    graph.paddingLeft   = 15.0;
    graph.paddingTop    = 40.0;
    graph.paddingRight  = 15.0;
    graph.paddingBottom = 40.0;


    // Get plotSpace from graph
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2)
                                                   length:CPTDecimalFromFloat(4)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2)
                                                   length:CPTDecimalFromFloat(4)];

    [graph addPlotSpace:plotSpace];


    // Get axisset from graph
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;


    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorGridLineStyle.lineWidth = 1.0f;
    majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75f];

    CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
    minorGridLineStyle.lineWidth = 1.0f;
    minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.25f];


    CPTXYAxis *x = axisSet.xAxis;
    x.title = @"X axis";
    x.titleOffset = -20.0f;
    x.titleLocation = CPTDecimalFromFloat(30.0f);

    x.majorGridLineStyle = majorGridLineStyle;
    x.minorGridLineStyle = minorGridLineStyle;

    x.majorIntervalLength = CPTDecimalFromInteger(1);
    x.minorTicksPerInterval = 0.5;
    x.plotSpace = plotSpace;       


    CPTXYAxis *y = axisSet.yAxis;
    y.majorIntervalLength = CPTDecimalFromInteger(1);
    y.minorTicksPerInterval = 0.5;
    //y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(-0.5);
    //y.preferredNumberOfMajorTicks = 8;
    y.plotSpace = plotSpace;


    // Create scatterplot
    CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
    dataSourceLinePlot.identifier = @"AllTests";

    CPTMutableLineStyle *lineStyle   = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
    //lineStyle.miterLimit             = 1.0f;
    lineStyle.lineWidth              = 3.0f;
    lineStyle.lineColor              = [CPTColor blueColor];
    dataSourceLinePlot.dataLineStyle = lineStyle;
    dataSourceLinePlot.dataSource    = self;
    [graph addPlot:dataSourceLinePlot];

    // Do a blue gradient
    CPTColor *areaColor1        = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
    CPTGradient *areaGradient1  = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
    areaGradient1.angle         = -90.0f;
    CPTFill *areaGradientFill   = [CPTFill fillWithGradient:areaGradient1];
    dataSourceLinePlot.areaFill = areaGradientFill;
    dataSourceLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];

    // Auto scale the plot space to fit the plot data
    // Extend the ranges by 30% for neatness
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]];
    CPTMutablePlotRange *xRange = [[plotSpace.xRange mutableCopy] autorelease];
    CPTMutablePlotRange *yRange = [[plotSpace.yRange mutableCopy] autorelease];
    [xRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
    [yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)];
    plotSpace.xRange = xRange;
    plotSpace.yRange = yRange;


    graph.legend                     = [CPTLegend legendWithGraph:graph];
    graph.legend.textStyle           = x.titleTextStyle;
    graph.legend.fill                = [CPTFill fillWithColor:[CPTColor darkGrayColor]];
    graph.legend.borderLineStyle     = x.axisLineStyle;
    graph.legend.cornerRadius        = 5.0;
    graph.legend.swatchSize          = CGSizeMake(25.0, 25.0);
    graph.legendAnchor               = CPTRectAnchorBottom;
    graph.legendDisplacement         = CGPointMake(0.0, 12.0);


    [self setView:hostingView];
}

2 个答案:

答案 0 :(得分:0)

删除此行:

[graph addPlotSpace:plotSpace];

您已使用一个绘图空间定位轴并将绘图添加到另一个绘图空间。如果您使用默认绘图空间执行所有操作,它应该可以正常工作。我认为在这样的简单图表中不需要两个不同的绘图空间。

答案 1 :(得分:0)

当我尝试使用最新的核心绘图源(版本6f43faadc647)时,代码运行正常,这很奇怪,因为自1.0版本以来似乎没有任何改变。