Monday, July 02, 2012

JavaFX 2.2 Pie Chart with JPA 2.0

Shipping Costs By Purchase Order
I saw an example of a Pie Chart using JavaFX. I thought that it looked really nice and I wondered how easy it would be to do using JPA 2.0, an example Apache Derby DB included in NetBeans, and NetBeans 7.2.

Well the first iteration took about 10 minutes, and the tweaking took another 15 minutes. Basically, if you asked me to do it again, I bet I could do it in under 5 minutes. My output looks really cool too.

I saw an old post using JavaFXScript to create a similar output which was used in JSF. I am wondering if I can do the same thing using version 2.2 of JavaFX, or if I should wait until the HTML libraries are complete.

The code for the project can be downloaded here: PieChartExample.zip

PieChartExample.java


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.bluelotussoftware.javafx.chart;
 
import com.bluelotussoftware.jpa.PurchaseOrder;
import com.sun.javafx.collections.ObservableListWrapper;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
 
/**
 *
 * @author John Yeary
 * @version 1.0
 */
public class PieChartExample extends Application {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage primaryStage) {
        List<data> list = new ArrayList<>();
        List<purchaseorder> purchaseOrders = getPurchaseOrders();
 
        for (PurchaseOrder p : purchaseOrders) {
            list.add(new Data(p.getOrderNum().toString(), p.getShippingCost().doubleValue()));
        }
 
        ObservableList<data> data = new ObservableListWrapper<>(list);
 
        PieChart pieChart = new PieChart();
        pieChart.setData(data);
 
        StackPane root = new StackPane();
        root.getChildren().add(pieChart);
 
        primaryStage.setTitle("Shipping Costs By Purchase Order");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }
 
    private List<purchaseorder> getPurchaseOrders() {
        List<purchaseorder> purchaseOrders = new ArrayList<>();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PieChartExamplePU");
        EntityManager em = emf.createEntityManager();
        TypedQuery<purchaseorder> tq = em.createNamedQuery("PurchaseOrder.findAll", PurchaseOrder.class);
        purchaseOrders.addAll(tq.getResultList());
        em.close();
        emf.close();
        return purchaseOrders;
    }
}

3 comments :

SWKHMER said...

your app working great but it is only work with the desktop application. Do you have any idea of to make it run on the web or by using the javawebstart?

SWKHMER said...

Your code is great and it also run and show up great with the windows application buy I wonder how to run it on the web or using java WebStart. I always got an error when I try to run on the browser. I've try Chrome, Firefox, IE. But it all are not work and I always got an error and display nothing.

Do you have any solution for that?

John Yeary said...

I have not tried either. I was waiting for the functionality to be complete for the Mac before I gave it a try. I understand that the functionality for Webstart is available in Windows right now.

Popular Posts