草庐IT

iphone - Core Plot ScatterPlot y 轴未缩放以适合屏幕和绘图

coder 2024-01-26 原文

我一直在为我的应用程序制作一组图表,但 ScatterPlot 有一个非常具体的问题。我无法让图表的 y 轴或高度重新缩放以适合屏幕。我花了几个小时在网上搜索答案,最接近的答案是遵循 raywenderlich.com 教程。但我似乎仍然无法压缩 y 轴以使图形适合屏幕。

理想情况下,我希望图表根据输入数据动态调整大小,因为绘制的数据差异可能从数十到数百不等。

这是我用来设置图形的代码:

-(void)configureAxes{
     // 1 - Create styles
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [CPTColor whiteColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor whiteColor];
    axisTextStyle.fontName = @"Helvetica-Bold";
    axisTextStyle.fontSize = 11.0f;
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor whiteColor];
    tickLineStyle.lineWidth = 2.0f;
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;
    // 2 - Get axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
    // 3 - Configure x-axis
    CPTAxis *x = axisSet.xAxis;
    x.title = @"Date";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 15.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.tickDirection = CPTSignNegative;

    //This next line gets the number of records in the array for the date axes
    CGFloat dateCount = [timesTest count];
    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];

    //Now we build an array of dates to label the axes
    NSInteger i = 0;
    //for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) {
    for (NSString *date in datesTest) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date  textStyle:x.labelTextStyle];
        CGFloat location = i++;
        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = x.majorTickLength;
        if (label) {
            [xLabels addObject:label];
            [xLocations addObject:[NSNumber numberWithFloat:location]];
        }
    }
    x.axisLabels = xLabels;
    x.majorTickLocations = xLocations;

    // 4 - Configure y-axis
    CPTAxis *y = axisSet.yAxis;
    y.title = @"Time";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -40.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = 16.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.majorTickLength = 4.0f;
    y.minorTickLength = 2.0f;
    y.tickDirection = CPTSignPositive;
    NSInteger majorIncrement = 100;
    NSInteger minorIncrement = 50;
    CGFloat yMax = 700.0f;  // should determine dynamically based on max time
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            label.offset = -y.majorTickLength - y.labelOffset;
            if (label) {
                [yLabels addObject:label];
            }
            [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;

}


#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [timesTest count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    //NSInteger valueCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count];
    NSInteger valueCount = [timesTest count];
    switch (fieldEnum) {
        case CPTScatterPlotFieldX:
            if (index < valueCount) {
                return [NSNumber numberWithUnsignedInteger:index];
            }
            break;

        case CPTScatterPlotFieldY:
            if ([plot.identifier isEqual:pbTest] == YES) {
                return [pbTest objectAtIndex:index];
            } else if ([plot.identifier isEqual:rollAvgTest] == YES) {
                return [rollAvgTest objectAtIndex:index];
            } else if ([plot.identifier isEqual:timesTest] == YES) {
                return [timesTest objectAtIndex:index];
            }
            break;
    }
    return [NSDecimalNumber zero];
}

这是我创建数组来测试图形的地方:

datesTest = [NSArray arrayWithObjects:@"4/27",
                                            @"4/28",
                                            @"4/29",
                                            nil];

        timesTest = [NSArray arrayWithObjects:
                     [NSDecimalNumber numberWithFloat:601.1],
                     [NSDecimalNumber numberWithFloat:614.4],
                     [NSDecimalNumber numberWithFloat:607.3],
                     nil];

        pbTest = [NSArray arrayWithObjects:
                     [NSDecimalNumber numberWithFloat:601.1],
                     [NSDecimalNumber numberWithFloat:601.1],
                     [NSDecimalNumber numberWithFloat:601.1],
                     nil];

        rollAvgTest = [NSArray arrayWithObjects:
                     [NSDecimalNumber numberWithFloat:602.1],
                     [NSDecimalNumber numberWithFloat:613.4],
                     [NSDecimalNumber numberWithFloat:605.3],
                     nil];

raywenderlich 项目编译和运行完美,所需的 y 轴完美缩放以适合屏幕,但我的没有,但我无法弄清楚我错过了什么。任何关于我哪里出错的建议将不胜感激。 编辑:添加绘图配置以响应评论...

