From dea1cc54d11a1eccea9ec98c98c41a00f42b9793 Mon Sep 17 00:00:00 2001 From: Jacob Christie <19879272+jjatie@users.noreply.github.com> Date: Tue, 12 Jan 2021 10:17:47 -0400 Subject: [PATCH] SPM GitHub Action (#4553) * Create swift.yml * Update project so SPM builds for all platforms * Fix AutoreleaseingUnsafePointer warning * Remove tests from github SPM action --- .github/workflows/swift.yml | 17 ++ .../Charts/Charts/BarLineChartViewBase.swift | 12 +- Source/Charts/Charts/ChartViewBase.swift | 4 +- Source/Charts/Charts/PieChartView.swift | 2 +- Source/Charts/Components/Description.swift | 2 +- Source/Charts/Components/MarkerView.swift | 7 +- Source/Charts/Components/YAxis.swift | 2 +- .../Charts/Renderers/AxisRendererBase.swift | 218 ------------------ .../Charts/Renderers/BarChartRenderer.swift | 2 +- .../HorizontalBarChartRenderer.swift | 2 +- .../Charts/Renderers/PieChartRenderer.swift | 2 +- Source/Charts/Renderers/XAxisRenderer.swift | 4 +- .../XAxisRendererHorizontalBarChart.swift | 2 +- Source/Charts/Renderers/YAxisRenderer.swift | 6 +- .../YAxisRendererHorizontalBarChart.swift | 2 +- Source/Charts/Utils/ChartUtils.swift | 4 +- Source/Charts/Utils/Platform.swift | 9 +- 17 files changed, 52 insertions(+), 245 deletions(-) create mode 100644 .github/workflows/swift.yml delete mode 100644 Source/Charts/Renderers/AxisRendererBase.swift diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml new file mode 100644 index 0000000000..a7c70de4b5 --- /dev/null +++ b/.github/workflows/swift.yml @@ -0,0 +1,17 @@ +name: Swift + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: macos-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + run: swift build -v diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index 0724cabed3..631ccd07d9 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -9,11 +9,13 @@ // https://github.com/danielgindi/Charts // -import Foundation import CoreGraphics +import Foundation -#if !os(OSX) - import UIKit +#if canImport(UIKit) +import UIKit +#elseif canImport(AppKit) +import AppKit #endif /// Base-class of LineChart, BarChart, ScatterChart and CandleStickChart. @@ -905,7 +907,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD } #if !os(OSX) - open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool + open override func gestureRecognizerShouldBegin(_ gestureRecognizer: NSUIGestureRecognizer) -> Bool { if !super.gestureRecognizerShouldBegin(gestureRecognizer) { @@ -917,7 +919,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD #endif #if os(OSX) - public func gestureRecognizerShouldBegin(gestureRecognizer: NSGestureRecognizer) -> Bool + public func gestureRecognizerShouldBegin(gestureRecognizer: NSUIGestureRecognizer) -> Bool { return nsuiGestureRecognizerShouldBegin(gestureRecognizer) } diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index b8e6fc1f4a..0ad5586534 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -104,7 +104,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate @objc open var noDataTextColor: NSUIColor = .labelOrBlack /// alignment of the no data text - @objc open var noDataTextAlignment: NSTextAlignment = .left + @objc open var noDataTextAlignment: TextAlignment = .left /// The renderer object responsible for rendering / drawing the Legend. @objc open lazy var legendRenderer = LegendRenderer(viewPortHandler: viewPortHandler, legend: legend) @@ -276,7 +276,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate context.saveGState() defer { context.restoreGState() } - let paragraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle + let paragraphStyle = MutableParagraphStyle.default.mutableCopy() as! MutableParagraphStyle paragraphStyle.minimumLineHeight = noDataFont.lineHeight paragraphStyle.lineBreakMode = .byWordWrapping paragraphStyle.alignment = noDataTextAlignment diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index edf00bb5a8..704c066bdf 100644 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -417,7 +417,7 @@ open class PieChartView: PieRadarChartViewBase } else { - let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle + let paragraphStyle = ParagraphStyle.default.mutableCopy() as! MutableParagraphStyle paragraphStyle.lineBreakMode = .byTruncatingTail paragraphStyle.alignment = .center diff --git a/Source/Charts/Components/Description.swift b/Source/Charts/Components/Description.swift index 0288931c04..ba455dc051 100644 --- a/Source/Charts/Components/Description.swift +++ b/Source/Charts/Components/Description.swift @@ -44,7 +44,7 @@ open class Description: ComponentBase open var position: CGPoint? = nil /// The text alignment of the description text. Default RIGHT. - @objc open var textAlign: NSTextAlignment = NSTextAlignment.right + @objc open var textAlign: TextAlignment = TextAlignment.right /// Font object used for drawing the description text. @objc open var font: NSUIFont diff --git a/Source/Charts/Components/MarkerView.swift b/Source/Charts/Components/MarkerView.swift index 5271fb642e..d80ad535b3 100644 --- a/Source/Charts/Components/MarkerView.swift +++ b/Source/Charts/Components/MarkerView.swift @@ -82,15 +82,14 @@ open class MarkerView: NSUIView, Marker options: nil)?[0] as? MarkerView #else - var loadedObjects = NSArray() - let loadedObjectsPointer = AutoreleasingUnsafeMutablePointer(&loadedObjects) + var loadedObjects: NSArray? = NSArray() if bundle.loadNibNamed( NSNib.Name(String(describing: self)), owner: nil, - topLevelObjects: loadedObjectsPointer) + topLevelObjects: &loadedObjects) { - return loadedObjects[0] as? MarkerView + return loadedObjects?[0] as? MarkerView } return nil diff --git a/Source/Charts/Components/YAxis.swift b/Source/Charts/Components/YAxis.swift index b2b1b75ac0..bf0beef6d1 100644 --- a/Source/Charts/Components/YAxis.swift +++ b/Source/Charts/Components/YAxis.swift @@ -78,7 +78,7 @@ open class YAxis: AxisBase @objc open var labelPosition = LabelPosition.outsideChart /// the alignment of the text in the y-label - @objc open var labelAlignment: NSTextAlignment = .left + @objc open var labelAlignment: TextAlignment = .left /// the horizontal offset of the y-label @objc open var labelXOffset: CGFloat = 0.0 diff --git a/Source/Charts/Renderers/AxisRendererBase.swift b/Source/Charts/Renderers/AxisRendererBase.swift deleted file mode 100644 index 302bdf5312..0000000000 --- a/Source/Charts/Renderers/AxisRendererBase.swift +++ /dev/null @@ -1,218 +0,0 @@ -// -// AxisRendererBase.swift -// Charts -// -// 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 CoreGraphics - -@objc(ChartAxisRendererBase) -open class AxisRendererBase: Renderer -{ - /// base axis this axis renderer works with - @objc open var axis: AxisBase? - - /// transformer to transform values to screen pixels and return - @objc open var transformer: Transformer? - - @objc public init(viewPortHandler: ViewPortHandler, transformer: Transformer?, axis: AxisBase?) - { - super.init(viewPortHandler: viewPortHandler) - - self.transformer = transformer - self.axis = axis - } - - /// Draws the axis labels on the specified context - @objc open func renderAxisLabels(context: CGContext) - { - fatalError("renderAxisLabels() cannot be called on AxisRendererBase") - } - - /// Draws the grid lines belonging to the axis. - @objc open func renderGridLines(context: CGContext) - { - fatalError("renderGridLines() cannot be called on AxisRendererBase") - } - - /// Draws the line that goes alongside the axis. - @objc open func renderAxisLine(context: CGContext) - { - fatalError("renderAxisLine() cannot be called on AxisRendererBase") - } - - /// Draws the LimitLines associated with this axis to the screen. - @objc open func renderLimitLines(context: CGContext) - { - fatalError("renderLimitLines() cannot be called on AxisRendererBase") - } - - /// Computes the axis values. - /// - /// - Parameters: - /// - min: the minimum value in the data object for this axis - /// - max: the maximum value in the data object for this axis - @objc open func computeAxis(min: Double, max: Double, inverted: Bool) - { - var min = min, max = max - - if let transformer = self.transformer - { - // calculate the starting and entry point of the y-labels (depending on zoom / contentrect bounds) - if viewPortHandler.contentWidth > 10.0 && !viewPortHandler.isFullyZoomedOutY - { - let p1 = transformer.valueForTouchPoint(CGPoint(x: viewPortHandler.contentLeft, y: viewPortHandler.contentTop)) - let p2 = transformer.valueForTouchPoint(CGPoint(x: viewPortHandler.contentLeft, y: viewPortHandler.contentBottom)) - - if !inverted - { - min = Double(p2.y) - max = Double(p1.y) - } - else - { - min = Double(p1.y) - max = Double(p2.y) - } - } - } - - computeAxisValues(min: min, max: max) - } - - /// Sets up the axis values. Computes the desired number of labels between the two given extremes. - @objc open func computeAxisValues(min: Double, max: Double) - { - guard let axis = self.axis else { return } - - let yMin = min - let yMax = max - - let labelCount = axis.labelCount - let range = abs(yMax - yMin) - - if labelCount == 0 || range <= 0 || range.isInfinite - { - axis.entries = [Double]() - axis.centeredEntries = [Double]() - return - } - - // Find out how much spacing (in y value space) between axis values - let rawInterval = range / Double(labelCount) - var interval = rawInterval.roundedToNextSignficant() - - // If granularity is enabled, then do not allow the interval to go below specified granularity. - // This is used to avoid repeated values when rounding values for display. - if axis.granularityEnabled - { - interval = interval < axis.granularity ? axis.granularity : interval - } - - // Normalize interval - let intervalMagnitude = pow(10.0, Double(Int(log10(interval)))).roundedToNextSignficant() - let intervalSigDigit = Int(interval / intervalMagnitude) - if intervalSigDigit > 5 - { - // Use one order of magnitude higher, to avoid intervals like 0.9 or 90 - // if it's 0.0 after floor(), we use the old value - interval = floor(10.0 * intervalMagnitude) == 0.0 ? interval : floor(10.0 * intervalMagnitude) - } - - var n = axis.centerAxisLabelsEnabled ? 1 : 0 - - // force label count - if axis.isForceLabelsEnabled - { - interval = Double(range) / Double(labelCount - 1) - - // Ensure stops contains at least n elements. - axis.entries.removeAll(keepingCapacity: true) - axis.entries.reserveCapacity(labelCount) - - var v = yMin - - for _ in 0 ..< labelCount - { - axis.entries.append(v) - v += interval - } - - n = labelCount - } - else - { - // no forced count - - var first = interval == 0.0 ? 0.0 : ceil(yMin / interval) * interval - - if axis.centerAxisLabelsEnabled - { - first -= interval - } - - let last = interval == 0.0 ? 0.0 : (floor(yMax / interval) * interval).nextUp - - if interval != 0.0 && last != first - { - for _ in stride(from: first, through: last, by: interval) - { - n += 1 - } - } - else if last == first && n == 0 - { - n = 1 - } - - // Ensure stops contains at least n elements. - axis.entries.removeAll(keepingCapacity: true) - axis.entries.reserveCapacity(labelCount) - - var f = first - var i = 0 - while i < n - { - if f == 0.0 - { - // Fix for IEEE negative zero case (Where value == -0.0, and 0.0 == -0.0) - f = 0.0 - } - - axis.entries.append(Double(f)) - - f += interval - i += 1 - } - } - - // set decimals - if interval < 1 - { - axis.decimals = Int(ceil(-log10(interval))) - } - else - { - axis.decimals = 0 - } - - if axis.centerAxisLabelsEnabled - { - axis.centeredEntries.reserveCapacity(n) - axis.centeredEntries.removeAll() - - let offset: Double = interval / 2.0 - - for i in 0 ..< n - { - axis.centeredEntries.append(axis.entries[i] + offset) - } - } - } -} diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index e04473d234..2aa69a828f 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -668,7 +668,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer } /// Draws a value at the specified x and y position. - @objc open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor, anchor: CGPoint, angleRadians: CGFloat) + @objc open func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: TextAlignment, color: NSUIColor, anchor: CGPoint, angleRadians: CGFloat) { if (angleRadians == 0.0) { diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index f61cf42fc7..d2fed814f3 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -324,7 +324,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer let barData = dataProvider.barData else { return } - let textAlign = NSTextAlignment.left + let textAlign = TextAlignment.left let valueOffsetPlus: CGFloat = 5.0 var posOffset: CGFloat diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index 33e23e44ea..18f571bc79 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -408,7 +408,7 @@ open class PieChartRenderer: NSObject, DataRenderer var pt2: CGPoint var labelPoint: CGPoint - var align: NSTextAlignment + var align: TextAlignment var line1Radius: CGFloat diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index db820a1e50..a20be3818d 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -249,7 +249,7 @@ open class XAxisRenderer: NSObject, AxisRenderer { guard let transformer = self.transformer else { return } - let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle + let paraStyle = ParagraphStyle.default.mutableCopy() as! MutableParagraphStyle paraStyle.alignment = .center let labelAttrs: [NSAttributedString.Key : Any] = [.font: axis.labelFont, @@ -453,7 +453,7 @@ open class XAxisRenderer: NSObject, AxisRenderer let xOffset: CGFloat = limitLine.lineWidth + limitLine.xOffset - let align: NSTextAlignment + let align: TextAlignment let point: CGPoint switch limitLine.labelPosition diff --git a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift index 18f5f9595c..a7429dbb69 100644 --- a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift @@ -254,7 +254,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer let xOffset = 4.0 + l.xOffset let yOffset = l.lineWidth + labelLineHeight + l.yOffset - let align: NSTextAlignment + let align: TextAlignment let point: CGPoint switch l.labelPosition diff --git a/Source/Charts/Renderers/YAxisRenderer.swift b/Source/Charts/Renderers/YAxisRenderer.swift index 74248f7fb3..1f5014c1d7 100644 --- a/Source/Charts/Renderers/YAxisRenderer.swift +++ b/Source/Charts/Renderers/YAxisRenderer.swift @@ -44,7 +44,7 @@ open class YAxisRenderer: NSObject, AxisRenderer let labelPosition = axis.labelPosition let xPos: CGFloat - let textAlign: NSTextAlignment + let textAlign: TextAlignment if dependency == .left { @@ -123,7 +123,7 @@ open class YAxisRenderer: NSObject, AxisRenderer fixedPosition: CGFloat, positions: [CGPoint], offset: CGFloat, - textAlign: NSTextAlignment) + textAlign: TextAlignment) { let labelFont = axis.labelFont let labelTextColor = axis.labelTextColor @@ -300,7 +300,7 @@ open class YAxisRenderer: NSObject, AxisRenderer let xOffset = 4.0 + l.xOffset let yOffset = l.lineWidth + labelLineHeight + l.yOffset - let align: NSTextAlignment + let align: TextAlignment let point: CGPoint switch l.labelPosition diff --git a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift index cd3a91f496..f4aa877d0a 100644 --- a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift @@ -258,7 +258,7 @@ open class YAxisRendererHorizontalBarChart: YAxisRenderer let xOffset = l.lineWidth + l.xOffset let yOffset = 2.0 + l.yOffset - let align: NSTextAlignment + let align: TextAlignment let point: CGPoint switch l.labelPosition diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index 2c3767a9e9..10fa091c85 100644 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -157,7 +157,7 @@ extension CGContext NSUIGraphicsPopContext() } - open func drawText(_ text: String, at point: CGPoint, align: NSTextAlignment, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat = 0.0, attributes: [NSAttributedString.Key : Any]?) + open func drawText(_ text: String, at point: CGPoint, align: TextAlignment, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat = 0.0, attributes: [NSAttributedString.Key : Any]?) { let drawPoint = getDrawPoint(text: text, point: point, align: align, attributes: attributes) @@ -227,7 +227,7 @@ extension CGContext NSUIGraphicsPopContext() } - private func getDrawPoint(text: String, point: CGPoint, align: NSTextAlignment, attributes: [NSAttributedString.Key : Any]?) -> CGPoint + private func getDrawPoint(text: String, point: CGPoint, align: TextAlignment, attributes: [NSAttributedString.Key : Any]?) -> CGPoint { var point = point diff --git a/Source/Charts/Utils/Platform.swift b/Source/Charts/Utils/Platform.swift index 270b803f47..927191cf44 100644 --- a/Source/Charts/Utils/Platform.swift +++ b/Source/Charts/Utils/Platform.swift @@ -5,7 +5,11 @@ alike, and for the chart library's usage of the APIs it is often sufficient to t types are aliased to either their UI* implementation (on iOS) or their NS* implementation (on OS X). */ #if os(iOS) || os(tvOS) import UIKit - + + + public typealias ParagraphStyle = NSParagraphStyle + public typealias MutableParagraphStyle = NSMutableParagraphStyle + public typealias TextAlignment = NSTextAlignment public typealias NSUIFont = UIFont public typealias NSUIImage = UIImage public typealias NSUIScrollView = UIScrollView @@ -64,6 +68,9 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple import Cocoa import Quartz + public typealias ParagraphStyle = NSParagraphStyle + public typealias MutableParagraphStyle = NSMutableParagraphStyle + public typealias TextAlignment = NSTextAlignment public typealias NSUIFont = NSFont public typealias NSUIImage = NSImage public typealias NSUIScrollView = NSScrollView