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

(v3.0 + Swift3.0)IChartAxisValueFormatter's protocol function pass on a unexpected value(double value discard) #1503

Closed
Huang-Libo opened this issue Sep 20, 2016 · 1 comment

Comments

@Huang-Libo
Copy link

Huang-Libo commented Sep 20, 2016


My suggestion:I think Whether to round a number should be decided by user, because some number is more significant before rounding, for example a UNIX timestamp in this issue---UNIX timestamp is intended to be formatted to a date string generally. Maybe we can add a property to let user to decide whether to round a number that is showed on xAxis.

Update again(Issue solved):

I have handle this problem by modifying a few lines code in Charts' source code in AxisRendererBase.swift...

  1. change:var interval = ChartUtils.roundToNextSignificant(number: Double(rawInterval)) to var interval = Double(rawInterval)

2.comment out these codes:

// Normalize interval
let intervalMagnitude = ChartUtils.roundToNextSignificant(number: pow(10.0, Double(Int(log10(interval)))))
let intervalSigDigit = Int(interval / intervalMagnitude)
if intervalSigDigit > 5
{
    // Use one order of magnitude higher, to avoid intervals like 0.9 or 90
    interval = floor(10.0 * Double(intervalMagnitude))
}

update(supplement):

the value(UNIX timestamp) that I got from stringForValue:axis: protocol function are all convert last five number to zero, this may cause obvious problem.

(lldb) po value
1473900000

(lldb) po value
1474000000

(lldb) po value
1474100000


My xAxis is set to show date by day:

xAxis.granularity = 60.0 * 60 * 24;

I implement my own date formatter with IChartAxisValueFormatter protocol, however, the protocol function

-(NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis

pass on a value that was not what I have set in ChartDataEntry's init function:

[[ChartDataEntry alloc] initWithX:xValue y:];

value is not equal to xValue, for example, I set x's value to 1473782400, but the value what I got from stringForValue:axis: protocol function is 147380000 , so it lead the chart render vertical grid and x's label at unexpected place:
unix timestamp issue

@Huang-Libo Huang-Libo changed the title (v3.0 + Swift3.0)IChartAxisValueFormatter protocol (v3.0 + Swift3.0)IChartAxisValueFormatter's protocol function pass on a unexpected value Sep 21, 2016
@Huang-Libo Huang-Libo changed the title (v3.0 + Swift3.0)IChartAxisValueFormatter's protocol function pass on a unexpected value (v3.0 + Swift3.0)IChartAxisValueFormatter's protocol function pass on a unexpected value(double value discard) Sep 21, 2016
@danielgindi
Copy link
Collaborator

The label that is showing - is showing the correct value for the x-value it points at.

So the short answer is - what you might actually need is to use the granularity feature.

The longer answer is:

The axis calculations are there for a reason. And there's no difference what kind of value you pass in - they are all on an axis, and you should be able to represent any point on the axis with your kind of value.
However, some times you want to control how small the steps between axis labels (or grid lines) can be. That's what granularity is there for.

Also a note: In Cocoa/CocoaTouch, NSTimeInterval is a more appropriate value to represent dates, and actually represents seconds. It is much more intuitive to work with, especially when tackling such problems. You can see the time chart demo.

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