-(void)configurePlots{
    // 1 - Get graph and plot space
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    // 2 - Create the three plots
    // FIRST PLOT is Personal Best - pbPlot
    // SECOND PLOT is Rolling Average - rollAvgPlot
    // THIRD PLOT is Individual swims - swimssPlot

    //Plot 1 - Peronal Best
    CPTScatterPlot *pbPlot = [[CPTScatterPlot alloc] init];
    pbPlot.dataSource = self;
    NSString *pbID = [[NSString alloc] initWithFormat:@"pbID"];
    pbPlot.identifier = pbID;
    CPTColor *pbColor = [CPTColor redColor];
    [graph addPlot:pbPlot toPlotSpace:plotSpace];

    //Plot 2 - Rolling Average
    CPTScatterPlot *rollAvgPlot = [[CPTScatterPlot alloc] init];
    rollAvgPlot.dataSource = self;
    NSString *rollAvgID = [[NSString alloc] initWithFormat:@"rollAvgID"];
    rollAvgPlot.identifier = rollAvgID;
    CPTColor *rollAvgColor = [CPTColor greenColor];
    [graph addPlot:rollAvgPlot toPlotSpace:plotSpace];

    //Plot 3 - Actual Swim Times
    CPTScatterPlot *swimsPlot = [[CPTScatterPlot alloc] init];
    swimsPlot.dataSource = self;
    NSString *timesID = [[NSString alloc] initWithFormat:@"timesID"];
    swimsPlot.identifier = timesID;
    CPTColor *swimsColor = [CPTColor blueColor];
    [graph addPlot:swimsPlot toPlotSpace:plotSpace];

    // 3 - Set up plot space
    //[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:pbPlot, rollAvgPlot, swimsPlot, nil]];
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:swimsPlot, nil]];
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace.xRange = xRange;
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
    plotSpace.yRange = yRange;

    // 4 - Create styles and symbols
    CPTMutableLineStyle *pbLineStyle = [pbPlot.dataLineStyle mutableCopy];
    pbLineStyle.lineWidth = 2.5;
    pbLineStyle.lineColor = pbColor;
    pbPlot.dataLineStyle = pbLineStyle;
    CPTMutableLineStyle *pbSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    pbSymbolLineStyle.lineColor = pbColor;
    CPTPlotSymbol *pbSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    pbSymbol.fill = [CPTFill fillWithColor:pbColor];
    pbSymbol.lineStyle = pbSymbolLineStyle;
    pbSymbol.size = CGSizeMake(6.0f, 6.0f);
    pbPlot.plotSymbol = pbSymbol;

    CPTMutableLineStyle *rollAvgLineStyle = [rollAvgPlot.dataLineStyle mutableCopy];
    rollAvgLineStyle.lineWidth = 1.0;
    rollAvgLineStyle.lineColor = rollAvgColor;
    rollAvgPlot.dataLineStyle = rollAvgLineStyle;
    CPTMutableLineStyle *rollAvgSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    rollAvgSymbolLineStyle.lineColor = rollAvgColor;
    CPTPlotSymbol *rollAvgSymbol = [CPTPlotSymbol starPlotSymbol];
    rollAvgSymbol.fill = [CPTFill fillWithColor:rollAvgColor];
    rollAvgSymbol.lineStyle = rollAvgSymbolLineStyle;
    rollAvgSymbol.size = CGSizeMake(6.0f, 6.0f);
    rollAvgPlot.plotSymbol = rollAvgSymbol;

    CPTMutableLineStyle *swimsLineStyle = [swimsPlot.dataLineStyle mutableCopy];
    swimsLineStyle.lineWidth = 2.0;
    swimsLineStyle.lineColor = swimsColor;
    swimsPlot.dataLineStyle = swimsLineStyle;
    CPTMutableLineStyle *swimsSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    swimsSymbolLineStyle.lineColor = swimsColor;
    CPTPlotSymbol *swimsSymbol = [CPTPlotSymbol diamondPlotSymbol];
    swimsSymbol.fill = [CPTFill fillWithColor:swimsColor];
    swimsSymbol.lineStyle = swimsSymbolLineStyle;
    swimsSymbol.size = CGSizeMake(6.0f, 6.0f);
    swimsPlot.plotSymbol = swimsSymbol;
}

最佳答案

检查您的地 block 标识符。在设置图时将标识符设置为一个值,并将它们与数据源中的其他值进行比较。该测试可能会失败,这意味着每个绘图的每个 y 值都是零 (0)。

关于iphone - Core Plot ScatterPlot y 轴未缩放以适合屏幕和绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109932/

有关iphone - Core Plot ScatterPlot y 轴未缩放以适合屏幕和绘图的更多相关文章

  1. 屏幕录制为什么没声音?检查这2项,轻松解决 - 2

    相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声

  2. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  3. python - 这些脚本语言中哪种更适合渗透测试? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭9年前。Improvethisquestion首先,我想避免一场关于语言的口水战。可供选择的语言有Perl、Python和Ruby。我想提一下,我对所有这些都很满意,但问题是我不能只专注于一个。例如,如果我看到一个很棒的Perl模块,我必须尝试一下。如果我看到一个不错的Python应用程序,我必须知道它是如何制作的。如果我看到RubyDSL或一些Ruby巫术,我就会迷上Ruby一段时间。目前我是一名Java开发人员,但计划在不久的将来

  4. ruby-on-rails - 在服务器端检测屏幕尺寸和像素密度? - 2

    我一直在做一些研究,我想我已经知道答案了,但我想知道是否有任何方法可以在不使用javascript或依赖CSS3媒体的情况下获得设备的屏幕尺寸和像素密度查询。本质上,我正在研究如何获取屏幕分辨率和像素密度,以便服务器可以决定在URI请求中为服务器提供哪个图像。到目前为止,我还没有发现任何证据表明这是可能的,但我想嘿,为什么不问问呢? 最佳答案 我不完全同意上面的正确答案。实际上,这个答案在很多情况下都是正确的……但理论上并非如此。通常向Web服务器发出的请求包含一个User-Agent字段,从理论上讲,该字段可用于识别有关设备屏幕分

  5. ruby - 以编程方式在 Ruby 中获取桌面的屏幕截图? - 2

    我问了这个关于takingapictureofawebpageprogrammatically的问题,我已经下载并获得了webkit2png工作(为博客等HTML页面拍照)。太酷了,谢谢你给我看!现在我想开始做更多的事情,比如能够在加载Flash网站和我的桌面后拍照。是否可以使用webkit2png为Flash网站拍照(考虑到您可能需要等待几秒钟才能加载)?但主要问题是,如何以编程方式为桌面拍照?这将使我能够更好地控制正在发生的事情。 最佳答案 您可以使用xwd(1)获取根窗口的屏幕截图:xwd-display:0-root|xwd

  6. ruby - 适合 Ruby 程序员的 RPG IV 书籍? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭11年前。我是一名Ruby程序员,由于目前的项目需要学习RPGIV。我想学习原始类型、数据结构、控制流、体系结构等。在线资源和纸质书一样好。我应该从哪本书或在线教程开始?

  7. ruby - Lisp - 是否适合网络编程/应用程序(交互式)? ruby 的方式是? php的方式是? - 2

    Lisp是否适合Web编程/应用程序(交互式),就像ruby​​和php一样?需要考虑的事情是:易于使用可部署性难度(尤其是对于编程初学者而言)(编辑)在阅读PaulGraham'sessay之后,我特别提到了CommonLisp.将是我的第一门编程语言。在这方面。这样做合适吗?我听说Clojure的宏功能不如CommonLisp的强大,这就是我尝试学习Clojure的原因。它教授编程并且非常强大。 最佳答案 Lisp是一个语系,而不是单一的语言。为了稍微回答您的问题,是的,存在用于各种Lisp方言的Web框架,例如用于Common

  8. iphone - 扩展 restful_authentication/AuthLogic 以支持匿名 iPhone 的延迟登录的最佳方法是什么? - 2

    我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符

  9. ruby - (Watir webdriver) 在 OSX 10.9 中使用多显示器时如何将浏览器移动到另一个屏幕 - 2

    我知道有一个名为browser.window.move_to(0,0)的函数可以将浏览器移动到不同的位置,但OSX10.9对它来说是全新的。有什么方法可以将浏览器移动到另一个桌面吗?例如。在“桌面2”中触发命令的控制台,但我希望浏览器出现在“桌面1”中。非常感谢! 最佳答案 哈,在我尝试这个之前我正要说这可能是不可能的:browser.window.move_to(-1200,0)我的第二台显示器位于主屏幕的左侧。有效。好问题。您需要花点时间才能正确使用它,但watir似乎能够使用整个显示器Canvas。例如,如果您的显示器位于主屏

  10. ruby - 如何使用 ruby​​ 和 unix 服务器截取网页的屏幕截图? - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。关闭7年前。Improvethisquestion我正在尝试以编程方式创建大量网页的缩略图,这些网页托管在我自己的基于ruby​​/rails的网站上。我希望能够编写一个独立的ruby​​代码,看起来像这样:require'awesome-screenshot-maker'items.eachdo|id|url="http://foo.com/bar/#{id}"shooter=AwesomeScreenshotMa

随机推荐