I am using the iOS-charts library to integrate LineChartView and I have to create below chart :

So far I have achieved this :

I am facing three issues here :
xAxis.Is it possible to add small mark above months as shown in the image for indicating months in xAxis ?
Implemented code :
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [50.0, 25.0, 50.0, 75.0, 100.0, 75.0]
setChart(dataPoints: months, values: unitsSold)
and the setChart method:
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: nil)
chartDataSet.circleRadius = 5
chartDataSet.circleHoleRadius = 2
chartDataSet.drawValuesEnabled = false
let chartData = LineChartData(dataSets: [chartDataSet])
lineChartView.data = chartData
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.drawGridLinesEnabled = false
lineChartView.xAxis.avoidFirstLastClippingEnabled = true
lineChartView.rightAxis.drawAxisLineEnabled = false
lineChartView.rightAxis.drawLabelsEnabled = false
lineChartView.leftAxis.drawAxisLineEnabled = false
lineChartView.pinchZoomEnabled = false
lineChartView.doubleTapToZoomEnabled = false
lineChartView.legend.enabled = false
}
Please help if anyone has worked on charts in swift.
1) To avoid crowd Chart shows limited data in graph axis by zooming user can see the entire data
2) For left and Right spacing: Give left and right constraints from interface builder
3) To hide description you can use below code
lineChartView.chartDescription?.text = " "
Hope this help you
I had the same problem. I did this to solve it:
ineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
ineChartView.xAxis.setLabelCount(months.count, force: true)
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With