hochileo / achartengine

Automatically exported from code.google.com/p/achartengine
0 stars 0 forks source link

Clickable Doughnut Chart Regions #325

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is not necessarily an issue, just an unsupported feature at this time. I 
have working code that makes this possible and will submit it shortly.

Original issue reported on code.google.com by max.baue...@gmail.com on 5 Jul 2013 at 2:59

GoogleCodeExporter commented 9 years ago
I wasn't sure where to put the code as I haven't used this before but here is 
my method:

//Find the name of the region clicked for a doughnut chart. This method takes 
in the graphical view of the chart and the motion event from on onTouchListener 
that is activated upon touching the view.

public String getCurrentSeries(View gV, MotionEvent me){
    //construct table for regions and their angles
    Object[][] table = new Object[names.length][4];

    //fill table with names and calculate sum of values
    double totalValue = 0;
    for(int i = 0; i < sizes.length; i++){
        //names is the array of Strings used to make the doughnut chart        
        table[i][0] = names[i];
        //sizes is the array of doubles used to make the doughnut chart
    totalValue += sizes[i];
    }

    //fill table with angle consumption, start angle, and end angle
    for(int j = 0; j < table.length; j++){
    table[j][1] = (sizes[j]/totalValue)*360;
    double startAngle = 0;
    for(int k = 0; k<j; k++){
        startAngle += (Double)table[k][1];
    }
    table[j][2] = startAngle;
    table[j][3] = (Double)table[j][1] + (Double)table[j][2];
    }

    float left = gV.getX();
    float top = gV.getY();
    float right = left + gV.getWidth();
    float bottom = top + gV.getHeight();

    float mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
    int radius = (int) (mRadius * 0.35 * mRenderer.getScale());
    int innerRadius = (int) (radius - (0.2 * mRadius + 1 * names.length));

    //Calculate angle
    float Cx = gV.getX() + (gV.getWidth()/2);
    float Cy = gV.getY() + (gV.getHeight()/2);

    float ClickX = me.getX();
    float ClickY = me.getY();

    double distanceX = ClickX - Cx;
    double distanceY = ClickY - Cy;
    double distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));

    if(distance > 0){
    double angle = Math.toDegrees(Math.asin(distanceY/distance));
    if(distanceX >= 0 && distanceY >= 0){
            //angle is correct
    } else if(distanceX >= 0 && distanceY < 0){
        //angle will be negative
        angle = 360 + angle;
    } else if(distanceX < 0 && distanceY >= 0){
        //angle is positive and needs to be adjusted down
        angle = 180 - angle;
    } else{
        //angle is negative and needs to be adjusted up
        angle = 180 - angle;
    }

        //find appropriate region
    if(distance <= radius && distance >= innerRadius){
        for(int i = 0; i<table.length; i++){
            if(angle >= (Double)table[i][2] && angle < (Double)table[i][3]){
            return (String)table[i][0];
        }
        }
    } else{
        return null;
    }
    }
    return null;
}

Original comment by max.baue...@gmail.com on 5 Jul 2013 at 3:12

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hi,

I am trying similar implementation for my project and trying really hard to 
integrate this method with demo code provided by 
achartengine(BudgetDoughnutChart.java). Could you please guide me how to 
integrate this method with achartengine demo code .

If you have not tested with achrtengine demo code. please help me with your own 
implementation to make sure that this method works.

Thanks in advance.

Original comment by urgane...@gmail.com on 19 Feb 2014 at 8:00

GoogleCodeExporter commented 9 years ago
I haven't used the demo code at all, and I know this code works well, although 
it could be improved some. Do you have specific questions on what do to do?

Original comment by max.baue...@gmail.com on 20 Feb 2014 at 5:59

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks for reply.. :-)

My requirement is, I need to have a doughnut chart and if I click on any 
segment of that chart it should show some value represented by that segment 
(and also that segments may gets zoom-in).

Hope this will give you an idea of what i want to do.

Original comment by urgane...@gmail.com on 20 Feb 2014 at 8:43

GoogleCodeExporter commented 9 years ago
Hi! Any news with the click of a Doughnut Chart series? I need the same, thank 
you very much!

Original comment by pablocru...@gmail.com on 7 Oct 2014 at 7:37