Skip to content

Commit dd91a86

Browse files
committed
Add more examples
1 parent 020df0f commit dd91a86

File tree

2 files changed

+126
-24
lines changed

2 files changed

+126
-24
lines changed

Example/SwiftChart/BasicChartViewController.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,23 @@ class BasicChartViewController: UIViewController, ChartDelegate {
2525
case 0:
2626

2727
// Simple chart
28-
let series = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])
29-
series.color = ChartColors.greenColor()
30-
chart.add(series)
31-
28+
let data = [
29+
(x: 0, y: 0),
30+
(x: 3, y: 2.5),
31+
(x: 4, y: 2),
32+
(x: 5, y: 2.3),
33+
(x: 7, y: 3),
34+
(x: 8, y: 2.2),
35+
(x: 9, y: 2.5)
36+
]
37+
let series = ChartSeries(data: data)
38+
series.area = true
39+
chart.xLabels = [0, 3, 6, 9, 12, 15, 18, 21, 24]
40+
chart.xLabelsFormatter = { String(Int(round($1))) + "h" }
41+
chart.add(series)
42+
// let series = ChartSeries(data: data)
43+
// chart.add(series)
44+
3245

3346
case 1:
3447

@@ -51,11 +64,14 @@ class BasicChartViewController: UIViewController, ChartDelegate {
5164
case 2:
5265

5366
// Chart with y-min, y-max and y-labels formatter
54-
55-
5667
let data: [Double] = [0, -2, -2, 3, -3, 4, 1, 0, -1]
5768

5869
let series = ChartSeries(data)
70+
series.colors = (
71+
above: ChartColors.greenColor(),
72+
below: ChartColors.yellowColor(),
73+
zeroLevel: -1
74+
)
5975
series.area = true
6076

6177
chart.add(series)

README.md

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ chart.add(series)
5454

5555
To run the example project, clone the repo, and run `pod install` from the Example directory first.
5656

57-
### To initialize a chart
57+
## To initialize a chart
5858

59-
#### From the Interface Builder
59+
### From the Interface Builder
6060

6161
The chart can be initialized from the Interface Builder. Drag a normal View into a View Controller and assign to it the `Chart` Custom Class from the Identity Inspector.
6262

63-
#### Programmatically
63+
### Programmatically
6464

6565
To initialize a chart programmatically, use the `Chart(frame: ...)` initializer, which requires a `frame`:
6666

@@ -75,7 +75,7 @@ let chart = Chart(frame: CGRectZero)
7575
// add constraints now
7676
```
7777

78-
### Adding series
78+
## Adding series
7979

8080
Initialize each series before adding them to the chart. To do so, pass an array to initialize a `ChartSeries` object:
8181

@@ -84,33 +84,40 @@ let series = ChartSeries([0, 6.5, 2, 8, 4.1, 7, -3.1, 10, 8])
8484
chart.add(series)
8585
```
8686

87-
By default, the values on the x-axis are the progressive indexes of the passed array. You can customize those values by passing an array of `(x: Double, y: Double)` tuples to the series initializer:
87+
**Result:**
88+
89+
<img width="400" alt="screen shot 2018-01-07 at 10 51 02" src="https://user-images.githubusercontent.com/120693/34648353-b66f352a-f398-11e7-98b9-9d15dcbdd692.png">
90+
91+
92+
As you can see, as default the values on the x-axis are the progressive indexes of the passed array. You can customize those values by passing an array of `(x: Double, y: Double)` tuples to the series initializer:
8893

8994
```swift
9095
// Create a new series specifying x and y values
9196
let data = [
92-
(x: 0, y: 0),
93-
(x: 0.5, y: 3.1),
94-
(x: 1.2, y: 2),
95-
(x: 2.1, y: -4.2),
96-
(x: 2.6, y: 1.1)
97+
(x: 0, y: 0),
98+
(x: 1, y: 3.1),
99+
(x: 4, y: 2),
100+
(x: 5, y: 4.2),
101+
(x: 7, y: 5),
102+
(x: 9, y: 9),
103+
(x: 10, y: 8)
97104
]
98105
let series = ChartSeries(data: data)
99106
chart.add(series)
100107
```
101108

102-
#### Multiple series
109+
**Result:**
103110

104-
Using the `chart.add(series: ChartSeries)` and `chart.add(series: Array<ChartSeries>)` methods you can add more series. Those will be indentified with a progressive index in the chart’s `series` property.
111+
<img width="400" src="https://user-images.githubusercontent.com/120693/34648477-f8a0c48a-f399-11e7-9e36-123171b6413b.png">
105112

106-
#### Partially filled series
113+
### Partially filled series
107114

108-
Use the `chart.xLabels` property to make the x-axis wider than the actual data. For example,
115+
Use the `chart.xLabels` property to make the x-axis showing more labels than those inferred from the actual data. For example,
109116

110117
```swift
111118
let chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
112119
let data = [
113-
(x: 0.0, y: 0),
120+
(x: 0, y: 0),
114121
(x: 3, y: 2.5),
115122
(x: 4, y: 2),
116123
(x: 5, y: 2.3),
@@ -125,9 +132,78 @@ chart.xLabelsFormatter = { String(Int(round($1))) + "h" }
125132
chart.add(series)
126133
```
127134

128-
will render:
135+
**Result:**
136+
137+
<img width="400" src="https://user-images.githubusercontent.com/120693/34648482-28818ee6-f39a-11e7-99d3-0eb0f1402f73.png">
138+
139+
140+
### Different colors above and below zero
141+
142+
The chart displays the series in different colors when below or above the zero-axis:
143+
144+
```swift
145+
let chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
146+
let data: [Double] = [0, -2, -2, 3, -3, 4, 1, 0, -1]
147+
148+
let series = ChartSeries(data)
149+
series.area = true
150+
151+
chart.add(series)
152+
153+
// Set minimum and maximum values for y-axis
154+
chart.minY = -7
155+
chart.maxY = 7
156+
157+
// Format y-axis, e.g. with units
158+
chart.yLabelsFormatter = { String(Int($1)) + "ºC" }
159+
```
160+
161+
**Result:**
162+
163+
<img width="410" src="https://user-images.githubusercontent.com/120693/34648596-3f0538be-f39c-11e7-9cb3-ea06c025b09c.png">
164+
165+
You can customize the zero-axis and the colors with the `colors` options in the `ChartSeries` class.
166+
167+
```swift
168+
series.colors = (
169+
above: ChartColors.greenColor(),
170+
below: ChartColors.yellowColor(),
171+
zeroLevel: -1
172+
)
173+
```
174+
175+
**Result:**
176+
177+
<img width="410" src="https://user-images.githubusercontent.com/120693/34648597-3f269158-f39c-11e7-90d3-d3dfb120c95d.png">
178+
179+
180+
### Multiple series
181+
182+
Using the `chart.add(series: ChartSeries)` and `chart.add(series: Array<ChartSeries>)` methods you can add more series. Those will be indentified with a progressive index in the chart’s `series` property.
183+
184+
185+
```swift
186+
let chart = Chart(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
187+
188+
let series1 = ChartSeries([0, 6, 2, 8, 4, 7, 3, 10, 8])
189+
series1.color = ChartColors.yellowColor()
190+
series1.area = true
191+
192+
let series2 = ChartSeries([1, 0, 0.5, 0.2, 0, 1, 0.8, 0.3, 1])
193+
series2.color = ChartColors.redColor()
194+
series2.area = true
195+
196+
// A partially filled series
197+
let series3 = ChartSeries([9, 8, 10, 8.5, 9.5, 10])
198+
series3.color = ChartColors.purpleColor()
199+
200+
chart.add([series1, series2, series3])
201+
```
202+
203+
**Result:**
204+
205+
<img width="412" alt="screen shot 2018-01-07 at 11 06 55" src="https://user-images.githubusercontent.com/120693/34648532-282fcda8-f39b-11e7-93f3-c502329752b5.png">
129206

130-
<img width="443" alt="" src="https://cloud.githubusercontent.com/assets/120693/17461649/26510f96-5c94-11e6-8324-46df266558dd.png">
131207

132208
## Touch events
133209

@@ -229,7 +305,17 @@ Some tips for debugging an hidden chart:
229305
* `area` – draws an area below the series’ line.
230306
* `line` – set it to `false` to hide the line (useful for drawing only the area).
231307
* `color` – the series color.
232-
* `colors` – a tuple to specify the color above or below the zero. For example, `(above: ChartColors.redColor(), below: ChartColors.blueColor(), -4)` will use red for values above `-4`, and blue for values below -4.
308+
* `colors` – a tuple to specify the color above or below the zero (or another value).
309+
310+
For example, to use red for values above `-4`, and blue for values below `-4`.
311+
```swift
312+
series.colors = (
313+
above: ChartColors.redColor(),
314+
below: ChartColors.blueColor(),
315+
zeroLevel: -4
316+
)
317+
````
318+
233319

234320
## `ChartDelegate`
235321

0 commit comments

Comments
 (0)