iosswiftchartslinechartios-charts

iOS Charts library and LineChart xAxis


I'm trying to create a LineChartView with values representing expenses for the current quarter. I need to print months and days with a customs number of labels for the xAxis. Unfortunately, the LineChart is generating a set of entries that don't suit my needs and I cannot change them. I need the grid lines at my custom positions that are the first day of the month and a set of days for the month and I want them at specific positions not where the LineChart wants. I've checked the code and seen that the entry values for the xAxis are calculated in XAxisRenderer.computeAxisValues and it looks like I have no way to specify their number and positions. The number of labels that I set it is not taken into account, and I do not have time to customize it.


Solution

  • There are 2 ways might achieve what you needed:

    1. Set the number of labels for x-axis, for example,

      let xAxis = chartView.xAxis
      xAxis.labelCount = 7
      //xAxis.drawLabelsEnabled = true
      

      Through this, exactly 7 grid lines as will be drawn, if you set drawLabelsEnabled as false, the min and max lines will be added in additional to those 7

    2. Use limit lines

      You can disable the drawing of grid lines, and use limit lines instead. In this way, you can draw line for whatever values you need, for example,

      let limitLine = ChartLimitLine(limit: xValue, label:
      labelForThisLine)
      limitLine.lineWidth = 0.5
      limitLine.lineColor = .black
      limitLine.valueTextColor = .black
      chartView.xAxis.addLimitLine(limitLine)