Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
how to add 3 legends for a single series barchart?? JAVAFX

HassanShakeel
Member Posts: 4
Here is my code to generate 10 bars of different colors. I want to add legend respectively but it only one shows yellow legend
1. I think it shows only 1 color because there is only 1 series. Is it possible to add more than 1 legend for a single series?
or
2. or can i display another image for legend in barchart??
output :http://i.stack.imgur.com/fSNu7.png
file i want to display in barchart:http://i.stack.imgur.com/cchch.png
1. I think it shows only 1 color because there is only 1 series. Is it possible to add more than 1 legend for a single series?
or
2. or can i display another image for legend in barchart??
output :http://i.stack.imgur.com/fSNu7.png
file i want to display in barchart:http://i.stack.imgur.com/cchch.png
.. public class DynamicallyColoredBarChart extends Application { @Override public void start(Stage stage) { final CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel("Bars"); final NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Value"); final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis); bc.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); for (int i = 0; i < 10; i++) { // change color of bar if value of i is >5 than red if i>8 than blue final XYChart.Data<String, Number> data = new XYChart.Data("Value " + i, i); data.nodeProperty().addListener(new ChangeListener<Node>() { @Override public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) { if (newNode != null) { if (data.getYValue().intValue() > 8) { newNode.setStyle("-fx-bar-fill: navy;"); } else if (data.getYValue().intValue() > 5) { newNode.setStyle("-fx-bar-fill: red;"); } } } }); series1.getData().add(data); } bc.getData().add(series1); stage.setScene(new Scene(bc)); stage.show(); } public static void main(String[] args) { launch(args); } } ...Edited by: 993431 on Mar 12, 2013 1:42 PM
Answers
-
Either:
1. Use a chart which displays multiple series, then you can allow the built-in legend to show OR
2. Use a single dynamically colored series have you have done and create your own custom legend.import javafx.application.Application; import javafx.scene.*; import javafx.scene.chart.*; import javafx.stage.Stage; public class ThreeSeriesBarChart extends Application { @Override public void start(Stage stage) { final CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel("Bars"); final NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Value"); final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis); XYChart.Series lowSeries = new XYChart.Series(); lowSeries.setName("Not Achieved"); XYChart.Series medSeries = new XYChart.Series(); medSeries.setName("Achieved"); XYChart.Series hiSeries = new XYChart.Series(); hiSeries.setName("Exceeded"); bc.setBarGap(0); bc.setCategoryGap(0); for (int i = 0; i < 10; i++) { final XYChart.Data<String, Number> data = new XYChart.Data("Value " + i, i); if (data.getYValue().intValue() > 8) { hiSeries.getData().add(data); } else if (data.getYValue().intValue() > 5) { medSeries.getData().add(data); } else { lowSeries.getData().add(data); } } bc.getData().setAll(lowSeries, medSeries, hiSeries); bc.getStylesheets().add(getClass().getResource("colored-chart.css").toExternalForm()); stage.setScene(new Scene(bc)); stage.show(); } public static void main(String[] args) { launch(args); } }
import javafx.application.Application; import javafx.beans.value.*; import javafx.geometry.Pos; import javafx.scene.*; import javafx.scene.chart.*; import javafx.scene.control.Label; import javafx.scene.layout.*; import javafx.scene.paint.Color; import javafx.scene.shape.*; import javafx.stage.Stage; public class DynamicallyColoredBarChart extends Application { @Override public void start(Stage stage) { final CategoryAxis xAxis = new CategoryAxis(); xAxis.setLabel("Bars"); final NumberAxis yAxis = new NumberAxis(); yAxis.setLabel("Value"); final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis); bc.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); for (int i = 0; i < 10; i++) { // change color of bar if value of i is >5 than red if i>8 than blue final XYChart.Data<String, Number> data = new XYChart.Data("Value " + i, i); data.nodeProperty().addListener(new ChangeListener<Node>() { @Override public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) { if (newNode != null) { if (data.getYValue().intValue() > 8) { newNode.setStyle("-fx-bar-fill: -fx-exceeded;"); } else if (data.getYValue().intValue() > 5) { newNode.setStyle("-fx-bar-fill: -fx-achieved;"); } else { newNode.setStyle("-fx-bar-fill: -fx-not-achieved;"); } } } }); series1.getData().add(data); } bc.getData().add(series1); LevelLegend legend = new LevelLegend(); legend.setAlignment(Pos.CENTER); VBox chartWithLegend = new VBox(); chartWithLegend.getChildren().setAll(bc, legend); chartWithLegend.getStylesheets().add(getClass().getResource("colored-chart.css").toExternalForm()); stage.setScene(new Scene(chartWithLegend)); stage.show(); } class LevelLegend extends GridPane { LevelLegend() { setHgap(10); setVgap(10); addRow(0, createSymbol("-fx-exceeded"), new Label("Exceeded")); addRow(1, createSymbol("-fx-achieved"), new Label("Achieved")); addRow(2, createSymbol("-fx-not-achieved"), new Label("Not Achieved")); getStyleClass().add("level-legend"); } private Node createSymbol(String fillStyle) { Shape symbol = new Ellipse(10, 5, 10, 5); symbol.setStyle("-fx-fill: " + fillStyle); symbol.setStroke(Color.BLACK); symbol.setStrokeWidth(2); return symbol; } } public static void main(String[] args) { launch(args); } }
/** colored-chart.css: place in same directory as other bar chart application files and setup your build system to copy it to the output directory */ .root { -fx-not-achieved: red; -fx-achieved: green; -fx-exceeded: blue; } .default-color0.chart-bar { -fx-bar-fill: -fx-not-achieved; } .default-color1.chart-bar { -fx-bar-fill: -fx-achieved; } .default-color2.chart-bar { -fx-bar-fill: -fx-exceeded; } .level-legend { -fx-padding: 10; -fx-border-width: 2; -fx-background-color: rgba(211, 211, 211, 0.5); -fx-border-color: derive(rgba(211, 211, 211, 0.7), 10%); }
-
Thank you so much your code works .. 1 more question is it possible to add value label of each bar on top of each bar??
and again thanks -
is it possible to add value label of each bar on top of each barYes, see: "how to display bar value on top of bar javafx"
http://stackoverflow.com/questions/15237192/how-to-display-bar-value-on-top-of-bar-javafx/15375168
This discussion has been closed.