Skip to content

Commit

Permalink
Work on x-values instead of x-indices (free for all :-)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Jul 27, 2016
1 parent 4d35b2f commit 3dbd57b
Show file tree
Hide file tree
Showing 155 changed files with 4,628 additions and 5,349 deletions.
84 changes: 44 additions & 40 deletions Charts/Charts.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

138 changes: 69 additions & 69 deletions Charts/Classes/Animation/ChartAnimationEasing.swift

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Charts/Classes/Animation/ChartAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public class ChartAnimator: NSObject
public var stopBlock: (() -> Void)?

/// the phase that is animated and influences the drawn values on the x-axis
public var phaseX: CGFloat = 1.0
public var phaseX: Double = 1.0

/// the phase that is animated and influences the drawn values on the y-axis
public var phaseY: CGFloat = 1.0
public var phaseY: Double = 1.0

private var _startTimeX: NSTimeInterval = 0.0
private var _startTimeY: NSTimeInterval = 0.0
Expand Down Expand Up @@ -122,7 +122,7 @@ public class ChartAnimator: NSObject
}
else
{
phaseX = CGFloat(elapsed / duration)
phaseX = Double(elapsed / duration)
}
}
if (_enabledY)
Expand All @@ -141,7 +141,7 @@ public class ChartAnimator: NSObject
}
else
{
phaseY = CGFloat(elapsed / duration)
phaseY = Double(elapsed / duration)
}
}
}
Expand Down
108 changes: 50 additions & 58 deletions Charts/Classes/Charts/BarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import CoreGraphics
/// Chart that draws bars.
public class BarChartView: BarLineChartViewBase, BarChartDataProvider
{
/// flag that enables or disables the highlighting arrow
private var _drawHighlightArrowEnabled = false

/// if set to true, all values are drawn above their bars, instead of below their top
private var _drawValueAboveBarEnabled = true

Expand All @@ -31,30 +28,38 @@ public class BarChartView: BarLineChartViewBase, BarChartDataProvider
super.initialize()

renderer = BarChartRenderer(dataProvider: self, animator: _animator, viewPortHandler: _viewPortHandler)
_xAxisRenderer = ChartXAxisRendererBarChart(viewPortHandler: _viewPortHandler, xAxis: _xAxis, transformer: _leftAxisTransformer, chart: self)

self.highlighter = BarChartHighlighter(chart: self)

_xAxis._axisMinimum = -0.5
}

internal override func calcMinMax()
{
super.calcMinMax()

guard let data = _data else { return }

let barData = data as! BarChartData
guard let data = self.data as? BarChartData
else { return }

// increase deltax by 1 because the bars have a width of 1
_xAxis.axisRange += 0.5
if self.autoScaleMinMaxEnabled
{
data.calcMinMax()
}

// extend xDelta to make space for multiple datasets (if ther are one)
_xAxis.axisRange *= Double(data.dataSetCount)
if fitBars
{
_xAxis.calculate(
min: data.xMin - data.barWidth / 2.0,
max: data.xMax - data.barWidth / 2.0)
}
else
{
_xAxis.calculate(min: data.xMin, max: data.xMax)
}

let groupSpace = barData.groupSpace
_xAxis.axisRange += Double(barData.xValCount) * Double(groupSpace)
_xAxis._axisMaximum = _xAxis.axisRange - _xAxis._axisMinimum
// calculate axis range (min / max) according to provided data
_leftAxis.calculate(
min: data.getYMin(.Left),
max: data.getYMax(.Left))
_rightAxis.calculate(
min: data.getYMin(.Right),
max: data.getYMax(.Right))
}

/// - returns: the Highlight object (contains x-index and DataSet index) of the selected value at the given touch point inside the BarChart.
Expand All @@ -73,18 +78,17 @@ public class BarChartView: BarLineChartViewBase, BarChartDataProvider
public func getBarBounds(e: BarChartDataEntry) -> CGRect
{
guard let
set = _data?.getDataSetForEntry(e) as? IBarChartDataSet
data = _data as? BarChartData,
set = data.getDataSetForEntry(e) as? IBarChartDataSet
else { return CGRectNull }

let barspace = set.barSpace
let y = CGFloat(e.value)
let x = CGFloat(e.xIndex)
let y = e.y
let x = e.x

let barWidth: CGFloat = 0.5
let barWidth = data.barWidth

let spaceHalf = barspace / 2.0
let left = x - barWidth + spaceHalf
let right = x + barWidth - spaceHalf
let left = x - barWidth / 2.0
let right = x + barWidth / 2.0
let top = y >= 0.0 ? y : 0.0
let bottom = y <= 0.0 ? y : 0.0

Expand All @@ -95,41 +99,28 @@ public class BarChartView: BarLineChartViewBase, BarChartDataProvider
return bounds
}

public override var lowestVisibleXIndex: Int
/// Groups all BarDataSet objects this data object holds together by modifying the x-position of their entries.
/// Leaves space as specified by the parameters.
/// Calls `notifyDataSetChanged()` afterwards.
///
/// - parameter fromX: the starting point on the x-axis where the grouping should begin
/// - parameter groupSpace: the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
/// - parameter barSpace: the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
public func groupBars(fromX fromX: Double, groupSpace: Double, barSpace: Double)
{
let step = CGFloat(_data?.dataSetCount ?? 0)
let div = (step <= 1.0) ? 1.0 : step + (_data as! BarChartData).groupSpace

var pt = CGPoint(x: _viewPortHandler.contentLeft, y: _viewPortHandler.contentBottom)
getTransformer(ChartYAxis.AxisDependency.Left).pixelToValue(&pt)

return Int((pt.x <= CGFloat(chartXMin)) ? 0.0 : (pt.x / div) + 1.0)
}

public override var highestVisibleXIndex: Int
{
let step = CGFloat(_data?.dataSetCount ?? 0)
let div = (step <= 1.0) ? 1.0 : step + (_data as! BarChartData).groupSpace

var pt = CGPoint(x: _viewPortHandler.contentRight, y: _viewPortHandler.contentBottom)
getTransformer(ChartYAxis.AxisDependency.Left).pixelToValue(&pt)
guard let barData = self.barData
else
{
print("You need to set data for the chart before grouping bars.", terminator: "\n")
return
}

return Int((pt.x >= CGFloat(chartXMax)) ? CGFloat(chartXMax) / div : (pt.x / div))
barData.groupBars(fromX: fromX, groupSpace: groupSpace, barSpace: barSpace)
notifyDataSetChanged()
}

// MARK: Accessors

/// flag that enables or disables the highlighting arrow
public var drawHighlightArrowEnabled: Bool
{
get { return _drawHighlightArrowEnabled; }
set
{
_drawHighlightArrowEnabled = newValue
setNeedsDisplay()
}
}

/// if set to true, all values are drawn above their bars, instead of below their top
public var drawValueAboveBarEnabled: Bool
{
Expand All @@ -152,13 +143,14 @@ public class BarChartView: BarLineChartViewBase, BarChartDataProvider
}
}

/// Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be fully displayed.
/// **default**: false
public var fitBars = false

// MARK: - BarChartDataProbider

public var barData: BarChartData? { return _data as? BarChartData }

/// - returns: true if drawing the highlighting arrow is enabled, false if not
public var isDrawHighlightArrowEnabled: Bool { return drawHighlightArrowEnabled }

/// - returns: true if drawing values above bars is enabled, false if not
public var isDrawValueAboveBarEnabled: Bool { return drawValueAboveBarEnabled }

Expand Down
Loading

0 comments on commit 3dbd57b

Please sign in to comment.