Core Plot: Tick Intervals

天大地大妈咪最大 提交于 2019-12-08 10:34:09

问题


I'm using Core Plot in an iOS-Application and want to format the steps of axis. All I get is steps of 0.5 and I don't know what else to change. My tick Locations should by 5, 10, 15 and 20.

Here is the code that draws my axis:

-(void) addAxis{

    // Create grid line styles
    CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
    majorGridLineStyle.lineWidth = 1.0f;
    majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75];

    NSSet *majorTickLocations = [NSSet setWithObjects:[NSDecimalNumber zero],
                                 [NSDecimalNumber numberWithUnsignedInteger:5],
                                 [NSDecimalNumber numberWithUnsignedInteger:10],
                                 [NSDecimalNumber numberWithUnsignedInteger:15],
                                 [NSDecimalNumber numberWithUnsignedInteger:20],
                                 nil];

    // Create axes
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
    CPTXYAxis *x = axisSet.xAxis;
    {
        x.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
        x.tickDirection = CPTSignNone;
        x.majorTickLocations = majorTickLocations;
        x.labelOffset = 6.0;
        x.labelRotation = M_PI/2;
        x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(19.0f)];
        x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(30.0f)];
        x.title = @"Jahre";
        x.titleOffset = 35.0f;
        x.titleLocation = CPTDecimalFromFloat(x.visibleRange.lengthDouble / 2.0);
        x.plotSpace = self.barPlotSpace;
    }

    CPTXYAxis *y = axisSet.yAxis;
    {
        y.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
        y.majorTickLocations = majorTickLocations;
        y.minorTicksPerInterval = 0;
        y.preferredNumberOfMajorTicks = 8;
        y.majorGridLineStyle = majorGridLineStyle;
        y.axisLineStyle = nil;
        y.majorTickLineStyle = nil;
        y.minorTickLineStyle = nil;
        y.labelOffset = 10.0;
        y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(30.0f)];
        y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(18.5f)];
        y.title = @"Sparbetrag";
        y.titleOffset = 40.0f;
        y.titleLocation = CPTDecimalFromFloat(y.visibleRange.lengthDouble / 2.0);
        y.plotSpace = barPlotSpace;
    }

    // Set axes
    self.graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
}

回答1:


Found the solution: Applying a theme changes the painting-behavior of the plot. If I don't apply a theme - everything is fine.

For this is not very intuitive. I would expect that a theme only changes colors or that some aspects can be overridden.




回答2:


That code should work fine--you'll end up with tick marks and labels at 0, 5, 10, 15, and 20. The settings for visibleRange and gridLinesRange may not be right--leave them at the default of nil to have the axes and grid lines extend the full width and height of the plot area.

If you want to change the number format of the labels, set the labelFormatter to a new NSNumberFormatter object configured to give the number format you want.



来源:https://stackoverflow.com/questions/9177737/core-plot-tick-intervals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!