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

Start value from X-Axis #2646

Closed
gsolanki1509 opened this issue Jul 27, 2017 · 8 comments
Closed

Start value from X-Axis #2646

gsolanki1509 opened this issue Jul 27, 2017 · 8 comments

Comments

@gsolanki1509
Copy link

screen shot 2017-07-27 at 5 43 17 pm

Hello,

I want to start my value from axis. But it always start with one step up.
Can you tell me, what am i doing wrong?

  let xAxis = barChartView.xAxis
    xAxis.labelPosition = .bottom
    xAxis.drawGridLinesEnabled = false
    xAxis.valueFormatter = IndexAxisValueFormatter(values:arrBarXAxis)
    xAxis.granularity = 1   // space between bars
    xAxis.labelTextColor = UIColor.darkGray
    xAxis.axisLineColor = UIColor.lightGray
    xAxis.drawAxisLineEnabled = true
    xAxis.labelFont = UIFont(name: "Calibri", size: 8)!
    
    barChartView.leftAxis.drawGridLinesEnabled = true
    barChartView.leftAxis.drawAxisLineEnabled = true
    barChartView.leftAxis.zeroLineColor = UIColor.red
    barChartView.leftAxis.drawZeroLineEnabled = false
    
    barChartView.leftAxis.valueFormatter = HourValueFormatter()
    barChartView.leftAxis.labelTextColor = UIColor.darkGray
    barChartView.leftAxis.labelFont = UIFont(name: "Calibri", size: 7)!

Thanks,
Gaurav

@liuxuan30
Copy link
Member

liuxuan30 commented Jul 28, 2017

check your open func stringForValue(_ value: Double, axis: AxisBase?) -> String in IndexAxisValueFormatter why one step up

@gsolanki1509
Copy link
Author

gsolanki1509 commented Jul 30, 2017

hi @liuxuan30 ,

I'm not doing anything much in "stringForValue" function

// using property
barChartView.leftAxis.valueFormatter = HourValueFormatter()

// Class
open class HourValueFormatter : NSObject, IAxisValueFormatter
{
    public override init() {
        //        super.init()
    
    }
    
    public func stringForValue(_ value: Double, axis: AxisBase?) -> String
    {
        print(value)
        return "\(forTailingZero(temp: value))"
        
    }
    
    func forTailingZero(temp: Double) -> String{
        let tempVar = String(format: "%g", temp)
        return tempVar
    }
    
}

I'm just removing decimal value.
I couldn't figure it out whats wrong.

@gsolanki1509
Copy link
Author

I even tried to manually do as below,

barChartView.leftAxis.valueFormatter = IndexAxisValueFormatter(values:["0","1","2","3","4","5"])

but result is same only.

@liuxuan30
Copy link
Member

It's the value you passed into stringForValue that matters.
BTW, your question seems irrelevant to your image. Repeat your question again. Your image shows "TH", "WE", but I just saw forTailingZero in your code.

@gsolanki1509
Copy link
Author

Let me clear my requirement once again.

The "X-axis" will consists values of past 7 seven days, starting from today.
eg :- "Tue", "Mon", "Sun"...."Wed".

And based on Days i have to shows number of request.

I have to convert all values to "Double" for left Axis. But while showing i need to remove those extra decimal. So i'm using "forTailingZero" function.

But whenever i pass that array for Left Axis values. It always start with one step up as shown in figure.

Kindly, let me know if you still have any doubt.

Thanks

@liuxuan30
Copy link
Member

If you are asking about Y(left) axis, why your title is Start value from X-Axis...

@liuxuan30
Copy link
Member

liuxuan30 commented Aug 3, 2017

Several points:

i need to remove those extra decimal

This can be done by changing valueFormatter properties, removing by yourself is too old school. modern value formatter is already very powerful. Left axis values are all Double already

But whenever i pass that array for Left Axis values. It always start with one step up as shown in figure.

I don't think you can pass that array for Left Axis values.. Y axis values are calculated by the library. You can set axisMinimum = 0 for left axis in your case.

Long answer: if you data range is [0, n], the library by default will create the y axis range like [a,b] where a < 0 and b > n, to make the chart looks nicer and all your content is within the canvas.

@gsolanki1509
Copy link
Author

This can be done by changing valueFormatter properties, removing by yourself is too old school. modern value formatter is already very powerful. Left axis values are all Double already

==> Thanks, i will apply that.

I don't think you can pass that array for Left Axis values.. Y axis values are calculated by the library. You can set axisMinimum = 0 for left axis in your case.

Long answer: if you data range is [0, n], the library by default will create the y axis range like [a,b] where a < 0 and b > n, to make the chart looks nicer and all your content is within the canvas.

==> Yeah once i set my axisMin and axisMax values then the Y axis start at proper position. For this i saw one behaviour like if my values are between (0,5). Then Y-Axis shows values in decimal. So, for this i kept condition and works fine now.. Thanks for your help :) 👍

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

2 participants