I'm using Charts to display a bar graph but, I cannot get the x-Axis to print out with the days of the week instead of: 0,1,2,3, etc.
Initializing chart:
var weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
func initializeChart() {
chartView.noDataText = "No Data"
chartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
chartView.xAxis.labelPosition = .bottom
chartView.chartDescription.text = "Revenue"
//chartView.xAxis.setLabelsToSkip(0)
chartView.legend.enabled = false
chartView.scaleYEnabled = false
chartView.scaleXEnabled = false
chartView.pinchZoomEnabled = false
chartView.doubleTapToZoomEnabled = false
chartView.leftAxis.axisMinimum = 0.0
chartView.leftAxis.axisMaximum = 100.00
chartView.highlighter = nil
chartView.rightAxis.enabled = false
chartView.xAxis.drawGridLinesEnabled = false
}
This is the data I'm populating:
if json != nil {
let week = json["week"]
let month = json["month"]
print("Check", week, month)
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.weekdays.count {
let day = self.weekdays[i]
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [(week[day].double)!])
dataEntries.append(dataEntry)
print("data entry", dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Weekdays")
chartDataSet.colors = ChartColorTemplates.material()
let chartData = BarChartData(dataSet: chartDataSet)
self.chartView.data = chartData
}
I just want to print the days of the week on the x-Axis. Could you help?
You need to set valueFormatter for X-axis, try this code. hope it helps!
if json != nil {
let week = json["week"]
let month = json["month"]
print("Check", week, month)
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.weekdays.count {
let day = self.weekdays[i]
let dataEntry = BarChartDataEntry(x: Double(i), yValues:
[(week[day].double)!] , data: weekdays as AnyObject?))
dataEntries.append(dataEntry)
print("data entry", dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Weekdays")
chartDataSet.colors = ChartColorTemplates.material()
let chartData = BarChartData(dataSet: chartDataSet)
let xAxisValue = chartView.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
self.chartView.data = chartData
}
extension DemoBaseViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return weekdays[Int(value)]
}
}
Related
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)
}
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))
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.
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
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.