3

I am creating a lineChart using ios-charts API in Swift language. On the xAxis there are dates in mm.dd-day format, for example 03.02-Wednesday. On the yAxis there are double values.I want to show on xAxis every monday and nothing else. The best solution what I found so far is lineChart.xAxis.setLabelsToSkip(6)

This skip 6 label but it is working just if the chart datas start with a date which is Monday. Like here: what I want

But if my chart values start with Tuesday, I will see just Tuesday labels.: what is not good

I had many ideas what should I do, but I couldn't find solution for it. I am wondering if there is a function, what can set every label on xAxis one-by-one, so i can iter with a for loop on xAxis.values and set label visible if it's Monday and skip if it's not Monday. Something like this:

pseudo:
for i in lineChart.xAxis.values{
  if isMonday(){
    //set label visible
  }else{
    //set label invisible
}}

But I will really thanksful for every other solution for my problem. I have already tried to use this, but it didn't change label:

lineChartView.xAxis.valueFormatter?.stringForXValue(i, original: "Monday", viewPortHandler: cell.lineChartView.viewPortHandler)

I've tried to use setValueForKey, but I don't have keys, just indexes, and I thinking about to start setLabelsToSkip from an exact x value, but this function used for the whole chart.

Thank you very much for your help.

faklyasgy
  • 826
  • 1
  • 6
  • 10

1 Answers1

1

your chart starts from Tuesday, and it shows Tuesday, which is by design, because it's first value. If you want always to display Monday, first, call lineChart.xAxis.setLabelsToSkip(1), this affects xAxis.axisLabelModulus, which we must assure we can check every x value to see if it's monday.

then, you can override x axis rendrer's drawLabels() like:

public override func drawLabels(context context: CGContext, pos: CGFloat, anchor: CGPoint)
{
    ... // same as super
    for i in self.minX.stride(to: min(self.maxX + 1, xAxis.values.count), by: xAxis.axisLabelModulus)
    {
       let label = xAxis.values[i]
       if (!isMonday(label)) // implement isMonday() yourself
       {
           continue
       }
    ... // same as super
}
Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • Thank you again for your great answer, it is working. Anyway I would like to correct you, because if I setLabelsToSkip to 1, than i will see every second monday, so to see every monday I had to setLabelsToSkip(0) – faklyasgy Mar 03 '16 at 14:16
  • it's up to you for the logic:) – Wingzero Mar 03 '16 at 14:18