xAxis标签的问题

时间:2018-02-20 09:10:44

标签: objective-c cptxygraph

我在打印xAxis标签时遇到问题 my try 对于这类问题的大多数答案是,给图形提供足够的空间.plotAreaFrame.paddingBottom

但我有足够的149!

我做错了什么?

- (void) initGraph
{
    UIView *viewToRemove = [self.grafikView viewWithTag:1];

    if(hostingView != nil)
        [viewToRemove removeFromSuperview];
    int maxWidth = self.grafikView.bounds.size.width;
    int maxHeight = self.grafikView.bounds.size.height;

    graph = [[CPTXYGraph alloc] initWithFrame:CGRectMake(10, 10, maxWidth-10, maxHeight-10)];
    graph.plotAreaFrame.borderLineStyle = nil;
    graph.plotAreaFrame.cornerRadius = 0.0f;

    graph.plotAreaFrame.paddingLeft = 50.0;
    graph.plotAreaFrame.paddingTop = 50.0;
    graph.plotAreaFrame.paddingRight = 50.0;
    graph.plotAreaFrame.paddingBottom = 140.0;

    CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle];
    textStyle.color = [CPTColor grayColor];
    textStyle.fontSize = 16.0f;
    textStyle.textAlignment = CPTTextAlignmentCenter;
    graph.titleTextStyle = textStyle;
    graph.titleDisplacement = CGPointMake(0.0f, -10.0f);
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;

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

    CPTXYAxis *xAxis = [axisSet xAxis];
    [xAxis setMajorIntervalLength:CPTDecimalFromInt(1)];
    [xAxis setMinorTickLineStyle:nil];
    [xAxis setLabelingPolicy:CPTAxisLabelingPolicyNone];
    [xAxis setLabelTextStyle:textStyle];
    [xAxis setLabelRotation:M_PI/4];

    NSArray *subjectsArray = [self getSubjectTitlesAsArray];
    [xAxis setAxisLabels:[NSSet setWithArray:subjectsArray]];

    CPTXYAxis *y = axisSet.yAxis;
    y.axisLineStyle = nil;
    y.majorTickLineStyle = nil;
    y.minorTickLineStyle = nil;
    y.majorIntervalLength = CPTDecimalFromString(@"500");
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
    y.titleOffset = 40.0f;
    y.titleLocation = CPTDecimalFromFloat(150.0f);
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];

    y.majorGridLineStyle = gridLineStyle;

    hostingView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, maxWidth, maxHeight)];
    hostingView.hostedGraph = graph;
    hostingView.tag = 1;
    [self.grafikView addSubview:hostingView];

    CPTGraph *graph = hostingView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;

    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0f) length:CPTDecimalFromInt((int)[self.monateArray count])];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.min-500) length:CPTDecimalFromDouble(self.max - self.min + 500)];

    CPTScatterPlot *planPlot = [[CPTScatterPlot alloc] init];
    planPlot.dataSource = self;
    planPlot.identifier = @"Plan";
    [graph addPlot:planPlot toPlotSpace:plotSpace];
    CPTMutableLineStyle *planLineStyle = [planPlot.dataLineStyle mutableCopy];
    planLineStyle.lineWidth = 3.0;
    planLineStyle.lineColor = COLORKURVE;
    planPlot.dataLineStyle = planLineStyle;

    CPTScatterPlot *istPlot = [[CPTScatterPlot alloc] init];
    istPlot.dataSource = self;
    istPlot.identifier = @"Ist";
    [graph addPlot:istPlot toPlotSpace:plotSpace];
    CPTMutableLineStyle *istLineStyle = [planPlot.dataLineStyle mutableCopy];
    istLineStyle.lineWidth = 3.0;
    istLineStyle.lineColor = COLORBALKEN;
    istPlot.dataLineStyle = istLineStyle;

    graph.legend = [CPTLegend legendWithGraph:graph];
    graph.legend.cornerRadius = 5.0;
    graph.legend.swatchSize = CGSizeMake(25.0, 25.0);
    graph.legendAnchor = CPTRectAnchorBottom;
    graph.legendDisplacement = CGPointMake(0.0, -10.0);
    graph.legend.numberOfColumns = 3;

}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    return [self.planArray count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    switch (fieldEnum)
    {
        case CPTScatterPlotFieldX:
        {
            return [NSNumber numberWithUnsignedInteger:index];
        }
            break;
        case CPTScatterPlotFieldY:
        {
            if ([plot.identifier isEqual:@"Plan"] == YES)
            {
                return self.planArray[index];
            }
            else if ([plot.identifier isEqual:@"Ist"] == YES)
            {
                return self.istArray[index];
            }
        }
            break;
    }
    return [NSDecimalNumber zero];
}

- (NSArray *)getSubjectTitlesAsArray
{
    NSMutableArray *labelArray = [NSMutableArray array];

    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    [textStyle setFontSize:10];

    for (int i = 0; i < [self.monateArray count]; i++)
    {
        CPTAxisLabel *axisLabel = [[CPTAxisLabel alloc] initWithText:self.monateArray[i] textStyle:textStyle];
        [axisLabel setTickLocation:CPTDecimalFromInt(i + 1)];
        [axisLabel setRotation:M_PI/4];
        [axisLabel setOffset:0.1];
        [labelArray addObject:axisLabel];
    }

    return [NSArray arrayWithArray:labelArray];
}

2 个答案:

答案 0 :(得分:0)

y.titleOffset = 40.0f;
y.titleLocation = CPTDecimalFromFloat(150.0f);

与上面的y轴代码一样,您必须给出x轴的偏移量和位置。

答案 1 :(得分:0)

我找到了!通过试验和错误; - )

问题在于:

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(self.min-500) length:CPTDecimalFromDouble(self.max - self.min + 500)];

我想用CPTDecimalFromDouble(self.min-500)以“第一个值”开始yAxis

更改为0后

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(self.max  + 500)];

我可以看到xAxis标签: enter image description here

如何在第一张图片中缩小t xAx并查看标签?