Tony Tony - 17 days ago 5
Swift Question

IOS Swift Charts3.0:How to add Xvals

In Charts2.3 We can use the follow code to add data.

code:

let chartDataSet = LineChartDataSet(yVals: dataEntries, label: "Units Sold")
let chartData = LineChartData(xVals: months, dataSet: chartDataSet)


But in Charts3.0, I can't found the parameter to add the months to my linechart

code:

var months: [String]!
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
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]
setChart(dataPoints: months, values: unitsSold)

func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "test")

let lineChartData = LineChartData()
lineChartData.addDataSet(lineChartDataSet)
self.lineChartView.chartDescription?.text = "TestLinechart"
}

Answer

For the below function you can set the value for the linechartdata

  lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)

  lineChartView.xAxis.granularity = 1.0

Hope that this will help you!!!!