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

Initial implementation of skewed X axis #455

Open
wants to merge 2 commits into
base: release-2.4
Choose a base branch
from

Conversation

keltonhalbert
Copy link

This is an initial attempt at a pull request to add the capability for skewed X-axis lines on an X-Y plot. It currently supports a skewed X axis for both linear and logarithmic Y axis types.

I use the words "initial" for a few reasons, one being that I'm relatively new to iOS development and Xcode and I'm not entirely sure about how and where to add appropriate tests for this new feature. Another reason for the word "initial" is that this can and should be extended to skewed Y axes as well, but I want to hold off until there's approval on the method of implementation in CPTXYPlotSpace. If a different implementation style is requested, then the functionality can be extended to the Y axis range. Finally, it's "initial" because the behavior of axis grid lines in this context is not super well defined. See #454 for questions/discussion on that. I'd like to be able to implement some good default behavior here, but so far my efforts have not been fruitful.

Looking forward to the review and suggestions.

@keltonhalbert
Copy link
Author

Okay, so I came up with some work-arounds for skewed X axis behavior and grid lines.

For starters, in drawGridLinesInContext within CPTXYAxis, I changed it such that

 CPTPlotSpace *thePlotSpace          = self.plotSpace;

becomes

CPTXYPlotSpace *thePlotSpace          = (CPTXYPlotSpace *)self.plotSpace;

because it allows for me to check the CPTScaleType for the case of being skewed. Now that I can check if we are dealing with an X-skewed axis, I can fix the issue where the Y-axis lines do not connect with the left hand axis border.

            // In the case of a skewed coordinate axis system, the Y axis lines will not connect with the origin
            // but instead only connect to the minimum slanted X axis line. This says to connect Y axis grid lines
            // all the way to the left hand side.
            if (thePlotSpace.xScaleType == CPTScaleTypeSkew) {
                if (self.coordinate == CPTCoordinateY) {
                    startViewPoint.x = 0;
                }
            }

This just manually sets the startViewPoint.x to the 0 value, or the left hand side of the frame.

Now, I tried messing around with generateFixedIntervalMajorTickLocations in CPTAxis to see if I could auto-generate a custom range that goes beyond the bottom-left corner and allows for the skewed axis lines in the top-right corner. Unfortunately, I couldn't get anywhere with it. HOWEVER - I did make it so that you can specify the majorTickLocations inside of the view controller, and it would respect those locations. It required modifying this logic portion of drawGridLinesInContext within CPTXYAxis:

            if ( labeledRange && ![labeledRange contains:locationDecimal]  {
                continue;
            }

to become

            // In the case of a skewed X plot space range, we will be providing the tick locations.
            // Because of this, we don't want to mask out the tick locations without labels. Keep
            // the labeled locations AND the skew lines that come from out of frame.
            if ( labeledRange && ![labeledRange contains:locationDecimal] &&
                !(thePlotSpace.xScaleType == CPTScaleTypeSkew) && !(self.coordinate == CPTCoordinateX)) {
                continue;
            }

Once again, it checks if this is the X-coordinate and if the coordinate is skewed. If it doesn't meet those criteria, then it filters like its normal behavior. If it is the X coordinate AND its skewed, then it it still plots the line. This at the very least allows for one-line code on the API end for providing the axis lines and allowing for them to be drawn, even when beyond the range of the lower-left corner.

@keltonhalbert
Copy link
Author

keltonhalbert commented Oct 7, 2021

Example image showing that the skewed X lines display beyond the lower-left corner, and that the Y grid lines now intersect the axis line itself.

        
        var major_tick_locations = [NSNumber]()
        for value in stride(from: -170.0, to: 60.0, by: 10.0) {
            major_tick_locations.append(NSNumber(value: value))
        }
       

        if let x = axisSet.xAxis {
            x.majorIntervalLength   = 10
            x.minorTicksPerInterval = 0
            x.labelTextStyle = axisTextStyle
            x.minorGridLineStyle = gridLineStyle
            x.majorGridLineStyle = gridLineStyle
            x.axisLineStyle = lineStyle
            x.axisConstraints = CPTConstraints(lowerOffset: 0.0)
            x.labelingPolicy = CPTAxisLabelingPolicy.locationsProvided
            x.majorTickLocations = Set(major_tick_locations)
            x.delegate = self
        }

IMG_0597

@eskroch eskroch self-assigned this Oct 9, 2021
@eskroch
Copy link
Member

eskroch commented Oct 9, 2021

Looking good so far. We need to add an example plot to the Plot Gallery app to demonstrate the capability and aid in testing. We should also add XY plot space unit tests for the new functionality. Let me know if you need help with either of those tasks.

@keltonhalbert
Copy link
Author

Cool cool, I'll start by seeing if I can add some XY plot space unit tests first, and then try adding it to the plot gallery. I'll pop by here if I have any troubles!

@keltonhalbert keltonhalbert mentioned this pull request Oct 13, 2021
@eskroch eskroch linked an issue Jul 9, 2023 that may be closed by this pull request
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

Successfully merging this pull request may close these issues.

Angled/Skewed Axis
2 participants