Skip to content

Commit 02ab02f

Browse files
committed
Updated Chartmarker drawing logic
1 parent 896cbfd commit 02ab02f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/MyStore/ChartMarker.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Charts
88
///
99
class ChartMarker: MarkerImage {
1010
@objc open var color: UIColor
11-
@objc open var arrowSize = CGSize(width: 15, height: 11)
11+
@objc open var arrowSize = Constants.arrowSize
1212
@objc open var font: UIFont
1313
@objc open var textColor: UIColor
1414
@objc open var insets: UIEdgeInsets
@@ -19,7 +19,7 @@ class ChartMarker: MarkerImage {
1919
private var _paragraphStyle: NSMutableParagraphStyle?
2020
private var _drawAttributes = [NSAttributedStringKey: AnyObject]()
2121

22-
@objc public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) {
22+
@objc public init(chartView: ChartViewBase?, color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) {
2323
self.color = color
2424
self.font = font
2525
self.textColor = textColor
@@ -28,6 +28,7 @@ class ChartMarker: MarkerImage {
2828
_paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
2929
_paragraphStyle?.alignment = .center
3030
super.init()
31+
self.chartView = chartView
3132
}
3233

3334
open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint {
@@ -44,25 +45,25 @@ class ChartMarker: MarkerImage {
4445

4546
let width = size.width
4647
let height = size.height
47-
let padding: CGFloat = 8.0
48+
let padding = Constants.offsetPadding
4849

4950
var origin = point
5051
origin.x -= width / 2
5152
origin.y -= height
5253

53-
if origin.x + offset.x < 0.0 {
54+
if (origin.x + offset.x) < 0.0 {
5455
offset.x = -origin.x + padding
55-
} else if let chart = chartView, origin.x + width + offset.x > chart.bounds.size.width {
56+
} else if let chart = chartView, (origin.x + width + offset.x) > chart.bounds.size.width {
5657
offset.x = chart.bounds.size.width - origin.x - width - padding
5758
}
5859

59-
if origin.y + offset.y < 0 {
60+
if (origin.y + offset.y) < 0 {
6061
offset.y = height + padding
61-
} else if let chart = chartView, origin.y + height + offset.y > chart.bounds.size.height {
62+
} else if let chart = chartView, (origin.y + height + offset.y) > chart.bounds.size.height {
6263
offset.y = chart.bounds.size.height - origin.y - height - padding
6364
}
6465

65-
return offset
66+
return CGPoint(x: round(offset.x), y: round(offset.y))
6667
}
6768

6869
open override func draw(context: CGContext, point: CGPoint) {
@@ -80,6 +81,7 @@ class ChartMarker: MarkerImage {
8081
size: size)
8182
rect.origin.x -= size.width / 2.0
8283
rect.origin.y -= size.height
84+
rect = rect.integral
8385

8486
context.saveGState()
8587
context.setFillColor(color.cgColor)
@@ -92,7 +94,8 @@ class ChartMarker: MarkerImage {
9294
context.addLine(to: CGPoint(
9395
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
9496
y: rect.origin.y + arrowSize.height))
95-
//arrow vertex
97+
98+
// Arrow vertex
9699
context.addLine(to: CGPoint(
97100
x: point.x,
98101
y: point.y))
@@ -126,7 +129,8 @@ class ChartMarker: MarkerImage {
126129
context.addLine(to: CGPoint(
127130
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
128131
y: rect.origin.y + rect.size.height - arrowSize.height))
129-
//arrow vertex
132+
133+
//Arrow vertex
130134
context.addLine(to: CGPoint(
131135
x: point.x,
132136
y: point.y))
@@ -148,6 +152,7 @@ class ChartMarker: MarkerImage {
148152
rect.origin.y += self.insets.top
149153
}
150154
rect.size.height -= self.insets.top + self.insets.bottom
155+
rect = rect.integral
151156
UIGraphicsPushContext(context)
152157
label.draw(in: rect, withAttributes: _drawAttributes)
153158
UIGraphicsPopContext()
@@ -176,3 +181,13 @@ class ChartMarker: MarkerImage {
176181
self.size = size
177182
}
178183
}
184+
185+
186+
// MARK: - Constants!
187+
//
188+
private extension ChartMarker {
189+
enum Constants {
190+
static let arrowSize = CGSize(width: 20, height: 14)
191+
static let offsetPadding: CGFloat = 4.0
192+
}
193+
}

WooCommerce/Classes/ViewRelated/Dashboard/MyStore/PeriodDataViewController.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,11 @@ extension PeriodDataViewController: ChartViewDelegate {
183183
return
184184
}
185185

186-
let marker = ChartMarker(color: StyleManager.wooSecondary,
187-
font: StyleManager.chartLabelFont,
188-
textColor: StyleManager.wooWhite,
189-
insets: Constants.chartMarkerInsets)
186+
let marker = ChartMarker(chartView: chartView,
187+
color: StyleManager.wooSecondary,
188+
font: StyleManager.chartLabelFont,
189+
textColor: StyleManager.wooWhite,
190+
insets: Constants.chartMarkerInsets)
190191
marker.minimumSize = Constants.chartMarkerMinimumSize
191192
marker.arrowSize = Constants.chartMarkerArrowSize
192193
chartView.marker = marker

0 commit comments

Comments
 (0)