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

Custom Format YAxis #1768

Closed
ajimenezjulio opened this issue Nov 2, 2016 · 2 comments
Closed

Custom Format YAxis #1768

ajimenezjulio opened this issue Nov 2, 2016 · 2 comments

Comments

@ajimenezjulio
Copy link

ajimenezjulio commented Nov 2, 2016

I am trying to use a custom format in YAxis, specifically I'm trying to put a "$" symbol and a thousands separator "," also I want to put the thousands separator on the value that is showing above the bars.

Looking in documentation, you use the NSNumberFormatter but I can't, XCode show me an error.

I'm using this code:
let formatoNumeros: NSNumberFormatter = NSNumberFormatter()
formatoNumeros.minimumFractionDigits = 0
formatoNumeros.maximumFractionDigits = 2
formatoNumeros.currencyGroupingSeparator = ","
formatoNumeros.negativeSuffix = " $"
formatoNumeros.positiveSuffix = " $"
yaxis.valueFormatter = formatoNumeros

The error appears on the line: yaxis.valueFormatter = formatoNumeros and it says: "Cannot assign value of type 'NSNumberFormatter' to type 'IAxisValueFormatter', also I've tried casting the variable but it cannot be posible and also have tried to use the NSNumberFormatter on the chartData, like this: chartData.setValueFormatter(formatoNumeros) but here XCode says that cannot assign value of type NSNumber.. to IValueFormatter.

And my questions is what would be the way or ways to customize the YAxis on BarCharts?

PS. The language I'm using is swift

Thanks!

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 3, 2016

that's because in Chart 3.0, axis changes to use IAxisValueFormatter, which is a protocol. You can conform to it then. If you don't want this high flexible formatter, then you can take DefaultAxisValueFormatter, and pass through a number formatter:

    public init(formatter: NumberFormatter)
    {
        super.init()

        self.formatter = formatter
    }

Just take a look at these classes.

    /// Sets the formatter to be used for formatting the axis labels.
    /// If no formatter is set, the chart will automatically determine a reasonable formatting (concerning decimals) for all the values that are drawn inside the chart.
    /// Use `nil` to use the formatter calculated by the chart.
    open var valueFormatter: IAxisValueFormatter?
    {
        get
        {
            if _axisValueFormatter == nil ||
                (_axisValueFormatter is DefaultAxisValueFormatter &&
                    (_axisValueFormatter as! DefaultAxisValueFormatter).hasAutoDecimals &&
                    (_axisValueFormatter as! DefaultAxisValueFormatter).decimals != decimals)
            {
                _axisValueFormatter = DefaultAxisValueFormatter(decimals: decimals)
            }

            return _axisValueFormatter
        }
        set
        {
            _axisValueFormatter = newValue ?? DefaultAxisValueFormatter(decimals: decimals)
        }
    }

@GuriSarao
Copy link

I am getting this error -

Cannot assign value of type 'DayAxisValueFormatter' to type '(any AxisValueFormatter)?'

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