Code Monkey home page Code Monkey logo

Comments (36)

4np avatar 4np commented on July 30, 2024 12

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...

from charts.

iainhunter avatar iainhunter commented on July 30, 2024 4

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?

from charts.

UberJason avatar UberJason commented on July 30, 2024 2

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

from charts.

liuxuan30 avatar liuxuan30 commented on July 30, 2024 1

@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.

from charts.

tejas-ardeshna avatar tejas-ardeshna commented on July 30, 2024 1

+1 Would love to see this feature

from charts.

Planet30 avatar Planet30 commented on July 30, 2024 1

Trying a simpler workaround using SwiftUI:
#4405

from charts.

danielgindi avatar danielgindi commented on July 30, 2024

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

from charts.

delebedev avatar delebedev commented on July 30, 2024

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

from charts.

danielgindi avatar danielgindi commented on July 30, 2024

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

from charts.

liuxuan30 avatar liuxuan30 commented on July 30, 2024

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

from charts.

UberJason avatar UberJason commented on July 30, 2024

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!

from charts.

toby-pondr avatar toby-pondr commented on July 30, 2024

+1 Would love to see this feature

from charts.

intspt avatar intspt commented on July 30, 2024

+1 Would love to see this feature

from charts.

mr-spod avatar mr-spod commented on July 30, 2024

+1 Would love to see this feature

from charts.

ChinmayDB avatar ChinmayDB commented on July 30, 2024

is this implemented?

from charts.

MargaretaKusan avatar MargaretaKusan commented on July 30, 2024

+1 Would love to see this feature

from charts.

firvorski avatar firvorski commented on July 30, 2024

+1

from charts.

simonbromberg avatar simonbromberg commented on July 30, 2024

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

from charts.

beddfaf916 avatar beddfaf916 commented on July 30, 2024

+1 would love to see that feature

from charts.

HPRaval avatar HPRaval commented on July 30, 2024

is this implemented? i need this.

from charts.

liuxuan30 avatar liuxuan30 commented on July 30, 2024

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

from charts.

irshad9998 avatar irshad9998 commented on July 30, 2024

+1 would love to see that feature

from charts.

mohsenasm avatar mohsenasm commented on July 30, 2024

+1 would love to see that feature

from charts.

nicoara avatar nicoara commented on July 30, 2024

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

from charts.

dbalaban avatar dbalaban commented on July 30, 2024

+1 would love to see that feature

from charts.

TaniaGoswami avatar TaniaGoswami commented on July 30, 2024

+1 this feature would be great!!

from charts.

russellbstephens avatar russellbstephens commented on July 30, 2024

+1

from charts.

thierryH91200 avatar thierryH91200 commented on July 30, 2024

look at #2452 and #2387

from charts.

littlemozart avatar littlemozart commented on July 30, 2024

+1 would love to see that feature

from charts.

coolcool1994 avatar coolcool1994 commented on July 30, 2024

+1 me too

from charts.

woodmicha avatar woodmicha commented on July 30, 2024

Could really use this feature....

from charts.

NSAmit avatar NSAmit commented on July 30, 2024

I do want this feature too.

from charts.

NSAmit avatar NSAmit commented on July 30, 2024

Waiting for it....

from charts.

bermanapps avatar bermanapps commented on July 30, 2024

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

from charts.

sarawanakumar avatar sarawanakumar commented on July 30, 2024

I do want this feature too..

from charts.

thierryH91200 avatar thierryH91200 commented on July 30, 2024

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

from charts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.