Skip to content

Commit

Permalink
Merge pull request #3291 from danielgindi/BalloonMarker
Browse files Browse the repository at this point in the history
Align Objc and Swift demos balloon marker
  • Loading branch information
liuxuan30 authored Feb 23, 2018
2 parents 8a0c223 + a5f6008 commit 64438c4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 90 deletions.
142 changes: 70 additions & 72 deletions ChartsDemo/Objective-C/Components/BalloonMarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,28 @@ import Charts

open class BalloonMarker: MarkerImage
{
@objc open var color: UIColor?
@objc open var color: UIColor
@objc open var arrowSize = CGSize(width: 15, height: 11)
@objc open var font: UIFont?
@objc open var textColor: UIColor?
@objc open var insets = UIEdgeInsets()
@objc open var font: UIFont
@objc open var textColor: UIColor
@objc open var insets: UIEdgeInsets
@objc open var minimumSize = CGSize()

fileprivate var label: String?
fileprivate var _labelSize: CGSize = CGSize()
fileprivate var _paragraphStyle: NSMutableParagraphStyle?
fileprivate var _drawAttributes = [NSAttributedStringKey : Any]()
fileprivate var _drawAttributes = [NSAttributedStringKey : AnyObject]()

@objc 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
super.init()
}

open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
Expand Down Expand Up @@ -101,69 +100,68 @@ open class BalloonMarker: MarkerImage
rect.origin.y -= size.height

context.saveGState()

if let color = color

context.setFillColor(color.cgColor)

if offset.y > 0
{
context.beginPath()
context.move(to: CGPoint(
x: rect.origin.x,
y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
y: rect.origin.y + arrowSize.height))
//arrow vertex
context.addLine(to: CGPoint(
x: point.x,
y: point.y))
context.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width,
y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width,
y: rect.origin.y + rect.size.height))
context.addLine(to: CGPoint(
x: rect.origin.x,
y: rect.origin.y + rect.size.height))
context.addLine(to: CGPoint(
x: rect.origin.x,
y: rect.origin.y + arrowSize.height))
context.fillPath()
}
else
{
if offset.y > 0
{
context.beginPath()
context.move(to: CGPoint(
x: rect.origin.x,
y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
y: rect.origin.y + arrowSize.height))
//arrow vertex
context.addLine(to: CGPoint(
x: point.x,
y: point.y))
context.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width,
y: rect.origin.y + arrowSize.height))
context.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width,
y: rect.origin.y + rect.size.height))
context.addLine(to: CGPoint(
x: rect.origin.x,
y: rect.origin.y + rect.size.height))
context.addLine(to: CGPoint(
x: rect.origin.x,
y: rect.origin.y + arrowSize.height))
context.fillPath()
}
else
{
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))
//arrow vertex
context.addLine(to: CGPoint(
x: point.x,
y: point.y))
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()
}
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))
//arrow vertex
context.addLine(to: CGPoint(
x: point.x,
y: point.y))
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()
}

if offset.y > 0 {
Expand Down Expand Up @@ -193,9 +191,9 @@ open class BalloonMarker: MarkerImage
label = newLabel

_drawAttributes.removeAll()
_drawAttributes[NSAttributedStringKey.font] = self.font
_drawAttributes[NSAttributedStringKey.paragraphStyle] = _paragraphStyle
_drawAttributes[NSAttributedStringKey.foregroundColor] = self.textColor
_drawAttributes[.font] = self.font
_drawAttributes[.paragraphStyle] = _paragraphStyle
_drawAttributes[.foregroundColor] = self.textColor

_labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero

Expand Down
47 changes: 29 additions & 18 deletions ChartsDemo/Swift/Components/BalloonMarker.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
//
// BalloonMarker.swift
// ChartsDemo-iOS
// ChartsDemo-Swift
//
// Created by Jacob Christie on 2017-07-09.
// Copyright © 2017 jc. All rights reserved.
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
// A port of MPAndroidChart for iOS
// Licensed under Apache License 2.0
//
// https://github.com/danielgindi/Charts
//

import Foundation
import Charts

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()
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 label: String?
fileprivate var _labelSize: CGSize = CGSize()
fileprivate var _paragraphStyle: NSMutableParagraphStyle?
fileprivate var _drawAttributes = [NSAttributedStringKey : AnyObject]()

public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) {
public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
{
self.color = color
self.font = font
self.textColor = textColor
Expand All @@ -33,7 +38,8 @@ public class BalloonMarker: MarkerImage {
super.init()
}

public override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint {
open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
{
var offset = self.offset
var size = self.size

Expand Down Expand Up @@ -77,7 +83,8 @@ public class BalloonMarker: MarkerImage {
return offset
}

public override func draw(context: CGContext, point: CGPoint) {
open override func draw(context: CGContext, point: CGPoint)
{
guard let label = label else { return }

let offset = self.offsetForDrawing(atPoint: point)
Expand All @@ -95,7 +102,8 @@ public class BalloonMarker: MarkerImage {

context.setFillColor(color.cgColor)

if offset.y > 0 {
if offset.y > 0
{
context.beginPath()
context.move(to: CGPoint(
x: rect.origin.x,
Expand Down Expand Up @@ -123,7 +131,9 @@ public class BalloonMarker: MarkerImage {
x: rect.origin.x,
y: rect.origin.y + arrowSize.height))
context.fillPath()
} else {
}
else
{
context.beginPath()
context.move(to: CGPoint(
x: rect.origin.x,
Expand Down Expand Up @@ -170,11 +180,13 @@ public class BalloonMarker: MarkerImage {
context.restoreGState()
}

public override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
{
setLabel(String(entry.y))
}

public func setLabel(_ newLabel: String) {
open func setLabel(_ newLabel: String)
{
label = newLabel

_drawAttributes.removeAll()
Expand All @@ -191,5 +203,4 @@ public class BalloonMarker: MarkerImage {
size.height = max(minimumSize.height, size.height)
self.size = size
}

}

0 comments on commit 64438c4

Please sign in to comment.