Vaibhav's Blog Space

Small code for PieChart...but !

Tuesday Jun 30, 2009

As already discussed, JavaFX 1.2 provide API set for Charts and Graphs. Though I decided to put my hand dirty in writing one 3D piechart from my own. With mine, you will get additional feature of explode in and out feature :). Well, action can be written with the existing chart API and I guess explode feature will also come soon.


Making 3D Pie chart is nothing but layering of 2D Pie Chart and here goes a small code :

Slice.fx

package piechart3d;

import java.lang.Math;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;

/**
 * @author Vaibhav Choudhary
 */

public class Slice extends CustomNode {

    public var color: Color;
    public var sAngle: Number = 0.0;
    public var len: Number = 0.0;
    public var xt = 0.0;
    public var yt = 0.0;


    function explodout():Boolean {
        var t = Timeline {

            repeatCount: 1
            keyFrames: [
                KeyFrame {
                    time: 0.25s
                    canSkip: true
                    values: [
                        xt =>
                        30 * Math.cos(2  *  Math.PI  *  (sAngle  +  len  /  2)  /  360) tween Interpolator.EASEBOTH,
                        yt =>
                        -30 * Math.sin(2  *  Math.PI  *  (sAngle  +  len  /  2)  /  360) tween Interpolator.EASEBOTH

                    ]
                }
            ]
        }
        t.play();
        return true
    }
    function explodein():Boolean {
        var t1 = Timeline {
            repeatCount: 1
            keyFrames: [
                KeyFrame {
                    time: 0.25s
                    canSkip: true
                    values: [
                        xt => 0,
                        yt => 0
                    ]
                }
            ]
        }
        t1.play();
        return true
    }

    public override function create(): Node {
        return Group {
            blocksMouse: true
            translateX: bind xt
            translateY: bind yt
            onMouseClicked: function( e: MouseEvent ):Void {
                if(xt == 0 and yt == 0) {
                    explodout();
                }
                            else
                explodein();
            }
            content: for(num in [0..25]) {[
                    Arc {
 
                        stroke: color
                        cache: true
                        fill: color
                        translateX: 0
                        translateY: (num + 1) * 1
                        centerX: 250
                        centerY: 250
                        radiusX: 150
                        radiusY: 60
                        startAngle: bind sAngle
                        length: bind len
                        type: ArcType.ROUND
                    }
                    Arc {
                        cache: true
 
                        fill: LinearGradient {
                            startX: 0.3
                            startY: 0.3
                            endX: 1.0
                            endY: 1.0
                            stops: [
                                Stop {
                                    color: color
                                    offset: 0.0
                                },
                                Stop {
                                    color: Color.WHITE
                                    offset: 1.0
                                },

                            ]
                        }
 
                        centerX: 250
                        centerY: 250
                        radiusX: 150
                        radiusY: 60
                        startAngle: bind sAngle
                        length: bind len
                        type: ArcType.ROUND
                    },
                ]
            }

        };
    }
}


Main.fx:
package piechart3d;


import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * @author Vaibhav Choudhary
 */

var slice1: Slice = Slice{color: Color.YELLOWGREEN, sAngle:0, len: 45};
var slice2: Slice = Slice{color: Color.BLUEVIOLET, sAngle:45, len: 80 };
var slice3: Slice = Slice{color: Color.PALETURQUOISE, sAngle:125, len: 80 };
var slice4: Slice = Slice{color: Color.DARKORANGE, sAngle:205, len: 100 };
var slice5: Slice = Slice{color: Color.FIREBRICK, sAngle:305, len: 55};


Stage {
    title: "Pie Chart - 3D"
    width: 550
    height: 580
    scene: Scene {
        fill: Color.WHITE
        content:[
           slice2, slice1,slice5,slice3,slice4
        ]
     }
    }


Anything here can be made generic to any extend. There is a for loop of 0..25 in Slice.fx which speaks about the thickness of chart :) and some mathematics in timeline speaks about explode feature.

Now if you compare this with PieChart that comes in API, you will see this has some jerky corners + Color combination is not as soluble. How that has been made is secret :).

JNLP Run :



[6] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

Good one.. I liked it.

Posted by Raghu Nair on July 14, 2009 at 04:25 PM IST #

bind chart's data?

Posted by Begin on July 19, 2009 at 08:34 AM IST #

Tq,chowdary!.....

could you send me the link where i can get the sourcecode of animated charts....

THanks in advance.....

Posted by Kallis on November 26, 2009 at 07:43 PM IST #

Tq,chowdary!.....

could you send me the link where i can get the sourcecode of animated charts....

THanks in advance.....

Posted by Kallis on November 26, 2009 at 07:44 PM IST #

The state's <a href=http://www.mynikeairmax.com/>Nike Air Max</a> decision in October to shrink the school <a href=http://www.mynikeairmax.com/>Air Max </a> year by 10 percent, giving it the fewest number <a href=http://www.mynikeairmax.com/>Air Max 90</a>of instructional days in the nation at 163<a href=http://www.mynikeairmax.com/>Air Max 95</a>, is adding to the already dismal reputation Hawaii's<a href=http://www.mynikeairmax.com/>nike Air Max 95</a> public schools have among<a href=http://www.mynikeairmax.com/>nike Air Max 90</a> servicemen and womenCol. Mike Davino, the director of<a href=http://www.mynikeairmax.com/>Air max ltd </a> manpower, personnel and administration<a href=http://www.mynikeairmax.com/>Air Max shoes </a> for the U.S. Pacific Command, said the truncated school year is yet another concern for officials who have long heard about servicemen and women avoiding Hawaii assignments because of the state's public education system.

Posted by nike air max on February 01, 2010 at 12:46 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed