xAxis dates not showing correctly
- Dominant language
- Swift
- Stars
- 28k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
I'm creating an exercise app and i'm creating a line chart to represent data on a users weight so they can track their progress over time. I'm feeding my chart data from an array of "weightStatistics" which is a CoreData Object which has 2 attributes one representing the weight (obviously) and the other a date which represents when the weight was recorded. The idea is to have the xAxis show the date in a "dd/MM/yy" date format and just show the values for the 'weight' on the relevant dates on the chart itself. This is my approach...
`
@IBOutlet weak var lineChartView: LineChartView!
weak var axisFormatDelegate: IAxisValueFormatter?
var weightStats = [WeightStatistics]()
override func viewDidLoad() {
axisFormatDelegate = self
lineChartView.delegate = self
setupLineChart()
}
func setupLineChart() {
lineChartView.animate(xAxisDuration: 0.7, yAxisDuration: 0.7)
lineChartView.dragEnabled = true
lineChartView.legend.form = .none
lineChartView.drawGridBackgroundEnabled = false
var dataEntries: [ChartDataEntry] = []
for stat in weightStats {
let timeIntervalForDate: TimeInterval = stat.date!.timeIntervalSince1970
let dataEntry = ChartDataEntry(x: Double(timeIntervalForDate), y: Double(stat.weight))
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
chartDataSet.drawValuesEnabled = false
let chartData = LineChartData(dataSet: chartDataSet)
lineChartView.data = chartData
let xaxis = lineChartView.xAxis
xaxis.valueFormatter = axisFormatDelegate
xaxis.labelCount = dataEntries.count
xaxis.granularityEnabled = true
xaxis.granularity = 1.0
xaxis.centerAxisLabelsEnabled = true
xaxis.avoidFirstLastClippingEnabled = true
xaxis.drawLimitLinesBehindDataEnabled = true
let rightAxis = lineChartView.rightAxis
rightAxis.enabled = false
let leftAxis = lineChartView.leftAxis
leftAxis.drawZeroLineEnabled = false
leftAxis.drawGridLinesEnabled = true
extension ViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yy"
let date = Date(timeIntervalSince1970: value)
return dateFormatter.string(from: date)
}
}
`
I'm formatting the xAxis to show dates relevant to the data i'm representing in my line chart, the dates however aren't updating correctly as it's only showing dates which are related to last element from my data source.

I've first label should be 01/03/17, i've printed the dates to the console and it shows "01/03/17, 02/03/17".
I've tried searching around but haven't found anything. Any help would be appreciated, thanks!
Contributor guide
Assessment
This issue has not been assessed yet.