Small code for PieChart...but !

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 :



Comments:

Good one.. I liked it.

Posted by Raghu Nair on July 14, 2009 at 09:25 AM IST #

bind chart's data?

Posted by Begin on July 19, 2009 at 01: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 11:43 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 11:44 AM 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 04:46 AM IST #

Christian Louboutin http://www.christianlouboutinmarts.com
Christian Louboutin Shoes http://www.christianlouboutinmarts.com
christian louboutin sale http://www.christianlouboutinmarts.com
Christian Louboutin Boots http://www.christianlouboutinmarts.com
Christian Louboutin Pumps http://www.christianlouboutinmarts.com
Christian Louboutin Sandals http://www.christianlouboutinmarts.com
tiffany wholesale http://www.toptiffanylinks.com
tiffany stores http://www.toptiffanylinks.com
replica tiffany http://www.toptiffanylinks.com
cheap jewelry http://www.toptiffanylinks.com
replica tiffany retail http://www.toptiffanylinks.com
pandora jewelry http://www.toptiffanylinks.com
replica pandora jewelry http://www.toptiffanylinks.com
ed hardy http://www.ed-onlineshop.com
ed hardy clothing http://www.ed-onlineshop.com
ed hardy t shirt http://www.ed-onlineshop.com
ed hardy jeans http://www.ed-onlineshop.com
wholesale ed hardy http://www.ed-onlineshop.com
ed hardy shirt http://www.ed-onlineshop.com
ed hardy boots http://www.ed-onlineshop.com
ed hardy swimwear http://www.edhardydream.com
ed hardy clothing http://www.edhardydream.com
ed hardy clothes http://www.edhardydream.com
ed hardy t shirts http://www.edhardydream.com
ed hardy jeans http://www.edhardydream.com
ed hardy sunglasses http://www.edhardydream.com
ed hardy sunglass http://www.edhardydream.com

Posted by china wholesale on March 07, 2010 at 01:54 AM IST #

I would like to introduce a very good company, electronic products dealer.
I have bought some products from company,the price was very cheap,and the products are very good quality!They accept paypal, credit card transactions,bank transfers,and more.Just have a look at this web page : http://www.nike-airyeezy.com
I am sure you will could save a lot of money!
Best regards!
web: http://www.victoriawear.com
http://www.homybuy.com

Posted by wwwnice on March 09, 2010 at 06:08 AM IST #

Post a Comment:
Comments are closed for this entry.
About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today