菜鸟教程小白 发表于 2022-12-12 20:51:51

ios - Core-Plot 多 y 轴对齐问题 - 可能与缩放有关


                                            <p><p>我的问题如下:</p>

<p>我正在尝试绘制两个数据集(一个以英尺为单位,一个以度为单位),以便可以比较它们(它们共享一个共同的 x 轴值)。</p>

<p>如您所见,我的数据似乎是正确交错的,但我无法让 yAxises 以我想要的方式运行。我希望 X 轴最大/最小值实际上与两个 Y 轴对齐。我的代码如下:</p>

<pre><code>- (void)initPlot {
    ;
    ;
    ;
    ;
    ;
}

- (void)configureHost {
    self.hostView = [(CPTGraphHostingView *) initWithFrame:self.view.bounds];
    self.hostView.allowPinchScaling = YES;
    ;
}

- (void)configureGraph {
    // 1 - Create the graph
    CPTGraph *graph = [ initWithFrame:self.hostView.bounds];
    ];
    self.hostView.hostedGraph = graph;


    // 2 - Set graph title

    NSString *title = &#34;,      self.flightRecording.originAirport,   self.flightRecording.destinationAirport];

    graph.title = title;


    // 3 - Create and set text style
    CPTMutableTextStyle *titleStyle = ;
    titleStyle.color = ;
    titleStyle.fontName = @&#34;Helvetica-Bold&#34;;
    titleStyle.fontSize = 16.0f;
    graph.titleTextStyle = titleStyle;
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    graph.titleDisplacement = CGPointMake(0.0f, -20.0f);


    // 4 - Set padding for plot area
    float padding = 10.0f;
    ;
    ;
    ;
    ;

    // 5 - Enable user interactions for plot space
    altSpace= (CPTXYPlotSpace *) graph.defaultPlotSpace;
    altSpace.allowsUserInteraction = NO;

    rollSpace = [ init];
    rollSpace.allowsUserInteraction = NO;
    ;

//
//    pitchSpace = [ init];
//    pitchSpace.allowsUserInteraction = NO;
//    ;
//
//
//    slipSpace = [ init];
//    slipSpace.allowsUserInteraction = NO;
//    ;
//
//
//    gSpace = [ init];
//    gSpace.allowsUserInteraction = NO;
//    ;
}

- (void)configurePlots {
    // 1 - Get graph and plot space
    CPTGraph *graph = self.hostView.hostedGraph;




    // 2 - Create the three plots

    // Custom AHRS Plot
    CPTScatterPlot *ahrsAltPlot = [ init];
    ahrsAltPlot.dataSource = self;
    ahrsAltPlot.identifier = @&#34;AHRSALT&#34;;
    CPTColor *ahrsAltColor = ;
    ;


    CPTScatterPlot *rollPlot = [ init];
    rollPlot.dataSource = self;
    rollPlot.identifier = @&#34;ROLL&#34;;
    CPTColor *rollColor = ;
    ;

    // 3 - Set up plot space
    CPTMutablePlotRange *xRange = ;
    xRange=;


//    ;
    altSpace.xRange = xRange;

    CPTMutablePlotRange *yRange = ;
    yRange = ;
    ;

    altSpace.yRange = yRange;

    rollSpace.xRange = xRange;

    CPTMutablePlotRange *yRangeRoll =;


    ;

    rollSpace.yRange =yRangeRoll;

    ];
    ];

    ;



    // 4 - Create styles and symbols


    // AHRS Style
    CPTMutableLineStyle *ahrsLineStyle = ;

    ahrsLineStyle.lineWidth = 2.5;
    ahrsLineStyle.lineColor = ;
    ahrsAltPlot.dataLineStyle = ahrsLineStyle;

}

