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

About BalloonMarker #2216

Closed
pwhsueh opened this issue Mar 3, 2017 · 9 comments
Closed

About BalloonMarker #2216

pwhsueh opened this issue Mar 3, 2017 · 9 comments

Comments

@pwhsueh
Copy link

pwhsueh commented Mar 3, 2017

I upgraded swift 2.3 to 3.0. I found the BalloonMarker is not working now.

It can't show marker in the chart . Could someone help me?

let marker =  BalloonMarker(color: UIColor(hex: "#000000"), font: UIFont.systemFont(ofSize: 12), textColor: UIColor(hex: "#000000"), insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
        marker.image = UIImage(named: "dashboard-point_heart")
        marker.size = CGSize(width: 50, height: 50)
        marker.chartView = self.chartView
@thierryH91200
Copy link
Contributor

Missing a line

chartView.marker = marker

@pwhsueh
Copy link
Author

pwhsueh commented Mar 3, 2017

@thierryH91200 It work for me !! Many thanks .

@pwhsueh
Copy link
Author

pwhsueh commented Mar 3, 2017

Another question is about the image property

In Swift 2.3

screen shot 2017-03-03 at 6 05 30 pm

in swift 3.0

screen shot 2017-03-03 at 6 04 00 pm

How can I use my customize image ?

@mayckonx
Copy link

mayckonx commented Mar 3, 2017

I'm having the same problem.

@pwhsueh
Copy link
Author

pwhsueh commented Mar 6, 2017

I found marker image is hided by default marker.so I changed the color to clear and add the following code

if let image = image {
    image.draw(in: rect)
}
  1. the init code
 let  marker =  BalloonMarker(color: UIColor.clear, font: UIFont.systemFont(ofSize: 12), textColor: UIColor.white, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
        marker.image = markerImage
        self.chartView.marker = marker

2.the BalloonMarker code

open class BalloonMarker: MarkerImage
{
    open var color: UIColor?
    open var arrowSize = CGSize(width: 15, height: 11)
    open var font: UIFont?
    open var textColor: UIColor?
    open var insets = UIEdgeInsets()
    open var minimumSize = CGSize()
    
    fileprivate var labelns: NSString?
    fileprivate var _labelSize: CGSize = CGSize()
    fileprivate var _paragraphStyle: NSMutableParagraphStyle?
    fileprivate var _drawAttributes = [String : AnyObject]()
    
    public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
    {
        super.init()
        
        self.color = color
        self.font = font
        self.textColor = textColor
        self.insets = insets
        
        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .center
    }
    
    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        let size = self.size
        var point = point
        point.x -= size.width / 2.0
        point.y -= size.height
        return super.offsetForDrawing(atPoint: point)
    }
    
    open override func draw(context: CGContext, point: CGPoint)
    {
        if labelns == nil
        {
            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
        
        if let image = image {
            image.draw(in: rect)
        }
        
        context.saveGState()
        
        if let color = color
        {
            context.setFillColor(color.cgColor)
            context.beginPath()
            context.move(to: CGPoint(
                x: rect.origin.x,
                y: rect.origin.y))
            context.addLine(to: CGPoint(
                x: rect.origin.x + rect.size.width,
                y: rect.origin.y))
            context.addLine(to: CGPoint(
                x: rect.origin.x + rect.size.width,
                y: rect.origin.y + rect.size.height - arrowSize.height))
            context.addLine(to: CGPoint(
                x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
                y: rect.origin.y + rect.size.height - arrowSize.height))
            context.addLine(to: CGPoint(
                x: rect.origin.x + rect.size.width / 2.0,
                y: rect.origin.y + rect.size.height))
            context.addLine(to: CGPoint(
                x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
                y: rect.origin.y + rect.size.height - arrowSize.height))
            context.addLine(to: CGPoint(
                x: rect.origin.x,
                y: rect.origin.y + rect.size.height - arrowSize.height))
            context.addLine(to: CGPoint(
                x: rect.origin.x,
                y: rect.origin.y))
            context.fillPath()
        }
        
        rect.origin.y += self.insets.top
        rect.size.height -= self.insets.top + self.insets.bottom
        
        UIGraphicsPushContext(context)
        
        labelns?.draw(in: rect, withAttributes: _drawAttributes)
        
        UIGraphicsPopContext()
        
        context.restoreGState()
    }
    
    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        setLabel(String(entry.y))
    }
    
    open func setLabel(_ label: String)
    {
        labelns = label as NSString
        
        _drawAttributes.removeAll()
        _drawAttributes[NSFontAttributeName] = self.font
        _drawAttributes[NSParagraphStyleAttributeName] = _paragraphStyle
        _drawAttributes[NSForegroundColorAttributeName] = self.textColor
        
        _labelSize = labelns?.size(attributes: _drawAttributes) ?? CGSize.zero
        
        var size = CGSize()
        size.width = _labelSize.width + self.insets.left + self.insets.right
        size.height = _labelSize.height + self.insets.top + self.insets.bottom
        size.width = max(minimumSize.width, size.width)
        size.height = max(minimumSize.height, size.height)
        self.size = size
    }
}

@pwhsueh pwhsueh closed this as completed Mar 6, 2017
@renatamakuch
Copy link

@pwhsueh how did you show two markers on line?

@pwhsueh
Copy link
Author

pwhsueh commented May 26, 2017

@xrena Just set the chart view highlightValues . Set an Highlight array to it.

@renatamakuch
Copy link

@pwhsueh can you show me how you use array to it? because I still have one marker :(

@pwhsueh
Copy link
Author

pwhsueh commented May 29, 2017

var minChartHighlightsEntry = Highlight

        for i in 0 ..< hrMinDims.count {
            
            if (hrMinDims[i] == minXValue && hrMinDims[i] != 0 ) {
                let hight = Highlight(x: Double(i), y: Double(hrMinDims[i]), dataSetIndex: 0)
                hight.dataIndex = 0
                minChartHighlightsEntry.append(hight)
            }
        }

....

        self.chartView.highlightValues(minChartHighlightsEntry)

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