如何在点后显示两位数的线条标签?

时间:2015-02-04 08:10:30

标签: ios objective-c core-plot ios8.1

我在我的Xcode项目中使用了核心图,并且在绘图值方面存在问题。

如何在CPTScatterPlot上显示数字? 我的情节只显示了一个点后的一位数(36.6)。 比如给我看36.665或36.001?

我添加了一个屏幕预览。

How show 36.21

INIT

-(void)initLinePlotTmp
{
    //Initialize and display Graph (x and y axis lines)
    self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphView.bounds];
    self.hostView = [[CPTGraphHostingView alloc] initWithFrame:self.graphView.bounds];
    self.hostView.hostedGraph = self.graph;
    [self.graphView addSubview:hostView];

    //apply styling to Graph
    [self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];

    //set graph backgound area transparent
    self.graph.backgroundColor = nil;
    self.graph.fill = nil;
    self.graph.plotAreaFrame.fill = nil;
    self.graph.plotAreaFrame.plotArea.fill = nil;

    //This removes top and right lines of graph
    self.graph.plotAreaFrame.borderLineStyle = nil;
    //This shows x and y axis labels from 0 to 1
    self.graph.plotAreaFrame.masksToBorder = NO;

    // set padding for graph from Left and Bottom
    self.graph.paddingBottom = 10;
    self.graph.paddingLeft = 50;
    self.graph.paddingRight = 0;
    self.graph.paddingTop = 10;

    //Define x and y axis range
    // x-axis from 0 to 100
    // y-axis from 0 to 300
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(plotXMinRange)
                                                    length:CPTDecimalFromInt(plotXMaxRange)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(plotYMinRange)
                                                    length:CPTDecimalFromInt(plotYMaxRange)];

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

    NSNumberFormatter *axisLabelFormatter = [[NSNumberFormatter alloc]init];
    [axisLabelFormatter setGeneratesDecimalNumbers:NO];
    [axisLabelFormatter setNumberStyle:NSNumberFormatterDecimalStyle];


    //Define x-axis properties
    //x-axis intermediate interval 2
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromInt(plotXInterval);
    axisSet.xAxis.minorTicksPerInterval = 1;
    axisSet.xAxis.minorTickLength = 5;
    axisSet.xAxis.majorTickLength = 7;
    axisSet.xAxis.title = @"Time(Hours)";
    axisSet.xAxis.titleOffset = 25;
    axisSet.xAxis.labelFormatter = axisLabelFormatter;

    //Define y-axis properties
    //y-axis intermediate interval = 50;
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromInt(plotYInterval);
    axisSet.yAxis.minorTicksPerInterval = 4;
    axisSet.yAxis.minorTickLength = 5;
    axisSet.yAxis.majorTickLength = 7;
    axisSet.yAxis.title = @"Temperature";
    axisSet.yAxis.titleOffset = 30;
    axisSet.yAxis.labelFormatter = axisLabelFormatter;


    //Define line plot and set line properties
    self.linePlot = [[CPTScatterPlot alloc] init];
    self.linePlot.dataSource = self;
    [self.graph addPlot:self.linePlot toPlotSpace:plotSpace];

    //set line plot style
    CPTMutableLineStyle *lineStyle = [self.linePlot.dataLineStyle mutableCopy];
    lineStyle.lineWidth = 2;
    lineStyle.lineColor = [CPTColor blackColor];
    self.linePlot.dataLineStyle = lineStyle;

    CPTMutableLineStyle *symbolineStyle = [CPTMutableLineStyle lineStyle];
    symbolineStyle.lineColor = [CPTColor blackColor];
    CPTPlotSymbol *symbol = [CPTPlotSymbol ellipsePlotSymbol];
    symbol.fill = [CPTFill fillWithColor:[CPTColor blackColor]];
    symbol.lineStyle = symbolineStyle;
    symbol.size = CGSizeMake(3.0f, 3.0f);
    self.linePlot.plotSymbol = symbol;

    //set graph grid lines
    CPTMutableLineStyle *gridLineStyle = [[CPTMutableLineStyle alloc] init];
    gridLineStyle.lineColor = [CPTColor grayColor];
    gridLineStyle.lineWidth = 0.5;
    axisSet.xAxis.majorGridLineStyle = gridLineStyle;
    axisSet.yAxis.majorGridLineStyle = gridLineStyle;


    }

这是我的情节初始函数。

3 个答案:

答案 0 :(得分:2)

创建NSNumberFormatter并将其配置为根据需要格式化标签。将绘图上的labelFormatter设置为新的格式化程序。

答案 1 :(得分:0)

NSLog(@"Digit with Three decimal Values = %.3f",999.123456789);

此Prints 999.123 //小数点后的三个值

相应地将文本分配给该标签。

答案 2 :(得分:0)

您将添加到:

leftAxis.valueFormatter = leftAxis.valueFormatter;

    //remove decimal
    NSNumberFormatter *vf = [[NSNumberFormatter alloc] init];
    vf.generatesDecimalNumbers = NO;
    data.valueFormatter = vf;