I am using the SwiftCharts library found here.
I have the following code:
class ChartTestViewController: UIViewController {
#IBOutlet weak var chartView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]
let letdoublearray = yaxis.map { Double($0) }
let tuples = zip(xaxis, letdoublearray).map { ($0, $1) }
let chartConfig = BarsChartConfig(
valsAxisConfig: ChartAxisConfig(from: 0, to: 8, by: 2)
)
let frame = CGRect(x: 0, y: 70, width: 300, height: 500)
let chart = BarsChart(
frame: frame,
chartConfig: chartConfig,
xTitle: "X axis",
yTitle: "Y axis",
bars: tuples,
color: UIColor.red,
barWidth: 20
)
self.chart = chart // error here
self.chartView = chart.view
I am getting an error at the self.chart = chart line saying chart is not a member of the class. I already have chart defined so I am not sure where else I need to create another chart variable.
Your view controller does not have a chart property. Simply add one.
class ChartTestViewController: UIViewController {
var chart: BarsChart!
#IBOutlet weak var chartView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// lots of code here
self.chart = chart
}
Related
I have created a simple bar chart with some data all in the same range( between 20 and 30).
the generated chart displays the data starting at just bellow 20 to just above 30.
How can I render the graph to display the data from 0 on the y axis
included below is my trial code and an image of the resulting graph
import UIKit
import Charts
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
generateChartData()
setChart() }
let temperatures = [22.1, 21.5, 27.3, 27.5, 30.0, 22.2,27.6, 20.0, 23.5]
let colours = [ "red", "blue", "green", "orange", "grey", "yellow", "black", "white"]
var combinedData:[BarChartDataEntry] = []
#IBOutlet weak var barChart: BarChartView!
func setChart() {
let chartDataSet = BarChartDataSet(entries: combinedData, label: "interesting data")
let chartData = BarChartData(dataSet: chartDataSet)
barChart.data = chartData
}
func generateChartData() {
for i in 0..<colours.count {
let thisEntry = BarChartDataEntry(x: Double(i), y: temperatures[i], data: colours[i])
combinedData.append(thisEntry)
}
}
}bar graph displaying truncated bars
You can set the axises minimum value, like this:
barChart.leftAxis.axisMinimum = 0
barChart.rightAxis.axisMinimum = 0
Or even not zero, but some minus value. Added here left and right axis for simplicity if you disable one of them.
`class PieChartViewController: DemoBaseViewController {
#IBOutlet var chartView: PieChartView!
#IBOutlet var sliderX: UISlider!
#IBOutlet var sliderY: UISlider!
#IBOutlet var sliderTextX: UITextField!
#IBOutlet var sliderTextY: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Pie Chart"
self.setup(pieChartView: chartView)
chartView.delegate = self
chartView.holeRadiusPercent = 0.8
chartView.usePercentValuesEnabled = true
chartView.transparentCircleRadiusPercent = 0
chartView.drawHoleEnabled = true
chartView.transparentCircleColor = UIColor.white.withAlphaComponent(1)
chartView.drawCenterTextEnabled = true
chartView.centerText = nil
let l = chartView.legend
l.enabled = false
//MARK: Entry label Styling (Value Label Above PieChart)
chartView.entryLabelColor = .black
chartView.entryLabelFont = .systemFont(ofSize: 14, weight: .medium)
//MARK: Setting Offset
let frameOffset = chartView.frame.width * 0.12
chartView.setExtraOffsets(left: frameOffset, top: frameOffset, right: frameOffset, bottom: frameOffset)
self.slidersValueChanged(nil)
}
override func updateChartData() {
if self.shouldHideData {
chartView.data = nil
return
}
self.setDataCount()
}
func setDataCount() {
let entry1 = PieChartDataEntry(value: 22, label: "Apple.It is a fruit")
let entry2 = PieChartDataEntry(value: 33, label: "Zebra.It is an animal")
let entry3 = PieChartDataEntry(value: 31, label: "Eagle.It is a bird")
let entry4 = PieChartDataEntry(value: 14, label: "Asia.It is a continent")
var enteries: [PieChartDataEntry] = []
enteries.append(entry1)
enteries.append(entry2)
enteries.append(entry3)
enteries.append(entry4)
let set = PieChartDataSet(entries: enteries, label: "Knowledge")
set.drawIconsEnabled = false
set.sliceSpace = 0
set.selectionShift = 0
set.colors = ChartColorTemplates.vordiplom()
+ ChartColorTemplates.joyful()
+ ChartColorTemplates.colorful()
+ ChartColorTemplates.liberty()
+ ChartColorTemplates.pastel()
+ [UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)]
// MARK: Setting Value Label
set.valueLinePart1OffsetPercentage = 0.20
set.valueLinePart1Length = 0.4
set.valueLinePart2Length = 0.4
set.valueLineColor = .black
set.valueTextColor = .black
set.xValuePosition = .outsideSlice
set.yValuePosition = .outsideSlice
let data = PieChartData(dataSet: set)
data.setValueFont(.systemFont(ofSize: 13, weight: .medium))
data.setValueTextColor(.black)
chartView.data = data
chartView.highlightValues(nil)
}
override func optionTapped(_ option: Option) {
chartView.setNeedsDisplay()
}
// MARK: - Actions
#IBAction func slidersValueChanged(_ sender: Any?) {
self.updateChartData()
}
}`
How to truncate or word wrap the value label in Pie Chart ?
I am using Charts Library in iOS.
If I add extra offset the pie chart shrink in smaller devices like iPhone 5s or iPhone SE.
If the text is large then the text goes out of the screen as you can see in the uploaded image. I am not able to find any properties (word wrap) to handle the large text issue.
In the black circles data that i wont to hide. I wonna to get simple green and red chat w\o text in elements.
I checked all the methods of PieChartView and wont find method to hide this data. Sims it integrated to elements...
Inplementation code:
import Charts
class ViewController: UIViewController {
#IBOutlet weak var pieChart: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
// pieChart.chartAnimator
// pieChart.drawCenterTextEnabled = false
// pieChart.drawHoleEnabled = false
let months = ["", ""]
let unitsSold = [60.0, 40.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
// let da = ChartDataEntry(
let dataEntry1 = ChartDataEntry(x: Double(0.0), y: 60.0)
let dataEntry2 = ChartDataEntry(x: Double(0.0), y: 40.0)
dataEntries.append(dataEntry1)
dataEntries.append(dataEntry2)
print(dataEntries[0].data as Any)
let pieChartDataSet = PieChartDataSet(entries: dataEntries, label: nil)
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChart.data = pieChartData
let colors: [UIColor] = [UIColor(cgColor: UIColor.red.cgColor), UIColor(cgColor: UIColor.green.cgColor)]
pieChartDataSet.colors = colors
}
You need to set the text color as clear, it will work fine. Check out following change in your current code
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChartData.setValueTextColor(NSUIColor.clear)
pieChart.data = pieChartData
I'm still new to swift so any help would be appreciated.
I have a simple set of pickerviews which are defined in 2 custom classes (ImageModelPicker and TimeModelPicker. When ever I load the ViewController that they are in directly there is no issue. When I load them after loading another (title screen) view controller their values throw: found nil while unwrapping an Optional value when using 2 different ViewControllers
Here is the main code:
#IBOutlet weak var pickerImageView: UIPickerView! // image pickerview
var imageModelPicker: ImageModelPicker! // initializing
#IBOutlet weak var pickerTimeView: UIPickerView! //time pickerview
var timeModelPicker: TimeModelPicker! // initializing
override func viewDidLoad() {
super.viewDidLoad()
// image model picker
self.imageModelPicker = ImageModelPicker()
self.imageModelPicker.modelData = Data.getData()
//rotate frame and delegate pickerImageView
let rotationAngle = -90 * (Double.pi/180)
self.pickerImageView.delegate = imageModelPicker
self.pickerImageView.dataSource = imageModelPicker
pickerImageView.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
pickerImageView.frame = CGRect(x: -100, y: (view.center.y) - 25, width: view.frame.width + 200, height: 50)
pickerImageView.selectRow(5, inComponent: 0, animated: true) // select default value
Actually, you are not initialize a variable by this code:
var imageModelPicker: ImageModelPicker!
As a result, you get an error, because you try to use nil instead of instance of the ImageModelPicker class.
Try this code:
#IBOutlet weak var pickerImageView: UIPickerView! // image pickerview
var imageModelPicker = ImageModelPicker() // initializing
#IBOutlet weak var pickerTimeView: UIPickerView! //time pickerview
var timeModelPicker: TimeModelPicker! // initializing
override func viewDidLoad() {
super.viewDidLoad()
// image model picker
self.imageModelPicker.modelData = Data.getData()
//rotate frame and delegate pickerImageView
let rotationAngle = -90 * (Double.pi/180)
self.pickerImageView.delegate = imageModelPicker
self.pickerImageView.dataSource = imageModelPicker
pickerImageView.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
pickerImageView.frame = CGRect(x: -100, y: (view.center.y) - 25, width: view.frame.width + 200, height: 50)
pickerImageView.selectRow(5, inComponent: 0, animated: true) // select default value
I am new in iOS and I want to implement a framework call PNChart to my project. I would like to increment the circle chart by one but the UI will not update. Please help.
import UIKit
import PNChart
class ViewController: UIViewController {
//global var
let number = NSNumber(value: 70)
//obj outlet
#IBOutlet weak var circleChart: PNCircleChart!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
let myCgrect = CGRect(
x: self.view.bounds.width/2 - 40,
y: self.view.bounds.height/2 - 40,
width: 100, height: 100)
let circleChart = PNCircleChart(
frame: myCgrect, total: Int(100) as NSNumber!,
current: number, clockwise: false,
shadow: false, shadowColor: UIColor.red)
circleChart?.backgroundColor = UIColor.clear
circleChart?.strokeColor = UIColor.blue
circleChart?.stroke()
self.view.addSubview(circleChart!)
}
#IBAction func increaseCircle(_ sender: Any) {
_ = NSNumber(value: number.intValue + 1)
print("btnpressed")
}
}// end uiviewcontroller
In your function increaseCircle you aren't doing anything with the information. You need to feed in this incremented number into your circleChart and redraw it. Since you're customizing Gindi's library you probably have your own functions for doing this.