-
Notifications
You must be signed in to change notification settings - Fork 120
Dashboard Mark 6: Adds charts to dashboard #247
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c332119
Added Charts pod
bummytime d43e14d
Added bar chart view to PeriodDataVC
bummytime 2b19638
Dashboard: Revenue value now uses totalSales instead of totalGrossSales
bummytime 21a6c5e
Charting WIP
bummytime cf297aa
Removed zero bars in chart
bummytime 17eca4e
Dashboard: chart tweaks
bummytime 24accac
WIP chart labels
bummytime 2076ee3
Cleaned up YAxis on charts
bummytime a0fbc0f
WIP date formatting for charting
bummytime a31062d
Added side padding to chart
bummytime ae57df7
Added style to chart
bummytime eb77873
Chart: code cleanup
bummytime 1a8a909
Cleaned up dates
bummytime 76e0563
Dashboard: defaulting 'year' history to 10 previous years
bummytime 2ddf05f
Merge branch 'develop' of github.com:woocommerce/woocommerce-ios into…
bummytime 70bedfc
Added chart markers
bummytime 6dc76cd
Now clears chart markers on viewDidDisappear
bummytime 896cbfd
Code review updates
bummytime 02ab02f
Updated Chartmarker drawing logic
bummytime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
WooCommerce/Classes/ViewRelated/Dashboard/MyStore/ChartMarker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import Foundation | ||
| import Charts | ||
|
|
||
|
|
||
| /// This class is a custom view which is displayed over a chart element (e.g. a Bar) when it is highlighted. | ||
| /// | ||
| /// See: https://github.com/danielgindi/Charts/blob/master/ChartsDemo-iOS/Swift/Components/BalloonMarker.swift | ||
| /// | ||
| class ChartMarker: MarkerImage { | ||
| @objc open var color: UIColor | ||
| @objc open var arrowSize = Constants.arrowSize | ||
| @objc open var font: UIFont | ||
| @objc open var textColor: UIColor | ||
| @objc open var insets: UIEdgeInsets | ||
| @objc open var minimumSize = CGSize() | ||
|
|
||
| private var label: String? | ||
| private var _labelSize: CGSize = CGSize() | ||
| private var _paragraphStyle: NSMutableParagraphStyle? | ||
| private var _drawAttributes = [NSAttributedStringKey: AnyObject]() | ||
|
|
||
| @objc public init(chartView: ChartViewBase?, color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) { | ||
| self.color = color | ||
| self.font = font | ||
| self.textColor = textColor | ||
| self.insets = insets | ||
|
|
||
| _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle | ||
| _paragraphStyle?.alignment = .center | ||
| super.init() | ||
| self.chartView = chartView | ||
| } | ||
|
|
||
| open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint { | ||
| var offset = self.offset | ||
| var size = self.size | ||
|
|
||
| if let image = image, size.width == 0.0 { | ||
| size.width = image.size.width | ||
| } | ||
|
|
||
| if let image = image, size.height == 0.0 { | ||
| size.height = image.size.height | ||
| } | ||
|
|
||
| let width = size.width | ||
| let height = size.height | ||
| let padding = Constants.offsetPadding | ||
|
|
||
| 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 CGPoint(x: round(offset.x), y: round(offset.y)) | ||
| } | ||
|
|
||
| open override func draw(context: CGContext, point: CGPoint) { | ||
| guard let label = label else { | ||
| return | ||
| } | ||
|
|
||
| let offset = self.offsetForDrawing(atPoint: 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 | ||
| rect = rect.integral | ||
|
|
||
| context.saveGState() | ||
| 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 { | ||
| 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 | ||
| rect = rect.integral | ||
| UIGraphicsPushContext(context) | ||
| label.draw(in: rect, withAttributes: _drawAttributes) | ||
| UIGraphicsPopContext() | ||
| context.restoreGState() | ||
| } | ||
|
|
||
| open override func refreshContent(entry: ChartDataEntry, highlight: Highlight) { | ||
| let hintString = entry.accessibilityValue ?? String(entry.y) | ||
| setLabel(hintString) | ||
| } | ||
|
|
||
| @objc open func setLabel(_ newLabel: String) { | ||
| label = newLabel | ||
|
|
||
| _drawAttributes.removeAll() | ||
| _drawAttributes[.font] = self.font | ||
| _drawAttributes[.paragraphStyle] = _paragraphStyle | ||
| _drawAttributes[.foregroundColor] = self.textColor | ||
| _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MARK: - Constants! | ||
| // | ||
| private extension ChartMarker { | ||
| enum Constants { | ||
| static let arrowSize = CGSize(width: 20, height: 14) | ||
| static let offsetPadding: CGFloat = 4.0 | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.