Enable zoom on ios-charts Radar - ios

In swift 3 I did this radarChart
But I didn't find how to enable zoom on it and I didn't find if this is even possible.
Here is the code that generated it :
func Radar() {
radarVw.xAxis.valueFormatter = self
var dataEntries: [RadarChartDataEntry] = []
for i in 0..<(useri?.skills!)!.count {
let dataEntry = RadarChartDataEntry(value: (useri?.skills!)![i]["level"].double!)
dataEntries.append(dataEntry)
skillsName.append((useri?.skills!)![i]["name"].string!)
}
let chartDataSet = RadarChartDataSet(values: dataEntries, label: "skills")
let chartData = RadarChartData(dataSet: chartDataSet)
radarVw.xAxis.labelHeight = 0.5
radarVw.xAxis.labelWidth = 0.5
radarVw.legend.enabled = false
radarVw.xAxis.wordWrapEnabled = true
radarVw.data = chartData
radarVw.chartDescription?.text = ""
}

Related

Not to start xAxis at zero in LineChart iOS Swift

I have created lineChartView. Below xAxis values are displayed hours with 1 hour time interval. My graph starts at 0 and I am not able to show proper xAxis label value.
I want to put extra space in xAxis starting so that the xAxis label can proper visible.
Here is my code:
func setChart(yValues: [Double], xValues: [String]) {
chartView.noDataText = "You need to provide data for the chart."
var dataEntries: [ChartDataEntry] = []
for i in 0..<yValues.count {
let dataEntry = ChartDataEntry(x: Double(i), y: yValues[i])
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(entries: dataEntries, label: "")
chartDataSet.colors = [.black]
chartDataSet.drawFilledEnabled = true
let chartData = LineChartData(dataSet: chartDataSet)
let chartFormatter = LineChartFormatter(labels: xValues)
chartView.xAxis.valueFormatter = chartFormatter
chartView.xAxis.labelCount = dataEntries.count
chartView.data = chartData
chartView.xAxis.granularity = 1.0
chartView.xAxis.granularityEnabled = true
chartView.xAxis.centerAxisLabelsEnabled = false
chartView.xAxis.axisMinimum = 0
chartView.xAxis.axisMaximum = Double(dataEntries.count)
chartView.setVisibleXRange(minXRange: 1.0, maxXRange: 3.0)
}

How to set BarChartDataEntry labels (String not Double) not xaxis labels

Hi all i need help i want to format BarChartDataEntry labels as shown in image.
My Code
var dataEntries: [BarChartDataEntry] = []
var dataEntriesBottom: [BarChartDataEntry] = []
for i in 0..<goals.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(goals[i]))
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
chartDataSet.colors = [.systemGreen]
let chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.4
chartView.xAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawAxisLineEnabled = false
chartView.xAxis.drawGridLinesEnabled = false
chartView.rightAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawLabelsEnabled = false
chartView.rightAxis.drawLabelsEnabled = false
chartView.leftAxis.drawGridLinesEnabled = false
chartView.rightAxis.drawGridLinesEnabled = false
chartView.xAxis.enabled = false
chartView.xAxis.drawLabelsEnabled = false
chartView.xAxis.granularity = 0
self.chartView.animate(yAxisDuration: 1.0)
self.chartView.legend.enabled = false
chartView.data = chartData
try something like this:
let myFormatter = NumberFormatter()
myFormatter.numberStyle = .decimal
myFormatter.maximumFractionDigits = 0
chartData.setValueFormatter(DefaultValueFormatter(formatter: myFormatter))

How to add string labels( xAxis labels) to horizontal bar in Charts framework

