Skip to content
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

View port handler nonnil #2980

Merged
merged 13 commits into from
Nov 27, 2017
Merged
3 changes: 1 addition & 2 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
_animator = Animator()
_animator.delegate = self

_viewPortHandler = ViewPortHandler()
_viewPortHandler.setChartDimens(width: bounds.size.width, height: bounds.size.height)
_viewPortHandler = ViewPortHandler(width: bounds.size.width, height: bounds.size.height)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in some cases, setChartDimens() is useful rather than re-init again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases it is, that is why the method still exists. In this case it is not useful, as the only thing that is happening is what already happens in the initializer I changed it to.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the initialize method, so it does need to be inited here.


chartDescription = Description()

Expand Down
36 changes: 14 additions & 22 deletions Source/Charts/Renderers/AxisRendererBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ open class AxisRendererBase: Renderer

/// transformer to transform values to screen pixels and return
@objc open var transformer: Transformer?

public override init()
{
super.init()
}

@objc public init(viewPortHandler: ViewPortHandler?, transformer: Transformer?, axis: AxisBase?)

@objc public init(viewPortHandler: ViewPortHandler, transformer: Transformer?, axis: AxisBase?)
{
super.init(viewPortHandler: viewPortHandler)

Expand Down Expand Up @@ -68,23 +63,20 @@ open class AxisRendererBase: Renderer
if let transformer = self.transformer
{
// calculate the starting and entry point of the y-labels (depending on zoom / contentrect bounds)
if let viewPortHandler = viewPortHandler
if viewPortHandler.contentWidth > 10.0 && !viewPortHandler.isFullyZoomedOutY
{
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
{
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)
}
min = Double(p1.y)
max = Double(p2.y)
}
}
}
Expand Down
16 changes: 5 additions & 11 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

@objc open weak var dataProvider: BarChartDataProvider?

@objc public init(dataProvider: BarChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler?)
@objc public init(dataProvider: BarChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)

Expand Down Expand Up @@ -207,10 +207,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

@objc open func drawDataSet(context: CGContext, dataSet: IBarChartDataSet, index: Int)
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler
else { return }
guard let dataProvider = dataProvider else { return }

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

