2

I am generating a multiple line chart using ios-charts library. (danielgindi/Charts). I am creating an array to for the x-axsis which is retrieved from Core Data and if there is no value for x week, i appended a 0.

I have an example array of (for the x values):

[[0.0,0.0,5.6,3.0,6.0,5.4,2.1,0.0,0.0,1.3,4.1,0.0],[5.2,0.0,3.2,7.0,8.9,0.0,5.0,0.0,4.6,0.0,0.0,0.0],[3.6,1.5,3.6,0.0,0.0,3.5,0.0,4.6,5.6,2.8,8.4,0.0]]

and for the y:

[Jan,Feb,March,April,May,June,July,August,Sept,Oct,Nov,Dec]

I have the following code to display these 3 lines within a line chart:

func setChart(xValues: [NSDate], valuesLineChart: [[Double]]) {
    chartView.descriptionText = ""
    chartView.noDataText = "You need to provide data for the chart."

    var dataSets : [LineChartDataSet] = [LineChartDataSet]()
    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
    }

    var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals2.append(ChartDataEntry(value: valuesLineChart[1][i], xIndex: i))
    }

    var yVals3 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals3.append(ChartDataEntry(value: valuesLineChart[2][i], xIndex: i))
    }

    let set1 = LineChartDataSet(yVals: yVals1, label: nil)
    let set2 = LineChartDataSet(yVals: yVals2, label: nil)
    let set3 = LineChartDataSet(yVals: yVals3, label: nil)

    set1.fillColor = UniversalStatic.data.colourArrayForGraph[0]
    set2.fillColor = UniversalStatic.data.colourArrayForGraph[1]
    set3.fillColor = UniversalStatic.data.colourArrayForGraph[2]

    dataSets.append(set1)
    dataSets.append(set2)
    dataSets.append(set3)

    set1.setColor(UniversalStatic.data.colourArrayForGraph[0])
    set2.setColor(UniversalStatic.data.colourArrayForGraph[1])
    set3.setColor(UniversalStatic.data.colourArrayForGraph[2])

    set1.lineWidth = 2
    set2.lineWidth = 2
    set3.lineWidth = 2

    set1.setCircleColor(UniversalStatic.data.colourArrayForGraph[0])
    set2.setCircleColor(UniversalStatic.data.colourArrayForGraph[1])
    set3.setCircleColor(UniversalStatic.data.colourArrayForGraph[2])

    set1.drawCircleHoleEnabled = false
    set2.drawCircleHoleEnabled = false
    set3.drawCircleHoleEnabled = false

    set1.circleRadius = 5.0
    set2.circleRadius = 5.0
    set3.circleRadius = 5.0

    let data: CombinedChartData = CombinedChartData(xVals: xValues)
    data.lineData = LineChartData(xVals: xValues, dataSets: dataSets)

    chartView.data = data

}

How can I avoid plotting the 0 values?

Thanks for any help

Jess Murray
  • 1,273
  • 1
  • 10
  • 32

2 Answers2

1

Are you looking to skip the 0.0 values entirely? You can check before appending them to the array.

Perhaps you can do:

  var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
  for i in 0..<xValues.count {
      if valuesLineChart[0][i] != 0.0 {
          yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
      }
  }

And similarly with the other for loops

Shen
  • 644
  • 5
  • 16
  • Yes i am looking to skip it entirely. I was considering this however I didn't think this would work. E.g [0.0,0.0,5.6] & [Jan,Feb,March], if i skipped the first 2, wouldn't it just assign the 5.6 to Jan? – Jess Murray Apr 06 '16 at 16:04
  • Okay, I see what you want now... You want line breaks within the chart. I believe there were developers in the past that requested this feature for the library but as far as I can tell you would need to isolate each new line after a line break and implement as a new dataset... It's getting complex to say the least because of issues with how the bezier path was drawn. I'm not sure if it's possible right now. – Shen Apr 06 '16 at 18:07
  • Ah ok. I don't really want line breaks, just no circle displayed on the 0.0 values and skips past it, almost like changing the 0's for nil. – Jess Murray Apr 06 '16 at 18:43
  • 1
    You should probably use `map` instead of `for-in`. – Sulthan Apr 09 '16 at 14:57
  • What If I don't want to draw any line over there? Because skipping 0 will directly map last value to next value via a straight line. – Rajneesh071 Nov 17 '21 at 16:27
1

The only way you can do that is overriding the default behavior.

It's rather simple actually, create a subclass of LineChartRenderer and override drawLinear (lines), drawValues (value rendering) and drawExtras (circles).

Just copy the implementation of the superclass methods (LineChartRenderer) there and add the required condition to skip zero values.

Then assign the custom renderer to your chart view.

Another option that could probably work without overriding the renderer is to provide a color for every value. Then you can just set clear color for zero values.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Thanks, I am new to programming in general but will give this a try. With setting a colour for every value, wouldn't the line attached the the 0, still be displayed? Ideally, for the 0 values the value before and after it are connected with a line. – Jess Murray Apr 09 '16 at 15:19
  • would the second option work? As I am very new to programming the first option seems a little tricky as i wouldn't really know where to start. – Jess Murray Apr 10 '16 at 21:12
  • @JessMurray The second is just an idea and I am not sure it would work correctly for your use case. The first one is safe but it is more work. – Sulthan Apr 10 '16 at 22:05