बाईं माउस क्लिक के द्वारा किसी चार्ट पर यह कोड ड्रा लाइनों और ले जाने केजावाएफएक्स 2.x: XY LineChart पर खींची गई रेखाओं को कैसे संपादित करें?
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 Lines 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);
Lines.MouseHandler mh = new Lines.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;
public MouseHandler(Pane pane) {
this.pane = pane;
}
@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 = null;
gotFirst = false;
}
}
else {
if(line != null) {
x2 = event.getX();
y2 = event.getY();
// update line
line.setEndX(x2);
line.setEndY(y2);
}
}
}
}
}
मेरा प्रश्न है: इस तरह के लाइनों कैसे संपादित करने के लिए एक बार साजिश रची?
उदाहरण के लिए, एक या एक से अधिक पंक्तियां खींची जाने के बाद, मैं इनमें से एक का चयन करना चाहूंगा, और दायां माउस क्लिक करके पॉप-अप मेनू का उपयोग करके, इसे हटाएं, लंबाई संशोधित करें, (इसे कम करने के लिए या अधिक) या लाइन ढलान बदलें।
धन्यवाद सब कुछ।
हाय भूपेंद्र, बहुत बहुत धन्यवाद! आपका कोड बहुत अच्छा है, वास्तव में सराहना की। –
एक आखिरी सवाल, क्या आप इस मुद्दे के साथ मेरी मदद कर सकते हैं? http://stackoverflow.com/questions/12405347/javafx-2-x-still-problems-with-draw-on-chart-zoom मैं हल करने में सक्षम नहीं हूं। यदि संभव हो तो मैं आपको ईमेल से संपर्क करना चाहता हूं , मेरा [email protected] है। धन्यवाद! –