diff --git a/ChartsDemo/Objective-C/Components/BalloonMarker.swift b/ChartsDemo/Objective-C/Components/BalloonMarker.swift index fd17888970..bb4ca3030b 100644 --- a/ChartsDemo/Objective-C/Components/BalloonMarker.swift +++ b/ChartsDemo/Objective-C/Components/BalloonMarker.swift @@ -42,11 +42,47 @@ open class BalloonMarker: MarkerImage 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) + var offset = self.offset + var size = self.size + + if size.width == 0.0 && image != nil + { + size.width = image!.size.width + } + if size.height == 0.0 && image != nil + { + size.height = image!.size.height + } + + let width = size.width + let height = size.height + let padding: CGFloat = 8.0 + + var origin = point + origin.x -= width / 2 + origin.y -= height + + if origin.x + offset.x < 0.0 + { + offset.x = -origin.x + padding + } + else if let chart = chartView, + origin.x + width + offset.x > chart.bounds.size.width + { + offset.x = chart.bounds.size.width - origin.x - width - padding + } + + if origin.y + offset.y < 0 + { + offset.y = height + padding; + } + else if let chart = chartView, + origin.y + height + offset.y > chart.bounds.size.height + { + offset.y = chart.bounds.size.height - origin.y - height - padding + } + + return offset } open override func draw(context: CGContext, point: CGPoint) @@ -68,36 +104,74 @@ open class BalloonMarker: MarkerImage 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() + 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() + } } - rect.origin.y += self.insets.top + 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) diff --git a/ChartsDemo/Swift/Components/BalloonMarker.swift b/ChartsDemo/Swift/Components/BalloonMarker.swift index 2fe3e93141..bc0128ef8c 100644 --- a/ChartsDemo/Swift/Components/BalloonMarker.swift +++ b/ChartsDemo/Swift/Components/BalloonMarker.swift @@ -34,11 +34,47 @@ public class BalloonMarker: MarkerImage { } public 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) + var offset = self.offset + var size = self.size + + if size.width == 0.0 && image != nil + { + size.width = image!.size.width + } + if size.height == 0.0 && image != nil + { + size.height = image!.size.height + } + + let width = size.width + let height = size.height + let padding: CGFloat = 8.0 + + var origin = point + origin.x -= width / 2 + origin.y -= height + + if origin.x + offset.x < 0.0 + { + offset.x = -origin.x + padding + } + else if let chart = chartView, + origin.x + width + offset.x > chart.bounds.size.width + { + offset.x = chart.bounds.size.width - origin.x - width - padding + } + + if origin.y + offset.y < 0 + { + offset.y = height + padding; + } + else if let chart = chartView, + origin.y + height + offset.y > chart.bounds.size.height + { + offset.y = chart.bounds.size.height - origin.y - height - padding + } + + return offset } public override func draw(context: CGContext, point: CGPoint) { @@ -58,36 +94,71 @@ public class BalloonMarker: MarkerImage { context.saveGState() 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.x += self.insets.left - rect.origin.y += self.insets.top - rect.size.width -= self.insets.left + self.insets.right + + 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() + } + + 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)