Tony Tony - 1 year ago 128
Swift Question

IOS Swift Charts3.0:How to add Xvals

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


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


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])
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "test")

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

Answer Source

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!!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download