javafx - 如何删除javafx折线图中的图例

标签 javafx javafx-2 javafx-8

enter image description here我正在尝试使用折线图绘制 javafx 条形图。每条条线均使用删除两点和线符号的垂直线绘制。我的应用程序中可能有很多系列(条形线),但只想显示两个图例。

目前,由于添加了许多系列,因此显示了图例。不知何故,我只能显示两个图例并隐藏其他图例。但现在隐藏图例使用的空间存在问题。

我当前的代码如下:-

    package graph;

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.XYChart;
    import javafx.scene.control.Tooltip;
    import javafx.stage.Stage;

    import com.sun.javafx.charts.Legend;


    public class BarGraphUsingLineChart extends Application {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    final MyLineChart<Number,Number> lineChart = 
            new MyLineChart<Number,Number>(xAxis,yAxis);

    private boolean valid=true;

    private boolean invalid=true;

    @Override public void start(Stage stage) {
        stage.setTitle("Bar Chart Using Lines");

        xAxis.setLabel("Month");

        lineChart.setTitle("BAR CHART DEMO");           


        ObservableList<XYChart.Series<Number,Number>> graphData = FXCollections.observableArrayList();

        for(int i=1; i<=10;i++)
        {
            if(i%2==0)
            {
                graphData.add(drawBarline(i*10, i*5, true));
            }
            else{
                graphData.add(drawBarline(i*10, i*5, false));
            }
        }
        //      Dont show symbol of line charts
        lineChart.setCreateSymbols(false);

        Scene scene  = new Scene(lineChart,800,600);       
        lineChart.setData(graphData);
        stage.setScene(scene);
        stage.getScene().getStylesheets().add("/graph/BarChart.css");

        updateStyleSheet();

        stage.show();
    }

    private XYChart.Series<Number, Number>  drawBarline(Number xAxis, Number yAxis, boolean valid)
    {

        XYChart.Series<Number, Number> channel_Series  = new XYChart.Series<Number, Number>();

        channel_Series.getData().add(new XYChart.Data<Number, Number>(yAxis, xAxis ));  

        channel_Series.getData().add(new XYChart.Data<Number, Number>(yAxis, 0.0 ));    

        if(valid)   {
            channel_Series.setName("Valid"); 
        }
        else
        {
            channel_Series.setName("Invalid");
        }

        return channel_Series;
    }
    private void updateStyleSheet()
    {
        for(Node symbol : lineChart.lookupAll(".chart-legend-item")){

            if(valid)
            {
                ((Legend)symbol.getParent()).getItems().get(0).setText("Valid");
                valid=false;
            }
            else if(invalid){
                ((Legend)symbol.getParent()).getItems().get(1).setText("Invalid"); 
                invalid=false;
            }
            else
            {
                symbol.setVisible(false);

            }
        }


        // Beloc code removes all the legends
        //lineChart.setLegendVisible(false);

        for (XYChart.Series<Number, Number> s : lineChart.getData()) {

            if(("Valid").equals(s.getName()))
            {
                s.getNode().setStyle("-fx-stroke: #0000FF; ");
            }
            else 
            {
                s.getNode().setStyle("-fx-stroke: #FF0000; ");
            }
            for (XYChart.Data<Number, Number> d : s.getData()) {
                Tooltip.install(d.getNode(), new Tooltip("Frequency: "+
                        d.getXValue()+ " THz, Power: "+
                        d.getYValue().doubleValue()+" unit"));
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

BarChart.css contains are as below:-

.default-color0.chart-legend-item-symbol{
    -fx-background-color: #0000FF;
 }
.default-color1.chart-legend-item-symbol{
    -fx-background-color: #FF0000;
 }

请帮我删除图例或缩小添加图例的组件。非常感谢

最佳答案

由于您已经在处理Legend,因此您可以使用其项目,删除不需要的项目,因此图例仅显示两个项目。

例如,使用流,您可以将前两项标记为“有效”/“无效”,将其余项标记为“删除”,最后只需删除最后这些项目。

private void updateStyleSheet() {
    Legend legend = (Legend)lineChart.lookup(".chart-legend");
    AtomicInteger count = new AtomicInteger();
    legend.getItems().forEach(item->{
        if(count.get()==0){
            item.setText("Valid");
        } else if(count.get()==1){
            item.setText("Invalid");
        } else {
            item.setText("Remove");
        }
        count.getAndIncrement();
    });
    legend.getItems().removeIf(item->item.getText().equals("Remove"));

    ...
}

关于javafx - 如何删除javafx折线图中的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818036/

相关文章:

java - 尝试在JavaFX中切换GraphicsContext的上下文

java - 将 TableColumn 的宽度绑定(bind)到标题或内容的宽度,以较高者为准?

image - 将图像添加到特定位置的按钮 JavaFX

JavaFX webview 不保存 cookie

java - 如何在创建的原始窗口内的 Controller 之间进行转换

css - 如何从javafx中的一个样式表为具有相同类的节点设置不同的样式

java - 在 JavaFX 2.0 中将标签置于节点的中心

java - 如何在 javaFX 2.0 应用程序中包装 swing 组件

css - Java FX TitledPane .title CSS 选择器

java - 如何从方法中选择随机图像进行显示?