Expand All @@ -226,9 +223,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
// draw the bar shadow before the values
if dataProvider.isDrawBarShadowEnabled
{
guard
let barData = dataProvider.barData
else { return }
guard let barData = dataProvider.barData else { return }

let barWidth = barData.barWidth
let barWidthHalf = barWidth / 2.0
Expand Down Expand Up @@ -355,17 +350,16 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler,
let barData = dataProvider.barData
else { return }

var dataSets = barData.dataSets

let valueOffsetPlus: CGFloat = 4.5
var posOffset: CGFloat
var negOffset: CGFloat
let drawValueAboveBar = dataProvider.isDrawValueAboveBarEnabled

for dataSetIndex in 0 ..< barData.dataSetCount
{
guard let dataSet = dataSets[dataSetIndex] as? IBarChartDataSet else { continue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class BarLineScatterCandleBubbleRenderer: DataRenderer
{
internal var _xBounds = XBounds() // Reusable XBounds object

public override init(animator: Animator, viewPortHandler: ViewPortHandler?)
public override init(animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)
}
Expand Down
15 changes: 5 additions & 10 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
{
@objc open weak var dataProvider: BubbleChartDataProvider?

@objc public init(dataProvider: BubbleChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler?)
@objc public init(dataProvider: BubbleChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)

Expand Down Expand Up @@ -59,10 +59,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer

@objc open func drawDataSet(context: CGContext, dataSet: IBubbleChartDataSet)
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler
else { return }
guard let dataProvider = dataProvider else { return }

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

Expand Down Expand Up @@ -124,9 +121,8 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer

open override func drawValues(context: CGContext)
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler,
guard let
dataProvider = dataProvider,
let bubbleData = dataProvider.bubbleData,
isDrawingValuesAllowed(dataProvider: dataProvider),
let dataSets = bubbleData.dataSets as? [IBubbleChartDataSet]
Expand Down Expand Up @@ -215,10 +211,9 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler,
let bubbleData = dataProvider.bubbleData
else { return }

context.saveGState()
defer { context.restoreGState() }

Expand Down
3 changes: 1 addition & 2 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
{
@objc open weak var dataProvider: CandleChartDataProvider?

@objc public init(dataProvider: CandleChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler?)
@objc public init(dataProvider: CandleChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)

Expand Down Expand Up @@ -236,7 +236,6 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler,
let candleData = dataProvider.candleData
else { return }

Expand Down
7 changes: 3 additions & 4 deletions Source/Charts/Renderers/ChartDataRendererBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class DataRenderer: Renderer
{
@objc open let animator: Animator

@objc public init(animator: Animator, viewPortHandler: ViewPortHandler?)
@objc public init(animator: Animator, viewPortHandler: ViewPortHandler)
{
self.animator = animator

Expand Down Expand Up @@ -53,8 +53,7 @@ open class DataRenderer: Renderer

@objc open func isDrawingValuesAllowed(dataProvider: ChartDataProvider?) -> Bool
{
guard let data = dataProvider?.data
else { return false }
return data.entryCount < Int(CGFloat(dataProvider?.maxVisibleCount ?? 0) * (self.viewPortHandler?.scaleX ?? 1.0))
guard let data = dataProvider?.data else { return false }
return data.entryCount < Int(CGFloat(dataProvider?.maxVisibleCount ?? 0) * viewPortHandler.scaleX)
}
}
7 changes: 2 additions & 5 deletions Source/Charts/Renderers/CombinedChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class CombinedChartRenderer: DataRenderer

internal var _drawOrder: [CombinedChartView.DrawOrder] = [.bar, .bubble, .line, .candle, .scatter]

@objc public init(chart: CombinedChartView?, animator: Animator, viewPortHandler: ViewPortHandler?)
@objc public init(chart: CombinedChartView?, animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)

Expand All @@ -40,10 +40,7 @@ open class CombinedChartRenderer: DataRenderer
{
_renderers = [DataRenderer]()

guard let
chart = chart,
let viewPortHandler = self.viewPortHandler
else { return }
guard let chart = chart else { return }

for order in drawOrder
{
Expand Down
12 changes: 4 additions & 8 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
var rects = [CGRect]()
}

public override init(dataProvider: BarChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler?)
public override init(dataProvider: BarChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(dataProvider: dataProvider, animator: animator, viewPortHandler: viewPortHandler)
}
Expand Down Expand Up @@ -181,10 +181,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer

open override func drawDataSet(context: CGContext, dataSet: IBarChartDataSet, index: Int)
{
guard let
dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler
else { return }
guard let dataProvider = dataProvider else { return }

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

Expand Down Expand Up @@ -305,8 +302,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
{
guard
let dataProvider = dataProvider,
let barData = dataProvider.barData,
let viewPortHandler = self.viewPortHandler
let barData = dataProvider.barData
else { return }

var dataSets = barData.dataSets
Expand Down Expand Up @@ -605,7 +601,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
{
guard let data = dataProvider?.data
else { return false }
return data.entryCount < Int(CGFloat(dataProvider?.maxVisibleCount ?? 0) * (self.viewPortHandler?.scaleY ?? 1.0))
return data.entryCount < Int(CGFloat(dataProvider?.maxVisibleCount ?? 0) * self.viewPortHandler.scaleY)
}

/// Sets the drawing position of the highlight object based on the riven bar-rect.
Expand Down
12 changes: 3 additions & 9 deletions Source/Charts/Renderers/LegendRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class LegendRenderer: Renderer
/// the legend object this renderer renders
@objc open var legend: Legend?

@objc public init(viewPortHandler: ViewPortHandler?, legend: Legend?)
@objc public init(viewPortHandler: ViewPortHandler, legend: Legend?)
{
super.init(viewPortHandler: viewPortHandler)

Expand All @@ -32,10 +32,7 @@ open class LegendRenderer: Renderer
/// Prepares the legend and calculates all needed forms, labels and colors.
@objc open func computeLegend(data: ChartData)
{
guard
let legend = legend,
let viewPortHandler = self.viewPortHandler
else { return }
guard let legend = legend else { return }

if !legend.isLegendCustom
{
Expand Down Expand Up @@ -194,10 +191,7 @@ open class LegendRenderer: Renderer

@objc open func renderLegend(context: CGContext)
{
guard
let legend = legend,
let viewPortHandler = self.viewPortHandler
else { return }
guard let legend = legend else { return }

if !legend.enabled
{
Expand Down
17 changes: 6 additions & 11 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class LineChartRenderer: LineRadarRenderer
{
@objc open weak var dataProvider: LineChartDataProvider?

@objc public init(dataProvider: LineChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler?)
@objc public init(dataProvider: LineChartDataProvider?, animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)

Expand Down Expand Up @@ -286,10 +286,7 @@ open class LineChartRenderer: LineRadarRenderer

@objc open func drawLinear(context: CGContext, dataSet: ILineChartDataSet)
{
guard
let dataProvider = dataProvider,
let viewPortHandler = self.viewPortHandler
else { return }
guard let dataProvider = dataProvider else { return }

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

Expand Down Expand Up @@ -502,10 +499,9 @@ open class LineChartRenderer: LineRadarRenderer
{
guard
let dataProvider = dataProvider,
let lineData = dataProvider.lineData,
let viewPortHandler = self.viewPortHandler
let lineData = dataProvider.lineData
else { return }

if isDrawingValuesAllowed(dataProvider: dataProvider)
{
var dataSets = lineData.dataSets
Expand Down Expand Up @@ -597,12 +593,11 @@ open class LineChartRenderer: LineRadarRenderer
{
guard
let dataProvider = dataProvider,
let lineData = dataProvider.lineData,
let viewPortHandler = self.viewPortHandler
let lineData = dataProvider.lineData
else { return }

let phaseY = animator.phaseY

let dataSets = lineData.dataSets

var pt = CGPoint()
Expand Down
4 changes: 1 addition & 3 deletions Source/Charts/Renderers/LineRadarRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ import CoreGraphics
@objc(LineRadarChartRenderer)
open class LineRadarRenderer: LineScatterCandleRadarRenderer
{
public override init(animator: Animator, viewPortHandler: ViewPortHandler?)
public override init(animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)
}

/// Draws the provided path in filled mode with the provided drawable.
@objc open func drawFilledPath(context: CGContext, path: CGPath, fill: Fill, fillAlpha: CGFloat)
{
guard let viewPortHandler = self.viewPortHandler
else { return }

context.saveGState()
context.beginPath()
Expand Down
5 changes: 1 addition & 4 deletions Source/Charts/Renderers/LineScatterCandleRadarRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import CoreGraphics
@objc(LineScatterCandleRadarChartRenderer)
open class LineScatterCandleRadarRenderer: BarLineScatterCandleBubbleRenderer
{
public override init(animator: Animator, viewPortHandler: ViewPortHandler?)
public override init(animator: Animator, viewPortHandler: ViewPortHandler)
{
super.init(animator: animator, viewPortHandler: viewPortHandler)
}
Expand All @@ -27,9 +27,6 @@ open class LineScatterCandleRadarRenderer: BarLineScatterCandleBubbleRenderer
/// :param: vertical
@objc open func drawHighlightLines(context: CGContext, point: CGPoint, set: ILineScatterCandleRadarChartDataSet)
{
guard let
viewPortHandler = self.viewPortHandler
else { return }

// draw vertical highlight lines
if set.isVerticalHighlightIndicatorEnabled
Expand Down
Loading