java当鼠标选中时,如何为TableView中的每一行创建PieChartFX?
如何将数据从javaFX中的表视图传递到PieChartFX
当鼠标选中时,我想在饼图中显示相同的行值
我想为每一行向PieChartFX插入数据,无论何时通过鼠标单击或鼠标释放选择特定的行
这是我的密码
package javaapplication26;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXjavaapplication26_TableView extends Application {
public TableView < Metrics > tableView = new TableView<Metrics>();
private ObservableList< Metrics > dataList = FXCollections.observableArrayList(
// /do something like this new Metrics(name,WMC,DIT,NOC ,CBO,RFC,LCOM , Ce, NPM),
// i want to add Metrics ADDED DYNAMICALLY after file is parsed
);
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("WMC", 100),
new PieChart.Data("DIT", 200),
new PieChart.Data("NOC", 50),
new PieChart.Data("CBO", 75),
new PieChart.Data("RFC", 110),
new PieChart.Data("LCOM", 300),
new PieChart.Data("Ca", 111),
new PieChart.Data("NPM", 30)
);
PieChart pieChart = new PieChart(pieChartData);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
TableColumn name = new TableColumn("name");
name.setCellValueFactory( new PropertyValueFactory<Metrics, String>("name"));
TableColumn WMCCol = new TableColumn("WMC");
WMCCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("WMC"));
TableColumn DITCol = new TableColumn("DIT");
DITCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("DIT"));
TableColumn NOCCol = new TableColumn("NOC");
NOCCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("NOC"));
TableColumn CBOCol = new TableColumn("CBO");
CBOCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("CBO"));
TableColumn RFCCol = new TableColumn("RFC");
RFCCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("RFC"));
TableColumn LCOMCol = new TableColumn("LCOM");
LCOMCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("LCOM"));
TableColumn ceCol = new TableColumn("Ca");
ceCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("ce"));
TableColumn NPMCol = new TableColumn("NPM");
NPMCol.setCellValueFactory( new PropertyValueFactory<Metrics, Double>("NPM"));
tableView.setItems(dataList);
tableView.getColumns().addAll(name,WMCCol,DITCol,NOCCol,CBOCol,RFCCol,LCOMCol,ceCol,NPMCol);
// PieChart pieChart = new PieChart(pieChartData);
VBox vBox = new VBox();
vBox.setSpacing(10);
vBox.getChildren().add(pieChart);
vBox.getChildren().add(tableView);
root.getChildren().add(vBox);
primaryStage.setScene(new Scene(root, 900, 900));
primaryStage.show();
List<Metrics> metric = readMetricFromCSV("C:\\Users\\acer\\Desktop\\11.txt");
// let's print all the metric read from CSV file
tableView.getItems().addAll(metric) ;
// pieChart.getItems().addAll(metric) ;
// ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
for (Metrics m : metric) {
// System.out.println(m);
}
}
public List<Metrics> readMetricFromCSV(String fileName) {
List<Metrics> metricsss = new ArrayList<>();
// tableView.getItems().addAll(metricsss);
Path pathToFile = Paths.get(fileName);
// create an instance of BufferedReader
// using try with resource, Java 7 feature to close resources
try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) {
br.readLine();
String line1=null;
// read the first line from the text file
String line = br.readLine();
while (line != null) { // loop until all lines are read
String[] attributes = line.split(" "); // the file, using a comma as the delimiter
Metrics valueOfMetric = createMetric(attributes);
metricsss.add(valueOfMetric); // adding metric into ArrayList
//skip empty line
// line.isEmpty() || line.trim().equals("") || line.trim().equals("\n"))
br.readLine();
line = br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return metricsss;
}
private Metrics createMetric(String[] metadata) {
String name = metadata[0];
int WMC = Integer.parseInt(metadata[1]);
int DIT = Integer.parseInt(metadata[2]);
int NOC = Integer.parseInt(metadata[3]);
int CBO = Integer.parseInt(metadata[4]);
int RFC = Integer.parseInt(metadata[5]);
int LCOM= Integer.parseInt(metadata[6]);
int Ce = Integer.parseInt(metadata[7]);
int NPM = Integer.parseInt(metadata[8]);
return new Metrics(name,WMC,DIT,NOC,CBO,RFC,LCOM,Ce,NPM);//,cc
}
public class Metrics {
private String name;
private int WMC;
private int DIT;
private int NOC;
private int CBO;
private int RFC;
private int LCOM;
private int Ce;
private int NPM;
public Metrics( String name,int WMC,int DIT,int NOC,int CBO,int RFC,int LCOM, int Ce, int NPM) {
this.name = name;
this.WMC = WMC;
this.DIT = DIT;
this.NOC = NOC;
this.CBO = CBO;
this.RFC = RFC;
this.LCOM = LCOM;
this.Ce = Ce;
this.NPM = NPM;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWMC() {
return WMC;
}
public void setWMC(int WMC) {
this.WMC = WMC;
}
public int getDIT() {
return DIT;
}
public void setDIT(int DIT) {
this.DIT = DIT;
}
public int getNOC() {
return NOC;
}
public void setNOC(int NOC) {
this.NOC = NOC;
}
public int getCBO() {
return CBO;
}
public void setCBO(int CBO) {
this.CBO = CBO;
}
public int getRFC() {
return RFC;
}
public void setRFC(int RFC) {
this.RFC = RFC;
}
public int getLCOM() {
return LCOM;
}
public void setLCOM(int LCOM) {
this.LCOM = LCOM;
}
public int getCe() {
return Ce;
}
public void setCe(int ce) {
Ce = ce;
}
public int getNPM() {
return NPM;
}
public void setNPM(int NPM) {
this.NPM = NPM;
}
}
}
# 1 楼答案
Pie Chart java example
我已经添加了以上代码行,以使用list创建observaleList
以上情况正在运行,如果行为空怎么办?我也改变了这个。(抱歉没有解释我不太懂英语)
对于图表,我们需要为图表创建数据集。setData(pieChartData); 参考下面的图片,你会明白的