使用Core Plot时轴上的数字不显示

时间:2011-11-01 17:08:30

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

我正在使用Core Plot library

为我的iOS应用制作图表

但是我的轴没有显示任何数字或标签:

Screenshot of graph

这是我的viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self generateExponentialDataSamples];
    [self generateGraphDataSamples];

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

    // Create graph from theme
    CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
    [graph applyTheme:theme];
    graph.paddingLeft = 30.0;
    graph.paddingTop = 30.0;
    graph.paddingRight = 30.0;
    graph.paddingBottom = 30.0;              

    hostingView.hostedGraph = graph;

    // Title
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor greenColor];
    textStyle.fontSize = 18.0f;
    textStyle.fontName = @"Helvetica";
    graph.title = @"My Progress";
    graph.titleTextStyle = textStyle;
    graph.titleDisplacement = CGPointMake(0.0f, -20.0f);

    double xAxisStart = 0.0;
    double xAxisLength = MAX_X_VALUE;
    double yAxisStart = 0.0;
    //double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"]doubleValue];
    double yAxisLength = 10;

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange  plotRangeWithLocation:CPTDecimalFromDouble(xAxisStart) length:CPTDecimalFromDouble(xAxisLength)];
    plotSpace.yRange = [CPTPlotRange  plotRangeWithLocation:CPTDecimalFromDouble(yAxisStart) length:CPTDecimalFromDouble(yAxisLength)];

    //Axis
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

    CPTXYAxis *x = axisSet.xAxis;
    x.minorTicksPerInterval = 0; //the number of minor ticks between every major tick
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); // The axis should begin in the origin
    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    x.title = @"x-axis";
    x.titleTextStyle = textStyle;

    CPTXYAxis *y = axisSet.yAxis;
    y.minorTicksPerInterval = 0;
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    y.title = @"y-axis";
    y.titleTextStyle = textStyle;

    CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc]init];
    dataSourceLinePlot.dataSource = self;

    // Add line style
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineWidth = 1.0f;
    lineStyle.lineColor = [CPTColor greenColor];
    dataSourceLinePlot.dataLineStyle = lineStyle;

    // create the fill area
    dataSourceLinePlot.areaFill = [CPTFill fillWithColor:[[CPTColor greenColor] colorWithAlphaComponent:0.2f]];
    dataSourceLinePlot.areaBaseValue = CPTDecimalFromString(@"0");

    [graph addPlot:dataSourceLinePlot];
}

这有点乱,但我真的很感激一些帮助。

1 个答案:

答案 0 :(得分:2)

设置每个轴的刻度方向:

x.tickDirection = CPTSignPositive;
y.tickDirection = CPTSignPositive;