/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsbo.fbg.systemdynamics.output;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 *
 * @author Matthias Stein
 */
public class ChartsViewer extends Application {

    private static ArrayList<Series> series;

    @Override
    public void start(Stage stage) {
        stage.setTitle("System Dynamics Chart");
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Value");
        //creating the chart
        final LineChart<Number, Number> lineChart
                = new LineChart<Number, Number>(xAxis, yAxis);

        lineChart.setTitle("System Dynamics Chart");
        //defining a series

        Scene scene = new Scene(lineChart, 800, 600);

        for (Series s : ChartsViewer.series) {
            lineChart.getData().add(s);
        }

        stage.setScene(scene);
        stage.show();
    }

    public static void setCSVFile(String csvFile) {
        ChartsViewer.series = new ArrayList<Series>();
        String[] lines = csvFile.split("\\r?\\n");
        String[] modelEntityNames = lines[0].split(";");

        for (int i = 0; i < modelEntityNames.length; i++) {
            Series s = new Series();
            s.setName(modelEntityNames[i]);

            for (int j = 1; j < lines.length; j++) {
                String line = lines[j];
                String valueString = line.split(";")[i];
                NumberFormat format = NumberFormat.getInstance(Locale.GERMANY);
                Number number;
                try {
                    number = format.parse(valueString);
                    double value = number.doubleValue();
                    s.getData().add(new XYChart.Data(j - 1, value));
                } catch (ParseException ex) {
                    Logger.getLogger(ChartsViewer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            ChartsViewer.series.add(s);
        }
    }

    public static void saveToFile(Image image) {
        File outputFile = new File("chart.png");
        BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
        try {
            ImageIO.write(bImage, "png", outputFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -1,189 +1,4 @@
-/*%0A * To change this license header, choose License Headers in Project Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor.%0A */%0A
 pack
@@ -589,16 +589,24 @@
 e.image.
+Writable
 Image;%0Ai
@@ -666,16 +666,58 @@
 O;%0A%0A/**%0A
+ * This class handels the chart printing.%0A
  *%0A * @a
@@ -789,16 +789,20 @@
 ation %7B%0A
+    
 %0A    pri
@@ -839,16 +839,20 @@
 series;%0A
+    
 %0A    @Ov
@@ -1249,16 +1249,71 @@
 yAxis);%0A
+        %0A        lineChart.setAnimated(false);%0A        
 %0A       
@@ -1441,16 +1441,24 @@
 , 600);%0A
+        
 %0A       
@@ -1543,24 +1543,32 @@
 ;%0A        %7D%0A
+        
 %0A        sta
@@ -1609,26 +1609,178 @@
 show();%0A    
-%7D%0A
+    %0A        ChartsViewer.saveToFile(scene);%0A    %7D%0A%0A    /**%0A     * Set CSV String and add series to chart.%0A     *%0A     * @param csvFile CSV String%0A     */
 %0A    public 
@@ -1983,16 +1983,24 @@
 t(%22;%22);%0A
+        
 %0A       
@@ -2133,16 +2133,28 @@
 es%5Bi%5D);%0A
+            
 %0A       
@@ -2850,52 +2850,198 @@
     
-public static void saveToFile(Image image) %7B
+/**%0A     * Method to save the chart as an image.%0A     *%0A     * @param scene Scene%0A     */%0A    public static void saveToFile(Scene scene) %7B%0A        WritableImage image = scene.snapshot(null);
 %0A   

Reformat code