2016-10-30 40 views
0

我正在使用散點圖在x軸上繪製線條和自定義標籤。我想要一些線條從y軸開始,並形成一些自定義標籤位置.PFA圖像。 enter image description here使用核心圖使用自定義標籤繪製散點圖

控制線應該從y軸開始,一直走到「Sat」,數據線應該從「Sun」開始,並應該畫到「Sat」。 任何一個可以幫助我如何實現這一目標

編輯1: - 正如埃裏克建議我試着用2記錄的控制線和數據線,對於情節喜正在返回的x的ticklocation陣列Axis和plot 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 
    } 

但隨後也將數據線條從零開始而不是「太陽」。 @Eric你可以幫我在這方面

回答

0

對於數據線,請確保從數據源返回的x值匹配相應的x軸標籤的滴答位置。從圖片看,它看起來像是一個錯誤的錯誤。

對於控制線,這些只需要兩個數據點,一個用於線的每一端。左點應該有繪圖空間xRangelocation的x值,並且右點應該具有xRangeend的x值。

+0

感謝您的回覆。我已經用代碼重新編輯了這個問題。你可以看看它,並幫助我。 – Gyanendra

+0

什麼是繪圖空間的xRange?另外,數據線的x值也是錯誤的。嘗試'返回(記錄* self.tickLocation.count)爲! NSNumber'。 (我沒有測試這個 - 你可能需要在那裏進行一些轉換。) –

+0

x和y的範圍就像這樣plotSpace.xRange = CPTPlotRange(location:0,length:NSNumber.init(value:self.axisArray.count) ) plotSpace.yRange = CPTPlotRange(位置:yMin,長度:範圍),我試着解決方案像「返回(INT(記錄)* self.tickLocation.count)作爲評論中提到的NSNumber」。它仍然只從0開始。 – Gyanendra

相關問題