Josh
Josh

Reputation: 1089

Multiple datasets with different number of points on one chart (iOS-Charts)

I am having problems with iOS Charts when I try to put two lines on the same chart with a different number of data points. I've pasted my test code below.

    testLineChartView.delegate = self
    testLineChartView.xAxis.enabled = true
    testLineChartView.xAxis.labelPosition = .Bottom
    testLineChartView.rightAxis.drawLabelsEnabled = false

    var allLineChartDataSets: [LineChartDataSet] = [LineChartDataSet]()

    var dataEntries: [ChartDataEntry] = []
    let dataPoints = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"]
    let values = [18.0, 4.0, 6.0, 3.0, 12.0, 16.0, 30]
    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
        println(dataPoints[i])
    }

    let lineChartDataSet1: LineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Temperature")
    allLineChartDataSets.append(lineChartDataSet1)


    var dataEntries2: [ChartDataEntry] = []
    let dataPoints2 = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Aug"]
    let values2 = [21.0, 5.0, 7.0, 10.0, 11.0, 18.0, 20]
    for i in 0..<dataPoints2.count {
        let dataEntry2 = ChartDataEntry(value: values2[i], xIndex: i)
        dataEntries2.append(dataEntry2)
        println(dataPoints2[i])
    }

    let lineChartDataSet2 = LineChartDataSet(yVals: dataEntries2, label: "Units Sold")
    lineChartDataSet2.setColor(UIColor.redColor())
    lineChartDataSet2.setCircleColor(UIColor.redColor())
    allLineChartDataSets.append(lineChartDataSet2)

    let allDataPoints: [String] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"]

     let lineChartData = LineChartData(xVals: allDataPoints, dataSets: allLineChartDataSets)

    testLineChartView.data = lineChartData

Here is the chart that is produced: enter image description here

As you can see, the August entry for dataset2 is showing as July. I tried to add an extra nil value for dataset2's July value, but that doesn't work. How do I get this to show correctly?

Upvotes: 2

Views: 3905

Answers (1)

Wingzero
Wingzero

Reputation: 9754

You misunderstand how it works. each xIndex will has its value, called dataEntry. You first create two series of 7 values, but at last you enlarged the xAxis. ios-charts uses xIndex to track where should it draw the value, not the label. Aug and Jul label both are at xIndex:6, in your dataSets. That's why you see they all are drawn on Jul, because it's xIndex is 6.

What you should do is first create the x values like

let allDataPoints: [String] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"] to indicate that I have 8 x values, not 7,

And then insert your values with their xIndex value, just ignore if it has no value at specific xIndex and continue the next one. Don't insert nil value, as recommended.

For example, for the data

let values2 = [21.0, 5.0, 7.0, 10.0, 11.0, 18.0, 20] (which has Aug data but no Jul data)

The xIndex should be [0,1,2,3,4,5,7] for each value, not [0,1,2,3,4,5,6] while you are creating dataEntry.

Upvotes: 4

Related Questions