Skip to content

Commit

Permalink
Allow bubble sizes to not be normalized against the dataset max (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Apr 13, 2016
1 parent 375d7ba commit 3938d5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class BubbleChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBubble
public var xMin: Double { return _xMin }
public var xMax: Double { return _xMax }
public var maxSize: CGFloat { return _maxSize }
public var normalizeSizeEnabled: Bool = true
public var isNormalizeSizeEnabled: Bool { return normalizeSizeEnabled }

public override func calcMinMax(start start: Int, end: Int)
{
Expand Down
1 change: 1 addition & 0 deletions Charts/Classes/Data/Interfaces/IBubbleChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public protocol IBubbleChartDataSet: IBarLineScatterCandleBubbleChartDataSet
var xMin: Double { get }
var xMax: Double { get }
var maxSize: CGFloat { get }
var isNormalizeSizeEnabled: Bool { get }

// MARK: - Styling functions and accessors

Expand Down
18 changes: 14 additions & 4 deletions Charts/Classes/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ public class BubbleChartRenderer: ChartDataRendererBase
}
}

private func getShapeSize(entrySize entrySize: CGFloat, maxSize: CGFloat, reference: CGFloat) -> CGFloat
private func getShapeSize(
entrySize entrySize: CGFloat,
maxSize: CGFloat,
reference: CGFloat,
normalizeSize: Bool) -> CGFloat
{
let factor: CGFloat = (maxSize == 0.0) ? 1.0 : sqrt(entrySize / maxSize)
let factor: CGFloat = normalizeSize
? ((maxSize == 0.0) ? 1.0 : sqrt(entrySize / maxSize))
: entrySize
let shapeSize: CGFloat = reference * factor
return shapeSize
}
Expand Down Expand Up @@ -84,6 +90,8 @@ public class BubbleChartRenderer: ChartDataRendererBase

CGContextSaveGState(context)

let normalizeSize = dataSet.isNormalizeSizeEnabled

// calcualte the full width of 1 step on the x-axis
let maxBubbleWidth: CGFloat = abs(_sizeBuffer[1].x - _sizeBuffer[0].x)
let maxBubbleHeight: CGFloat = abs(viewPortHandler.contentBottom - viewPortHandler.contentTop)
Expand All @@ -97,7 +105,7 @@ public class BubbleChartRenderer: ChartDataRendererBase
_pointBuffer.y = CGFloat(entry.value) * phaseY
_pointBuffer = CGPointApplyAffineTransform(_pointBuffer, valueToPixelMatrix)

let shapeSize = getShapeSize(entrySize: entry.size, maxSize: dataSet.maxSize, reference: referenceSize)
let shapeSize = getShapeSize(entrySize: entry.size, maxSize: dataSet.maxSize, reference: referenceSize, normalizeSize: normalizeSize)
let shapeHalf = shapeSize / 2.0

if (!viewPortHandler.isInBoundsTop(_pointBuffer.y + shapeHalf)
Expand Down Expand Up @@ -261,6 +269,8 @@ public class BubbleChartRenderer: ChartDataRendererBase

trans.pointValuesToPixel(&_sizeBuffer)

let normalizeSize = dataSet.isNormalizeSizeEnabled

// calcualte the full width of 1 step on the x-axis
let maxBubbleWidth: CGFloat = abs(_sizeBuffer[1].x - _sizeBuffer[0].x)
let maxBubbleHeight: CGFloat = abs(viewPortHandler.contentBottom - viewPortHandler.contentTop)
Expand All @@ -270,7 +280,7 @@ public class BubbleChartRenderer: ChartDataRendererBase
_pointBuffer.y = CGFloat(entry.value) * phaseY
trans.pointValueToPixel(&_pointBuffer)

let shapeSize = getShapeSize(entrySize: entry.size, maxSize: dataSet.maxSize, reference: referenceSize)
let shapeSize = getShapeSize(entrySize: entry.size, maxSize: dataSet.maxSize, reference: referenceSize, normalizeSize: normalizeSize)
let shapeHalf = shapeSize / 2.0

if (!viewPortHandler.isInBoundsTop(_pointBuffer.y + shapeHalf)
Expand Down
2 changes: 2 additions & 0 deletions ChartsRealm/Classes/Data/RealmBubbleDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class RealmBubbleDataSet: RealmBarLineScatterCandleBubbleDataSet, IBubble
public var xMin: Double { return _xMin }
public var xMax: Double { return _xMax }
public var maxSize: CGFloat { return _maxSize }
public var normalizeSizeEnabled: Bool = true
public var isNormalizeSizeEnabled: Bool { return normalizeSizeEnabled }

internal override func buildEntryFromResultObject(object: RLMObject, atIndex: UInt) -> ChartDataEntry
{
Expand Down

0 comments on commit 3938d5b

Please sign in to comment.