如何在JavaFX折线图中删除图例 [英] How to remove legends in javafx line chart

查看:85
本文介绍了如何在JavaFX折线图中删除图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用折线图绘制javafx条形图.每条条线都使用一条垂直线绘制,该垂直线已删除了两个点并删除了线符号.我的应用程序中可能有很多系列(条形线),但只想显示两个图例.

I am trying to plot javafx bar graph using line chart. Each bar line is drawn using a vertical line drawn with two points and line symbol removed. There could be many series(Bar line) in my application but want to show only two legends only.

当前显示了图例,其中添加了许多系列.不知何故,我只能显示两个图例而隐藏其他图例.但是现在隐藏的图例使用的空间存在问题.

Currently legends were shown as many series been added. Somehow i am able to show only two legends and hided others. But now problem exist with spaces used by hided legends.

我当前的代码如下:-

    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;
 }

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

Please help me to remove legends or shrink the components where legends are been added. Thanks alot

推荐答案

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

Since you are already dealing with Legend, you can work with its items, removing those you don't need, so the legend shows only two items.

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

Using streams, you can mark the first two items as "Valid"/"Invalid" and the rest as "Remove", for instance, and finally you just remove these last items.

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折线图中删除图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