1

I'm using the ios-charts library and I would like to add some horizontal padding to my line charts so that the line does not start immediately at the border of the graph.

This is my current chart:

enter image description here

but I would like the blue line to have some padding as shown below. The rest should remain as it is. The reference gray lines should still take the entire width as they currently do.

enter image description here

Jan
  • 7,444
  • 9
  • 50
  • 74

1 Answers1

1

I found it. This "padding" is actually ruled by the chart.xAxis.axisMinimum and chart.xAxis.axisMaximum. Those values are automatically set to the data min x and max x.

So if I want a left padding I just have to set a chart.xAxis.axisMinimum

In my case, I want around 10% of the x values to be padded, so I calculate it as

// dates is an array of Date representing my x values
if let maxX = dates
    .map(\.timeIntervalSince1970)
    .max(),
let minX = dates
    .map(\.timeIntervalSince1970)
    .min() {

    let spanX = maxX - minX
    let padding = spanX * 0.1
    let axisMinimum = minX - padding
    
    // set the left padding
    chart.xAxis.axisMinimum = axisMinimum

}
Jan
  • 7,444
  • 9
  • 50
  • 74