Not matching available overloads error with ioscharts - swift

I want to set up bar charts with Swift 3 and follow this tutorial.
I created a barChartView within my viewController and the data values in viewDidLoad:
var months: [String]!
var counts: [Double]!
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
counts = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
Then I call the setChart function with the values:
setChart(dataPoints: months, values: counts)
The function itself looks as follows:
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
countPerCountryView.data = chartData
}
I am getting two error warnings when trying to build, here:
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
Argument labels '(value:, xindex:)' do not match any available overloads
and here:
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
Cannot invoke initializer for type 'BarChartData' with an argument list of type '(xvals: [String]!, dataSet: BarChartDataSet)'

iOS-Chart has had some changes:
BarChartDataEntry(value:xIndex:) was changed to BarChartDataEntry(x: y:)
BarChartData(xVals: dataSet:) was changed to BarChartData(dataSet:)
So you need something like this
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(dataSet: chartDataSet)
countPerCountryView.data = chartData
}
Also if you want to set labels for xAxis you need to implement IAxisValueFormatter protocol with method stringForValue( _: axis:)
You can do it in your ViewController, for example:
class ViewController: UIViewController, IAxisValueFormatter {
// your code ...
// implement delegate method
func stringForValue(_ dataPointIndex: Double, axis: AxisBase?) -> String {
return someDataPoints[Int(dataPointIndex)]
}
func setChart(dataPoints: [String], values: [Double]){
// your code ...
// Set delegate
lineChartView.xAxis.valueFormatter = self
}
// your code ...
}

Charts 3.0 has some breaking changes. Try this
class BarChartViewController: UIViewController, ChartViewDelegate {
#IBOutlet weak var barChartView: BarChartView!
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
barChartView.delegate = self
barChartView.xAxis.valueFormatter = self
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
setChart(values: unitsSold)
}
func setChart(values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
let chartData = BarChartDataSet()
for (i, val) in values.enumerated(){
_ = chartData.addEntry(BarChartDataEntry(x: Double(i), y: val))
}
barChartView.data = BarChartData(dataSet: chartData)
}
}
extension BarChartViewController: IAxisValueFormatter{
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return months[ Int(value) % months.count]
}
}

Related

LineCharts doesn't show data from Realm

I want to show my data to LineCharts.
I have checked the Realm Studio, there is data saved in the Realm. It just doesn't show anything one the View.
Maybe something going wrong with my code.
import UIKit
import Charts
import RealmSwift
class GraphViewController: UIViewController {
#IBOutlet weak var linechart: LineChartView!
weak var axisFormatDelegate: IAxisValueFormatter?
override func viewDidLoad() {
super.viewDidLoad()
axisFormatDelegate = self
// Do any additional setup after loading the view.
//setLineGraph()
updateChartWithData()
}
func updateChartWithData() {
var dataEntries: [ChartDataEntry] = []
let scoreCounts = getScoreFromDatabase()
//let visitorCounts = getScoreFromDatabase()
for i in 0..<scoreCounts.count {
let timeIntervalForDate: TimeInterval = scoreCounts[i].exactdate.timeIntervalSince1970
let dataEntry = ChartDataEntry(x: Double(timeIntervalForDate), y: Double(scoreCounts[i].datesc))
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(entries: dataEntries, label: "Score count" )
linechart.data = LineChartData(dataSet: chartDataSet)
linechart.chartDescription?.text = "Your Score"
let xaxis = linechart.xAxis
xaxis.valueFormatter = axisFormatDelegate
}
func getScoreFromDatabase() -> Results<DateScore> {
let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "MyInMemoryRealm"))
return realm.objects(DateScore.self)
}
}
extension GraphViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY/MM/DD"
return dateFormatter.string(from: Date(timeIntervalSince1970: value))
}
}
Here is my Realm class
import Foundation
import RealmSwift
class DateScore: Object {
#objc dynamic var datesc : Int16 = 0
#objc dynamic var exactdate : Date = Date()
}
And I got error message below is this related?
[Unknown process name] CGAffineTransformInvert: singular matrix.
I am a beginner. Can anyone help me?

Bar Chart showing nil in swift

I am using the Charts library to show bar charts but it still shows nil. I have set the view as BarChartView.
#IBOutlet weak var barChartView: BarChartView!
func setChart() {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<testArray.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(testArray[i]))
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "logs")
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData
}
and I do call setChart() in viewDidLoad()

iOS Charts remove decimal from yValues

