2016-10-30 7 views
0

散布図を使用してx軸に線とカスタムラベルを描画しています。私はいくつかのラインが正確にy軸といくつかのカスタムラベルの位置を形成するようにしたいと思います.PFAイメージ。 enter image description hereコアプロットを使用してカスタムラベルを使用して散布図を描画する

制御線はy軸から開始し、 "Sat"まで進み、データ線は "Sun"形式で開始し、 "Sat"まで描画する必要があります。 xのticklocation配列を返す午前私はコントロールライン用とデータ線のための2レコードにしようとしたエリックによって示唆されるようにプロットXIのために、 - : は、いずれもこの

EDIT 1を達成するためにどのように私を助けることができます軸とプロットyiはグラフのy値を返します。これを行うことによって、制御線は適切であるが、データ線は常にゼロから始まる。 PFA添付画像enter image description here

は、ここで私は以下のデータ・ソース用のX軸

if(self.axisArray.count == 7) { 
       tickLocation = [1, 2, 3, 4, 5, 6, 7] 
      } 
      else { 
       tickLocation = [1, 2, 3, 4, 5, 6, 7, 8] 
      } 

      var xLocations = [NSNumber]() 
      var labelLocation: Int = -1 
      for number in tickLocation { 
       labelLocation += 1 
       let newLabel = CPTAxisLabel.init(text: axisArray[labelLocation] as? String, textStyle: axisLabelStyle) 
       newLabel.tickLocation = number as! NSNumber 
       newLabel.offset = x.labelOffset + x.majorTickLength 
       xLocations.append(NSNumber(value: number as! Double)) 
       customLabel.append(newLabel) 
      } 
      x.labelingPolicy = CPTAxisLabelingPolicy.none 
      x.axisLabels = Set(customLabel) 
      x.majorTickLocations = Set(xLocations) 

のラベルを作成するために書いていたコードは、コード

func numberOfRecords(for plot: CPTPlot) -> UInt 
    { 
     if(plot.identifier as! String == BloodGlucoseGraphView.kUpperControlLineIdentifier) { 
      return UInt(2) 
     } 
     else if(plot.identifier as! String == BloodGlucoseGraphView.kLowerControlLineIdentifier) { 
      return UInt(2) 
     } 
     else if(plot.identifier as! String == BloodGlucoseGraphView.kFirstGraphIdentfifier) { 
      return UInt(self.beforeMealData.count) 
     } 
     else if(plot.identifier as! String == BloodGlucoseGraphView.kSecondGraphIdentfifier) { 
      return UInt(self.afterMealData.count) 
     } 
     else { 
      return UInt(0) 
     } 
    } 

    func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? 
    { 
     let plotField = CPTScatterPlotField(rawValue: Int(field)) 
     if(plot.identifier as! String == BloodGlucoseGraphView.kUpperControlLineIdentifier) { 
      if(plotField == .X) { 
        return Int(record) 
      } 
      else if(plotField == .Y) { 
        return 6 
      } 
     } 
     else if(plot.identifier as! String == BloodGlucoseGraphView.kLowerControlLineIdentifier) { 
      if(plotField == .X) { 
       return Int(record) 
      } 
      else if(plotField == .Y) { 
        return 5.2 
      } 
     } 
      if(plot.identifier as! String == BloodGlucoseGraphView.kFirstGraphIdentfifier) { 
       if(plotField == .X) { 
        return self.tickLocation[Int(record)] as! NSNumber 
       } 
       else if(plotField == .Y) { 
        return self.afterMealData[Int(record)] 
       } 
      } 
      else if(plot.identifier as! String == BloodGlucoseGraphView.kSecondGraphIdentfifier) { 
       if(plotField == .X) { 
        return self.tickLocation[Int(record)] as! NSNumber 
       } 
       else if(plotField == .Y) { 
        return self.beforeMealData[Int(record)] 
       } 
      } 
     return nil 
    } 

しかし、またデータです行は "Sun"の形式ではなくゼロから始まっています。 @Ericはこれに関して私を助けてください。

答えて

0

データラインについては、データソースから返されたx値が対応するx軸ラベルのティック位置と一致することを確認してください。画像から、それはオフ・バイ・ワン・エラーのように見えます。

コントロールラインの場合、ラインの両端に1つずつ、2つのデータポイントが必要です。左の点はプロット空間xRangelocationのx値を持ち、右の点はxRangeendのx値を持つ必要があります。

+0

返信いただきありがとうございます。私はコードでもう一度質問を編集しました。あなたはそれを見て、私を助けてくれますか? – Gyanendra

+0

プロット空間の 'xRange'とは何ですか?また、データラインのx値は間違っています。 'return(record * self.tickLocation.count)を! NSNumber'。 –

+0

xとyの範囲は次のようになります。plotSpace.xRange = CPTPlotRange(location:0、length:NSNumber.init(value:self.axisArray.count) ) plotSpace.yRange = CPTPlotRange(場所:yMin、長さ:範囲)と私はコメントに記載されている "return(Int(record)* self.tickLocation.count)NSNumberとして"のようなソリューションを試してみました。まだ0から始まっています。 – Gyanendra

関連する問題