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

spaceMin and spaceMax are not percentages #2314

Closed
jvmk opened this issue Apr 1, 2017 · 6 comments
Closed

spaceMin and spaceMax are not percentages #2314

jvmk opened this issue Apr 1, 2017 · 6 comments

Comments

@jvmk
Copy link

jvmk commented Apr 1, 2017

Hi,

Thank you for a fantastic library!

According to the Wiki for MPAndroidChart, spaceMin and spaceMax should accept a percentage (assuming these are the iOS counterparts of setSpaceBottom and setSpaceTop). However, the provided values are simply subtracted/added from/to the calculated min/max:

    /// Calculates the minimum, maximum and range values of the YAxis with the given minimum and maximum values from the chart data.
    /// - parameter dataMin: the y-min value according to chart data
    /// - parameter dataMax: the y-max value according to chart
    open func calculate(min dataMin: Double, max dataMax: Double)
    {
        // if custom, use value as is, else use data value
        var min = _customAxisMin ? _axisMinimum : (dataMin - spaceMin) // HERE: Should subtract a percentage!
        var max = _customAxisMax ? _axisMaximum : (dataMax + spaceMax) // HERE: Should add a percentage!
        
        // temporary range (before calculations)
        let range = abs(max - min)
        
        // in case all values are equal
        if range == 0.0
        {
            max = max + 1.0
            min = min - 1.0
        }
        
        _axisMinimum = min
        _axisMaximum = max
        
        // actual range
        axisRange = abs(max - min)
    } 
@jvmk
Copy link
Author

jvmk commented Apr 1, 2017

Below can be used as a workaround for achieving the behavior specified in the docs (note that the suggested code must be re-executed whenever chart data changes):

// extend the x-axis by 5 percent (of the axis range) at its minimum and maximum points (10 percent total)
chart.xAxis.spaceMin = chart.xAxis.axisRange * 0.05 
chart.xAxis.spaceMax = chart.xAxis.axisRange * 0.05

@liuxuan30
Copy link
Member

liuxuan30 commented Apr 5, 2017

Seems you looked at different properties.

/// Extra spacing for axisMinimum to be added to automatically calculated axisMinimum
open var spaceMin: Double = 0.0
/// Extra spacing for axisMaximum to be added to automatically calculated axisMaximum
open var spaceMax: Double = 0.0

/// axis space from the largest value to the top in percent of the total axis range
open var spaceTop = CGFloat(0.1)
/// axis space from the smallest value to the bottom in percent of the total axis range
open var spaceBottom = CGFloat(0.1)

They are different

@jvmk
Copy link
Author

jvmk commented Apr 5, 2017

@liuxuan30 Aha, what tricked me is that I tried to use these properties on the xAxis. Apparently they are only defined for the yAxis (this is also the case for MPAndroidChart, so it is indeed consistent). Any idea why this is not defined for the xAxis?

@liuxuan30
Copy link
Member

good question, x axis first is an index based axis, different from y axis. Then in chart 3.0, x axis is redesigned to behave like y axis, and I guess this is left behind. However, spaceTop and bottom is definitely for y axis, if you want this, it should be spaceLeft and right?

@danielgindi what you think? Should we add spaceLeft and spaceRight for x axis?

@jvmk
Copy link
Author

jvmk commented Apr 6, 2017

@liuxuan30 thank you for the explanation! I don't know about the naming -- one could also argue that spaceBottom and spaceTop are fine names if they match with how values ascend/descend on the x-axis. As this can be reversed (afaik), the implementation would obviously be more complex as these two properties would have to take into account whether or not the x-axis is reversed.

Off-topic: I would like to rotate the labels of my ChartLimitLines (please see this Stack Overflow question). Any chance you could provide me with some hints on how to achieve this, please?

@liuxuan30
Copy link
Member

answered in SO. Just remember, it's all about calculating the anchor point.

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

2 participants