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

Chartdata collection refactor #3024

Merged
merged 29 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6e80eb0
Added Collection conformances
jjatie Nov 18, 2017
d36d11b
[#3018]
jjatie Nov 19, 2017
36ca566
Fixed required initializers
jjatie Nov 20, 2017
cb32b08
ChartData adopts ExressibleByArrayLiteral
jjatie Dec 1, 2017
ad0d148
Merge remote-tracking branch 'origin/chartdata-collection-conformance…
jjatie Dec 1, 2017
fcd9fa2
Modified demos
jjatie Dec 1, 2017
716f182
Pulled latest 4.0.0 and code style fixes
jjatie Jan 6, 2018
ca5afad
Updates for PR
jjatie Jan 11, 2018
0d41175
Pulled latest base
jjatie Jan 11, 2018
8976b95
PR review fixes
jjatie Jan 12, 2018
f28d3d5
Removed unnecessary `get` from subscripts.
jjatie Jan 12, 2018
793e437
Merge remote-tracking branch 'origin/chartdata-collection-conformance…
jjatie Jan 15, 2018
583dab6
Disabled `remove(at:)` for CombinedChartView
jjatie Jan 15, 2018
0cce64d
Merge remote-tracking branch 'origin/chartdata-collection-conformance…
jjatie Jan 15, 2018
47b6a1c
Removed redundant methods
jjatie Jan 15, 2018
c0b7d65
Relocated `appendEntry(_:todataSet:)`
jjatie Jan 15, 2018
1d1e380
Merge remote-tracking branch 'origin/chartdata-collection-conformance…
jjatie Jan 15, 2018
a71f87c
Removed methods from CombinedChartData
jjatie Jan 15, 2018
efc5a72
Merge remote-tracking branch 'origin/chartdata-collection-conformance…
jjatie Jan 15, 2018
bfb750b
Merge branch '4.0.0' into chartdata-collection-refactor
jjatie Jan 15, 2018
6adde98
Merge branch '4.0.0' into chartdata-collection-refactor
jjatie Jan 15, 2018
ae8279a
Merge branch '4.0.0' into chartdata-collection-refactor
jjatie Jan 16, 2018
6de1114
Merge branch '4.0.0' into chartdata-collection-refactor
jjatie Jan 16, 2018
37b29ad
Merge branch '4.0.0' of https://github.com/danielgindi/Charts into ch…
jjatie Jan 21, 2018
329e00c
Fixed merge conflicts
jjatie Jan 21, 2018
2572f04
Merge remote-tracking branch 'upstream/4.0.0' into chartdata-collecti…
jjatie Jan 23, 2018
31a76eb
Fixed merge conflicts
jjatie Jan 23, 2018
b7e6f93
updated demos
jjatie Jan 26, 2018
eb8e031
Updated macOS demos
jjatie Jan 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ChartsDemo/Swift/DemoBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ class DemoBaseViewController: UIViewController, ChartViewDelegate {
func handleOption(_ option: Option, forChartView chartView: ChartViewBase) {
switch option {
case .toggleValues:
for set in chartView.data!.dataSets {
for set in chartView.data! {
Copy link
Member

Choose a reason for hiding this comment

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

I had a feeling that for set in data is less natural than for set in data.dataSets, what you think? Maybe need time to adapt?
So in the future, we will deprecate data.dataSet?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's just new to you. We need to continue to support data.dataSet as long as we keep Obj-C compatibility.

set.drawValuesEnabled = !set.drawValuesEnabled
}
chartView.setNeedsDisplay()

case .toggleIcons:
for set in chartView.data!.dataSets {
for set in chartView.data! {
set.drawIconsEnabled = !set.drawIconsEnabled
}
chartView.setNeedsDisplay()
Expand Down Expand Up @@ -159,7 +159,7 @@ class DemoBaseViewController: UIViewController, ChartViewDelegate {
updateChartData()

case .toggleBarBorders:
for set in chartView.data!.dataSets {
for set in chartView.data! {
if let set = set as? BarChartDataSet {
set.barBorderWidth = set.barBorderWidth == 1.0 ? 0.0 : 1.0
}
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AnotherBarChartViewController: DemoBaseViewController {
}

var set1: BarChartDataSet! = nil
if let set = chartView.data?.dataSets.first as? BarChartDataSet {
if let set = chartView.data?.first as? BarChartDataSet {
set1 = set
set1?.values = yVals
chartView.data?.notifyDataChanged()
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Swift/Demos/BarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class BarChartViewController: DemoBaseViewController {
}

var set1: BarChartDataSet! = nil
if let set = chartView.data?.dataSets.first as? BarChartDataSet {
if let set = chartView.data?.first as? BarChartDataSet {
set1 = set
set1.values = yVals
chartView.data?.notifyDataChanged()
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Swift/Demos/BubbleChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class BubbleChartViewController: DemoBaseViewController {
set3.setColor(ChartColorTemplates.colorful()[2], alpha: 0.5)
set3.drawValuesEnabled = true

let data = BubbleChartData(dataSets: [set1, set2, set3])
let data = [set1, set2, set3] as BubbleChartData
Copy link
Member

Choose a reason for hiding this comment

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

may I know which protocol method enables this convenient conversion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ExpressibleByArrayLiteral

data.setDrawValues(false)
data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 7)!)
data.setHighlightCircleWidth(1.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class CandleStickChartViewController: DemoBaseViewController {

override func optionTapped(_ option: Option) {
if .toggleShadowColorSameAsCandle ~= option {
for set in chartView.data!.dataSets as! [CandleChartDataSet] {
for set in chartView.data as! CandleChartData {
set.shadowColorSameAsCandle = !set.shadowColorSameAsCandle
}
chartView.notifyDataSetChanged()
Expand Down
6 changes: 3 additions & 3 deletions ChartsDemo/Swift/Demos/CombinedChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CombinedChartViewController: DemoBaseViewController {
override func optionTapped(_ option: Option) {
switch option {
case .toggleLineValues:
for set in chartView.data!.dataSets {
for set in chartView.data! {
if let set = set as? LineChartDataSet {
set.drawValuesEnabled = !set .drawValuesEnabled

Expand All @@ -103,7 +103,7 @@ class CombinedChartViewController: DemoBaseViewController {
chartView.setNeedsDisplay()

case .toggleBarValues:
for set in chartView.data!.dataSets {
for set in chartView.data! {
if let set = set as? BarChartDataSet {
set.drawValuesEnabled = !set .drawValuesEnabled
}
Expand Down Expand Up @@ -171,7 +171,7 @@ class CombinedChartViewController: DemoBaseViewController {
let barWidth = 0.45 // x2 dataset
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"

let data = BarChartData(dataSets: [set1, set2])
let data: BarChartData = [set1, set2]
data.barWidth = barWidth

// make this BarData object grouped
Expand Down
10 changes: 5 additions & 5 deletions ChartsDemo/Swift/Demos/CubicLineChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,31 @@ class CubicLineChartViewController: DemoBaseViewController {
override func optionTapped(_ option: Option) {
switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

case .toggleCircles:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawCirclesEnabled = !set.drawCirclesEnabled
}
chartView.setNeedsDisplay()

case .toggleCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
}
chartView.setNeedsDisplay()

case .toggleStepped:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .stepped) ? .linear : .stepped
}
chartView.setNeedsDisplay()

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
}
chartView.setNeedsDisplay()
Expand Down
10 changes: 5 additions & 5 deletions ChartsDemo/Swift/Demos/LineChart1ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,31 @@ class LineChart1ViewController: DemoBaseViewController {
override func optionTapped(_ option: Option) {
switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
Copy link
Member

Choose a reason for hiding this comment

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

want to know which method enables this as well.
And, it seems weird that a set object is a ChartData type? what you think

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See comment below

Copy link
Member

Choose a reason for hiding this comment

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

@jjatie for set in chartView.data as! LineChartData here I'm confused, as set implies a dataset class, while it casts to a chartdata class. is the name good here?

Copy link
Member

Choose a reason for hiding this comment

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

oh wait. Seems I asked a wrong question?
it's chartView.data being casted to LineChartData?

set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

case .toggleCircles:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawCirclesEnabled = !set.drawCirclesEnabled
}
chartView.setNeedsDisplay()

case .toggleCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
}
chartView.setNeedsDisplay()

case .toggleStepped:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .stepped) ? .linear : .stepped
}
chartView.setNeedsDisplay()

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
}
chartView.setNeedsDisplay()
Expand Down
12 changes: 6 additions & 6 deletions ChartsDemo/Swift/Demos/LineChart2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class LineChart2ViewController: DemoBaseViewController {
set3.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1)
set3.drawCircleHoleEnabled = false

let data = LineChartData(dataSets: [set1, set2, set3])
let data: LineChartData = [set1, set2, set3]
data.setValueTextColor(.white)
data.setValueFont(.systemFont(ofSize: 9))

Expand All @@ -145,31 +145,31 @@ class LineChart2ViewController: DemoBaseViewController {
override func optionTapped(_ option: Option) {
switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

case .toggleCircles:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawCirclesEnabled = !set.drawCirclesEnabled
}
chartView.setNeedsDisplay()

case .toggleCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
}
chartView.setNeedsDisplay()

case .toggleStepped:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .stepped) ? .linear : .stepped
}
chartView.setNeedsDisplay()

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
}
chartView.setNeedsDisplay()
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Swift/Demos/LineChartFilledViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class LineChartFilledViewController: DemoBaseViewController {
return CGFloat(self.chartView.leftAxis.axisMaximum)
}

let data = LineChartData(dataSets: [set1, set2])
let data: LineChartData = [set1, set2]
data.setDrawValues(false)

chartView.data = data
Expand Down
10 changes: 5 additions & 5 deletions ChartsDemo/Swift/Demos/LineChartTimeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,31 @@ class LineChartTimeViewController: DemoBaseViewController {
override func optionTapped(_ option: Option) {
switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

case .toggleCircles:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawCirclesEnabled = !set.drawCirclesEnabled
}
chartView.setNeedsDisplay()

case .toggleCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
}
chartView.setNeedsDisplay()

case .toggleStepped:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .stepped) ? .linear : .stepped
}
chartView.setNeedsDisplay()

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
}
chartView.setNeedsDisplay()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MultipleBarChartViewController: DemoBaseViewController {
let set4 = BarChartDataSet(values: yVals4, label: "Company D")
set4.setColor(UIColor(red: 255/255, green: 102/255, blue: 0/255, alpha: 1))

let data = BarChartData(dataSets: [set1, set2, set3, set4])
let data: BarChartData = [set1, set2, set3, set4]
data.setValueFont(.systemFont(ofSize: 10, weight: .light))
data.setValueFormatter(LargeValueFormatter())

Expand Down
8 changes: 4 additions & 4 deletions ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,25 @@ class MultipleLinesChartViewController: DemoBaseViewController {
override func optionTapped(_ option: Option) {
switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

case .toggleCircles:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.drawCirclesEnabled = !set.drawCirclesEnabled
}
chartView.setNeedsDisplay()

case .toggleCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
}
chartView.setNeedsDisplay()

case .toggleStepped:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for set in chartView.data as! LineChartData {
set.mode = (set.mode == .stepped) ? .linear : .stepped
}
chartView.setNeedsDisplay()
Expand Down
6 changes: 3 additions & 3 deletions ChartsDemo/Swift/Demos/RadarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class RadarChartViewController: DemoBaseViewController {
set2.drawHighlightCircleEnabled = true
set2.setDrawHighlightIndicators(false)

let data = RadarChartData(dataSets: [set1, set2])
let data: RadarChartData = [set1, set2]
data.setValueFont(.systemFont(ofSize: 8, weight: .light))
data.setDrawValues(false)
data.setValueTextColor(.white)
Expand All @@ -166,14 +166,14 @@ class RadarChartViewController: DemoBaseViewController {
chartView.rotationEnabled = !chartView.rotationEnabled

case .toggleFilled:
for set in chartView.data!.dataSets as! [RadarChartDataSet] {
for set in chartView.data as! RadarChartData {
set.drawFilledEnabled = !set.drawFilledEnabled
}

chartView.setNeedsDisplay()

case .toggleHighlightCircle:
for set in chartView.data!.dataSets as! [RadarChartDataSet] {
for set in chartView.data as! RadarChartData {
set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled
}
chartView.setNeedsDisplay()
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Swift/Demos/ScatterChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ScatterChartViewController: DemoBaseViewController {
set3.setColor(ChartColorTemplates.colorful()[2])
set3.scatterShapeSize = 8

let data = ScatterChartData(dataSets: [set1, set2, set3])
let data: ScatterChartData = [set1, set2, set3]
data.setValueFont(.systemFont(ofSize: 7, weight: .light))

chartView.data = data
Expand Down
9 changes: 3 additions & 6 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
/// - returns: `true` if the chart is empty (meaning it's data object is either null or contains no entries).
@objc open func isEmpty() -> Bool
{
guard let data = data else { return true }
return data.entryCount <= 0
return data?.isEmpty ?? true
}

/// Lets the chart know its underlying data has changed and should perform all necessary recalculations.
Expand Down Expand Up @@ -251,13 +250,11 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
}


if _defaultValueFormatter is DefaultValueFormatter
if let formatter = _defaultValueFormatter as? DefaultValueFormatter
{
// setup the formatter with a new number of digits
let digits = reference.decimalPlaces

(_defaultValueFormatter as? DefaultValueFormatter)?.decimals
= digits
formatter.decimals = digits
}
}

Expand Down
11 changes: 4 additions & 7 deletions Source/Charts/Data/Implementations/Standard/BarChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ open class BarChartData: BarLineScatterCandleBubbleChartData
/// - parameter barSpace: The space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
@objc open func groupBars(fromX: Double, groupSpace: Double, barSpace: Double)
{
let setCount = _dataSets.count
if setCount <= 1
{
guard !isEmpty else {
print("BarData needs to hold at least 2 BarDataSets to allow grouping.", terminator: "\n")
return
}

let max = maxEntryCountSet
let maxEntryCount = max?.entryCount ?? 0

Expand All @@ -61,7 +59,7 @@ open class BarChartData: BarLineScatterCandleBubbleChartData

let interval = groupWidth(groupSpace: groupSpace, barSpace: barSpace)

for i in stride(from: 0, to: maxEntryCount, by: 1)
for i in 0..<maxEntryCount
{
let start = fromX
fromX += groupSpaceWidthHalf
Expand Down Expand Up @@ -104,7 +102,6 @@ open class BarChartData: BarLineScatterCandleBubbleChartData
/// - parameter barSpace:
@objc open func groupWidth(groupSpace: Double, barSpace: Double) -> Double
{
return Double(_dataSets.count) * (self.barWidth + barSpace) + groupSpace
return Double(count) * (self.barWidth + barSpace) + groupSpace
}

}
Loading