如何使用getPosition获取最后一次数据录入的位置?图表荚

How to use getPosition to get the last data entry position? Charts Pod

我正在尝试为最后一个数据入口点添加一个 UIImage,但是当我使用 getPosition 时,图像位于错误的位置。或者还有其他方法吗?我尝试了 答案,但图像位置仍然不正确。

 if dataPoint.count > 0 {
            for i in 0..<dataPoint.count {
                let value = ChartDataEntry(x: Double(i), y: dataPoint[i].running)
                lineChartEntry.append(value)                
       }
}

 let point = lineChartView.getPosition(entry: lineChartEntry[dataPoint.count - 1], axis: .left)
                let image = UIImage(named: "heart")
                let imageView = UIImageView(image: image!)
                imageView.frame = CGRect(x: point, y: point, width:15, height: 15)
                lineChartView.addSubview(imageView)

> po point  (59.11698190789474, 28.672200520833343)
>   - x : 59.11698190789474
>   - y : 28.672200520833343

做了更多的研究和测试,我终于让它工作了。

在您设置 LineChartDataSet()

的位置
//data line setup
let line = LineChartDataSet(entries: lineChartEntry, label: "")
line.mode = .linear                                          
line.colors = [color]                              
line.fillColor = .clear                                     
line.drawFilledEnabled = true
line.drawCirclesEnabled = true

//remove old imageView when the new data comes in
if let viewWithTag = self.view.viewWithTag(100) {
    viewWithTag.removeFromSuperview()
}

var colors = [UIColor]()
for i in 0..<lineChartEntry.count {
    //only the last circle will show the image
    if(i == lineChartEntry.count - 1) {
        colors.append(color)
        
        let point = lineChartView.getPosition(entry: lineChartEntry[lineChartEntry.count - 1], axis: .left) //get the x and y CGPoint
        let image = UIImage(named: "heart")
        let imageView = UIImageView(image: image!)
        imageView.tag = (100)
        
        if !point.x.isNaN && !point.y.isNaN {
            imageView.frame = CGRect(x: point.x - 15, y: point.y - 15, width:30, height: 30) //set the frame in center
            imageView.contentMode = .center
            lineChartView.addSubview(imageView)
        }
    } else {
        colors.append(UIColor.clear)
    }
}

.
.
.
//other line data configurations