Created
December 15, 2015 09:55
-
-
Save jeremyhalin/2796935e1ef3caf86c5c to your computer and use it in GitHub Desktop.
ChartFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, 2015, Formation Tablettes. All rights reserved. | |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
* | |
* Please contact us or visit www.britweb.fr if you need additional information or have any questions. | |
*/ | |
package com.logic; | |
import com.entity.Proposal; | |
import com.entity.Question; | |
import com.entity.Quiz; | |
import java.awt.BorderLayout; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import java.io.File; | |
import java.util.List; | |
import javafx.application.Platform; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.embed.swing.JFXPanel; | |
import javafx.scene.Node; | |
import javafx.scene.Scene; | |
import javafx.scene.chart.BarChart; | |
import javafx.scene.chart.CategoryAxis; | |
import javafx.scene.chart.NumberAxis; | |
import javafx.scene.chart.XYChart; | |
import javafx.scene.chart.XYChart.Data; | |
import javafx.scene.text.Text; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javafx.beans.value.*; | |
import javafx.geometry.Bounds; | |
import javafx.geometry.Pos; | |
import javafx.scene.*; | |
import javafx.scene.chart.*; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.*; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.*; | |
import javafx.scene.text.Text; | |
import javafx.stage.Stage; | |
/** | |
* | |
* @author Florian | |
*/ | |
public class ChartFrame extends JFrame { | |
private final JFXPanel fxPanel = new JFXPanel(); | |
private Quiz quiz; | |
private Question question; | |
private String questionName; | |
private String nbQuestion; | |
private final String normal = "#34495e"; | |
private final String good = "#2ecc71"; | |
private final String wrong = "#e74c3c"; | |
private final CategoryAxis xAxis = new CategoryAxis(); | |
private final NumberAxis yAxis = new NumberAxis(0d, new Integer(QuizProcess.getInstance().getNbLearnersOnQuiz()).doubleValue() + 0.1d, 1d); | |
private final BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis); | |
private final XYChart.Series<String, Number> series = new XYChart.Series<>(); | |
public ChartFrame(int idQuiz, int idQuestion, String nbQuestion){ | |
quiz = Quizzes.getInstance().getQuizById(idQuiz); | |
question = quiz.getQuestionById(idQuestion); | |
questionName = nbQuestion + question.getQuestion(); | |
initSwingComponents(); | |
initFxComponents(); | |
} | |
private void initSwingComponents(){ | |
JPanel mainPanel = new JPanel(new BorderLayout()); | |
mainPanel.add(fxPanel, BorderLayout.CENTER); | |
this.add(mainPanel); | |
this.setTitle("Statistiques"); | |
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
this.setSize(800,800); | |
this.addWindowListener( new WindowAdapter() { | |
@Override | |
public void windowClosing(WindowEvent we) { | |
QuizProcess.getInstance().statsOpened = false; | |
} | |
} ); | |
} | |
private void initFxComponents(){ | |
Platform.runLater(() -> { | |
barChart.setLegendVisible(false); | |
barChart.setAnimated(false); | |
barChart.setBarGap(0.0); | |
xAxis.setLabel("Réponse"); | |
yAxis.setLabel("Nombre de fois répondue"); | |
updateData(); | |
final Scene scene = new Scene(barChart); | |
barChart.setData(FXCollections.observableArrayList(series)); | |
// barChart.getData().add(series); | |
fxPanel.setScene(scene); | |
// barChart.getStylesheets().add(getClass().getResource("/config/colors.css").toExternalForm()); | |
}); | |
} | |
public void setIdQuiz(int idQuiz){ | |
quiz = Quizzes.getInstance().getQuizById(idQuiz); | |
} | |
public void setIdQuestion(int idQuestion){ | |
question = quiz.getQuestionById(idQuestion); | |
questionName = nbQuestion + question.getQuestion(); | |
} | |
public void setNbQuestion(String nbQuestion){ | |
this.nbQuestion = nbQuestion; | |
} | |
public void updateData(){ | |
Platform.runLater(() -> { | |
barChart.setTitle(questionName); | |
int count = 0; | |
boolean temp = false; | |
String proposal; | |
// clearLabels(); | |
series.getData().clear(); | |
// barChart.getData().clear(); | |
List<Integer> listGoodAnswers = QuizProcess.getInstance().getGoodAnswersForQuestion(quiz.getId(), question.getId()); | |
for(Proposal p : question.getListProposals()){ | |
count++; | |
for(int id : listGoodAnswers){ | |
if(p.getId() == id){ | |
temp = true; | |
break; | |
} | |
} | |
final boolean goodAnswer = temp; | |
final int nbAnswersForProposal = Answers.getInstance().getNbAnswersForProposal(quiz.getId(), question.getId(), p.getId()); | |
// proposal = p.getProposal(); | |
// StringBuilder sb = new StringBuilder(proposal); | |
// int i = 0; | |
// while ((i = sb.indexOf(" ", i + 30)) != -1) | |
// sb.replace(i, i + 1, "\n"); | |
final XYChart.Data<String, Number> data = new XYChart.Data(Integer.toString(count), nbAnswersForProposal); | |
data.nodeProperty().addListener(new ChangeListener<Node>() { | |
@Override | |
public void changed(ObservableValue<? extends Node> ov, Node oldNode, final Node node) { | |
if (node != null) { | |
if(goodAnswer) | |
setNodeStyle(data, good); | |
else | |
setNodeStyle(data, wrong); | |
if(nbAnswersForProposal > 0) | |
displayLabelForData(data); | |
} | |
} | |
}); | |
series.getData().add(data); | |
} | |
}); | |
} | |
/** Change color of bar if value of i is <5 then red, if >5 then green if i>8 then blue */ | |
private void setNodeStyle(XYChart.Data<String, Number> data, String color) { | |
Node node = data.getNode(); | |
node.setStyle("-fx-bar-fill: " + color + ";"); | |
// if (data.getYValue().intValue() > 8) { | |
// node.setStyle("-fx-bar-fill: -fx-exceeded;"); | |
// } else if (data.getYValue().intValue() > 5) { | |
// node.setStyle("-fx-bar-fill: -fx-achieved;"); | |
// } else { | |
// node.setStyle("-fx-bar-fill: -fx-not-achieved;"); | |
// } | |
} | |
// private void clearLabels(){ | |
// barChart | |
// } | |
/** places a text label with a bar's value above a bar node for a given XYChart.Data */ | |
private void displayLabelForData(XYChart.Data<String, Number> data) { | |
final Node node = data.getNode(); | |
final Text dataText = new Text(data.getYValue() + ""); | |
node.parentProperty().addListener(new ChangeListener<Parent>() { | |
@Override public void changed(ObservableValue<? extends Parent> ov, Parent oldParent, Parent parent) { | |
Group parentGroup = (Group) parent; | |
parentGroup.getChildren().add(dataText); | |
} | |
}); | |
node.boundsInParentProperty().addListener(new ChangeListener<Bounds>() { | |
@Override public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds) { | |
dataText.setLayoutX( | |
Math.round( | |
bounds.getMinX() + bounds.getWidth() / 2 - dataText.prefWidth(-1) / 2 | |
) | |
); | |
dataText.setLayoutY( | |
Math.round( | |
bounds.getMinY() - dataText.prefHeight(-1) * 0.5 | |
) | |
); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/34286062/how-to-clear-text-added-in-a-javafx-barchart