Skip to content

Commit

Permalink
add support for grouped bars in CombinedChartView.
Browse files Browse the repository at this point in the history
add a new demo view controller called GroupedCombinedChartViewController, to illustrate multiple bars + multiple line
  • Loading branch information
liuxuan30 committed Aug 4, 2015
1 parent 2177f7d commit 80aa1b3
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Charts/Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
13D38A561B709EB0009D4C4D /* CombinedChartHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13D38A551B709EB0009D4C4D /* CombinedChartHighlighter.swift */; };
55E356531ADC63BF00A57971 /* BubbleChartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E356521ADC63BF00A57971 /* BubbleChartView.swift */; };
55E356571ADC63CD00A57971 /* BubbleChartData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E356541ADC63CD00A57971 /* BubbleChartData.swift */; };
55E356581ADC63CD00A57971 /* BubbleChartDataEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E356551ADC63CD00A57971 /* BubbleChartDataEntry.swift */; };
Expand Down Expand Up @@ -93,6 +94,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
13D38A551B709EB0009D4C4D /* CombinedChartHighlighter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CombinedChartHighlighter.swift; sourceTree = "<group>"; };
55E356521ADC63BF00A57971 /* BubbleChartView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BubbleChartView.swift; sourceTree = "<group>"; };
55E356541ADC63CD00A57971 /* BubbleChartData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BubbleChartData.swift; sourceTree = "<group>"; };
55E356551ADC63CD00A57971 /* BubbleChartDataEntry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BubbleChartDataEntry.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -198,6 +200,7 @@
5B0032441B6524AD00B6A2FE /* ChartHighlight.swift */,
5B0032461B6524D300B6A2FE /* ChartRange.swift */,
5B0032481B6525FC00B6A2FE /* ChartHighlighter.swift */,
13D38A551B709EB0009D4C4D /* CombinedChartHighlighter.swift */,
5B00324A1B652BF900B6A2FE /* BarChartHighlighter.swift */,
5B00324C1B65351C00B6A2FE /* HorizontalBarChartHighlighter.swift */,
);
Expand Down Expand Up @@ -482,6 +485,7 @@
5B680D231A9D17C30026A057 /* ChartYAxis.swift in Sources */,
5B6A54A91AA66BBA000F57C2 /* RadarChartView.swift in Sources */,
5B378F171AD500A4009414A4 /* ChartAnimationEasing.swift in Sources */,
13D38A561B709EB0009D4C4D /* CombinedChartHighlighter.swift in Sources */,
5B6A548F1AA66A7A000F57C2 /* HorizontalBarChartRenderer.swift in Sources */,
5B6A54741AA5DEDC000F57C2 /* ChartXAxisRenderer.swift in Sources */,
5B6A547C1AA5DF02000F57C2 /* ChartXAxisRendererHorizontalBarChart.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import Foundation
import CoreGraphics
import UIKit.UIGestureRecognizer
import UIKit

/// Base-class of LineChart, BarChart, ScatterChart and CandleStickChart.
public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
Expand Down
69 changes: 69 additions & 0 deletions Charts/Classes/Charts/CombinedChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public class CombinedChartView: BarLineChartViewBase
/// the fill-formatter used for determining the position of the fill-line
internal var _fillFormatter: ChartFillFormatter!

internal var _barChartXMax = Double(0.0)
internal var _barChartXMin = Double(0.0)
internal var _barChartDeltaX = CGFloat(0.0)

internal var _barChartLeftYAxisRenderer: ChartYAxisRenderer!
internal var _barChartRightYAxisRenderer: ChartYAxisRenderer!

internal var _barChartLeftAxisTransformer: ChartTransformer!
internal var _barChartRightAxisTransformer: ChartTransformer!

/// enum that allows to specify the order in which the different data objects for the combined-chart are drawn
@objc
public enum CombinedChartDrawOrder: Int
Expand All @@ -37,7 +47,15 @@ public class CombinedChartView: BarLineChartViewBase

_fillFormatter = BarLineChartFillFormatter(chart: self)

_barChartLeftAxisTransformer = ChartTransformer(viewPortHandler: _viewPortHandler)
_barChartRightAxisTransformer = ChartTransformer(viewPortHandler: _viewPortHandler)

_barChartLeftYAxisRenderer = ChartYAxisRenderer(viewPortHandler: _viewPortHandler, yAxis: _leftAxis, transformer: _leftAxisTransformer)
_barChartRightYAxisRenderer = ChartYAxisRenderer(viewPortHandler: _viewPortHandler, yAxis: _rightAxis, transformer: _rightAxisTransformer)

renderer = CombinedChartRenderer(chart: self, animator: _animator, viewPortHandler: _viewPortHandler)

_highlighter = CombinedChartHighlighter(chart: self)
}

override func calcMinMax()
Expand Down Expand Up @@ -72,11 +90,62 @@ public class CombinedChartView: BarLineChartViewBase
_chartXMin = 0.0
_chartXMax = Double(_data.xValCount - 1)
}

if (self.barData !== nil)
{
var barData = self.barData

_barChartXMin = _chartXMin
_barChartXMax = _chartXMax
_barChartDeltaX = _deltaX
// increase deltax by 1 because the bars have a width of 1
_barChartDeltaX += 0.5

// extend xDelta to make space for multiple datasets (if ther are one)
_barChartDeltaX *= CGFloat(barData.dataSetCount)

var maxEntry = _data.xValCount

var groupSpace = barData.groupSpace
_barChartDeltaX += CGFloat(maxEntry) * groupSpace
_barChartXMax = Double(_barChartDeltaX) - _barChartXMin
}

_deltaX = CGFloat(abs(_chartXMax - _chartXMin))
}
}

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

_barChartRightAxisTransformer.prepareMatrixValuePx(chartXMin: _barChartXMin, deltaX: _barChartDeltaX, deltaY: CGFloat(_rightAxis.axisRange), chartYMin: _rightAxis.axisMinimum)
_barChartLeftAxisTransformer.prepareMatrixValuePx(chartXMin: _barChartXMin, deltaX: _barChartDeltaX, deltaY: CGFloat(_leftAxis.axisRange), chartYMin: _leftAxis.axisMinimum)
}

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

_barChartRightAxisTransformer.prepareMatrixOffset(_rightAxis.isInverted)
_barChartLeftAxisTransformer.prepareMatrixOffset(_leftAxis.isInverted)
}

/// Returns the Transformer class that contains all matrices and is
/// responsible for transforming values into pixels on the screen and
/// backwards.
public func getBarChartTransformer(which: ChartYAxis.AxisDependency) -> ChartTransformer
{
if (which == .Left)
{
return _barChartLeftAxisTransformer
}
else
{
return _barChartRightAxisTransformer
}
}

public override var data: ChartData?
{
get
Expand Down
Loading

0 comments on commit 80aa1b3

Please sign in to comment.