- (void)configureAxes {
    // 1 - Create styles
    CPTMutableTextStyle *axisTitleStyle = ;
    axisTitleStyle.color = ;
    axisTitleStyle.fontName = @&#34;Helvetica-Bold&#34;;
    axisTitleStyle.fontSize = 12.0f;


    CPTMutableLineStyle *axisLineStyle = ;
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = ;


    CPTMutableTextStyle *axisTextStyle = [ init];
    axisTextStyle.color = ;
    axisTextStyle.fontName = @&#34;Helvetica-Bold&#34;;
    axisTextStyle.fontSize = 11.0f;


    CPTMutableLineStyle *tickLineStyle = ;
    tickLineStyle.lineColor = ;
    tickLineStyle.lineWidth = 2.0f;


    CPTMutableLineStyle *gridLineStyle = ;
    tickLineStyle.lineColor = ;
    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 = @&#34;Flight Time&#34;;
    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;


    NSMutableSet *xLabels = ;
    NSMutableSet *xLocations = ;

    NSInteger i = 0;

    for (i; i &lt; maxEpoch; i += 600) {

      int hour = i / (68 * 60);
      int min   = i / 60;
      CPTAxisLabel *label = [ initWithText: textStyle:x.labelTextStyle];

      CGFloat location = i++;
      label.tickLocation = CPTDecimalFromCGFloat(location);
      label.offset = x.majorTickLength;

      if (label) {
      ;
      ];
      }
    }


    x.axisLabels = xLabels;
    x.majorTickLocations = xLocations;
    // 4 - Configure y-axis


    CPTAxis *y = axisSet.yAxis;
    y.title = @&#34;Altitude in Feet&#34;;
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = 20.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;


    y.labelTextStyle = axisTextStyle;
    y.labelOffset = 21.0f;


    y.majorTickLineStyle = axisLineStyle;
    y.majorTickLength = 20.0f;
    y.minorTickLength = 10.0f;

    y.tickDirection = CPTSignNegative;



    // Axis #2

    CPTXYAxis *yRoll = [ init];
    yRoll.title = @&#34;Degrees&#34;;

    // Styling
    yRoll.titleTextStyle = axisTitleStyle;
    yRoll.labelTextStyle = axisTextStyle;
    yRoll.axisLineStyle = axisLineStyle;
    yRoll.majorTickLineStyle = axisLineStyle;

    yRoll.plotSpace = rollSpace;
    yRoll.delegate = self;
    yRoll.labelingPolicy = CPTAxisLabelingPolicyNone;

    yRoll.coordinate = CPTCoordinateY;
    yRoll.tickDirection = CPTSignNone;
    yRoll.separateLayers = NO;

    yRoll.tickDirection = CPTSignNegative;

    yRoll.labelOffset = 21.0f;

    yRoll.axisConstraints = ;

    yRoll.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);

    NSMutableSet *rollLabels = ;
    NSMutableSet *rollLocations = ;

    for (int i =0; i &lt;= 40; i+= 5) {

      CPTAxisLabel *label = [ initWithText: textStyle:yRoll.labelTextStyle];
      CGFloat location = i;
      label.tickLocation = CPTDecimalFromCGFloat(location);
      label.offset = -16.0f;

      if (label) {
            ;
            ];
      }
    }

    yRoll.axisLabels = rollLabels;
    yRoll.majorTickLocations = rollLocations;


    NSInteger majorIncrement = 500;
    NSInteger minorIncrement = 100;
    CGFloat yMax = maxAlt;// should determine dynamically based on max price
    NSMutableSet *yLabels = ;
    NSMutableSet *yMajorLocations = ;
    NSMutableSet *yMinorLocations = ;


    for (NSInteger j = minorIncrement; j &lt;= yMax; j += minorIncrement) {
      NSUInteger mod = j % majorIncrement;
      if (mod == 0) {
            CPTAxisLabel *label = [ initWithText: textStyle:y.labelTextStyle];

            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            label.offset = -y.majorTickLength - y.labelOffset;
            if (label) {
                ;
            }
            ];
      } else {
            ];
      }
    }



    // Position the y2 axis
//    y2.axisConstraints = ;

    axisSet.yAxis.axisConstraints = ;

    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;

    self.hostView.hostedGraph.axisSet.axes = ;


    // Position floating YAxis
//    y2.axisConstraints = ;
}
</code></pre>

<p> <img src="/image/J7h6B.png" alt="enter image description here"/> </p>

<p>编辑::</p>

<p>我尝试添加以下几行,但它们也没有做任何事情</p>

<pre><code>y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
</code></pre>

<p>实际上我似乎正在取得进展:似乎如果我删除</p>

<pre><code>yAxis2.axisConstraints = ;
</code></pre>

<p>然后我的轴实际上现在似乎固定到 x 轴(虽然它不在屏幕上),但它是一个开始。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题已解决:</p>

<p>我有一些相互矛盾的指令。</p>

<p>为了实现我所需要的,我不得不使用这两个调用:</p>

<pre><code>yAxis2.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(maxEpoch); // maximum X value
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
</code></pre>

<p>然后为了确保可以正确看到所有内容,我不得不使用填充值:</p>

<pre><code>float padding = 50.0f;
;
;
;
;
</code></pre>

<p>而且我必须移除所有会搞砸事情的约束:</p>

<pre><code>//    yAxis2.axisConstraints = ;
//    y.axisConstraints = ;
</code></pre>

<p> <img src="/image/Jkuud.png" alt="enter image description here"/> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Core-Plot 多 y 轴对齐问题 - 可能与缩放有关,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22690821/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22690821/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Core-Plot 多 y 轴对齐问题 - 可能与缩放有关