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

Is it possible to show x-Axis gridlines when there's only one ChartDataEntry? #3444

Closed
JCMcLovin opened this issue May 7, 2018 · 0 comments

Comments

@JCMcLovin
Copy link
Contributor

I'm using Charts 3.1.1 and I can't figure out how to make the chart display x-Axis gridlines when there is only one data point to display:

missing_gridlines

In this example, I want the chart to display the xAxis gridlines and labels after Apr 25, which would be Apr 26, Apr 27, Apr28, Apr 29, Apr 30, May 1 because I've set my min and max like this:

chartView.setVisibleXRange(minXRange: 7.0, maxXRange: 7.0)

The date(s) on the x-Axis come from a property of each ChartDataEntry. XAxisRenderer uses xAxis.entries to generated the gridlines in its renderGridlines(context:) method. So, in my example of just one datapoint, that means there's just one entry so I get the single xAxis gridline.

I created this class to generate enough dates to fill in the blanks when there isn't a datapoint for each subsequent day that I want to show:

class AxisValuesGenerator: NSObject {
    var days: [String]? = []
    
    func generatexAxisDates(liftEvents: [LiftEvent]) -> [String]? {
        var dates: [Date] = []
        let intervalBetweenDates: TimeInterval = 3600 * 24 // 3600 = 1 hour
        let firstDate = (liftEvents.first?.date)! // - intervalBetweenDates
        var lastDate = liftEvents.last?.date
        
        if Double(liftEvents.count) < 7 {
            let daysToAdd = intervalBetweenDates * Double(7 - liftEvents.count)
            lastDate = lastDate?.addingTimeInterval(daysToAdd)
            for _ in 0...6 {
                dates = self.intervalDates(from: firstDate, to: lastDate!, with: intervalBetweenDates)
            }
        } else {
            dates = self.intervalDates(from: firstDate, to: lastDate!, with: intervalBetweenDates)
        }
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMM d"
        
        days = dates.map {dateFormatter.string(from: $0)}
        
        return days
        
    }

func intervalDates(from startDate: Date, to endDate:Date, with interval:TimeInterval) -> [Date] {
    guard interval > 0 else { return [] }
    
    var dates:[Date] = [startDate]
    var currentDate = startDate
    
    while currentDate <= endDate {
        currentDate = currentDate.addingTimeInterval(interval)
        dates.append(currentDate)
    }
    
    return dates
}

and it does generate the dates I want:

▿ Optional<Array<String>>   ▿ some : 7 elements
   - 0 : "Apr 25"
   - 1 : "Apr 26"
   - 2 : "Apr 27"
   - 3 : "Apr 28"
   - 4 : "Apr 29"
   - 5 : "Apr 30"
   - 6 : "May 1"
   - 7 : "May 2"

but the chart won't display them or their gridlines.

How can I get those vertical x-Axis gridlines to appear?

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