gpt4 book ai didi

JavaFX 图表轴标签格式

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:36 24 4
gpt4 key购买 nike

我写了一个简单的程序来监控我的 ping。我目前正在使用具有自动测距功能的 NumberAxis,每次 ping 之后,我都会在末尾添加新数据,删除第一个数据并为 X 轴位置增加 totalCount 变量.

我希望 X 轴标签为:

  • 显示自开始以来耗时。因此,例如,以 0.25s ping 间隔进行的第 1100 次 ping 的标签将是 4m 35s
  • 显示自 ping 以来的时间。这将要求标签保持静止(不随情节移动)并且顺序相反。

这两个(最好是第一个)中的任何一个都可以实现吗?我想我必须为此使用 CategoryAxis,但我不确定如何创建无限数量的类别并选择仅显示完整分钟。是否可以保留 NumberAxis 以便更轻松地处理传入数据并仅更改标签文本格式?我已经有一种方法可以将秒数转换为 00h 00m 00s 格式。

还有一件事,我认为与自动范围有关,图表不会在每次输入后刷新,但只有在超过给定范围的 10% 时才会刷新。因此对于图片中的 1000 范围,它将绘制 100 个新的 ping,然后将所有内容向左移动 100 个位置。我能否以某种方式更改它以在 1 次 ping 后移动?

ping

不确定是否相关,但我会发布代码:

Controller

public class GuiController implements Initializable {

@FXML
Button startButton, stopButton;
@FXML
TextField sField, nField, ipField;
@FXML
LineChart<Integer, Integer> chart;
@FXML
Label timeLabel, pingLabel;

ScheduledService<Integer> scheduler;
ObservableList<Data<Integer, Integer>> data;
public static int totalCount = 0;

private String getTime(double seconds) {
int h = (int) (seconds / 3600);
int m = (int) ((seconds % 3600) / 60);
int s = (int) (seconds % 60);
return String.format("%dh %dm %ds", h, m, s);
}

public void start() {
if (sField.getText().isEmpty() || Double.parseDouble(sField.getText()) == 0)
sField.setText("0.1");
data = FXCollections.observableArrayList();
int size = Integer.parseInt(nField.getText());
stop = false;
flip();
XYChart.Series<Integer, Integer> series = new Series<>();
for (int i = 0; i < size; i++) {
series.getData().add(new XYChart.Data<Integer, Integer>(totalCount++, 0));
}
chart.getData().clear();
chart.getData().add(series);
scheduler.setPeriod(Duration.seconds(Double.parseDouble(sField.getText())));
scheduler.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

@Override
public void handle(WorkerStateEvent event) {
if (series.getData().size() >= size)
series.getData().remove(0);
series.getData().add(new XYChart.Data<>(totalCount++, scheduler.getValue()));
updatePingLabel(scheduler.getValue());
}
});
scheduler.restart();
}

public void stop() {
scheduler.cancel();
stop = true;
flip();
totalCount = 0;
}

public static boolean isNumeric(String str) {
return str.matches("?\\d+(\\.\\d+)?");
}

public void flip() {
ipField.setDisable(!ipField.isDisabled());
nField.setDisable(!nField.isDisabled());
sField.setDisable(!sField.isDisabled());
startButton.setDisable(!startButton.isDisabled());
stopButton.setDisable(!stopButton.isDisabled());
}

public void updatePingLabel(int ping) {
pingLabel.setText(ping + "ms");
if (ping < 80)
pingLabel.setTextFill(Color.LAWNGREEN);
if (ping >= 80 && ping < 150)
pingLabel.setTextFill(Color.GOLD);
if (ping >=150 && ping < 400)
pingLabel.setTextFill(Color.ORANGE);
if (ping >= 400)
pingLabel.setTextFill(Color.RED);
}


@Override
public void initialize(URL arg0, ResourceBundle arg1) {
chart.getXAxis().setVisible(false);
chart.getXAxis().setAutoRanging(true);
stopButton.setDisable(true);
chart.getYAxis().setAutoRanging(true);
sField.textProperty().addListener(new ParamsChangeListener());
nField.textProperty().addListener(new ParamsChangeListener());
scheduler = new ScheduledService<Integer>() {
@Override
protected Task<Integer> createTask() {
return new PingTask(ipField.getText());
}
};
}

class ParamsChangeListener implements ChangeListener<String> {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue,
String newValue) {
if (isNumeric(newValue))
timeLabel.setText(getTime(Double.parseDouble(sField.getText())
* Integer.parseInt(nField.getText())));
}
}
}

Ping 任务

public class PingTask extends Task<Integer> {

int time;

String address;

public PingTask(String text) {
address = text;
}

@Override
protected Integer call() throws Exception {
try {
String cmd = "";
if (System.getProperty("os.name").startsWith("Windows")) {
cmd = "ping -n 1 " + address;
} else {
cmd = "ping -c 1 " + address;
}
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String inputLine = in.readLine();
while ((inputLine != null)) {
if (inputLine.startsWith("Reply from")) {
String[] parts = inputLine.split("[ =ms]");
time = Integer.parseInt(parts[9]);
break;
}
inputLine = in.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
return time;
}
}

FXML

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.GuiController">
<center>
<LineChart fx:id="chart" alternativeRowFillVisible="false" animated="false" createSymbols="false" horizontalZeroLineVisible="false" legendVisible="false" maxHeight="1.7976931348623157E308" verticalGridLinesVisible="false" verticalZeroLineVisible="false" BorderPane.alignment="CENTER">
<xAxis>
<NumberAxis animated="false" forceZeroInRange="false" minorTickCount="0" minorTickLength="0.0" minorTickVisible="false" side="BOTTOM" tickMarkVisible="false" tickUnit="1.0" upperBound="200.0" />
</xAxis>
<yAxis>
<NumberAxis animated="false" autoRanging="false" forceZeroInRange="true" minorTickCount="0" minorTickLength="0.0" minorTickVisible="false" side="LEFT" tickLabelGap="5.0" tickUnit="20.0" />
</yAxis>
</LineChart>
</center>
<left>
<VBox alignment="TOP_CENTER" spacing="10.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER" VBox.vgrow="NEVER">
<children>
<Label text="IP " />
<TextField fx:id="ipField" prefWidth="100.0" text="euw.leagueoflegends.com">
<opaqueInsets>
<Insets />
</opaqueInsets>
</TextField>
</children>
<padding>
<Insets top="5.0" />
</padding>
</HBox>
<HBox alignment="CENTER" VBox.vgrow="NEVER">
<children>
<Label text="Ping co " />
<TextField fx:id="sField" alignment="TOP_RIGHT" prefWidth="60.0" text="0.25" HBox.hgrow="NEVER">
<HBox.margin>
<Insets right="5.0" />
</HBox.margin>
</TextField>
<Label text="s" />
</children>
<padding>
<Insets top="5.0" />
</padding>
</HBox>
<HBox alignment="CENTER">
<children>
<Label text="Rysuj " />
<TextField fx:id="nField" alignment="CENTER_RIGHT" prefWidth="60.0" text="1000" HBox.hgrow="NEVER" />
<Label text=" próbek" />
</children>
<padding>
<Insets top="5.0" />
</padding>
</HBox>
<HBox alignment="CENTER" VBox.vgrow="NEVER">
<children>
<Label text="Pokaż " />
<Label fx:id="timeLabel" text="0h 4m 10s" />
</children>
</HBox>
<HBox alignment="CENTER" spacing="10.0" VBox.vgrow="NEVER">
<children>
<Button fx:id="startButton" mnemonicParsing="false" onAction="#start" prefWidth="50.0" text="Start" />
<Button fx:id="stopButton" mnemonicParsing="false" onAction="#stop" prefWidth="50.0" text="Stop" />
</children>
</HBox>
<Label fx:id="pingLabel" text="0ms" textAlignment="CENTER">
<font>
<Font name="System Bold" size="40.0" />
</font>
</Label>
</children>
<padding>
<Insets left="5.0" right="5.0" />
</padding>
</VBox>
</left>
</BorderPane>

编辑

我尝试使用格式化程序,但在 application.GuiController$XAxisLabelConverter.toString(GuiController.java: 1) 我不知道该怎么办。

    class XAxisLabelConverter extends StringConverter<Integer> {

double interval;
int n;

public XAxisLabelConverter(double interval, int n) {
this.interval = interval;
this.n = n;
}

@Override
public Integer fromString(String arg0) {

return null;
}

@Override
public String toString(Integer value) {
if (value < n) {
return "";
} else {
return getTime(value.intValue() * interval);
}
}

}

start()方法中

    ((ValueAxis<Integer>) chart.getXAxis()).setTickLabelFormatter(new XAxisLabelConverter(
Double.parseDouble(sField.getText()),size));

最佳答案

您可以添加 formatter to a number axis .

您可能只想删除 x 轴。它并没有真正添加任何信息。如果您只是显示相隔 0.25 秒的最后 100 个 ping,那么您真的不需要轴来知道它们何时发生。

由于轴的范围,图表仅在 100 次 ping 后移动。间隔将取决于总范围。改变这个的唯一方法是关闭自动量程并自己设置最大、最小和大小。你可以用这个 constructor .

如果你想格式化你需要一个专门的转换器,因为你需要修改数字。 Chart<..,Number> 需要将数字转换为字符串,因此使用 StringConverter<Number> ,例如。

    xAxis.setTickLabelFormatter(new StringConverter<Number>() {
@Override
public String toString(Number object) {
return (object.intValue() * 0.25) + "s";
}

@Override
public Number fromString(String string) {
return 0;
}
});

附言。我只在 Windows 上使用过它,但您可以看看它的性能是否比新进程更好。

http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int)

关于JavaFX 图表轴标签格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740005/

24 4 0