我有这个折线图,它使用JavaFX任务显示一些数据:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
public class LineChartSample extends Application {
@Override public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
//defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
//creating the chart
final LineChart<Number,Number> lineChart =
new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
series.getData().add(new XYChart.Data(5, 34));
series.getData().add(new XYChart.Data(6, 36));
series.getData().add(new XYChart.Data(7, 22));
series.getData().add(new XYChart.Data(8, 45));
series.getData().add(new XYChart.Data(9, 43));
series.getData().add(new XYChart.Data(10, 17));
series.getData().add(new XYChart.Data(11, 29));
series.getData().add(new XYChart.Data(12, 25));
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
当我必须插入行值时,如果我有空数据,我感兴趣的是如何显示标签&#34;没有数据&#34;进入图表?
有这样的例子吗?
答案 0 :(得分:2)
有趣的问题,因为XYChart.Data是最终的,我取得的最好成绩是:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class LineChartSample extends Application {
static void checkNoData(XYChart.Series<Integer,Double> series) {
double d1 = 0;
XYChart.Data<Integer,Double> last = null;
for (Object data : series.getData()) {
if (data instanceof XYChart.Data<?,?>) {
XYChart.Data<Integer,Double> cdata = (XYChart.Data<Integer,Double>)data;
if (last != null && last.getYValue() == null) {
double mid = (d1 + cdata.getYValue())/2;
last.setYValue(mid);
Text nodata = new Text("no data");
nodata.setTranslateY(nodata.getLayoutBounds().getHeight()/2);
last.setNode(nodata);
}
if (last != null) d1 = last.getYValue();
last = cdata;
}
}
if (last != null && last.getYValue() == null) {
last.setYValue(d1);
Text nodata = new Text("no data");
nodata.setTranslateY(nodata.getLayoutBounds().getHeight()/2);
last.setNode(nodata);
}
}
@Override public void start(Stage stage) {
stage.setTitle("Line Chart Sample");
//defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
//creating the chart
final LineChart<Number,Number> lineChart =
new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
series.getData().add(new XYChart.Data(1, 23.0));
series.getData().add(new XYChart.Data(2, 14.0));
series.getData().add(new XYChart.Data(3, 15.0));
series.getData().add(new XYChart.Data(4, 24.0));
series.getData().add(new XYChart.Data(5, null));
series.getData().add(new XYChart.Data(6, 36.0));
series.getData().add(new XYChart.Data(7, 22.0));
series.getData().add(new XYChart.Data(8, 45.0));
series.getData().add(new XYChart.Data(9, 43.0));
series.getData().add(new XYChart.Data(10, 17.0));
series.getData().add(new XYChart.Data(11, 29.0));
series.getData().add(new XYChart.Data(12, 25.0));
checkNoData(series);
Scene scene = new Scene(lineChart,800,600);
lineChart.getData().add(series);
GUIHelper.allowImageDrag(lineChart);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}