Skip to content

Commit

Permalink
Playgrounds (#13)
Browse files Browse the repository at this point in the history
* playgrounds

* cleanup

* playground
  • Loading branch information
ay42 authored Sep 14, 2020
1 parent 3f74ad6 commit 6cdef24
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/Packages
/*.xcodeproj
xcuserdata/
.swiftpm/
161 changes: 161 additions & 0 deletions Playgrounds/Charts.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//: Examples in Playground form for Xcode 12+
import SwiftUI
import Charts
import Shapes

import PlaygroundSupport

struct ContentView: View {
@State var data1: [CGFloat] = (2010...2020).map { _ in .random(in: 0.1...1.0) }
@State var data2: [CGFloat] = (0..<50).map { _ in .random(in: 0.1...1.0) }
@State var data3: [CGFloat] = (0..<100).map { _ in .random(in: 0.1...1.0) }

@State var data4: [CGFloat] = (0..<100).map { _ in .random(in: 0.4...1.0) }
@State var data5: [CGFloat] = (0..<100).map { _ in .random(in: 0.1...0.3) }
@State var data6: [CGFloat] = (0..<100).map { _ in .random(in: 0.3...0.4) }

@State var matrixData1: [[CGFloat]] = (0..<20).map { _ in (0..<3).map { _ in CGFloat.random(in: 0.00...0.33) } }

var chart1: some View {
HStack {
VStack {
AxisLabels(.vertical, data: 1...10, id: \.self) {
Text("\(110 - $0 * 10)")
.fontWeight(.bold)
.font(Font.system(size: 8))
.foregroundColor(.secondary)
}
.frame(width: 20)

Rectangle().foregroundColor(.clear).frame(width: 20, height: 20)
}


VStack {
Chart(data: data1)
.chartStyle(
LineChartStyle(.quadCurve, lineColor: .blue, lineWidth: 5)
)
.padding()
.background(
GridPattern(horizontalLines: 10 + 1, verticalLines: data1.count + 1)
.inset(by: 1)
.stroke(Color.gray.opacity(0.1), style: .init(lineWidth: 2, lineCap: .round))
)
.frame(height: 300)


AxisLabels(.horizontal, data: 2010...2020, id: \.self) {
Text("\($0)".replacingOccurrences(of: ",", with: ""))
.fontWeight(.bold)
.font(Font.system(size: 8))
.foregroundColor(.secondary)
}
.frame(height: 20)
.padding(.horizontal, 1)
}
.layoutPriority(1)
}

.padding()
}
var chart2: some View {
Chart(data: data2)
.chartStyle(
AreaChartStyle(.quadCurve, fill:
LinearGradient(gradient: .init(colors: [Color.red.opacity(0.8), Color.red.opacity(0.2)]), startPoint: .top, endPoint: .bottom)
)
)
.background(
Color.gray.opacity(0.1)
.overlay(
GridPattern(horizontalLines: data2.count)
.inset(by: 1)
.stroke(Color.red.opacity(0.2), style: .init(lineWidth: 1, lineCap: .round))
)
)
.cornerRadius(16)
.frame(height: 300)
.padding()
}
var chart3: some View {
Chart(data: data3)
.chartStyle(
ColumnChartStyle(column: Capsule().foregroundColor(.green), spacing: 2)
)
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(16)
.frame(height: 300)
.padding()
}
var chart4: some View {
Chart(data: matrixData1)
.chartStyle(
StackedAreaChartStyle(.quadCurve, colors: [.yellow, .orange, .red])
)
.background(
Color.gray.opacity(0.1)
.overlay(
GridPattern(horizontalLines: data2.count)
.inset(by: 1)
.stroke(Color.red.opacity(0.2), style: .init(lineWidth: 1, lineCap: .round))
)
)
.cornerRadius(16)
.frame(height: 300)
.padding()
}
var chart5: some View {
ZStack {
Chart(data: data4)
.chartStyle(
LineChartStyle(.quadCurve, lineColor: .purple, lineWidth: 3)
)

Chart(data: data4)
.chartStyle(
AreaChartStyle(.quadCurve, fill:
LinearGradient(gradient: .init(colors: [Color.purple.opacity(0.8), Color.purple.opacity(0.2)]), startPoint: .top, endPoint: .bottom)
)
)

Chart(data: data5)
.chartStyle(
ColumnChartStyle(column: Color.white.opacity(0.5), spacing: 2)
)

Chart(data: data6)
.chartStyle(
LineChartStyle(.line, lineColor: Color.white.opacity(0.2), lineWidth: 3)
)
}.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(16)
.frame(height: 300)
.padding()
}

@State var selectedChart = 1
var body: some View {
VStack {
Picker("Charts", selection: $selectedChart) {
Text("Line").tag(1)
Text("Area").tag(2)
Text("Column").tag(3)
Text("Stacked Area").tag(4)
Text("ZStacked").tag(5)
}.pickerStyle(SegmentedPickerStyle()).padding()

ZStack {
if selectedChart == 1 { chart1 }
if selectedChart == 2 { chart2 }
if selectedChart == 3 { chart3 }
if selectedChart == 4 { chart4 }
if selectedChart == 5 { chart5 }
}
}.frame(width: 700)
}
}

PlaygroundPage.current.setLiveView(ContentView())
4 changes: 4 additions & 0 deletions Playgrounds/Charts.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

0 comments on commit 6cdef24

Please sign in to comment.