Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating LineChartView in Swift 4

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

enter image description here

So far I have achieved this :

enter image description here

I am facing three issues here :

  • Feb and Jun month text is missing in xAxis.
  • Have to add Left and right space in the chart.
  • Remove "Description Label" text visible in the chart.

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.

like image 978
Amit Avatar asked Dec 10 '25 15:12

Amit


2 Answers

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

like image 144
Ganesh Manickam Avatar answered Dec 13 '25 06:12

Ganesh Manickam


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.

like image 40
Raphael_BG Avatar answered Dec 13 '25 06:12

Raphael_BG