2015-01-07 42 views
1

enter image description here我想要繪製使用折線圖的javafx條形圖。每個小節線都使用一個用兩點繪製的垂直線和線符號去除。在我的應用程序中可能有很多系列(Bar line),但只想顯示兩個圖例。如何刪除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; 
} 

請幫我刪除的傳說或縮小的地方增加了傳說的成分。非常感謝

+0

該代碼不可編譯。 – Roland

回答

3

由於您已經在處理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")); 

    ... 
} 
+0

我做了一個類似的變化,在java7和它的工作,謝謝Jose,Pereda – Kishore

+0

非常感謝,這正是我想要更新我的PieChart的圖例元素中的樣式。 – DominikStyp