【问题标题】:Center the axis in UIView using Core Plot in xcode使用 xcode 中的 Core Plot 将 UIView 中的轴居中
【发布时间】:2015-03-06 12:37:16
【问题描述】:

我刚刚将 Core Plot 合并到我的项目中,我需要做的是在图表中绘制不同的圆圈。我已经设置了图表,它看起来不错,但有些问题我似乎无法解决。我发现缺少 Core Plot 的文档,这些示例也没有什么帮助。

我一直在使用这个例子来绘制我的图表,散点图之一,即第 2 部分中的最后一个:http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2

当我绘制图表时,我会在 UIView 的左下角看到坐标轴,就像示例一样。但我希望我的轴绘制在 UIView 的中间,所以我希望 origo 位于我的视图的中心。我怎么做?我能够移动轴,使它们在视图中间相互交叉,但 origo 仍在其他地方。

我还想动态更改轴的范围,我似乎也无法解决。我想发送一个值,例如6.0,调整轴的范围和中心是我的看法,如下图。

【问题讨论】:

    标签: ios objective-c xcode graph core-plot


    【解决方案1】:

    您可以使用相对轴约束在绘图区域中居中轴。约束值介于 0 和 1 之间,对应于绘图区域的边缘。使用 0.5 将轴准确定位在中间。

    x.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0.5];
    y.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0.5];
    

    【讨论】:

    • 嗯,这样做肯定会将轴设置为中心。如果我不使用axisConstraints,则轴的交叉点位于左下角的 (0.0, 0.0) 处。如果我添加axisConstraints,轴会居中,但交叉点不在(3.0,3.0)处,并且轴可以彼此独立移动(因为我总是指定轴的交叉点应该始终在中心) .我没有在屏幕中间得到 (0.0, 0.0),我只得到中间的交叉点 (3.0, 3.0),因为我将图表设置在 -6.0 到 6.0 之间。
    • 如果您总是希望轴保持在一个位置,在同一坐标处交叉,您需要使用绘图空间委托来约束更改。跨度>
    • 我并不总是希望坐标系停留在一个地方,我只希望轴总是在一个地方相互交叉,并且轴交叉点在视图中居中启动。之后,用户应该能够在该视图中四处移动、缩放等。不过会调查情节空间代表!谢谢
    【解决方案2】:

    没多久我就修好了。但这是一种痛苦。关于此的 0 个文档。我会在这里写一篇关于如何做的文章。

    -----如何-- (我使用的所有额外代码都存在于 OP 的教程链接中)

    在配置图表时,您可以使用一些函数来设置 xRange 和 yRange。玩弄这些给了我非常奇怪的结果,但我想我现在已经弄清楚它们是如何联系起来的。

    我已将教程中 - (void)configureGraph 方法中的第 5 步替换为 `// 5 - 启用绘图空间的用户交互 // 我已经从教程中更改了这个...

    // This value is the passing value for the graph. That is, it's the value I'd like each side of the axes to show.
    float coordinateValue = 6.0f;
    
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    
    // This code sets up the range of the two axes. I set it to start on coordinateValue.
    // By setting the CPTDecimalFromDouble to 2.0 * coordinateValue I specify how long the axes are going to be.
    // Since they are going to be twice as long as the value I want on each side I have to multiply with 2.
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-coordinateValue) length:CPTDecimalFromDouble(2.0 * coordinateValue)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-coordinateValue) length:CPTDecimalFromDouble(2.0 * coordinateValue)];
    
    
    // Here I obtain each axis in order to do specifications on them.
    // Axes
    CPTXYAxisSet *axisSet           = (CPTXYAxisSet *)graph.axisSet;
    
    CPTXYAxis *x                    = axisSet.xAxis;
    // Setting majorIntervalLength states which value should be given a number. 3.0 sets it to each third,
    // which means that my axis will show 0.0--|--|--3.0--|--|--6.0-->x
    x.majorIntervalLength           = CPTDecimalFromDouble(3.0f);
    x.orthogonalCoordinateDecimal   = CPTDecimalFromDouble(0.0);
    
    // How many ticks there should be for each interval.
    x.minorTicksPerInterval         = 3;
    
    CPTXYAxis *y                    = axisSet.yAxis;
    y.majorIntervalLength           = CPTDecimalFromDouble(3.0f);
    y.orthogonalCoordinateDecimal   = CPTDecimalFromDouble(0.0);
    y.minorTicksPerInterval         = 3;
    
    plotSpace.allowsUserInteraction = YES;`
    

    我的图表没有居中显示我的 UIView 中心的原点,轴范围从 -6.0 到 6.0。

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多