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.
My current code is as below:-
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
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.