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

Custom x-axis labels won't show without backing data #3557

Closed
bsachetta opened this issue Jul 14, 2018 · 9 comments
Closed

Custom x-axis labels won't show without backing data #3557

bsachetta opened this issue Jul 14, 2018 · 9 comments

Comments

@bsachetta
Copy link

What did you do?

I created a date-based chart that shows symptom intensity (1-10) that a user reports during his or her week.

Each day of the week is based on a number. That is, Sunday is the first day of the week so it equals 0, Monday is the second day of the week so it equals 1, and so on.

Let's say the user enters the following: a pain intensity of 10 on Tuesday, a pain intensity of 7 on Friday, and a pain intensity of 2 on Saturday. In data format, this would look like the following:

[day: 2, intensity: 10]
[day: 6, intensity: 7]
[day: 7, intensity: 2]

My goal is to get the chart to have 7 x-axis labels, one for each day of the week ("S", "M", "T", "W", "T", "F", "S" ), even for days for which there is no data present.

I created a custom axis formatter to retrieve the strings for the labels as referenced in this issue (#1340), like so:

public class CustomChartFormatter: NSObject, IAxisValueFormatter {

    var days: = ["S", "M", "T", "W", "T", "F", "S"]

    public func stringForValue(value: Double, axis: AxisBase?) -> String {
        return days[Int(value)]
    }

}

I also set the number of labels to be equal to the number of days and forced granularity to 1 like this:

self.xAxis.labelCount = 7
self.xAxis.forceLabelsEnabled = true
self.xAxis.granularity = 1

Unfortunately, this does not produce a label for each day on the chart. My hunch is that this happens because stringForValue is only ever called based on the days entered (2, 6, and 7) and not for the other days of the week.

What did you expect to happen?

I expected to see one x-axis label for each day.

What happened instead?

Instead of getting one x-axis label for each day, I got 7 labels, all corresponding to the backing data. That is, I got labels for Tuesday, Friday, and Saturday, but no other days. See screenshot below.

screen shot 2018-07-14 at 11 18 08 am

Charts Environment

Charts version/Branch/Commit Number: 3.1.1
Xcode version: 9.4.1
Swift version: 4.1
Platform(s) running Charts: iOS
macOS version running Xcode: High Sierra

Other

I have opened a Stack Overflow for this question: https://stackoverflow.com/questions/51333345/ios-charts-library-x-axis-labels-without-backing-data-not-showing

@JCMcLovin
Copy link
Contributor

That’s a really nice looking app. I have the same issue so I hope we get an answer. I upvoted your question on SO.

@bsachetta
Copy link
Author

Thanks for the input. That would be a real bummer if the library doesn't support it. I tried adding blank data points for the days that don't have data like this: ChartDataEntry(x: Double(i), y: 0) but that didn't work either since the library graphs those points rather than skips them. I reached out to some colleagues as well. Hopefully something turns up.

@JCMcLovin
Copy link
Contributor

If you find a solution and post it here I, and I’m sure many others, would be very grateful.

@bsachetta
Copy link
Author

I certainly will post if I come up with something. On a deadline with this project so I may need to move to a different library soon unfortunately though.

@liuxuan30
Copy link
Member

liuxuan30 commented Jul 20, 2018

I have added some comments on SO

@bsachetta
Copy link
Author

Thank you very much! I have given it a first pass and what you have suggested appears to be working. Once I have tested thoroughly I will post an update on S/O and close this thread.

@bsachetta
Copy link
Author

bsachetta commented Jul 20, 2018

I have updated my stack overflow answer to show what I did to get the labels working. Many thanks to Wingzero - would have never got it working without your suggestions.

https://stackoverflow.com/questions/51333345/ios-charts-library-x-axis-labels-without-backing-data-not-showing

@liuxuan30
Copy link
Member

good job!

@fanbaoyuanbees360
Copy link

fanbaoyuanbees360 commented Jan 16, 2023

I have a relatively simple processing here, just modify the axis of the demo LineChart1ViewController:

let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.axisMinimum = 0
xAxis.axisMaximum = 5
xAxis.granularityEnabled = true
xAxis.granularity = 1

You do not need to set xAxis.valueFormatter and xAxisRenderer.

ChartDataEntry:

let values = (0...1).map { (i) -> ChartDataEntry in
let val = Double(i)
return ChartDataEntry(x: Double(i), y: val, icon: #imageLiteral(resourceName: "icon"))
}

The result is shown below:
截屏2023-01-16 上午10 12 37

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

No branches or pull requests

4 participants