I am using charts framework for drawing charts. I need to add some strings in left of every bars. In my code always there are two bars, one for Income and one for Expense. I want to show these string aside of every bar.
Below you can see my codes:
let ys1 = [model.totalIncome, abs(model.totalExpense)]
var yse1 = ys1.enumerated().map { x, y -> BarChartDataEntry in
return BarChartDataEntry(x: Double(x), y: y)
}
let count = 2
let data = BarChartData()
let ds1 = BarChartDataSet(values: yse1, label: "")
ds1.colors = [UIColor.red, UIColor.blue]
// Number formatting of Values
ds1.valueFormatter = YAxisValueFormatter()
ds1.valueFont = UIFont(fontType: .H6)!
ds1.valueTextColor = UIColor.dark_text
// Data set
data.addDataSet(ds1)
horizontalBarChartView.data = data
// General setting
horizontalBarChartView.xAxis.drawLabelsEnabled = false
horizontalBarChartView.setScaleEnabled(false)
// Grid
horizontalBarChartView.xAxis.drawGridLinesEnabled = false
horizontalBarChartView.leftAxis.gridLineDashLengths = [15.0, 15.0]
horizontalBarChartView.leftAxis.gridColor = UIColor.palette_28
horizontalBarChartView.rightAxis.drawGridLinesEnabled = false
// Disable number formatting of leftAxis and rightAxis
horizontalBarChartView.leftAxis.enabled = false
horizontalBarChartView.rightAxis.enabled = false
horizontalBarChartView.xAxis.valueFormatter =
IndexAxisValueFormatter(values: ["Income","Expense"])
horizontalBarChartView.xAxis.granularityEnabled = true
horizontalBarChartView.xAxis.granularity = 1
// setViewPortOffsets
let digitCount = Int(data.yMax).digitCount
let leftOffcet: CGFloat = digitCount > 2 ? CGFloat((digitCount - 1) * 10) : 10.0
horizontalBarChartView.setViewPortOffsets(left: leftOffcet, top: 0, right: 0, bottom: 50)
horizontalBarChartView.leftAxis.axisMinimum = 0
And my view:
lazy var horizontalBarChartView: HorizontalBarChartView = {
let chartView = HorizontalBarChartView()
chartView.contentMode = .scaleAspectFit
chartView.drawBordersEnabled = false
chartView.drawGridBackgroundEnabled = false
chartView.gridBackgroundColor = UIColor.clear
chartView.legend.enabled = false
chartView.chartDescription = nil
chartView.highlighter = nil
chartView.clipsToBounds = true
chartView.translatesAutoresizingMaskIntoConstraints = false
return chartView
}()
I guess below code should add these labels, however, it is not working
horizontalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Income","Expense"])
horizontalBarChartView.xAxis.granularityEnabled = true
horizontalBarChartView.xAxis.granularity = 1
Update
See orange rectangle. I need these labels.
I am using an extension for this
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
}
}
you can use this in code like this
chartView.setBarChartData(xValues: xEntries, yValues: yEntries, label: "Income")
where,
//You need to add values dynamically
var yEntries: [Double] = [1000, 200, 500]
var xEntries: [String] = ["Income1", "Income2", "Income3"]
Hope this works.
Charts framework is a great tool for drawing charts. You should study it perfectly. I used an old code in our project (copy/paste), so it caused some problems for drawing xAxis labels in my chart.
Just comment below line:
horizontalBarChartView.xAxis.drawLabelsEnabled = false
When you create your BarChartDataSet you can pass the label.
let data = BarChartData()
let ds1 = BarChartDataSet(values: yse1, label: "Income")
ds1.colors = [UIColor.red, UIColor.blue]
Hope this helps.
self.ChartObj.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in
print(index)
return YourArray.object(at: Int(index)) as! String
})
self.ChartObj.xAxis.setLabelCount(YourArray.count, force: true)
Try this code

How to set correct labels is iOS Charts bar chart and colour columns to suit?

I have an iOS Chart, as per the image below, and the labels are showing incorrectly (or not what I want).
As you can see they both say 'Games Played' and are the same colour, as is each bar in my graph.
How can I change the colour of each bar and show the correct respective labels, 'Won', 'Drawn', 'Lost'?
This is my code:
override func viewDidLoad() {
super.viewDidLoad()
let results = ["Won", "Drawn", "Lost"]
let games = [totalWins, totalDraws, totalLosses]
setChart(dataPoints: results, values: games)
}
func setChart(dataPoints: [String], values: [Double]){
let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()
barChartView.noDataText = "you need to provide some data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
formato.stringForValue (Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
let chartData = BarChartData(dataSets: [chartDataSet])
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
barChartView.leftAxis.forceLabelsEnabled = true
barChartView.rightAxis.forceLabelsEnabled = true
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.rightAxis.enabled = false
barChartView.drawGridBackgroundEnabled = false
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
}
Simply add chartDataSet.colors = ChartColorTemplates.material() or you can add your array of UIColor in order to customise according to your need.

How to use xAxis with Label in Charts-frameworks

As title,After I check How to add Strings on X Axis in iOS-charts? this post. I still don't know where can I put "formato.stringForValue(Double(i), axis: taxis)" in for-in loop, as I know, when I implement this protocol ,this function will send out a "String".My question is how does it connect to my xAxis data??
Following is my code ,please refer to it.
//setChartData
func setChart(dataPoints:[String],values:[Double]){
self.barChartView.noDataText = "You need to provide data for the chart."
let xaxis = XAxis()
let fomatter = BarChartFomatter()
fomatter.setValues(values: dataPoints)
var dataEntries:[BarChartDataEntry] = []
for i in 0..<dataPoints.count{
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
*fomatter.stringForValue(Double(i), axis: xaxis)*
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let data = BarChartData(dataSet: chartDataSet)
xaxis.valueFormatter = fomatter
self.barChartView.backgroundColor = UIColor.clear
self.barChartView.xAxis.drawLabelsEnabled = false
self.barChartView.chartDescription?.enabled = false
self.barChartView.legend.enabled = false
self.barChartView.rightAxis.enabled = false
self.barChartView.xAxis.valueFormatter = xaxis.valueFormatter
self.barChartView.xAxis.labelPosition = .bottom
//self.barChartView.description = "Test String"
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1.0 //default granularity is 1.0, but it is better to be explicit
barChartView.xAxis.decimals = 0
self.barChartView.data = data
}
And my BarChartFomatter as follow:
import UIKit
#objc(BarChartFomatter)
open class BarChartFomatter: NSObject,IAxisValueFormatter {
var names = [String]()
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return names[Int(value)]
}
public func setValues(values:[String]){
self.names = values
}
}
Result:
There is no text under the chart
Please help me , thanks

Resources