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

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.

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)
}

Enable zoom on ios-charts Radar

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 = ""
}

How to add values to this iOS Bar Chart in White and 14px using Swift?

I have a bar chart which currently looks like this:
I would like to put the value of each bar at the end of the bar in white.
This is my current code:
viewDidLoad{
self.results = ["Lost", "Drawn", "Won"]
let games = [total_Losses, total_Draws, total_Wins]
self.setChart(dataPoints: self.results, values: games)
}
func setChart(dataPoints: [String], values: [Double]){
let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()
let xAxis : XAxis = self.barChartView.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
xAxis.labelFont = UIFont(name: "Avenir", size: 14.0)!
xAxis.labelTextColor = UIColor.white
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
let chartData = BarChartData(dataSets: [chartDataSet])
chartDataSet.colors = ChartColorTemplates.material()
barChartView.leftAxis.enabled = true
barChartView.legend.enabled = false
barChartView.chartDescription?.text = ""
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
}
How do I add values to the bars in this chart, in white and pixel size 14?
The answers so far have failed to work - I think the problem may lie within:
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
formato.stringForValue (Double(i), axis: xaxis) // This line has a warning: "Result of call to 'stringForValue(_:axis:)' is unused
}
This above snippet of code calls this class below:
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
var matches: [String]! = ["Lost", "Drawn", "Won"]
public func stringForValue(_ value: Double, axis: AxisBase?) -> String
{
return matches[Int(value)]
}
}
You can set the color and fonts using the BarChartData class object. For your case, it is chartData
Follow below code in order to set the required fonts and color of labels.
chartData.setValueFont(UIFont.systemFont(ofSize: 14.0))
chartData.setValueTextColor(UIColor.white)
In case you want to show the label within the bar then set your BarChartView object property drawValueAboveBarEnabled to false. It will render your labels within the bars.
barChartView.drawValueAboveBarEnabled = false

How to add labels for XAxis for BarChartView in ios-charts

I added a bar chart to the storyboard, but I cannot properly set labels for my data entries.
here is my code:
var names = ["aaa", "bbb", "ccc", "ddd"]
var values = [230.0, 280.0, 450.0, 340.0]
setChart(dataPoints: names, values: values)
setChart function:
func setChart(dataPoints: [String], values: [Double])
{
let formatter = BarChartFormatter()
formatter.setValues(values: dataPoints)
let xaxis:XAxis = XAxis()
barChartView.noDataText = "You need to provide data for the chart."
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: "موجودی")
let chartData = BarChartData(dataSet: chartDataSet)
xaxis.valueFormatter = formatter
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = false
barChartView.data = chartData
}
and finally the formatter:
#objc(BarChartFormatter)
public class BarChartFormatter: 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
}
}
but it didn't work well as shown below:
as shown here, it add 6 labels instead 4 labels, and it also has duplicates.
I already read this solution, however, as you can see, it has some issues yet.
How can I solve this problem?
I think you can try to set the following properties:
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
The source code tells you a lot about the properties above. https://github.com/danielgindi/Charts/blob/master/Source/Charts/Components/AxisBase.swift

iOS Charts -> Last value doesn't appear

I have some troubles with the library, when I create a barChart I have to add an extra value to my values array in order to show all my original values, and in the lineChart the last value appears in the line graph but it doesn't have its value above the point like the others.
I let you my code and some pictures showing the problem:
BarChart: My xAxis have a custom format showing months.
override func viewDidLoad() {
super.viewDidLoad()
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]
max = unitsSold.maxElement()
setChart(unitsSold)
}
func setChart(values: [Double]) {
let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()
barChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = BarChartDataEntry(x: Double(i), y:values[i])
dataEntries.append(dataEntry)
formato.stringForValue(Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.colors = ChartColorTemplates.joyful()
barChartView.fitBars = true //Para que las barras inicial y final se muestren completas
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.xAxis.labelPosition = .Bottom
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.axisMaximum = max + 2
barChartView.rightAxis.enabled = false
barChartView.drawValueAboveBarEnabled = false
barChartView.descriptionText = ""
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
}
LineChart: My xAxis have a custom format showing months.
override func viewDidLoad() {
super.viewDidLoad()
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
max = unitsSold.maxElement()
setChart(unitsSold)
}
func setChart(values: [Double]) {
let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()
var dataEntries: [ChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
formato.stringForValue(Double(i), axis: xaxis)
}
xaxis.valueFormatter = formato
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
lineChartDataSet.colors = ChartColorTemplates.joyful()
lineChartDataSet.circleColors = ChartColorTemplates.joyful()
lineChartDataSet.drawCircleHoleEnabled = false
let lineChartData = LineChartData( dataSet: lineChartDataSet)
lineChartData.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 7))
lineChartView.fitScreen()
lineChartView.xAxis.labelPosition = .Bottom
lineChartView.xAxis.axisMaximum = Double(values.count)
lineChartView.leftAxis.axisMinimum = 0
lineChartView.leftAxis.axisMaximum = max + 3
lineChartView.rightAxis.enabled = false
lineChartView.descriptionText = ""
lineChartView.xAxis.drawGridLinesEnabled = false
lineChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
lineChartView.xAxis.valueFormatter = xaxis.valueFormatter
lineChartView.data = lineChartData
}
The responses are: For barChart December doesn't appear and in lineChart the last value doesn't show its value above it, I hope someone can help.

Resources