JavaFX Arc create pie chart

Description

JavaFX Arc create pie chart


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

public class Main extends Application {
  final double SIZE = 400.0;
  final double RADIUS = SIZE / 2.0;
  final static int HTML = 20;
  final static int JAVA = 10;
  final static int CSS = 30;
  final static int SQL = 40;

  @Override/*from   w w w .j a v  a2  s.com*/
  public void start(Stage primaryStage) {

    // Create the pie slices
    double startAngle = 0;
    Arc html = new Arc(RADIUS, RADIUS, RADIUS, RADIUS, startAngle, 360 * (HTML / 100.0));
    html.setFill(Color.RED);
    html.setType(ArcType.ROUND);
    startAngle += 360 * (HTML / 100.0);
    Arc java = new Arc(RADIUS, RADIUS, RADIUS, RADIUS, startAngle, 360 * (JAVA / 100.0));
    java.setFill(Color.BLUE);
    java.setType(ArcType.ROUND);
    startAngle += 360 * (JAVA / 100.0);
    Arc css = new Arc(RADIUS, RADIUS, RADIUS, RADIUS, startAngle, 360 * (CSS / 100.0));
    css.setFill(Color.GREEN);
    css.setType(ArcType.ROUND);
    startAngle += 360 * (CSS / 100.0);
    Arc sql = new Arc(RADIUS, RADIUS, RADIUS, RADIUS, startAngle, 360 * (SQL / 100.0));
    sql.setFill(Color.ORANGE);
    sql.setType(ArcType.ROUND);

    // Create the text labels
    Text tHtml = new Text("HTML -- " + HTML + "%");
    tHtml.setX(RADIUS + RADIUS / 8);
    tHtml.setY(RADIUS - RADIUS / 4);
    Text tJava = new Text("Java -- " + JAVA + "%");
    tJava.setX(RADIUS);
    Text tCss = new Text("CSS -- " + CSS + "%");
    tCss.setY(RADIUS);
    Text tSql = new Text("SQL -- " + SQL + "%");
    tSql.setX(RADIUS);
    tSql.setY(RADIUS + RADIUS);

    Pane pie = new Pane();
    pie.getChildren().addAll(html, java, css, sql, tHtml, tJava, tCss, tSql);

    StackPane pane = new StackPane();
    pane.setAlignment(Pos.CENTER);
    pane.setPadding(new Insets(20));
    pane.getChildren().add(pie);

    Scene scene = new Scene(pane);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    Application.launch(args);
  }
}



PreviousNext

Related