Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charts 3.0: Custom xAxis labels with gaps in data? #1584

Closed
ryanschneider opened this issue Sep 29, 2016 · 1 comment
Closed

Charts 3.0: Custom xAxis labels with gaps in data? #1584

ryanschneider opened this issue Sep 29, 2016 · 1 comment

Comments

@ryanschneider
Copy link

I'm using the technique described here: #1496 to provide custom xAxis data.

In my case, I'm showing a fixed "weekly" view so always want the axis values to be the last seven days, e.g. ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] but I don't always have data for every day.

Previously this worked ok, since the xAxis and ChartDataEntrys were divorced, but I'm having trouble doing this now.

One technique which comes close is to add ChartDataEntry's with a nan y value, like this contrived example:

var xAxisValues: [String] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let i = Int(value)
    return xAxisValues[i]
}

func fillChart(data: [Double?]) {
    //data is a length-7 array, entry 0 is Monday, 1 Tuesday, etc.  nils mean no data for that day
   var values=  [ChartDataEntry]()
    for (x, y) in data.enumerated() {
        if let y = y {
           values.append(ChartDataEntry(x: Double(i), y: y))
        }
        else {
            // set y to nan
            values.append(ChartDataEntry(x: Double(i), y: Double.nan))
        }
    }

    let dataSet = LineChartDataSet(values: values, label: "Data")
    //... etc
}

This appears to work ok, but gives a lot of runtime errors in CoreGraphics, because the nan values aren't filtered out in, for example, LineChartRenderer.drawHorizontalBezier(context:dataSet).

So, my question is, is this a viable route to go down, in which case the nan values need to be filtered somewhere (e.g. in ChartDataSet.entryForIndex()), or is there a better way?

If you think using nan like this is the correct approach, I can help with the code updates to remove the CoreGraphics runtime warnings, but didn't want to go down that route if there's a better way.

I feel like chasing down the corner cases with nan values might be a rabbit-hole though. I think at a minimum, all the functions in ChartDataSet that reference _values would need to change in some way.

I can probably set up an example project to demonstrate what I'm doing if that'll help.

@ryanschneider
Copy link
Author

Ok, it looks like adding the nan values is unnecessary. What I had to do was force the min and max of the xAxis like so:

        sevenDayChart.xAxis.axisMinimum = 0.0
        sevenDayChart.xAxis.axisMaximum = 6.0
        sevenDayChart.xAxis.valueFormatter = self

This seems to work as-is w/ 3.0 master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant