In the code that we defined in the Getting ready section, the BarChart view can be created with a frame and a background color, and then bars can be added in the form of a Bar struct containing a value and a color. The BarChart view uses these to create subviews of the correct relative size and scale to represent the values of the bars.
Let's write some code to make use of our BarChart view:
- Enter the following into the playground at the bottom:
let barView = BarChart(frame: CGRect(x: 0, y: 0, width: 300, height:
300))
barView.backgroundColor = .white
let bar1 = Bar(value: 20, color: Color(red: 1, green: 0, blue: 0))
let bar2 = Bar(value: 40, color: Color(red: 0, green: 1, blue: 0))
let bar3 = Bar(value: 25, color: Color(red: 0, green: 0, blue: 1))
barView.bars = [bar1, bar2, bar3]
- Press the blue play button at the bottom-left of the playground window to execute the code. As your code executes, you will see that the playground sidebar fills up with information...