Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dailyprogrammer.medium.datacollator;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.chart.LineChart;
- import javafx.scene.chart.NumberAxis;
- import javafx.scene.chart.XYChart;
- import javafx.stage.Stage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.function.Function;
- public class Graph extends Application {
- private String chartTitle;
- private String xAxisLabel;
- private String yAxisLabel;
- XYChart.Series<Number, Number> absData;
- XYChart.Series<Number, Number> apprxData;
- public Graph() {
- absData = new XYChart.Series<>();
- apprxData = new XYChart.Series<>();
- readData(new File("src/dailyprogrammer/medium/datacollator/input"));
- makeApproxData();
- }
- @Override
- public void start(Stage stage) {
- NumberAxis xAxis = new NumberAxis();
- NumberAxis yAxis = new NumberAxis();
- LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
- stage.setTitle("Data Collator From Jamaica");
- lineChart.setTitle(chartTitle);
- xAxis.setLabel(xAxisLabel);
- yAxis.setLabel(yAxisLabel);
- Scene scene = new Scene(lineChart, 800, 600);
- lineChart.getData().addAll(absData, apprxData);
- stage.setScene(scene);
- scene.getStylesheets().add("/dailyprogrammer/medium/datacollator/chart.css");
- stage.show();
- }
- public void readData(File file) {
- try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
- String[] chartData = reader.readLine().split(":");
- chartTitle = chartData[1];
- xAxisLabel = chartData[2];
- yAxisLabel = chartData[3];
- reader.lines().forEach(line -> {
- String[] tokens = line.split(":");
- double x = Double.parseDouble(tokens[0]);
- double y = Double.parseDouble(tokens[1]);
- XYChart.Data<Number, Number> data = new XYChart.Data<>(x, y);
- absData.getData().add(data);
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void makeApproxData() {
- double xUpper = absData.getData().stream()
- .mapToDouble(xy -> (double)xy.getXValue())
- .average().getAsDouble();
- double yUpper = absData.getData().stream()
- .mapToDouble(xy -> (double)xy.getYValue())
- .average().getAsDouble();
- Function<XYChart.Data<Number, Number>, Double> f1 = xy -> {
- double x = (double)xy.getXValue();
- double y = (double)xy.getYValue();
- return (x - xUpper) * (y - yUpper);
- };
- Function<XYChart.Data<Number, Number>, Double> f2 = xy -> {
- double x = (double)xy.getXValue();
- return Math.pow(x - xUpper, 2);
- };
- double m =
- absData.getData().stream()
- .mapToDouble(xy ->f1.apply(xy)).sum()
- /
- absData.getData().stream()
- .mapToDouble(xy -> f2.apply(xy)).sum();
- double c = yUpper - (m * xUpper);
- absData.getData().stream()
- .mapToDouble(xy -> (double)xy.getXValue())
- .forEach(x -> {
- double y = m * x + c;
- XYChart.Data<Number, Number> data = new XYChart.Data<>(x, y);
- apprxData.getData().add(data);
- });
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement