我试图在每次用户从选择框中选择不同的项目时获得AreaChart的更新,但是有些错误,我不知道为什么。这是第一张图片,当第一个值作为默认值给出时(一切看起来都很好):
以下是代码:
FXMLDocumentController.java
package javafxapplication17;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML
private AreaChart fitimetChart;
@FXML
private ComboBox box;
@Override
public void initialize(URL url, ResourceBundle rb) {
fitimetChart.getXAxis().setAutoRanging(true);
fitimetChart.getYAxis().setAutoRanging(true);
box.getItems().addAll("Viti Paraprak","Nentor");
box.setValue("Viti Paraprak");
fitimetBox();
}
public void addFitimetNentorToChart()
{
double nje=10;
double dy=0;
double tre=20;
double kater=0;
double pes=2;
double gjashte=0;
double shtate=0;
double tete=10;
double nente=0;
double dhjete=0;
double njembedhjet=100;
double dymbedhjet=120;
double trembedhjet=40;
double katermbedhjet=40;
double pesembedhjet=80;
double gjashtembedhjet=87;
double shtatembedhjet=0;
double tetembedhjet=0;
double nentembedhjet=0;
double njezet=0;
double njezetenje=0;
double njezetedy=0;
double njezetetre=120;
double njezetekater=0;
double njezetepes=102;
double njezetegjashte=0;
double njezeteshtate=40;
double njezetetete=50;
double njezetenente=90;
double tridhjete=0;
XYChart.Series series = new XYChart.Series<>();
series.setName("Selami Hasani");
series.getData().add(new XYChart.Data("1", nje));
series.getData().add(new XYChart.Data("2", dy));
series.getData().add(new XYChart.Data("3", tre));
series.getData().add(new XYChart.Data("4", kater));
series.getData().add(new XYChart.Data("5", pes));
series.getData().add(new XYChart.Data("6", gjashte));
series.getData().add(new XYChart.Data("7", shtate));
series.getData().add(new XYChart.Data("8", tete));
series.getData().add(new XYChart.Data("9", nente));
series.getData().add(new XYChart.Data("10", dhjete));
series.getData().add(new XYChart.Data("11", njembedhjet));
series.getData().add(new XYChart.Data("12", dymbedhjet));
series.getData().add(new XYChart.Data("13", trembedhjet));
series.getData().add(new XYChart.Data("14", katermbedhjet));
series.getData().add(new XYChart.Data("15", pesembedhjet));
series.getData().add(new XYChart.Data("16", gjashtembedhjet));
series.getData().add(new XYChart.Data("17", shtatembedhjet));
series.getData().add(new XYChart.Data("18", tetembedhjet));
series.getData().add(new XYChart.Data("19", nentembedhjet));
series.getData().add(new XYChart.Data("20", njezet));
series.getData().add(new XYChart.Data("21", njezetenje));
series.getData().add(new XYChart.Data("22", njezetedy));
series.getData().add(new XYChart.Data("23", njezetetre));
series.getData().add(new XYChart.Data("24", njezetekater));
series.getData().add(new XYChart.Data("25", njezetepes));
series.getData().add(new XYChart.Data("26", njezetegjashte));
series.getData().add(new XYChart.Data("27", njezeteshtate));
series.getData().add(new XYChart.Data("28", njezetetete));
series.getData().add(new XYChart.Data("29", njezetenente));
series.getData().add(new XYChart.Data("30", tridhjete));
fitimetChart.getData().clear();
fitimetChart.getData().add(series);
fitimetChart.setTitle("Fitimet e Muajit Nentor");
}
public void addFitimetVjetoreToChart()
{
double janar=20;
double shkurt=30;
double mars=40;
double prill=0;
double maj=10;
double qershor=100;
double korrik=50;
double gusht=12;
double shtator=85;
double tetor=0;
double nentor=10;
double dhjetor=50;
XYChart.Series series = new XYChart.Series<>();
series.setName("Selami Hasani");
series.getData().add(new XYChart.Data("Janar", janar));
series.getData().add(new XYChart.Data("Shkurt", shkurt));
series.getData().add(new XYChart.Data("Mars", mars));
series.getData().add(new XYChart.Data("Prill", prill));
series.getData().add(new XYChart.Data("Maj", maj));
series.getData().add(new XYChart.Data("Qershor", qershor));
series.getData().add(new XYChart.Data("Korrik", korrik));
series.getData().add(new XYChart.Data("Gusht", gusht));
series.getData().add(new XYChart.Data("Shtator", shtator));
series.getData().add(new XYChart.Data("Tetor", tetor));
series.getData().add(new XYChart.Data("Nentor", nentor));
series.getData().add(new XYChart.Data("Dhjetor", dhjetor));
fitimetChart.getData().clear();
fitimetChart.getData().add(series);
}
public void clearChart()
{
fitimetChart.getData().clear();
}
@FXML
public void fitimetBox()
{
if(box.getSelectionModel().isSelected(0))
{
clearChart();
addFitimetVjetoreToChart();
}
if(box.getSelectionModel().isSelected(1))
{
clearChart();
addFitimetNentorToChart();
}
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.AreaChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication17.FXMLDocumentController">
<center>
<VBox alignment="CENTER" BorderPane.alignment="CENTER">
<children>
<ComboBox fx:id="box" onAction="#fitimetBox" prefWidth="150.0" />
<AreaChart fx:id="fitimetChart" title="Fitimet">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</AreaChart>
</children>
</VBox>
</center>
</BorderPane>
JavaFXApplication17.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication17 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
P.S。我做了clearChart()方法,因为如果我不首先清除图表,它只是继续编写新的XData,而不清除第一个。
当用户点击每个折线图顶部的小圆圈时,也可能显示数字值(Y)
答案 0 :(得分:1)
我只需要更新Java版本,现在一切都很好。
答案 1 :(得分:0)