First, I apologize for the English ... lol
The first time you run the process to generate the chart, runs smoothly, but the second time on, the error is displayed: "java.lang.IllegalStateException: Application launch must not be called more than once"
Help me, I do not know what to do to work.
Application code:
package reports;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import models.dao.ReportDAO;
import models.dto.ReportHarvestersTimeManagmentDTO;
public class ReportPerfCompHarvTime extends Application {
private ReportDAO reportDAO = new ReportDAO();
private String folder;
private String chartName;
public static void main(String[] args) {
launch(args);
}
public void run(String pFolder, String pChartName) throws Throwable {
this.folder = pFolder;
this.chartName = pChartName;
launch();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void start (Stage stage) {
try {
// Stage stg = new Stage();
// stage.setTitle("GESTÃO DO TEMPO (%)");
Platform.setImplicitExit(false);
NumberAxis xAxis = new NumberAxis();
CategoryAxis yAxis = new CategoryAxis();
BarChart<Number, String> bc = new BarChart<Number, String>(xAxis, yAxis);
// bc.setTitle("GESTÃO DO TEMPO (%)");
xAxis.setTickLabelRotation(90);
yAxis.setLabel("Frente");
List<ReportHarvestersTimeManagmentDTO> listReport = reportDAO.lstReportHarvestersTimeManagmentDTO();
BigDecimal value = new BigDecimal(0);
BigDecimal value100 = new BigDecimal(100);
XYChart.Series series = new XYChart.Series();
for (ReportHarvestersTimeManagmentDTO lst : listReport) {
series.setName(lst.getActivityName());
if (lst.getWorkedHours().compareTo(BigDecimal.ZERO) > 0) {
value = new BigDecimal(0);
value = lst.getTimeManagmentHours().divide(lst.getWorkedHours(), 2, RoundingMode.HALF_UP);
value = value.multiply(value100);
series.getData().add(new XYChart.Data(value.doubleValue(), lst.getWorkfrontID().toString()));
}
}
bc.getData().add(series);
bc.setAnimated(false);
bc.applyCss();
bc.layout();
Scene scene = new Scene(bc, 800, 600);
stage.setScene(scene);
stage.show();
WritableImage image = bc.snapshot(new SnapshotParameters(), null);
File file = new File(this.folder + this.chartName + ".png");
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
} finally {
stage.close();
Platform.exit();
}
}
}