ios - 如何显示x轴标题?

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

我使用 Xcode 开发适用于 iOS 8 的应用程序。 我想创建一个温度图。 我向图表添加了值,它显示了图表,但隐藏了 x 轴和 x 标题。

我该如何解决这个问题?

这是我的功能:

#import "CorePlot-CocoaTouch.h"
@property CPTScatterPlot *linePlot;
@property (nonatomic, strong) CPTGraphHostingView *hostView;
@property (nonatomic, strong) CPTGraph *graph;

初始化

-(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;


    }

向图表添加值

-(void)addHRValueToGraph:(int)data
    {
        [tmpValues addObject:[NSDecimalNumber numberWithInt:data]];
        if ([tmpValues count] > plotXMaxRange) {
            plotXMaxRange = plotXMaxRange + plotXMaxRange;
            plotXInterval = plotXInterval + plotXInterval;
            [self updatePlotSpace];
        }
        [self.graph reloadData];
    }

viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    LogDebug(@"viewDidLoad");

    tmpValues = [[NSMutableArray alloc]init];
    tmpDBvalues= [[NSMutableArray alloc]init];

    plotXMaxRange = 24;
    plotXMinRange = 1;

    plotYMaxRange = 5;
    plotYMinRange = 35

    plotYInterval = 1;
    plotXInterval = 2;

}

No x-axis

最佳答案

X 轴需要知道相对于 Y 轴的绘制位置。

尝试在这一行中添加:

axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(35);

关于ios - 如何显示x轴标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28208635/

相关文章:

c# - 从多线程数据源平滑更新图表

algorithm - 从邻接表计算入度图

python - 在 Python 中使用邻接表构建节点图

iPhone 6.1 模拟器

ios - 将动画与 UILabel 结合使用

ios - 我如何处理没有我自己的代码迹象的崩溃?

ios - AVPlayer 和 Video 的屏幕截图

ios - cocos2d如何连续移动背景图片

iphone - 我们可以使用企业帐户将 WebGL 应用程序部署为 native iOS 应用程序吗

ios - Swift 或 Objective C 中的 CalDAV 处理