How to remove decimals from y values in iOS Charts?
Im using the latest iOS Charts release with Swift3
Thanks for every one who tried to help, here was the fix, adding the below code
let formatter = NumberFormatter()
formatter.numberStyle = .none
formatter.maximumFractionDigits = 0
formatter.multiplier = 1.0
chartData.valueFormatter = DefaultValueFormatter(formatter: formatter)
to the setBarChartData func
func setBarChartData(xValues: [String], yValues: [Double], label: String) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
let chartData = BarChartData(dataSet: chartDataSet)
let formatter = NumberFormatter()
formatter.numberStyle = .none
formatter.maximumFractionDigits = 0
formatter.multiplier = 1.0
chartData.valueFormatter = DefaultValueFormatter(formatter: formatter)
let chartFormatter = BarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
self.data = chartData
}
Could also use:
let format = NumberFormatter()
format.numberStyle = .none
let formatter = DefaultValueFormatter(formatter: format)
data.setValueFormatter(formatter)
You need to set delegate for value formatter in DataSet like below
Obj-C :
//DataSet 1
LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithValues:values label:#"outstanding"];
set1.valueFormatter = self;
Use below delegate method for formatting your value :
#pragma mark - IChartValueFormatter
- (NSString * _Nonnull)stringForValue:(double)value entry:(ChartDataEntry * _Nonnull)entry dataSetIndex:(NSInteger)dataSetIndex viewPortHandler:(ChartViewPortHandler * _Nullable)viewPortHandler{
//Format your value what you want here
return [NSString stringWithFormat:#"%0.f",value];
}
Confirm Protocol :
#interface YourViewController ()<ChartViewDelegate,IChartValueFormatter>
For Swift you need to create Extension of BarChart and use below methods in it
Swift :
extension BarChartView {
private class BarChartFormatter: NSObject, IAxisValueFormatter {
var labels: [String] = []
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return labels[Int(value)]
}
init(labels: [String]) {
super.init()
self.labels = labels
}
}
func setBarChartData(xValues: [String], yValues: [Double], label: String) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
let chartData = BarChartData(dataSet: chartDataSet)
let chartFormatter = BarChartFormatter(labels: xValues)
let xAxis = XAxis()
xAxis.valueFormatter = chartFormatter
self.xAxis.valueFormatter = xAxis.valueFormatter
self.data = chartData
}
}
Call Above Extension Method like this :
func setChart(){
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 3.0, 6.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
barChartView.setBarChartData(xValues: months, yValues: unitsSold, label: "Monthly Sales")
}
hope you will get your formatted value on line chart.
After lots of research and test. i was able to remove the decimal.
First unlock the pod chartViewBase
Remove two lines from the { } i.e 'digits' from chartViewBase pod.
Before:
if let formatter = defaultValueFormatter as? DefaultValueFormatter
{
// setup the formatter with a new number of digits
let digits = reference.decimalPlaces
formatter.decimals = digits
}
After:
if let formatter = defaultValueFormatter as? DefaultValueFormatter
{
}
It will look like following::

Combined Charts. Swift 2.2

I am trying to create a lineChart ontop of a Bar chart. I have the following code so far:
class AnalysisViewController: UIViewController, ChartViewDelegate {
func setChart(xValues: [String], valuesBarChart: [Double], valuesLineChart: [Double]) {
barChartView.descriptionText = ""
barChartView.noDataText = "You need to provide data for the chart."
var yValsBarChart: [BarChartDataEntry] = []
var yValsLineChart : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
yValsBarChart.append(BarChartDataEntry(value: valuesBarChart[i], xIndex: i))
yValsLineChart.append(ChartDataEntry(value: valuesLineChart[i] - 1, xIndex: i))
}
let lineChartDataSet = LineChartDataSet(yVals: yValsLineChart, label: nil)
let barChartDataSet = BarChartDataSet(yVals: yValsBarChart, label: nil)
let data: CombinedChartData = CombinedChartData(xVals: xValues)
data.barData = BarChartData(xVals: xValues, dataSets: [barChartDataSet])
data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartDataSet])
barChartView.data = data
barChartView.leftAxis.customAxisMin = 0
UniversalStatic.data.generateColoursForGraph(budgetName.count)
barChartDataSet.colors = UniversalStatic.data.colourArrayForGraph
setDesignChart()
}
}
I am getting the following error on the terminal:
Could not cast value of type 'Charts.CombinedChartData' (0x3d5d70) to 'Charts.BarChartData' (0x3cfc54).
as well as:
I don't really know what i have done wrong as I haven't touched the 'BarChartView' where the error is appearing.
Any help would be appreciated :)
As i initially created the BarChart first and then tried to add the line chart, i forgot to change the view from a BarChartView to a CombinedChartView!

iOS-charts Pie chart crashes in Xcode 7.1

I was doing a tutorial on AppCoda for iOS-charts the bar chart portion on the app worked fine but the pie chart portion crashes with the the error: fatal error: unexpectedly found nil while unwrapping a optional value. It crashes on this command pieChartView.data = pieChartData. Here is the code:
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries)
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartDataSet.label = ""
pieChartView.data = pieChartData
var colors: [UIColor] = []
colors.append(UIColor.greenColor())
colors.append(UIColor.blueColor())
colors.append(UIColor.blackColor())
pieChartDataSet.colors = colors
}
anyone have any idea why??