HomeiOS Developmentios - fill between two strains in a graphic in xcode(swift)?

ios – fill between two strains in a graphic in xcode(swift)?


I’ve three arrays. xValues, yMinValues and yMaxValues. I wish to present a graphic and this graphic should draw two strains by my yminValues and yMaxValues array.Then fill with a shade between this strains. I draw all strains however I get a graphic like in that photograph. May you assist me?

My Code is : enter picture description right here

class CustomView: UIView {
    let xValues: [CGFloat] = [0, 2, 4, 6, 8, 10, 12, 14]
    let yMaxValues: [CGFloat] = [4, 6, 9, 10, 10, 11, 10, 11]
    let yMinValues: [CGFloat] = [2, 4, 5, 6, 7, 8, 9, 11]
    
    override func draw(_ rect: CGRect) {
        tremendous.draw(rect)
        
        guard let context = UIGraphicsGetCurrentContext() else { return }
        
        // Arka planı beyaz yapın
        UIColor.white.setFill()
        context.fill(rect)
        
        // Grafik boyutlarını alın
        let graphWidth = rect.width
        let graphHeight = rect.peak
        
        // Min ve max değerleri belirleyin
        let minValue = min(yMinValues.min() ?? 0.0, yMinValues.max() ?? 0.0)
        let maxValue = max(yMaxValues.min() ?? 0.0, yMaxValues.max() ?? 0.0)
        
        // Y değerlerini ölçeklendirin
        let yScaleFactor = graphHeight / (maxValue - minValue)
        
        // X değerlerini ölçeklendirin
        let xScaleFactor = graphWidth / CGFloat(xValues.depend - 1)
        
        // Grafik alanını doldurun
        let yFillValuesPath = UIBezierPath()

        // YFillValues için line çizin
        for i in 0..<xValues.depend {
            let x = CGFloat(i) * xScaleFactor
            let yFill = graphHeight - (yMinValues[i] - minValue) * yScaleFactor // Burada yMinValues kullanılmalı
            if i == 0 {
                yFillValuesPath.transfer(to: CGPoint(x: x, y: yFill))
            } else {
                yFillValuesPath.addLine(to: CGPoint(x: x, y: yFill))
            }

            // Noktalara yFillValues değerlerini yaz
            let pointLabel = UILabel(body: CGRect(x: x - 15, y: yFill - 30, width: 30, peak: 30))
            pointLabel.textual content = "(yMinValues[i])"
            pointLabel.textColor = UIColor.black
            pointLabel.textAlignment = .middle
            pointLabel.font = UIFont.systemFont(ofSize: 10)
            addSubview(pointLabel)
        }
        
        // Grafik alanını doldurun
        let fillColor = UIColor(crimson: 0.5, inexperienced: 0.5, blue: 1.0, alpha: 0.5)
        fillColor.setFill()
        
        yFillValuesPath.addLine(to: CGPoint(x: yMaxValues.final ?? 0, y: graphHeight - (yMaxValues.final ?? 0 - minValue) * yScaleFactor)) // yMinValues kullanılmalı
        yFillValuesPath.addLine(to: CGPoint(x: yMinValues.first ?? 0, y: graphHeight - (yMinValues.first ?? 0 - maxValue) * yScaleFactor)) // yMinValues kullanılmalı
    
        
        yFillValuesPath.fill()

        yFillValuesPath.shut()
        
        // YValues için line çizin
        let yValuesPath = UIBezierPath()
        for i in 0..<xValues.depend {
            let x = CGFloat(i) * xScaleFactor
            let y = graphHeight - (yMaxValues[i] - minValue) * yScaleFactor
            if i == 0 {
                yValuesPath.transfer(to: CGPoint(x: x, y: y))
            } else {
                yValuesPath.addLine(to: CGPoint(x: x, y: y))
            }
            
            // Noktalara yValues değerlerini yaz
            let pointLabel = UILabel(body: CGRect(x: x - 15, y: y - 30, width: 30, peak: 30))
            pointLabel.textual content = "(yMaxValues[i])"
            pointLabel.textColor = UIColor.black
            pointLabel.textAlignment = .middle
            pointLabel.font = UIFont.systemFont(ofSize: 10)
            addSubview(pointLabel)
        }
        
        // Çizgileri çizin
        let lineColor = UIColor.black.cgColor
        context.setStrokeColor(lineColor)
        yValuesPath.stroke()
        yFillValuesPath.stroke()
        
        // X ekseni çizin
        context.transfer(to: CGPoint(x: 0, y: graphHeight))
        context.addLine(to: CGPoint(x: graphWidth, y: graphHeight))
        context.strokePath()
        
        // Y ekseni çizin
        context.transfer(to: CGPoint(x: 0, y: graphHeight))
        context.addLine(to: CGPoint(x: 0, y: 0))
        context.strokePath()
        
        // X değerlerini gösterin
        for i in 0..<xValues.depend {
            let x = CGFloat(i) * xScaleFactor
            let label = UILabel(body: CGRect(x: x - 15, y: graphHeight + 5, width: 30, peak: 20))
            label.textual content = "(xValues[i])"
            label.textColor = UIColor.black
            label.textAlignment = .middle
            label.font = UIFont.systemFont(ofSize: 12)
            addSubview(label)
        }
        
        // Y değerlerini gösterin
        for i in 0..<yMaxValues.depend {
            let y = graphHeight - (yMaxValues[i] - minValue) * yScaleFactor
            let label = UILabel(body: CGRect(x: -35, y: y - 10, width: 30, peak: 20))
            label.textual content = "(yMaxValues[i])"
            label.textColor = UIColor.black
            label.textAlignment = .proper
            label.font = UIFont.systemFont(ofSize: 12)
            addSubview(label)
        }
    }
}

I wish to present a graphic stuffed with a shade between yMinValues array and yMaxValues.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments