Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to round the corners of the BalloonMarker? #3276

Closed
emildesign opened this issue Feb 20, 2018 · 3 comments
Closed

Is there a way to round the corners of the BalloonMarker? #3276

emildesign opened this issue Feb 20, 2018 · 3 comments

Comments

@emildesign
Copy link

I want to add small rounded corners on the marker label, is it support?

@liuxuan30
Copy link
Member

I just remember someone posted code about this but couldn't find it right now. Please try search. If I was wrong, then you have to draw the rounding thing by yourself.

@spyroid
Copy link

spyroid commented Oct 17, 2018

I want to add small rounded corners on the marker label, is it support?

 open override func draw(context: CGContext, point: CGPoint) {
        guard let label = label else { return }
        
        let offset = self.offsetForDrawing(atPoint: point)
        let size = self.size
        
        var rect = CGRect(
            origin: CGPoint(
                x: point.x + offset.x,
                y: point.y + offset.y),
            size: size)
        rect.origin.x -= size.width / 2.0
        rect.origin.y -= size.height
        
        context.saveGState()
        
        context.setFillColor(color.cgColor)
        
        if offset.y > 0 {
            
            context.beginPath()
            let rect2 = CGRect(x: rect.origin.x, y: rect.origin.y + arrowSize.height, width: rect.size.width, height: rect.size.height - arrowSize.height)
            let clipPath = UIBezierPath(roundedRect: rect2, cornerRadius: 5.0).cgPath
            context.addPath(clipPath)
            context.closePath()
            context.fillPath()
            
            // arraow vertex
            context.beginPath()
            let p1 = CGPoint(x: rect.origin.x + rect.size.width / 2.0 - arrowSize.width / 2.0, y: rect.origin.y + arrowSize.height + 1)
            context.move(to: p1)
            context.addLine(to: CGPoint(x: p1.x + arrowSize.width, y: p1.y))
            context.addLine(to: CGPoint(x: point.x, y: point.y))
            context.addLine(to: p1)

            context.fillPath()
            
        } else {
            context.beginPath()
            let rect2 = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height - arrowSize.height)
            let clipPath = UIBezierPath(roundedRect: rect2, cornerRadius: 5.0).cgPath
            context.addPath(clipPath)
            context.closePath()
            context.fillPath()

            // arraow vertex
            context.beginPath()
            let p1 = CGPoint(x: rect.origin.x + rect.size.width / 2.0 - arrowSize.width / 2.0, y: rect.origin.y + rect.size.height - arrowSize.height - 1)
            context.move(to: p1)
            context.addLine(to: CGPoint(x: p1.x + arrowSize.width, y: p1.y))
            context.addLine(to: CGPoint(x: point.x, y: point.y))
            context.addLine(to: p1)

            context.fillPath()
        }
        
        if offset.y > 0 {
            rect.origin.y += self.insets.top + arrowSize.height
        } else {
            rect.origin.y += self.insets.top
        }
        
        rect.size.height -= self.insets.top + self.insets.bottom
        
        UIGraphicsPushContext(context)
        
        label.draw(in: rect, withAttributes: _drawAttributes)
        
        UIGraphicsPopContext()
        
        context.restoreGState()
    }

@azad389
Copy link

azad389 commented Aug 7, 2023

adding path to context didn't work
I have created a fully customisable one using xib and passed rect and values and managed bubble using the bool condition and handled date using the global block

var isTop = false
if offset.y > 0 {
isTop = true
rect.origin.y += self.insets.top + arrowSize.height
rect.size.height -= self.insets.bottom
} else {
isTop = false
rect.origin.y += self.insets.top
rect.size.height -= self.insets.top
}

(UIApplication.shared.delegate as? AppDelegate)?.bindValue.value = ChartMarkerModel(isTop: isTop, value: label, rect: rect, color: color, font: font, textColor: textColor)

class ChartMarkerView: UIView {

@IBOutlet private var contentView: UIView!
@IBOutlet weak var lblPrice: UILabel!
@IBOutlet weak var bubbleTop: UIImageView!
@IBOutlet weak var bubbleBottom: UIImageView!
@IBOutlet weak var centerImage: UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        // Setup view from .xib file
        
        xibSetup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // Setup view from .xib file
        xibSetup()
    }

    private func xibSetup() {
        Bundle.main.loadNibNamed("ChartMarkerView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.backgroundColor = .clear
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        layer.cornerRadius = 8
        clipsToBounds = true
        layer.masksToBounds = true
    }

}

let markerView = BalloonMarker(color: UIColor.clear,
font: UIFont.systemFont(ofSize: 12, weight: .medium),
textColor:chartColors[5],
insets: UIEdgeInsets(top: 8, left: 1, bottom: 8, right: 1))
(UIApplication.shared.delegate as? AppDelegate)?.bindValue.bind{[weak self] model in
guard let self = self else{return}

        let marker = ChartMarkerView(frame: model?.rect ?? .zero)     
        marker.lblPrice.text = model?.value
        marker.lblPrice.textColor = model?.textColor
        marker.lblPrice.font = model?.font
        if model?.isTop ?? false{
            marker.frame.origin.y -= 20
            marker.bubbleTop.isHidden = false
            marker.bubbleBottom.isHidden = true
        }else{
            marker.frame.origin.y -= 4
            marker.bubbleBottom.isHidden = false
            marker.bubbleTop.isHidden = true
        }
        self.chartView.addSubview(marker)
    }
    markerView.chartView = chartView
    markerView.minimumSize = CGSize(width: 80, height: 60)
    chartView.marker = markerView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants