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

Stacked column - incorrect sum #1223

Closed
sebastianbochan opened this Issue Sep 3, 2012 · 7 comments

Comments

Projects
None yet
4 participants
@sebastianbochan
Contributor

sebastianbochan commented Sep 3, 2012

The problem appears when we use decimal numbers like this: http://jsfiddle.net/W4keF/5/

Use math.Round() solves problem but not it is not perfect, because when we would like to have two stacks, first (22.61, 12.34) and second (123.4567, 456,789000021) then it will not be appropriate.

@TorsteinHonsi

This comment has been minimized.

Collaborator

TorsteinHonsi commented Sep 3, 2012

I'm not sure I grasp the problem here... In your chart, and also when I enter the numbers from your description, it looks okay to me: http://jsfiddle.net/highcharts/W4keF/6/

Can you elaborate a bit?

@sebastianbochan

This comment has been minimized.

Contributor

sebastianbochan commented Sep 3, 2012

@Abhoryo

This comment has been minimized.

Abhoryo commented Sep 4, 2012

If the formatter isn't used I see in the first example :

10.52 + 22.61 = 33.129999999995 instead of 33.13

@sebastianbochan

This comment has been minimized.

Contributor

sebastianbochan commented Sep 4, 2012

Yes exacly, so it looks like a bug.

@TorsteinHonsi

This comment has been minimized.

Collaborator

TorsteinHonsi commented Sep 4, 2012

Ah, yes. A JavaScript float bug.

@TecHunter

This comment has been minimized.

TecHunter commented Sep 14, 2012

I've fixed it for my project. while my solution isn't really nice and fast it's working. maybe you could adapt:
With :

function safeSum(a,b){
    var tabA = (a + "").split(".");
    var tabB = (b + "").split(".");
    var decA = tabA.length>1?tabA[1].length:0;
    var decB = tabB.length>1?tabB[1].length:0;
    a = decA>0?(tabA[0]+tabA[1])*1.0:a;
    b = decB>0?(tabB[0]+tabB[1])*1.0:b;
    var diff = decA-decB;
    if(diff >0){
        //a has more decimals than b
        b=b*Math.pow(10,diff);
        return (a+b)/Math.pow(10,decA);
    }else if (diff<0){
        //a has more decimals than b
        a=a*Math.pow(10,-diff);
                return (a+b)/Math.pow(10,decB);
    }else{
        return (a+b)/Math.pow(10,decA);
    }       
}

I changed the lines 6376 in highchart.js with version 2.3.5

y = pointStack[x] =
    defined(pointStack[x]) ?
    pointStack[x] + y : y;

to:

y = pointStack[x] =
    defined(pointStack[x]) ?
    safeSum(pointStack[x] , y) : y;

it works :)

@TorsteinHonsi

This comment has been minimized.

Collaborator

TorsteinHonsi commented Sep 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment