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

Custom marker gets displayed wrong on the latest value #1883

Closed
jackalcool opened this issue Nov 25, 2016 · 1 comment
Closed

Custom marker gets displayed wrong on the latest value #1883

jackalcool opened this issue Nov 25, 2016 · 1 comment

Comments

@jackalcool
Copy link

I have a little issue when trying to display the Balloon Marker on the latest chart value. When I tap on other nodes the popup is rendered well, only the latest value gets cut.

baloon

This is my custom marker code, taken from your swift example

public class BalloonMarker: MarkerImage
{
  public var color: UIColor?
  public var arrowSize = CGSize(width: 15, height: 11)
  public var font: UIFont?
  public var textColor: UIColor?
  public var insets = UIEdgeInsets()
  public var minimumSize = CGSize()
  public var currency: String!
  
  private var labelns: NSString?
  private var _labelSize: CGSize = CGSize()
  private var _paragraphStyle: NSMutableParagraphStyle?
  private 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.defaultParagraphStyle().mutableCopy() as? NSMutableParagraphStyle
    _paragraphStyle?.alignment = .Center
  }
  
  public override func offsetForDrawingAtPos(point: CGPoint) -> CGPoint
  {
    let size = self.size
    var point = point
    point.y -= size.height
    return super.offsetForDrawingAtPos(point)
  }
  
  public override func draw(context context: CGContext, point: CGPoint)
  {
    if (labelns == nil)
    {
      return
    }
    
    let offset = self.offsetForDrawingAtPos(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
    
    let rounding = (rect.size.height - arrowSize.height) / 2
    let bubbleFrame = CGRect(x: rect.origin.x, y: rect.origin.y+1, width: rect.width, height: rect.height - arrowSize.height)
    let bubblePath = UIBezierPath(roundedRect: bubbleFrame, byRoundingCorners: UIRectCorner.AllCorners, cornerRadii: CGSize(width: rounding, height: rounding))
    
    color!.setFill()
    bubblePath.fill()
    
    CGContextSaveGState(context)
    
    CGContextSetFillColorWithColor(context, (color?.CGColor)!)
    CGContextBeginPath(context)

    
    CGContextMoveToPoint(context,
                         rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
                         rect.origin.y + rect.size.height - arrowSize.height)
    CGContextAddLineToPoint(context,
                            rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
                            rect.origin.y + rect.size.height - arrowSize.height)
    CGContextAddLineToPoint(context,
                            rect.origin.x + rect.size.width / 2.0,
                            rect.origin.y + rect.size.height)
    CGContextAddLineToPoint(context,
                            rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
                            rect.origin.y + rect.size.height - arrowSize.height)
    
    CGContextFillPath(context)
    
    rect.origin.y += self.insets.top
    rect.size.height -= self.insets.top + self.insets.bottom
    
    UIGraphicsPushContext(context)
    
    labelns?.drawInRect(rect, withAttributes: _drawAttributes)
    
    UIGraphicsPopContext()
    
    CGContextRestoreGState(context)
  }
  
  public override func refreshContent(entry entry: ChartDataEntry, highlight: Highlight) {
    
    let numberFormatter = NSNumberFormatter()
    numberFormatter.locale = NSLocale.currentLocale()
    numberFormatter.numberStyle = .DecimalStyle
    numberFormatter.maximumFractionDigits = 2
    numberFormatter.minimumFractionDigits = 2
    
    let price = currency + numberFormatter.stringFromNumber(NSNumber(float: Float(entry.y)))!
    
    setLabel(price)
  }
  
  public func setLabel(label: String) {
    labelns = label as NSString
    
    _drawAttributes.removeAll()
    _drawAttributes[NSFontAttributeName] = self.font
    _drawAttributes[NSParagraphStyleAttributeName] = _paragraphStyle
    _drawAttributes[NSForegroundColorAttributeName] = self.textColor
    
    _labelSize = labelns?.sizeWithAttributes(_drawAttributes) ?? CGSizeZero
    
    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
  }
}
@liuxuan30
Copy link
Member

liuxuan30 commented Nov 29, 2016

It sees like the marker is trying to avoid being cut? I see this in ChartsDemo lien chart too. check out super.offsetForDrawing()

By using #1741, it might display better.

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

2 participants