From dcddfe753e8882ffa7636483d19cdff94956af1e Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 15 Jan 2018 11:20:40 -0400 Subject: [PATCH 1/2] Reimplemented #1741 --- .../Components/BalloonMarker.swift | 137 ++++++++++++----- .../Swift/Components/BalloonMarker.swift | 139 +++++++++++++----- 2 files changed, 210 insertions(+), 66 deletions(-) diff --git a/ChartsDemo/Objective-C/Components/BalloonMarker.swift b/ChartsDemo/Objective-C/Components/BalloonMarker.swift index fd17888970..4acadc536f 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,71 @@ 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 d18b3d2823..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,34 +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.y += self.insets.top + + 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) From fe7c08b4201b754f0ccd100a929926797ca7d4f2 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 21 Jan 2018 21:45:53 -0400 Subject: [PATCH 2/2] Fixed code style --- ChartsDemo/Objective-C/Components/BalloonMarker.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ChartsDemo/Objective-C/Components/BalloonMarker.swift b/ChartsDemo/Objective-C/Components/BalloonMarker.swift index 4acadc536f..bb4ca3030b 100644 --- a/ChartsDemo/Objective-C/Components/BalloonMarker.swift +++ b/ChartsDemo/Objective-C/Components/BalloonMarker.swift @@ -104,7 +104,8 @@ open class BalloonMarker: MarkerImage if let color = color { - if offset.y > 0 { + if offset.y > 0 + { context.beginPath() context.move(to: CGPoint( x: rect.origin.x, @@ -132,7 +133,9 @@ open 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,