Skip to content

Commit

Permalink
Chartdata collection refactor (#3024)
Browse files Browse the repository at this point in the history
* Added Collection conformances

MutableCollection
RandomAccessCollection
RangeReplaceableCollection

* [#3018] Refactored use of `ChartData` to use new `Collection` conformances

* Fixed required initializers

* ChartData adopts ExressibleByArrayLiteral

* Modified demos to take advantage of collection conformance.

* Removed unnecessary `get` from subscripts.

* Removed redundant methods

* Relocated `appendEntry(_:todataSet:)`

* Removed methods from CombinedChartData
  • Loading branch information
jjatie committed Jan 26, 2018
1 parent 34d2940 commit 3ebee41
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ open class BarDemoViewController: NSViewController
let data = BarChartData()
let ds1 = BarChartDataSet(values: yse1, label: "Hello")
ds1.colors = [NSUIColor.red]
data.addDataSet(ds1)
data.append(ds1)

let ds2 = BarChartDataSet(values: yse2, label: "World")
ds2.colors = [NSUIColor.blue]
data.addDataSet(ds2)
data.append(ds2)

let barWidth = 0.4
let barSpace = 0.05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ open class LineDemoViewController: NSViewController
let data = LineChartData()
let ds1 = LineChartDataSet(values: yse1, label: "Hello")
ds1.colors = [NSUIColor.red]
data.addDataSet(ds1)
data.append(ds1)

let ds2 = LineChartDataSet(values: yse2, label: "World")
ds2.colors = [NSUIColor.blue]
data.addDataSet(ds2)
data.append(ds2)
self.lineChartView.data = data

self.lineChartView.gridBackgroundColor = NSUIColor.white
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ open class PieDemoViewController: NSViewController

ds1.colors = ChartColorTemplates.vordiplom()

data.addDataSet(ds1)
data.append(ds1)

let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.lineBreakMode = .byTruncatingTail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ open class RadarDemoViewController: NSViewController
let data = RadarChartData()
let ds1 = RadarChartDataSet(values: yse1, label: "Hello")
ds1.colors = [NSUIColor.red]
data.addDataSet(ds1)
data.append(ds1)

let ds2 = RadarChartDataSet(values: yse2, label: "World")
ds2.colors = [NSUIColor.blue]
data.addDataSet(ds2)
data.append(ds2)
self.radarChartView.data = data
self.radarChartView.chartDescription.text = "Radarchart Demo"

Expand Down
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! {
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
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 case let set as CandleChartDataSet in chartView.data! {
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
12 changes: 7 additions & 5 deletions ChartsDemo/Swift/Demos/CubicLineChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,35 @@ class CubicLineChartViewController: DemoBaseViewController {
}

override func optionTapped(_ option: Option) {
guard let data = chartView.data else { return }

switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

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

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

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

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
}
chartView.setNeedsDisplay()
Expand Down
12 changes: 7 additions & 5 deletions ChartsDemo/Swift/Demos/LineChart1ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,35 @@ class LineChart1ViewController: DemoBaseViewController {
}

override func optionTapped(_ option: Option) {
guard let data = chartView.data else { return }

switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

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

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

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

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
}
chartView.setNeedsDisplay()
Expand Down
14 changes: 8 additions & 6 deletions ChartsDemo/Swift/Demos/LineChart2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,41 +135,43 @@ 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))

chartView.data = data
}

override func optionTapped(_ option: Option) {
guard let data = chartView.data else { return }

switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

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

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

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

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
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
12 changes: 7 additions & 5 deletions ChartsDemo/Swift/Demos/LineChartTimeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,35 @@ class LineChartTimeViewController: DemoBaseViewController {
}

override func optionTapped(_ option: Option) {
guard let data = chartView.data else { return }

switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

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

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

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

case .toggleHorizontalCubic:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
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
10 changes: 6 additions & 4 deletions ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,29 @@ class MultipleLinesChartViewController: DemoBaseViewController {
}

override func optionTapped(_ option: Option) {
guard let data = chartView.data else { return }

switch option {
case .toggleFilled:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.drawFilledEnabled = !set.drawFilledEnabled
}
chartView.setNeedsDisplay()

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

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

case .toggleStepped:
for set in chartView.data!.dataSets as! [LineChartDataSet] {
for case let set as LineChartDataSet in data {
set.mode = (set.mode == .stepped) ? .linear : .stepped
}
chartView.setNeedsDisplay()
Expand Down
8 changes: 5 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 @@ -151,6 +151,8 @@ class RadarChartViewController: DemoBaseViewController {
}

override func optionTapped(_ option: Option) {
guard let data = chartView.data else { return }

switch option {
case .toggleXLabels:
chartView.xAxis.drawLabelsEnabled = !chartView.xAxis.drawLabelsEnabled
Expand All @@ -166,14 +168,14 @@ class RadarChartViewController: DemoBaseViewController {
chartView.rotationEnabled = !chartView.rotationEnabled

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

chartView.setNeedsDisplay()

case .toggleHighlightCircle:
for set in chartView.data!.dataSets as! [RadarChartDataSet] {
for case let set as RadarChartDataSet in data {
set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled
}
chartView.setNeedsDisplay()
Expand Down
Loading

0 comments on commit 3ebee41

Please sign in to comment.