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

setVisibleXRangeMaximum is behaving unexpectedly #3678

Closed
1 task done
tundsD53 opened this issue Oct 3, 2018 · 2 comments
Closed
1 task done

setVisibleXRangeMaximum is behaving unexpectedly #3678

tundsD53 opened this issue Oct 3, 2018 · 2 comments

Comments

@tundsD53
Copy link

tundsD53 commented Oct 3, 2018

What did you do?

We're currently using the LineChart class with two datasets on the graph, one dataset is using the default linear mode and the other is using the stepped mode. When configuring both LineChartDataSets, we are using a value for the y axis which is pretty standard. The issue arises when we are trying to set the xAxis of the graph.

On the xAxis we need to display the dates for each point, in order to do this we are using the current date in milliseconds and using this for the x value i.e. 1515801600000.0. So that when we want to format the xAxis we can just retrieve the value within the formatter and parse it into a date string using the DateFormatter class.

This is all good up until we want to use setVisibleXRangeMaximum to limit the graph to only show 12 values. In order to try and accomplish this we are using the following piece of code after setting the line chart data.

if let data = data?.getDataSetByIndex(0) as? LineChartDataSet {
   lineChartView?.setVisibleXRangeMaximum(data.entryForIndex(11)!.x)
}

This doesn't zoom into the current 12 values on the graph instead it just shows all 12 values. Initially I was passing 12 into setVisibleXRangeMaximum which is of course the wrong implementation since it was only zooming into 12 on the xAxis.

I've used the example project to compare this behaviour and have realised that it works when using small numbers e.g. 0 - 10, 100 - 1000 etc. If I was to set the visible x range to 5 or 50, the graph will zoom into these values. But when it comes to handling larger numbers like dates in milliseconds it doesn't handle it very well.

What did you expect to happen?

I expected the first 12 values to be visible on the graph and the ability to scroll through the graph to see other values.

What happened instead?

Instead the whole graph was visible and wasn't zoomed in.

@mikerz
Copy link

mikerz commented Oct 5, 2018

The visible range is the span of x axis values that you want to see. If you're setting the span of all values to the highest value, then you will include all values.

What you need to do is calculate the different between data.entryForIndex(11) and data.entryForIndex(0) and use that as your range.

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 24, 2018

/// If you call this method, chart must have data or it has no effect.

make sure you read the doc before trying.

    /// Sets the size of the area (range on the x-axis) that should be maximum visible at once (no further zooming out allowed).
    ///
    /// If this is e.g. set to 10, no more than a range of 10 values on the x-axis can be viewed at once without scrolling.
    ///
    /// If you call this method, chart must have data or it has no effect.
    @objc open func setVisibleXRangeMaximum(_ maxXRange: Double)
    {
        let xScale = _xAxis.axisRange / maxXRange
        _viewPortHandler.setMinimumScaleX(CGFloat(xScale))
    }
    
    /// Sets the size of the area (range on the x-axis) that should be minimum visible at once (no further zooming in allowed).
    ///
    /// If this is e.g. set to 10, no less than a range of 10 values on the x-axis can be viewed at once without scrolling.
    ///
    /// If you call this method, chart must have data or it has no effect.
    @objc open func setVisibleXRangeMinimum(_ minXRange: Double)
    {
        let xScale = _xAxis.axisRange / minXRange
        _viewPortHandler.setMaximumScaleX(CGFloat(xScale))
    }

    /// Limits the maximum and minimum value count that can be visible by pinching and zooming.
    ///
    /// e.g. minRange=10, maxRange=100 no less than 10 values and no more that 100 values can be viewed
    /// at once without scrolling.
    ///
    /// If you call this method, chart must have data or it has no effect.
    @objc open func setVisibleXRange(minXRange: Double, maxXRange: Double)
    {
        let minScale = _xAxis.axisRange / maxXRange
        let maxScale = _xAxis.axisRange / minXRange
        _viewPortHandler.setMinMaxScaleX(
            minScaleX: CGFloat(minScale),
            maxScaleX: CGFloat(maxScale))
    }

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

3 participants