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

Named axis support? #35

Open
delebedev opened this issue Apr 18, 2015 · 36 comments
Open

Named axis support? #35

delebedev opened this issue Apr 18, 2015 · 36 comments

Comments

@delebedev
Copy link

2015-04-19 00-48-45 lifts rimmer sk last -1

Looked at android wiki, but did not find the possibility to name x or y axis.

Is that possible?

@danielgindi
Copy link
Collaborator

Everything that is supported in the Android version is supported here with the exact same API !

@delebedev
Copy link
Author

@danielgindi I understand this and already read the docs but maybe I've missed something, do you know the answer to my question?

@danielgindi
Copy link
Collaborator

Well currently it is not implemented - I currently am not planning on implementing, so you can do it :-)
Or you can ask for it on the MPAndroidChart repo and when it's done I'll pull it here too

@UberJason
Copy link

+1, This would be awesome and an important feature - we're all taught since grade school to title our axes! :)

@liuxuan30
Copy link
Member

liuxuan30 commented Aug 17, 2016

it is possible. I have done this before. just similar to rotating the x axis labels. The whole point is calculating the correct anchor point for rotation.

I could do a PR, but the question is, what I did is simply 90 rotation and centered in the middle of y axis with a simple text and text color, font. Once we officially support it, people would ask more, like different angle, position, multiple lines, etc... That's painful

@UberJason
Copy link

I don't quite follow your explanation, because there's no axis label property to apply rotation and centering to. Do you just mean you manually added a label to the chart view and manually applied some transforms to it?

Respectfully, I suggest that even basic axis labels would be a huge benefit, and you wouldn't be forced to support any more features than you want to (after all, you're currently not planning to support this one). Hope you'll reconsider!

@liuxuan30
Copy link
Member

liuxuan30 commented Aug 22, 2016

@UberJason I mean I subclassed the axis class and apply an axisName property and made toe rotation. Most customized work could use subclass + override to achieve.

I am on vacation.. so I may check my old code and file a PR for v3.

@toby-pondr
Copy link

+1 Would love to see this feature

3 similar comments
@tejas-ardeshna
Copy link

+1 Would love to see this feature

@intspt
Copy link

intspt commented Dec 12, 2016

+1 Would love to see this feature

@mr-spod
Copy link

mr-spod commented Dec 12, 2016

+1 Would love to see this feature

@ChinmayDB
Copy link

is this implemented?

@MargaretaKusan
Copy link

+1 Would love to see this feature

@firvorski
Copy link

+1

@simonbromberg
Copy link
Contributor

Is there a way to get a frame position that one could use to insert any custom UIView?

@beddfaf916
Copy link

+1 would love to see that feature

@HPRaval
Copy link

HPRaval commented May 13, 2017

is this implemented? i need this.

@liuxuan30
Copy link
Member

liuxuan30 commented May 15, 2017

There is a PR #2387 , but not reviewed yet.

@irshad9998
Copy link

+1 would love to see that feature

@mohsenasm
Copy link

+1 would love to see that feature

@nicoara
Copy link

nicoara commented Sep 11, 2017

+1 would love to see this feature. .....

@dbalaban
Copy link

+1 would love to see that feature

@TaniaGoswami
Copy link

+1 this feature would be great!!

@russellbstephens
Copy link
Contributor

+1

@thierryH91200
Copy link
Contributor

look at #2452 and #2387

@littlemozart
Copy link

+1 would love to see that feature

@coolcool1994
Copy link

+1 me too

@woodmicha
Copy link

Could really use this feature....

@NSAmit
Copy link

NSAmit commented Jul 24, 2018

I do want this feature too.

@NSAmit
Copy link

NSAmit commented Jul 24, 2018

Waiting for it....

@bermanapps
Copy link

Me too............

@sarawanakumar
Copy link

I do want this feature too..

@thierryH91200
Copy link
Contributor

I do want this feature too.. I'm tired for every update of the rewrite

@iainhunter
Copy link

I've seen a lot of talk on adding axis titles on multiple threads, but still haven't found a code example of how to add them. I'm using Swift 4.2. My chart is working great other than I can't get axis titles to show up. Does anyone have a code example?

@4np
Copy link
Contributor

4np commented Jul 26, 2019

I just ran into this as well. It's too bad this feature request has been open for about 4 years now. To implement it I wrote my own custom y-axis renderer. To accomplish such a title on the left axis you can do something like this:

In your chart configuration:

leftAxis.xOffset = 45
leftYAxisRenderer = MyYAxisRenderer(viewPortHandler: viewPortHandler, yAxis: leftAxis, transformer: getTransformer(forAxis: .left))

And create your custom renderer:

class MyYAxisRenderer: YAxisRenderer {
    private static let titleLabelPadding: CGFloat = 20
    
    /**
     Unfortunately iOS Charts has marked many of its methods with internal visibily
     so they cannot be customized. Instead you often need to re-implement logic from
     the charting framework.
    */
    override func renderAxisLabels(context: CGContext) {
        // Render the y-labels.
        super.renderAxisLabels(context: context)
        
        // Render the y-axis title using our custom renderer.
        renderTitle(title: "Lorem ipsum dolor sit amet", inContext: context, x: MyYAxisRenderer.titleLabelPadding)
    }
}

// MARK: Y-Axis titles.
private extension MyYAxisRenderer {
    func renderTitle(title: String, inContext context: CGContext, x: CGFloat) {
        guard let yAxis = self.axis as? YAxis else { return }
        
        let attributes: [NSAttributedString.Key: Any] = [
            .font: yAxis.labelFont,
            .foregroundColor: yAxis.labelTextColor
        ]
        
        // Determine the chart title's y-position.
        let titleSize = title.size(withAttributes: attributes)
        let verticalTitleSize = CGSize(width: titleSize.height, height: titleSize.width)
        let point = CGPoint(x: x, y: (viewPortHandler.chartHeight - verticalTitleSize.height) / 2)
        
        // Render the chart title.
        ChartUtils.drawText(context: context,
                            text: title,
                            point: point,
                            attributes: attributes,
                            anchor: .zero,
                            angleRadians: .pi / -2)
    }
}

This example will render the y-axis title with a left offset / padding of 20px, and the y-axis labels with a left offset of 45px.

I hope this helps some people...

@Planet30
Copy link

Trying a simpler workaround using SwiftUI:
#4405

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