-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
How to Modify X Axis values Swift? #1340
Comments
I modify x-axis value in scatter chart and it is working now. 1: let dataEntry = BarChartDataEntry(x: Double(i+2), y:values[i], data: months ) 2: let chartData = BarChartData() -> let chartData = BarChartData(dataset) Hope it can help you. |
Thank you NyoSeint, you're right with your method you can modify x-axis but just in a numerical way, i'm trying to put Strings in this case months in x-axis |
have you checked the ChartsDemo? I believe there are answers. |
I am also looking for an answer to this, opened a case earlier here - #1331 Couldnt find an answer in the demos, please let me know if you find a solution. thanks |
I found the solution, maybe another one can solve this problem in a better way, without create a new class, well this is what I found: First you nedd to create a new class, which will be your formatter class and add the IAxisValueFormater interface, then you use the method stringForValue to customize the new values, it takes two parameters, the first is the actual value (you can see it like the index) and second is the axis where the value is.
} Now in your file at the begining of your setChart() function you have to create two variables one for your formatter class and one for the axis class:
Next, go ahead at the end of the for in loop and pass the index and axis to your formatter variable:
After looping add the format to your axis variable:
The final step is to add the new xaxisformatted to the barChartView:
And that's all the other lines in the setChart() function just keep it where they are, I hope it can help you. |
@ajimenezjulio This is the indeed correct way :-) We are using formatters now, due to independent x-values. This is of course less comfortable for those who used to have simplistic x-indexes array, but opens a whole world of possibilities. This removed the constraint of having to predefined the x-indexes with their labels. I'm sure we'll come up with new ways of writing less code for simplistic situations like this, in the future. |
Please give us back an easy way to customize the xaxis. Thx. |
Any update ? |
@dante20007 Sorry, you can't have it all. People want to be able to add entries at any x-index, not just 0, 1, 2, but also 0, 1.3, 2.7... An array can't represent labels for such values. |
What if we want to have months displayed on the X axis ? not possible too ? Thx. |
Hi I try to add this solution but when I move to the right it said index out of range, the I know I have just 12 month |
I also need an easy way to customize the x-axis.. I went back to the previous version of charts because of this. |
This is a easy way to customize my problem was that when I move to the right start to render again so I modify the lib and put to ignore if the index when render |
Sorry I was talking about "How to Modify X Axis values Swift?" using Strings |
Yes me too. I used same answer of @ajimenezjulio |
I am maintaining a project that is in Objective-C, and although I managed to reproduce the answer of @ajimenezjulio it would not compile: for some reason the IAxisValueFormatter was never recognized. |
I had a similar problem. Documentation is too short, but working with x Axis require some lines of code. Using the code provided by @ajimenezjulio I have made class extension for simplifying the use BarChartView. Maybe it will be useful for someone.
Usage
|
Hey, can have other charts extension too? |
I'm getting an error " BarChartFormatter does not conform to protocol IAxisValueFormatter. Why did you do this? It was so easy to use in Android Studio. Does ANYONE know how to get an array ["J", "F", etc... into the X axis? I'm screwed here. These "examples are above my skill level. |
Is there a solution for ObjC to format the X Axis with custom labels ? |
in YourViewControllerViewController declare var months: [String] = String and set values of month of you needed |
Hi, |
Yeah, I wish I understood more how func stringForValue(_ value: Double, axis: AxisBase?) -> String {
let index = Int(value)
if index < labels.count && index >= 0 {
return labels[index]
} else {
return ""
}
} |
Open button |
@robotsquidward - Me too. It would be nice if the documentation explained how it worked or, better yet, one of the demos showed how to do it. I imagine there are a lot of people who need to use strings for labels but there's no clear explanation or example that I can find that would help. I just see a bunch of answers that say |
@AlexSmet dataContent1 = ["MON", "TUE", "WED", "THU", "FRI"] |
@benzai |
@benzai I found I had to tweak @AlexSmet's approach slightly and set
When |
The documentation markup says 'if true, the set number of y-labels will be forced' implying that this is specifically for the y-labels but it sounds like it applies to both x and y-axis labels, correct? |
Can I set String label in left axis? |
To LineChart :
|
How are you today. |
HI @danielgindi : Any New updates on this labels display. short code |
With the new release i had some troubles to create some graphs, the previous code was:
You can pass the values for example an array of months using the line:
After the new release the code to implement the same graph is:
I was trying a few hours but i couldn't find a way to modify the X axis values, i hope someone can help me, thanks!!
The text was updated successfully, but these errors were encountered: