以不同的速率绘制多个散点线?

时间:2016-02-15 06:59:07

标签: xcode swift plot real-time core-plot

我使用CorePlot实现实时绘图,但我有不同采样率的两种不同数据。

我的心电图采样率为250Hz,Resp采样率为50Hz。我想在同一个图表中进行实时绘图。

首先,我尝试以不同的计数修改数组,但是当它转到numberForPlot函数时,错误发生了。似乎recordIndex应该是相同的数量。

我该怎么办?我应该修改CorePlot方法吗?或者只是修改我的代码?

谢谢!

func plotChartTest(ecg:[Int], resp:[Int], packNum: Int){
    if (packNum == 1){

        ...

        self.newGraph.addPlot(respLinePlot)
        self.newGraph.addPlot(ecgLinePlot)

        // Add some initial data

        for i in 0 ..< tempEcgArray.count {
            let x = Double(i) * 0.01
            let y = 0.0
            //let y = Double(fecg[i])/65535.0 + 0.05
            let dataPoint: plotDataType = [.X: x, .Y:y]
            self.ecgDataForPlot.append(dataPoint)
            //ecgContentArray.append(dataPoint)
        }

        //self.ecgDataForPlot = ecgContentArray

        for i in 0 ..< tempRespArray.count {
            let x = Double(i) * 0.01
            let y = 0.0
            let dataPoint: plotDataType = [.X: x, .Y: y]
            self.respDataForPlot.append(dataPoint)
        }
        // self.respDataForPlot = respContentArray
    }

    if (currentIndex % 50 == 0){
        let i = 0
        respDataForPlot.removeAtIndex(currentIndex+i)
        respDataForPlot.insert([.X: Double(currentIndex+i)*0.01, .Y: Double(resp[i])/65535.0], atIndex: currentIndex+i)
        respLinePlot.deleteDataInIndexRange(NSMakeRange(currentIndex+i, 1))
        respLinePlot.insertDataAtIndex(UInt(currentIndex+i), numberOfRecords: 1)
    }

    for i in 0 ..< 5{
        ecgDataForPlot.removeAtIndex(currentIndex+i)
        ecgDataForPlot.insert([.X: Double(currentIndex+i)*0.01, .Y: Double(ecg[i])/65535.0], atIndex: currentIndex+i)
        ecgLinePlot.deleteDataInIndexRange(NSMakeRange(currentIndex+i, 1))
        ecgLinePlot.insertDataAtIndex(UInt(currentIndex+i), numberOfRecords: 1)
    }
    currentIndex = currentIndex + 5
}


// MARK: - Plot Data Source Methods
//Set the number of data points the plot has
func numberOfRecordsForPlot(plot: CPTPlot) -> UInt
{
    let plotID = plot.identifier as! String
    if (plotID == "ECG"){
        return UInt(self.ecgDataForPlot.count)
    }
    else if (plotID == "RESP"){
        return UInt(self.respDataForPlot.count)
    }
    return 0
}

//Returns the data point for each plot by using the plot identifier set above
func numberForPlot(plot: CPTPlot, field: UInt, recordIndex: UInt) -> AnyObject?
{
    let plotField = CPTScatterPlotField(rawValue: Int(field))
    let plotID    = plot.identifier as! String
    print("ID: \(plotID), Index: \(recordIndex)")
    if let respNum = self.respDataForPlot[Int(recordIndex)][plotField!], let ecgNum = self.ecgDataForPlot[Int(recordIndex)][plotField!]{
        if (plotField! == .Y) && (plotID == "RESP") {
            return respNum as NSNumber
        }
        else {
            return ecgNum as NSNumber
        }
    }
    else {
        return nil
    }
}

1 个答案:

答案 0 :(得分:1)

在数据源numberForPlot()函数中,仅为单个图解包数据点。由于数据数组具有不同的长度,因此在绘制较长的数组时,查找将对较短的数组失败。

if (plotID == "ECG") {
    if let ecgNum = self.ecgDataForPlot[Int(recordIndex)][plotField!] {
        return ecgNum as NSNumber
    }
    else {
        return 0 as NSNumber
    }
}
else if (plotID == "RESP") {
    if let respNum = self.respDataForPlot[Int(recordIndex)][plotField!] {
        return respNum as NSNumber
    }
    else {
        return 0 as NSNumber
    }
}
else {
    return 0 as NSNumber
}