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

SOLVED: Round number less than zero x values issue #1916

Closed
jacogasp opened this issue Dec 4, 2016 · 6 comments
Closed

SOLVED: Round number less than zero x values issue #1916

jacogasp opened this issue Dec 4, 2016 · 6 comments

Comments

@jacogasp
Copy link

jacogasp commented Dec 4, 2016

I tried several methods and formatter to properly print x values with numbers smaller than zero. I can't print the "zero" digit before the dot, e.g:
Number ->Printed number

  • 0.0 -> .0
  • 0.7 -> .7

The issue is present both with not perfectly rounder numbers (eg. 0.3452) and with perfectly rounder numbers (eg 0.8)

let format = NumberFormatter()
format.minimumIntegerDigits = 1
let formatter = DefaultValueFormatter(formatter: format)
chartView.xAxis.valueFormatter = (formatter as? IAxisValueFormatter)

The code above doesn't work and also experiments with granularity did not work.

Swift/Charts 3.0.1

Found a solution with this tutorial

EDIT: Create an extension of the ChartViewController

// MARK: axisFormatDelegate
extension ChartViewController: IAxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.decimalSeparator = "."
        formatter.minimumFractionDigits = 1
        return formatter.string(from: NSNumber(value: value))!
    }
}

Inside the class of ChartViewController declare a variable of type IAxisValueFormatter? and inside viewDidLoad() set this new variable equal to self

class ChartViewController {
   weak var axisFormatDelegate: IAxisValueFormatter?
   ...
   override func viewDidLoad() {
       axisFormatDelegate = self
   }
}

Finally use lineChartView.xAxis.valueFormatter = axisFormatDelegate to set the proper formatter.

@Kristina777
Copy link

I have this problem too.

@liuxuan30
Copy link
Member

liuxuan30 commented Dec 8, 2016

I tried in ChartsDemo bar chart:

    leftAxisFormatter.negativeSuffix = @" $";
    leftAxisFormatter.positiveSuffix = @" $";
    leftAxisFormatter.minimumIntegerDigits = 1;
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
    leftAxis.labelCount = 8;
    leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];
(lldb) po formatter?.string(from:0.1)
▿ Optional<String>
  - some : "0.1 $"

It works fine.
Please debug on your side for below func, and carefully check the formatter whether the condition is not met and start a new one. If you found it's a bug, please give step to reproduce, and reopen this thread.

    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)
        }
    }

@jacogasp
Copy link
Author

jacogasp commented Dec 8, 2016

I'm sorry but I said I'm writing in Swift but you replied with the objective-c example.

I think you haven't even read my code but, I don't know why, you closed the topic without giving an acceptable answer.

@liuxuan30
Copy link
Member

liuxuan30 commented Dec 9, 2016

I read the code, run tests and debug with ChartsDemo, saying it's fine and needs you to debug on your side to know why the formatter is replaced or changed so minimumIntegerDigits became 0.
Then I got a down thumb and saying I am not reading the question or code and get complained for using Objective-C where it's the main language in Apple's ecosystem.. If I knew this before I won't spend my time at all in the first place.
I think we know who's not actually reading.

@jacogasp
Copy link
Author

jacogasp commented Dec 9, 2016

I'm sorry if I was a little hard with you, but I'm learning coding by my self, I opened a thread because I can't find a solution to my problem and you close the thread without giving me actually a real solution. And if you close immediately the thread I get much less chances to be reached by other people (I have no doubt that Charts API works well).
I know that obj-c is the main apple language, but sometimes is not so simple to translate in Swift, mostly for a beginner who started with it.

Anyway I found this holy solution, now I write it down.
Cheers man and good work with this wonderful API

@jacogasp jacogasp changed the title Round number less than zero x values issue SOLVED: Round number less than zero x values issue Dec 9, 2016
@liuxuan30
Copy link
Member

liuxuan30 commented Dec 12, 2016

It's fine. I close the issues because we need to keep it clean knowing what are the true issues and what are questions.
As I said, I have post the code that you should debug on. It should be the solution. I tried it myself already. The formatter won't change its minimumIntegerDigits after you set it, unless the formatter is being replaced or you changed some where else.

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