Put Days of The Week On Graph, Charts

Put Days of The Week On Graph, Charts



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?





Have you configured value formatter for x-axis? If not try to set it.
– Rikesh Subedi
2 days ago




1 Answer
1



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







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Help:Category

How can temperature be calculated given relative humidity and dew point?

I have a recursive function to validate tree graph and need a return condition