Lines grid JavaFX 8.0 LineChart
Hi all. I did the update to JavaFX 8.0 and the LineCharts grid lines have become dashed. I want them to be strong, as they were before the update. Can someone help me? Thank you
This can be corrected using the following CSS rule:
.chart-horizontal-grid-lines, .chart-vertical-grid-lines { -fx-stroke-dash-array: null; }
Moreover, the graphic object, you can search for content that have these two classes of style and set manually running dash array to an empty array.
Tags: Java
Similar Questions
-
How to remove the lines/grid texture 3D
I am trying to change a texture, but if you change the photoshop texture shows these black lines that get in the way. Is there a way to delete these?
If you set your 3d workspace, you can uncheck the box UV overlays from the right panel.
I hope this helps!
-
JAPI for essbase grid line limit
Hi Experts,
My grid is supposed to pick up about 1400 lines that can be performed with add essbase.
But I got error max outdated lines, when I try to retrieve data by opening a CubeView in Java.
Error is like this:
GRAVE: Cannot perform view cube operation. Error of Essbase. Maximum lines error number [1000] exceeded.
My piece of code is like this:
OPZ = cv.createIEssOpZoomIn ();
opz.setPreference (true, IEssOpZoomIn.EEssZoomInPreference.BOTTOM_LEVEL);
cv.performOperation (opz);
I couldn't actually find how to change the limit of max line grid, can you please advise?
Thanks in advance,
AhmetIbrahima wrote:
GRAVE: Cannot perform view cube operation. Error of Essbase. Maximum lines error number [1000] exceeded.Hi Ahmet,
The row limit is in the essbase.properties file. Here's mine set to return an unlimited number of lines:
service.olap.dataQuery.grid.maxRows = 0
If you use the APS mode, the essbase.properties file that you need to change is your APS Server. After you make the change, don't forget to restart your APS Server. If you use connections in embedded/direct mode, you must put the essbase.properties file in your CLASSPATH.
Tim Tow
Applied OLAP, Inc. -
LineChart resize in Voletfractionne
By adding a vertical split pane in a Voletfractionne, this overlap the line chart drawn.
What I would like to have is a line chart that get height resized when I go up and down the divider: I think that a listener would do the job, but of course, I am wrong in my code below.
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.chart.Chart; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.control.SplitPane; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.scene.text.Text; import javafx.stage.Stage; /** * * @author Alberto Pedroni <[email protected]> */ public class LineChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); // defining the axes final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis); // defining a series XYChart.Series<Number, Number> series = new XYChart.Series<>(); // populating the series with data series.getData().add(new XYChart.Data<Number, Number>(1, 23)); series.getData().add(new XYChart.Data<Number, Number>(2, 14)); series.getData().add(new XYChart.Data<Number, Number>(3, 15)); series.getData().add(new XYChart.Data<Number, Number>(4, 24)); series.getData().add(new XYChart.Data<Number, Number>(5, 34)); series.getData().add(new XYChart.Data<Number, Number>(6, 36)); series.getData().add(new XYChart.Data<Number, Number>(7, 22)); series.getData().add(new XYChart.Data<Number, Number>(8, 45)); series.getData().add(new XYChart.Data<Number, Number>(9, 43)); series.getData().add(new XYChart.Data<Number, Number>(10, 17)); series.getData().add(new XYChart.Data<Number, Number>(11, 29)); series.getData().add(new XYChart.Data<Number, Number>(12, 25)); final Pane root = new Pane(); SplitPane sp = new SplitPane(); sp.setOrientation(Orientation.VERTICAL); sp.setDividerPosition(0, 0.7); final Pane pane1 = new Pane(); lineChart.getData().add(series); lineChart.setPrefSize(800, 600); lineChart.setLegendVisible(false); pane1.getChildren().add(lineChart); pane1.setPrefSize(800, 600); Pane pane2 = new Pane(); root.heightProperty().addListener(new ChangeListener<Number>() { @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) { lineChart.setPrefSize(pane1.getPrefWidth(), pane1.getPrefHeight()); } }); sp.getItems().addAll(pane1, pane2); root.getChildren().add(sp); Scene scene = new Scene(root, 800, 600); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
In this case, maybe use an AnchorPane for Pane1 and anchor your chart to it.
final AnchorPane pane1 = new AnchorPane(); pane1.setTopAnchor(lineChart, 0.0); pane1.setBottomAnchor(lineChart, 0.0);
-
LineChart change color dynamically
I have a StackPane with a line chart and a button: I would like to change the color by clicking on a button, that is, from white to black.
How to get there?
Thank you.
import java.util.Set; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import static javafx.scene.paint.Color.RED; import javafx.stage.Stage; /** * * @author Alberto */ public class SimpleLineChart extends Application { @Override public void start(Stage primaryStage) { final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis); XYChart.Series series = new XYChart.Series(); series.getData().add(new XYChart.Data(1, 100)); series.getData().add(new XYChart.Data(2, 200)); series.getData().add(new XYChart.Data(3, 50)); series.getData().add(new XYChart.Data(4, 75)); series.getData().add(new XYChart.Data(5, 110)); series.getData().add(new XYChart.Data(6, 300)); series.getData().add(new XYChart.Data(7, 111)); series.getData().add(new XYChart.Data(8, 30)); series.getData().add(new XYChart.Data(9, 75)); series.getData().add(new XYChart.Data(10, 55)); series.getData().add(new XYChart.Data(11, 225)); series.getData().add(new XYChart.Data(12, 99)); lineChart.setCreateSymbols(false); lineChart.setAlternativeColumnFillVisible(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setLegendVisible(false); lineChart.setAnimated(false); lineChart.setVerticalZeroLineVisible(true); lineChart.setEffect(null); lineChart.getData().add(series); String color = "White"; String lineColor = "-fx-stroke: " + color + ";"; Set<Node> lineNode = lineChart.lookupAll(".series0"); for (final Node line : lineNode) { line.setStyle(lineColor); } StackPane sp = new StackPane(); Button btn = new Button(); btn.setText("test"); btn.setOnMousePressed(new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent me){ System.out.println("prova print"); } }); sp.getChildren().addAll(lineChart, btn); Scene scene = new Scene(sp, primaryStage.getWidth(), primaryStage.getHeight()); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
You already have the code to change the color of the line on the graph and the code to respond to the key (even if you use onAction, not onMousePressed). Just put it together:
btn.setOnAction(new EventHandler
() { @Override public void handle(ActionEvent event) { Set lineNode = lineChart.lookupAll(".series0"); for (final Node line : lineNode) { line.setStyle("-fx-stroke: black;"); } } }); -
Get rid of BarChart legend and create a horizontal line to a specific value
Hey,.
I work with barregraphes.
1st screen: I want to disable the legend of my Barchart.
http://imgur.com/wMo6Tfv, DRiNA9C
Second screen: I want to create a line in my Barchart on a specific value 700 as seen on the screen.
http://imgur.com/wMo6Tfv, DRiNA9C #1
Is this Possible? Also: what object to set the ID to change the color bar. I tried
Chart histogram;
barchart.setId ("Chart");
CSS:
#barchart
{
-fx-background-color: red;
}
But it did not work. Thank you!No, you do not miss anything. I had a temporary brain freeze. You can add nodes directly to an arbitrary region.
If this makes it a little harder. You need to add the line to the container in which your chart is maintained, and of course this means that you understand the coordinates of the line relative to this container. To simplify a bit, axis has a scale property of conversion of units of the axis to display units, and also according to the docs of css, the table has a child with the css class "table-horizontal-zero line." So one possible strategy is to enter this line and figure the change in the coordinates needed him. There is still work to do to find the correct coordinates relative to the container, and if you want it to run with things move (for example, when the user resizes the window), you need to do a lot of link.
This seems to work:
import java.util.Arrays; import java.util.List; import javafx.application.Application; import javafx.beans.binding.DoubleBinding; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Region; import javafx.scene.shape.Line; import javafx.stage.Stage; import javafx.util.StringConverter; public class LineChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(); yAxis.setTickLabelFormatter(new StringConverter
() { @Override public Number fromString(String string) { return Double.parseDouble(string); } @Override public String toString(Number value) { return String.format("%2.2f", value); } }); xAxis.setLabel("Month"); final LineChart lineChart = new LineChart ( xAxis, yAxis); lineChart.setTitle("Stock Monitoring, 2010"); lineChart.setAnimated(false); final XYChart.Series series = new XYChart.Series<>(); series.setName("My portfolio"); final List > data = Arrays.asList( new XYChart.Data ("Jan", 23), new XYChart.Data ("Feb", 14), new XYChart.Data ("Mar", 15), new XYChart.Data ("Apr", 24), new XYChart.Data ("May", 34), new XYChart.Data ("Jun", 36), new XYChart.Data ("Jul", 22), new XYChart.Data ("Aug", 45), new XYChart.Data ("Sep", 43), new XYChart.Data ("Oct", 17), new XYChart.Data ("Nov", 29), new XYChart.Data ("Dec", 25)); series.getData().addAll(data); final AnchorPane chartContainer = new AnchorPane(); AnchorPane.setTopAnchor(lineChart, 0.0); AnchorPane.setBottomAnchor(lineChart, 0.0); AnchorPane.setLeftAnchor(lineChart, 0.0); AnchorPane.setRightAnchor(lineChart, 0.0); chartContainer.getChildren().add(lineChart); Button button = new Button("Show line"); button.setOnAction(new EventHandler () { @Override public void handle(ActionEvent event) { final double lineLevel = 35; final Region chartRegion = (Region) lineChart .lookup(".chart-plot-background"); final Line zeroLine = (Line) lineChart .lookup(".chart-horizontal-zero-line"); final DoubleProperty startX = new SimpleDoubleProperty(0); final DoubleProperty endX = new SimpleDoubleProperty(0); final DoubleProperty y = new SimpleDoubleProperty(0); startX.bind(new DoubleBinding() { { super.bind(chartRegion.boundsInParentProperty()); } @Override protected double computeValue() { double x = chartRegion.getBoundsInParent().getMinX(); for (Node n = zeroLine.getParent().getParent(); n != chartContainer && n.getParent() != null; n = n.getParent()) { x += n.getBoundsInParent().getMinX(); } return x; } }); endX.bind(new DoubleBinding() { { super.bind(chartRegion.boundsInParentProperty()); } @Override protected double computeValue() { double x = chartRegion.getBoundsInParent().getMaxX(); for (Node n = zeroLine.getParent().getParent(); n != chartContainer && n.getParent() != null; n = n.getParent()) { x += n.getBoundsInParent().getMinX(); } return x; } }); y.bind(new DoubleBinding() { { super.bind(chartRegion.boundsInParentProperty(), yAxis.scaleProperty(), zeroLine.startYProperty()); } @Override protected double computeValue() { double y = zeroLine.getStartY() + lineLevel * yAxis.getScale(); for (Node n = zeroLine.getParent().getParent(); n != chartContainer && n.getParent() != null; n = n.getParent()) { y += n.getBoundsInParent().getMinY(); } return y; } }); Line line = new Line(); line.startXProperty().bind(startX); line.endXProperty().bind(endX); line.startYProperty().bind(y); line.endYProperty().bind(y); chartContainer.getChildren().add(line); } }); BorderPane root = new BorderPane(); root.setCenter(chartContainer); root.setTop(button); Scene scene = new Scene(root, 800, 600); lineChart.getData().add(series); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } -
XY LineChart move left/right
In the code below that line can be moved (dragged) left / right side of the left mouse click and drag
My question is: how to modify this code so to stop this movement left / right when the X value is == 0 (no need to strengthen negative X values) and X == end value for NumberAxis? (in this case is 16).import javafx.application.Application; import javafx.beans.property.SimpleDoubleProperty; import javafx.event.EventHandler; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; public class XYMove extends Application { BorderPane pane; XYChart.Series series1 = new XYChart.Series(); SimpleDoubleProperty rectinitX = new SimpleDoubleProperty(); SimpleDoubleProperty rectX = new SimpleDoubleProperty(); SimpleDoubleProperty rectY = new SimpleDoubleProperty(); @Override public void start(Stage stage) { final NumberAxis xAxis = new NumberAxis(4, 16, 1); final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); xAxis.setAnimated(false); yAxis.setAnimated(false); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { @Override public String toString(Number object) { return String.format("%7.5f", object); } }); final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setAnimated(false); lineChart.setLegendVisible(false); series1.getData().add(new XYChart.Data(1, 0.53185)); series1.getData().add(new XYChart.Data(2, 0.532235)); series1.getData().add(new XYChart.Data(3, 0.53234)); series1.getData().add(new XYChart.Data(4, 0.538765)); series1.getData().add(new XYChart.Data(5, 0.53442)); series1.getData().add(new XYChart.Data(6, 0.534658)); series1.getData().add(new XYChart.Data(7, 0.53023)); series1.getData().add(new XYChart.Data(8, 0.53001)); series1.getData().add(new XYChart.Data(9, 0.53589)); series1.getData().add(new XYChart.Data(10, 0.53476)); series1.getData().add(new XYChart.Data(11, 0.53023)); series1.getData().add(new XYChart.Data(12, 0.53001)); series1.getData().add(new XYChart.Data(13, 0.53589)); series1.getData().add(new XYChart.Data(14, 0.53476)); pane = new BorderPane(); pane.setCenter(lineChart); Scene scene = new Scene(pane, 800, 600); lineChart.getData().addAll(series1); stage.setScene(scene); scene.setOnMouseClicked(mouseHandler); scene.setOnMouseDragged(mouseHandler); scene.setOnMouseEntered(mouseHandler); scene.setOnMouseExited(mouseHandler); scene.setOnMouseMoved(mouseHandler); scene.setOnMousePressed(mouseHandler); scene.setOnMouseReleased(mouseHandler); stage.show(); } EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) { rectinitX.set(mouseEvent.getX()); } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED || mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane.getCenter(); NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); double Tgap = xAxis.getWidth()/(xAxis.getUpperBound() - xAxis.getLowerBound()); double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound(); double Delta=0.3; if(mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED){ if(rectinitX.get() < mouseEvent.getX()){ newXlower=xAxis.getLowerBound()-Delta; newXupper=xAxis.getUpperBound()-Delta; } else if(rectinitX.get() > mouseEvent.getX()){ newXlower=xAxis.getLowerBound()+Delta; newXupper=xAxis.getUpperBound()+Delta; } xAxis.setLowerBound( newXlower ); xAxis.setUpperBound( newXupper ); } rectinitX.set(mouseEvent.getX()); } } }; public static void main(String[] args) { launch(args); } }
Thank you allfinal NumberAxis xAxis = new NumberAxis(4, 16, 1);
Just change to this part:
if (rectinitX.get() < mouseEvent.getX() && newXlower >= 0) { newXlower = xAxis.getLowerBound() - Delta; newXupper = xAxis.getUpperBound() - Delta; } else if (rectinitX.get() > mouseEvent.getX() && newXupper <= 16) { newXlower = xAxis.getLowerBound() + Delta; newXupper = xAxis.getUpperBound() + Delta; }
I just added: * & newXlower > = 0 * and * & newXupper<=>=>
Published by: 981077 on January 14, 2013 06:47
-
LineChart css y-axis Set side ladder
In a code that draws a line chart, I added a css string to choose from Y price scale to display to the right or the left of the graph.
Here is the code
I think I'm missing something, because that line is now all collapsed on the left side of the graph.import java.util.Set; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class XyChart extends Application { @Override public void start(Stage stage) { stage.setTitle("Line plot"); final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(1, 21,0.1); yAxis.setTickUnit(1); yAxis.setPrefWidth(35); yAxis.setMinorTickCount(10); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){ @Override public String toString(Number object){ String label; label = String.format("%7.2f", object.floatValue()); return label; } }); final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 1)); series1.getData().add(new XYChart.Data("Feb", 4)); series1.getData().add(new XYChart.Data("Mar", 2.5)); series1.getData().add(new XYChart.Data("Apr", 5)); series1.getData().add(new XYChart.Data("May", 6)); series1.getData().add(new XYChart.Data("Jun", 8)); series1.getData().add(new XYChart.Data("Jul", 12)); series1.getData().add(new XYChart.Data("Aug", 8)); series1.getData().add(new XYChart.Data("Sep", 11)); series1.getData().add(new XYChart.Data("Oct", 13)); series1.getData().add(new XYChart.Data("Nov", 10)); series1.getData().add(new XYChart.Data("Dec", 20)); BorderPane pane = new BorderPane(); pane.setCenter(lineChart); Scene scene = new Scene(pane, 800, 600); lineChart.setAnimated(false); lineChart.getData().addAll(series1); String priceSide = "-fx-side: right;"; Set<Node> axisNode = lineChart.lookupAll(".axis"); for(final Node axis : axisNode){ axis.setStyle(priceSide); } stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
Just to comment on this part
and trace it correctly: same problem happens if I useString priceSide = "-fx-side: right;"; Set<Node> axisNode = lineChart.lookupAll(".axis"); for(final Node axis : axisNode){ axis.setStyle(priceSide); }
What's wrong with this code?"-fx-side: left;";
Thank you.You want to just set right on the NumberAxis side. You set right on the NumberAxis side and axis desabscisses. You can see this in test (axis instanceof NumberAxis) before calling setStyle your loop for.
-
How to change a line drawn at the same time?
Copy the following code draws a line XY Chart and lines to draw on the table by left click of the mouse and if mouse hover, line get selected turns red and can be deleted or moved.
What I want to do now is to change the length of line or the slope by selecting one of the points end or tail (A and B on photo)import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; public class LinesEdit extends Application { Path path; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(1, 21, 0.1); yAxis.setTickUnit(1); yAxis.setPrefWidth(35); yAxis.setMinorTickCount(10); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { @Override public String toString(Number object) { String label; label = String.format("%7.2f", object.floatValue()); return label; } }); final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 1)); series1.getData().add(new XYChart.Data("Feb", 4)); series1.getData().add(new XYChart.Data("Mar", 2.5)); series1.getData().add(new XYChart.Data("Apr", 5)); series1.getData().add(new XYChart.Data("May", 6)); series1.getData().add(new XYChart.Data("Jun", 8)); series1.getData().add(new XYChart.Data("Jul", 12)); series1.getData().add(new XYChart.Data("Aug", 8)); series1.getData().add(new XYChart.Data("Sep", 11)); series1.getData().add(new XYChart.Data("Oct", 13)); series1.getData().add(new XYChart.Data("Nov", 10)); series1.getData().add(new XYChart.Data("Dec", 20)); BorderPane bp = new BorderPane(); bp.setCenter(lineChart); Scene scene = new Scene(bp, 800, 600); lineChart.setAnimated(false); lineChart.getData().addAll(series1); LinesEdit.MouseHandler mh = new LinesEdit.MouseHandler( bp ); bp.setOnMouseClicked( mh ); bp.setOnMouseMoved( mh ); stage.setScene(scene); path = new Path(); path.setStrokeWidth(1); path.setStroke(Color.BLACK); scene.setOnMouseDragged(mh); scene.setOnMousePressed(mh); bp.getChildren().add(path); stage.setScene(scene); stage.show(); } class MouseHandler implements EventHandler< MouseEvent > { private boolean gotFirst = false; private Line line; private Pane pane; private double x1, y1, x2, y2; private LineHandler lineHandler; public MouseHandler( Pane pane ) { this.pane = pane; lineHandler = new LineHandler(pane); } class LineHandler implements EventHandler< MouseEvent > { double x, y; Pane pane; public LineHandler(Pane pane){ this.pane = pane; } @Override public void handle( MouseEvent e ) { Line l = (Line) e.getSource(); // remove line on right click if( e.getEventType() == MouseEvent.MOUSE_PRESSED && e.isSecondaryButtonDown() ) { pane.getChildren().remove( l ); } else if( e.getEventType() == MouseEvent.MOUSE_DRAGGED && e.isPrimaryButtonDown() ) { double tx = e.getX(); double ty = e.getY(); double dx = tx - x; double dy = ty - y; l.setStartX( l.getStartX() + dx ); l.setStartY( l.getStartY() + dy ); l.setEndX( l.getEndX() + dx ); l.setEndY( l.getEndY() + dy ); x = tx; y = ty; } else if( e.getEventType() == MouseEvent.MOUSE_ENTERED ) { // just to show that the line is selected x = e.getX(); y = e.getY(); l.setStroke( Color.RED ); } else if( e.getEventType() == MouseEvent.MOUSE_EXITED ) { l.setStroke( Color.BLACK ); } // should not pass event to the parent e.consume(); } } @Override public void handle( MouseEvent event ) { if( event.getEventType() == MouseEvent.MOUSE_CLICKED ) { if( !gotFirst ) { x1 = x2 = event.getX(); y1 = y2 = event.getY(); line = new Line( x1, y1, x2, y2 ); pane.getChildren().add( line ); gotFirst = true; } else { line.setOnMouseEntered( lineHandler ); line.setOnMouseExited( lineHandler ); line.setOnMouseDragged( lineHandler ); line.setOnMousePressed( lineHandler ); // to consume the event line.setOnMouseClicked( lineHandler ); line.setOnMouseReleased( lineHandler ); line = null; gotFirst = false; } } else { if( line != null ) { x2 = event.getX(); y2 = event.getY(); // update line line.setEndX( x2 ); line.setEndY( y2 ); } } } } }
[http://s16.postimage.org/f32p2a8md/A02503.png]
and end new game x, there point. How to get there?
Thank you.Hello. You can use the event handler OnContextMenuRequested to edit and delete the line:
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Side; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.MenuItem; import javafx.scene.input.ContextMenuEvent; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.stage.WindowEvent; public class LinesEdit extends Application { Path path; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(1, 21, 0.1); yAxis.setTickUnit(1); yAxis.setPrefWidth(35); yAxis.setMinorTickCount(10); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { @Override public String toString(Number object) { String label; label = String.format("%7.2f", object.floatValue()); return label; } }); final LineChart
lineChart = new LineChart (xAxis, yAxis); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 1)); series1.getData().add(new XYChart.Data("Feb", 4)); series1.getData().add(new XYChart.Data("Mar", 2.5)); series1.getData().add(new XYChart.Data("Apr", 5)); series1.getData().add(new XYChart.Data("May", 6)); series1.getData().add(new XYChart.Data("Jun", 8)); series1.getData().add(new XYChart.Data("Jul", 12)); series1.getData().add(new XYChart.Data("Aug", 8)); series1.getData().add(new XYChart.Data("Sep", 11)); series1.getData().add(new XYChart.Data("Oct", 13)); series1.getData().add(new XYChart.Data("Nov", 10)); series1.getData().add(new XYChart.Data("Dec", 20)); BorderPane bp = new BorderPane(); bp.setCenter(lineChart); Scene scene = new Scene(bp, 800, 600); lineChart.setAnimated(false); lineChart.getData().addAll(series1); LinesEdit.MouseHandler mh = new LinesEdit.MouseHandler(bp); bp.setOnMouseClicked(mh); bp.setOnMouseMoved(mh); stage.setScene(scene); path = new Path(); path.setStrokeWidth(1); path.setStroke(Color.BLACK); scene.setOnMouseDragged(mh); scene.setOnMousePressed(mh); bp.getChildren().add(path); stage.setScene(scene); stage.show(); } class MouseHandler implements EventHandler< MouseEvent> { private boolean gotFirst = false; private Line line; private Pane pane; private double x1, y1, x2, y2; private LineHandler lineHandler; public MouseHandler(Pane pane) { this.pane = pane; lineHandler = new LineHandler(pane); } class LineHandler implements EventHandler< MouseEvent> { double x, y; Pane pane; public LineHandler(Pane pane) { this.pane = pane; } @Override public void handle(final MouseEvent e) { final Line l = (Line) e.getSource(); final double x_ = e.getX(); final double y_ = e.getY(); final ContextMenu contextMenu = new ContextMenu(); MenuItem item1 = new MenuItem("Edit"); item1.setOnAction(new EventHandler () { public void handle(ActionEvent e) { l.setEndX(x_); l.setEndY(y_); System.out.println("Edit"); } }); MenuItem item2 = new MenuItem("Delete"); item2.setOnAction(new EventHandler () { public void handle(ActionEvent e) { pane.getChildren().remove(l); System.out.println("Delete"); } }); contextMenu.getItems().addAll(item1, item2); l.setOnContextMenuRequested( new EventHandler () { @Override public void handle(ContextMenuEvent event) { contextMenu.show(l, Side.RIGHT, 0, 0); System.out.println("Hello World!"); } }); // remove line on right click if (e.getEventType() == MouseEvent.MOUSE_PRESSED && e.isSecondaryButtonDown()) { // pane.getChildren().remove( l ); } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED && e.isPrimaryButtonDown()) { double tx = e.getX(); double ty = e.getY(); double dx = tx - x; double dy = ty - y; l.setStartX(l.getStartX() + dx); l.setStartY(l.getStartY() + dy); l.setEndX(l.getEndX() + dx); l.setEndY(l.getEndY() + dy); x = tx; y = ty; } else if (e.getEventType() == MouseEvent.MOUSE_ENTERED) { // just to show that the line is selected x = e.getX(); y = e.getY(); l.setStroke(Color.RED); } else if (e.getEventType() == MouseEvent.MOUSE_EXITED) { l.setStroke(Color.BLACK); } // should not pass event to the parent e.consume(); } } @Override public void handle(MouseEvent event) { if (event.getEventType() == MouseEvent.MOUSE_CLICKED) { if (!gotFirst) { x1 = x2 = event.getX(); y1 = y2 = event.getY(); line = new Line(x1, y1, x2, y2); pane.getChildren().add(line); gotFirst = true; } else { line.setOnMouseEntered(lineHandler); line.setOnMouseExited(lineHandler); line.setOnMouseDragged(lineHandler); line.setOnMousePressed(lineHandler); // to consume the event line.setOnMouseClicked(lineHandler); line.setOnMouseReleased(lineHandler); line = null; gotFirst = false; } } else { if (line != null) { x2 = event.getX(); y2 = event.getY(); // update line line.setEndX(x2); line.setEndY(y2); } } } } } -
The extension of the horizontal line on subpane (s)?
This code draws two lines of XYChart in a Voletfractionne and draw a horizontal line only in the upper pane.
I wish I had this horizontal line also in lower subpane (s) when the mouse pointer is moved to the lower pane.
How to get there?
Thank you
Here is the code
Published by: 932518 on 27-whole-2012 5.14import javafx.application.Application; import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.geometry.Orientation; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.*; import javafx.scene.control.SplitPane; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.LineBuilder; import javafx.stage.Stage; public class XyChartHorizontalLine extends Application { SplitPane splitPane1 = null; BorderPane pane1; BorderPane pane2; Line LH; XYChart.Series series1 = new XYChart.Series(); XYChart.Series series2 = new XYChart.Series(); @Override public void start(Stage stage) { stage.setTitle("Lines plot"); final NumberAxis xAxis = new NumberAxis(1, 12, 1); final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); xAxis.setAnimated(false); xAxis.setScaleX(0); yAxis.setAnimated(false); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { @Override public String toString(Number object) { return String.format("%7.5f", object); } }); final LineChart<Number, Number> lineChart1 = new LineChart<Number, Number>(xAxis, yAxis); lineChart1.setCreateSymbols(false); lineChart1.setAlternativeRowFillVisible(false); lineChart1.setAnimated(false); lineChart1.setLegendVisible(false); series1.getData().add(new XYChart.Data(1, 0.53185)); series1.getData().add(new XYChart.Data(2, 0.532235)); series1.getData().add(new XYChart.Data(3, 0.53234)); series1.getData().add(new XYChart.Data(4, 0.538765)); series1.getData().add(new XYChart.Data(5, 0.53442)); series1.getData().add(new XYChart.Data(6, 0.534658)); series1.getData().add(new XYChart.Data(7, 0.53023)); series1.getData().add(new XYChart.Data(8, 0.53001)); series1.getData().add(new XYChart.Data(9, 0.53589)); series1.getData().add(new XYChart.Data(10, 0.53476)); series1.getData().add(new XYChart.Data(11, 0.530123)); series1.getData().add(new XYChart.Data(12, 0.531035)); lineChart1.getData().addAll(series1); pane1 = new BorderPane(); pane1.setCenter(lineChart1); splitPane1 = new SplitPane(); splitPane1.setOrientation(Orientation.VERTICAL); splitPane1.getItems().addAll(pane1); splitPane1.setDividerPosition(0, 1); final NumberAxis xAxis2 = new NumberAxis(1, 12, 1); final NumberAxis yAxis2 = new NumberAxis(); yAxis2.setTickUnit(1); yAxis2.setPrefWidth(35); yAxis2.setMinorTickCount(10); yAxis2.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis2){ @Override public String toString(Number object){ String label; label = String.format("%7.2f", object.floatValue()); return label; } }); LineChart<Number, Number>lineChart2 = new LineChart<Number, Number>(xAxis2, yAxis2); lineChart2.setAlternativeRowFillVisible(false); lineChart2.setLegendVisible(false); lineChart2.setAnimated(false); lineChart2.setCreateSymbols(false); //XYChart.Series series2 = new XYChart.Series(); series2.getData().add(new XYChart.Data(1, 1)); series2.getData().add(new XYChart.Data(2, 3)); series2.getData().add(new XYChart.Data(3, 1.5)); series2.getData().add(new XYChart.Data(4, 3)); series2.getData().add(new XYChart.Data(5, 4.5)); series2.getData().add(new XYChart.Data(6, 5)); series2.getData().add(new XYChart.Data(7, 4)); series2.getData().add(new XYChart.Data(8, 8)); series2.getData().add(new XYChart.Data(9, 16.5)); series2.getData().add(new XYChart.Data(10, 13.9)); series2.getData().add(new XYChart.Data(11, 17)); series2.getData().add(new XYChart.Data(12, 20)); lineChart2.getData().addAll(series2); pane2 = new BorderPane(); pane2.setCenter(lineChart2); Platform.runLater(new Runnable() { @Override public void run() { double percSplit; splitPane1.getItems().addAll(pane2); ObservableList<SplitPane.Divider> splitDiv = splitPane1.getDividers(); percSplit = 1/(double)(splitDiv.size()+1); for (int i = 0; i< splitDiv.size(); i++) { splitPane1.setDividerPosition(i, percSplit); percSplit += 1/(double)(splitDiv.size()+1); } } }); Scene scene = new Scene(splitPane1, 800, 600); stage.setScene(scene); pane1.setOnMouseMoved(mouseHandler); LH=LineBuilder.create() .startX(0) .startY(0) .endX(10) .endY(.535) .strokeWidth(1) .stroke(Color.BLACK) .build(); pane1.getChildren().add(LH); stage.show(); } EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane1.getCenter(); NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound(); double xAxisShift = getSceneShift(xAxis); double yAxisShift = getSceneShift(yAxis); if(mouseEvent.getX()>xAxisShift && mouseEvent.getX()<xAxisShift+xAxis.getWidth()){ LH.setStartX(xAxisShift); LH.setStartY(mouseEvent.getY()); LH.setEndX(xAxisShift+xAxis.getWidth()); LH.setEndY(mouseEvent.getY()); } } } }; private static double getSceneShift(Node node) { double shift = 0; do { shift += node.getLayoutX(); node = node.getParent(); } while (node != null); return shift; } public static void main(String[] args) { launch(args); } }
(1) when the cursor is moved from the lower pane above, I always drew senior bulkhead, is it possible to delete or hide? My goal is to not get as a line drawn to the position of the pointer
Toggle the visible property for the lines:
LH.setVisible (false);
LH2.setVisible (true);(2) how do I modify your code in the case of the subcomponents more (> 2)?
I think that you can implement the same logic as with 2 components:
Here's the modified code:
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.application.Application; import javafx.application.Platform; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.geometry.Orientation; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.*; import javafx.scene.control.SplitPane; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.LineBuilder; import javafx.stage.Stage; public class XyChartHorizontalLine extends Application { SplitPane splitPane1 = null; BorderPane pane1; BorderPane pane2; Line LH; Line LH2; XYChart.Series series1 = new XYChart.Series(); XYChart.Series series2 = new XYChart.Series(); @Override public void start(Stage stage) { stage.setTitle("Lines plot"); final NumberAxis xAxis = new NumberAxis(1, 12, 1); final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); xAxis.setAnimated(false); xAxis.setScaleX(0); yAxis.setAnimated(false); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { @Override public String toString(Number object) { return String.format("%7.5f", object); } }); final LineChart
lineChart1 = new LineChart (xAxis, yAxis); lineChart1.setCreateSymbols(false); lineChart1.setAlternativeRowFillVisible(false); lineChart1.setAnimated(false); lineChart1.setLegendVisible(false); series1.getData().add(new XYChart.Data(1, 0.53185)); series1.getData().add(new XYChart.Data(2, 0.532235)); series1.getData().add(new XYChart.Data(3, 0.53234)); series1.getData().add(new XYChart.Data(4, 0.538765)); series1.getData().add(new XYChart.Data(5, 0.53442)); series1.getData().add(new XYChart.Data(6, 0.534658)); series1.getData().add(new XYChart.Data(7, 0.53023)); series1.getData().add(new XYChart.Data(8, 0.53001)); series1.getData().add(new XYChart.Data(9, 0.53589)); series1.getData().add(new XYChart.Data(10, 0.53476)); series1.getData().add(new XYChart.Data(11, 0.530123)); series1.getData().add(new XYChart.Data(12, 0.531035)); lineChart1.getData().addAll(series1); pane1 = new BorderPane(); pane1.setCenter(lineChart1); splitPane1 = new SplitPane(); splitPane1.setOrientation(Orientation.VERTICAL); splitPane1.getItems().addAll(pane1); splitPane1.setDividerPosition(0, 1); final NumberAxis xAxis2 = new NumberAxis(1, 12, 1); final NumberAxis yAxis2 = new NumberAxis(); yAxis2.setTickUnit(1); yAxis2.setPrefWidth(35); yAxis2.setMinorTickCount(10); yAxis2.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis2) { @Override public String toString(Number object) { String label; label = String.format("%7.2f", object.floatValue()); return label; } }); LineChart lineChart2 = new LineChart (xAxis2, yAxis2); lineChart2.setAlternativeRowFillVisible(false); lineChart2.setLegendVisible(false); lineChart2.setAnimated(false); lineChart2.setCreateSymbols(false); //XYChart.Series series2 = new XYChart.Series(); series2.getData().add(new XYChart.Data(1, 1)); series2.getData().add(new XYChart.Data(2, 3)); series2.getData().add(new XYChart.Data(3, 1.5)); series2.getData().add(new XYChart.Data(4, 3)); series2.getData().add(new XYChart.Data(5, 4.5)); series2.getData().add(new XYChart.Data(6, 5)); series2.getData().add(new XYChart.Data(7, 4)); series2.getData().add(new XYChart.Data(8, 8)); series2.getData().add(new XYChart.Data(9, 16.5)); series2.getData().add(new XYChart.Data(10, 13.9)); series2.getData().add(new XYChart.Data(11, 17)); series2.getData().add(new XYChart.Data(12, 20)); lineChart2.getData().addAll(series2); pane2 = new BorderPane(); pane2.setCenter(lineChart2); Platform.runLater(new Runnable() { @Override public void run() { double percSplit; splitPane1.getItems().addAll(pane2); ObservableList splitDiv = splitPane1.getDividers(); percSplit = 1 / (double) (splitDiv.size() + 1); for (int i = 0; i < splitDiv.size(); i++) { splitPane1.setDividerPosition(i, percSplit); percSplit += 1 / (double) (splitDiv.size() + 1); } } }); Scene scene = new Scene(splitPane1, 800, 600); stage.setScene(scene); pane1.setOnMouseMoved(mouseHandler); LH = LineBuilder.create() .startX(0) .startY(0) .endX(10) .endY(.535) .strokeWidth(1) .stroke(Color.BLACK) .build(); pane1.getChildren().add(LH); LH2=LineBuilder.create() .startX(300) .startY(300) .endX(310) .endY(300.535) .strokeWidth(1) .stroke(Color.BLACK) .build(); pane2.getChildren().add(LH2); pane2.setOnMouseMoved(mouseHandler2); stage.show(); } EventHandler mouseHandler = new EventHandler () { @Override public void handle(MouseEvent mouseEvent) { if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { LH.setVisible(true); LH2.setVisible(false); LineChart lineChart = (LineChart ) pane1.getCenter(); NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); double newXlower = xAxis.getLowerBound(), newXupper = xAxis.getUpperBound(); double xAxisShift = getSceneShift(xAxis); double yAxisShift = getSceneShift(yAxis); if (mouseEvent.getX() > xAxisShift && mouseEvent.getX() < xAxisShift + xAxis.getWidth()) { LH.setStartX(xAxisShift); LH.setStartY(mouseEvent.getY()); LH.setEndX(xAxisShift + xAxis.getWidth()); LH.setEndY(mouseEvent.getY()); } } } }; EventHandler mouseHandler2 = new EventHandler () { @Override public void handle(MouseEvent mouseEvent) { if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { LH.setVisible(false); LH2.setVisible(true); LineChart lineChart = (LineChart ) pane1.getCenter(); NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); double newXlower = xAxis.getLowerBound(), newXupper = xAxis.getUpperBound(); double xAxisShift = getSceneShift(xAxis); double yAxisShift = getSceneShift(yAxis); if (mouseEvent.getX() > xAxisShift && mouseEvent.getX() < xAxisShift + xAxis.getWidth()) { LH2.setStartX(xAxisShift); LH2.setStartY(mouseEvent.getY()); LH2.setEndX(xAxisShift + xAxis.getWidth()); LH2.setEndY(mouseEvent.getY()); } } } }; private static double getSceneShift(Node node) { double shift = 0; do { shift += node.getLayoutX(); node = node.getParent(); } while (node != null); return shift; } public static void main(String[] args) { launch(args); } } -
The code below to get an ObservableList of lines and fill a ComboBox: the question is that when a line is selected, it is displayed as "fx-shot of...» "such as the following pictures
http://S8.postimg.org/5fgejym05/Z039.PNG
http://S8.postimg.org/c9qp9nen9/Z040.PNG
How I change my code to display the selected line?
Thank you all.
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.ContentDisplay; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author utente */ public class ProvaComboRendering extends Application { @Override public void start(Stage primaryStage) { StackPane root = new StackPane(); ComboBox<String> cb = new ComboBox<>(); cb.setPrefSize(150, 20); root.getChildren().add(cb); Scene scene = new Scene(root, 300, 250); primaryStage.setScene(scene); primaryStage.show(); ObservableList<String> data = FXCollections.observableArrayList( "-fx-stroke-dash-array: 12 2 4 2;", "-fx-stroke-dash-array: 2 2;", "-fx-stroke-dash-array: 15 5.0 15 5.0;", "-fx-stroke-dash-array: 0.8 8.0;", "-fx-stroke-dash-array: 3 3;", "-fx-stroke-dash-array: 6 3;"); cb.setItems(data); cb.setCellFactory(new Callback<ListView<String>, ListCell<String>>(){ @Override public ListCell<String> call(ListView<String> p){ return new ListCell<String>(){ private final Group group; private final Line line; private final Rectangle rect; { setContentDisplay(ContentDisplay.GRAPHIC_ONLY); rect = new Rectangle(120, 20, Color.WHITE); line = new Line(0, 10, 120, 10); group = new Group(); group.getChildren().addAll(rect,line); } @Override protected void updateItem(String style, boolean empty){ super.updateItem(style, empty); if(style != null && !empty){ line.setStyle(style); setGraphic(group); } } }; } }); } public static void main(String[] args) { launch(args); } }
SetButtonCell (...) allows you to configure the display in the part 'button' from the drop-down list box. Here I just moved your anonymous inner class to a nested class in order to use it again. (Also changed the transparent background.)
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.ContentDisplay; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author utente */ public class ProvaComboRendering extends Application { @Override public void start(Stage primaryStage) { StackPane root = new StackPane(); ComboBox
cb = new ComboBox<>(); cb.setPrefSize(150, 20); root.getChildren().add(cb); Scene scene = new Scene(root, 300, 250); primaryStage.setScene(scene); primaryStage.show(); ObservableList data = FXCollections.observableArrayList( "-fx-stroke-dash-array: 12 2 4 2;", "-fx-stroke-dash-array: 2 2;", "-fx-stroke-dash-array: 15 5.0 15 5.0;", "-fx-stroke-dash-array: 0.8 8.0;", "-fx-stroke-dash-array: 3 3;", "-fx-stroke-dash-array: 6 3;"); cb.setItems(data); cb.setCellFactory(new Callback , ListCell >() { @Override public ListCell call(ListView p) { return new LineListCell(); } }); cb.setButtonCell(new LineListCell()); } public static void main(String[] args) { launch(args); } public static class LineListCell extends ListCell { private final Group group; private final Line line; private final Rectangle rect; public LineListCell() { setContentDisplay(ContentDisplay.GRAPHIC_ONLY); rect = new Rectangle(120, 20, Color.TRANSPARENT); line = new Line(0, 10, 120, 10); group = new Group(); group.getChildren().addAll(rect, line); } @Override protected void updateItem(String style, boolean empty) { super.updateItem(style, empty); if (style != null && !empty) { line.setStyle(style); setGraphic(group); } } } } -
I would like to create a class that extends LineChart, but I don't know how.
Is someone can provide me with an example?
Thank you
I created a class using the observable list
import javafx.collections.ObservableList; import javafx.scene.chart.Axis; import javafx.scene.chart.LineChart; /** * * @author Konrad */ public class LineChartTest extends LineChart { public LineChartTest(Axis axis, Axis axis1, ObservableList ol) { super(axis, axis1, ol); } }
Granted I use 8 JavaFX, and I know originally had to use generic drugs, but when I try to add
it does not work. -
Calculation value of axis based on, click in the chart area.
Hey there, I'm still working on my graphic feature and so far I've been able to get help on these forums.
I need to be able to calculate the values of the corresponding axis in a click in the chart area. I see no real utility method in the NumberAxis class which will help me to do it. The only thing I can think is to get the bounds of the array and that relate to what I know about the range of the values I put in.
It is a line chart with two NumberAxis. I want to display on click, (x, y)-value as PR. the axis of the point clicked. Is it possible that I can do this easily, or I just need to design a work around?
Yes, it's a little tricky, but not too bad. It would be probably good for XYChart
have public X getXValue (Point2D) and the public methods of getYValue (Point2D) Y. This works, however: import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Point2D; import javafx.scene.Scene; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.input.MouseEvent; import javafx.stage.Stage; public class LineChartSample extends Application { @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); // defining the axes final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("Number of Month"); // creating the chart final LineChart
lineChart = new LineChart (xAxis, yAxis); lineChart.setTitle("Stock Monitoring, 2010"); // defining a series XYChart.Series series = new XYChart.Series<>(); series.setName("My portfolio"); // populating the series with data series.getData().add(new XYChart.Data (1, 23)); series.getData().add(new XYChart.Data (2, 14)); series.getData().add(new XYChart.Data (3, 15)); series.getData().add(new XYChart.Data (4, 24)); series.getData().add(new XYChart.Data (5, 34)); series.getData().add(new XYChart.Data (6, 36)); series.getData().add(new XYChart.Data (7, 22)); series.getData().add(new XYChart.Data (8, 45)); series.getData().add(new XYChart.Data (9, 43)); series.getData().add(new XYChart.Data (10, 17)); series.getData().add(new XYChart.Data (11, 29)); series.getData().add(new XYChart.Data (12, 25)); Scene scene = new Scene(lineChart, 800, 600); lineChart.getData().add(series); stage.setScene(scene); stage.show(); lineChart.setOnMouseClicked(new EventHandler () { @Override public void handle(MouseEvent event) { Point2D xAxisInScene = xAxis.localToScene(new Point2D(0, 0)); Point2D yAxisInScene = yAxis.localToScene(new Point2D(0, 0)); double xInScene = event.getSceneX(); double yInScene = event.getSceneY(); double x = (xInScene - xAxisInScene.getX()) / xAxis.getScale() + xAxis.getLowerBound(); double y = yAxis.getUpperBound() + (yInScene - yAxisInScene.getY()) / yAxis.getScale(); System.out.printf("[%.1f, %.1f]%n", x, y); } }); } public static void main(String[] args) { launch(args); } } -
Copy the following code draws a simple chart of the XYLine
I like to shoot arrows on the table by left click of the mouse button and moved, as in this exampleimport javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class XyChart extends Application { @Override public void start(Stage stage) { stage.setTitle("Line plot"); final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(1, 21,0.1); yAxis.setTickUnit(1); yAxis.setPrefWidth(35); yAxis.setMinorTickCount(10); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){ @Override public String toString(Number object){ String label; label = String.format("%7.2f", object.floatValue()); return label; } }); final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 1)); series1.getData().add(new XYChart.Data("Feb", 4)); series1.getData().add(new XYChart.Data("Mar", 2.5)); series1.getData().add(new XYChart.Data("Apr", 5)); series1.getData().add(new XYChart.Data("May", 6)); series1.getData().add(new XYChart.Data("Jun", 8)); series1.getData().add(new XYChart.Data("Jul", 12)); series1.getData().add(new XYChart.Data("Aug", 8)); series1.getData().add(new XYChart.Data("Sep", 11)); series1.getData().add(new XYChart.Data("Oct", 13)); series1.getData().add(new XYChart.Data("Nov", 10)); series1.getData().add(new XYChart.Data("Dec", 20)); BorderPane pane = new BorderPane(); pane.setCenter(lineChart); Scene scene = new Scene(pane, 800, 600); lineChart.setAnimated(false); lineChart.getData().addAll(series1); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
[http://s8.postimage.org/5xgu0j6kl/A02480.png]
How to do this?
Thank you!
Published by: 932518 on November 6, 2012 4.32Hello. It is possible to shoot arrows on a chart. Please try the updated example the:
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.scene.shape.Polygon; import javafx.stage.Stage; public class XyChart extends Application { Path path; BorderPane pane = new BorderPane(); double startx = 0; double starty = 0; double endx = 0; double endy = 0; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { final CategoryAxis xAxis = new CategoryAxis(); final NumberAxis yAxis = new NumberAxis(1, 21, 0.1); yAxis.setTickUnit(1); yAxis.setPrefWidth(35); yAxis.setMinorTickCount(10); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { @Override public String toString(Number object) { String label; label = String.format("%7.2f", object.floatValue()); return label; } }); final LineChart
lineChart = new LineChart (xAxis, yAxis); lineChart.setCreateSymbols(false); lineChart.setAlternativeRowFillVisible(false); lineChart.setLegendVisible(false); XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Jan", 1)); series1.getData().add(new XYChart.Data("Feb", 4)); series1.getData().add(new XYChart.Data("Mar", 2.5)); series1.getData().add(new XYChart.Data("Apr", 5)); series1.getData().add(new XYChart.Data("May", 6)); series1.getData().add(new XYChart.Data("Jun", 8)); series1.getData().add(new XYChart.Data("Jul", 12)); series1.getData().add(new XYChart.Data("Aug", 8)); series1.getData().add(new XYChart.Data("Sep", 11)); series1.getData().add(new XYChart.Data("Oct", 13)); series1.getData().add(new XYChart.Data("Nov", 10)); series1.getData().add(new XYChart.Data("Dec", 20)); pane.setCenter(lineChart); Scene scene = new Scene(pane, 800, 600); lineChart.setAnimated(false); lineChart.getData().addAll(series1); path = new Path(); path.setStrokeWidth(1); path.setStroke(Color.BLACK); scene.setOnMouseReleased(mHandler); scene.setOnMousePressed(mHandler); pane.getChildren().add(path); stage.setScene(scene); stage.show(); } EventHandler mHandler = new EventHandler () { @Override public void handle(MouseEvent me) { if (me.getEventType() == MouseEvent.MOUSE_PRESSED) { startx = me.getX(); starty = me.getY(); path.getElements().add(new MoveTo(startx, starty)); } else if (me.getEventType() == MouseEvent.MOUSE_RELEASED) { endx = me.getX(); endy = me.getY(); path.getElements().add(new LineTo(endx, endy)); Polygon arrow = new Polygon(); arrow.getPoints().addAll(new Double[]{ 0.0, 5.0, -5.0, -5.0, 5.0, -5.0}); double angle = Math.atan2(endy - starty, endx - startx) * 180 / 3.14; arrow.setRotate((angle - 90)); arrow.setTranslateX(startx); arrow.setTranslateY(starty); arrow.setTranslateX(endx); arrow.setTranslateY(endy); pane.getChildren().add(arrow); } } }; } -
want to 4500 all-in-one series
I bought an envy4500 all in one series and after all tests and installation correctly, I had only one problem: when I print from excel files including 'grid' the printer will print all the lines in files. Sometimes jump the line grids, then print two or three grids correctily and back to the grid (lines). I made a copy of old copies and the copy was perfect.
Thank you
jbtpa
Impression in 'best' or 'quality' mode should solve your problem.
Alternatively, you can also use any other border style in Excel and it should print correctly.
I would be grateful if this does not resolve your problem, or a problem on something other then Excel, border 'Style 2' in mode 'Normal' or 'Draft' hearing.
Thank you!
Maybe you are looking for
-
Change the axis label w.r.t. the scale automatically in the waveform graph
Hello Is it possible to change category axis labels and the value the axis according to the scale automatically? I have a waveform graph which is automatically resized and values on the y-axis of milli, micro and nano volts according to the results o
-
Windows 3.1 can be made to run on a machine that already has Windows XP OS on it?
I have a legacy written in FORTRAN 77 program. I am trying to load MS FORTRAN Profession Workbench on my computer that has the Windows XP operating system. But Ms. FORTRAN Professional Workbench runs under Windows 3.1, but I don't know if or how it
-
I have constant 'elevator music' playing in the background, how do I get rid of him?
Computer on which I run and the music begins. I don't know where it comes from. I tried right-clicking the screensaver and checked properties but no help. Media Player checked but on help
-
Driver for Aspire 521 E5 problems
Model: Aspire E5 - 521(UEFI) BIOS version: ATI VGA VER015.042.000.002.000000Windows operating system of origin: 8.1New operating system: Windows 7 64 bitQuestion type: Could not find Chipset Device Driver PCI, PCI encryption/decryption controller, SM
-
When I turned on my computer, after that my children have been on it, I knew down in the right corner of my screen, what he said... "internet protected offshore mode." It is said that the internet protected mode on since we got the laptop for Christm