From 7dc73cfff52b0c0730502ae4045319257d6ad535 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Fri, 28 Apr 2017 19:14:34 +0200
Subject: [PATCH] Fixed #28 - add charts

added barchart and linechart
---
 .../budgetmaster/logic/Helpers.java           |  33 ++-
 .../deadlocker8/budgetmaster/main/Main.java   |   2 +-
 .../budgetmaster/ui/ChartController.java      | 188 +++++++++++-------
 .../deadlocker8/budgetmaster/ui/ChartTab.fxml | 113 ++++-------
 4 files changed, 184 insertions(+), 152 deletions(-)

diff --git a/src/de/deadlocker8/budgetmaster/logic/Helpers.java b/src/de/deadlocker8/budgetmaster/logic/Helpers.java
index a3761e796..20822ef9e 100644
--- a/src/de/deadlocker8/budgetmaster/logic/Helpers.java
+++ b/src/de/deadlocker8/budgetmaster/logic/Helpers.java
@@ -5,11 +5,12 @@ import java.net.URLEncoder;
 import java.text.DecimalFormat;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
 
 public class Helpers
 {
 	public static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("0.00");
-	
+
 	public static String getURLEncodedString(String input)
 	{
 		try
@@ -21,7 +22,7 @@ public class Helpers
 			return input;
 		}
 	}
-	
+
 	public static String getDateString(LocalDate date)
 	{
 		if(date == null)
@@ -31,4 +32,32 @@ public class Helpers
 		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 		return date.format(formatter);
 	}
+
+	public static ArrayList<String> getMonthList()
+	{
+		ArrayList<String> monthNames = new ArrayList<>();
+		monthNames.add("Januar");
+		monthNames.add("Februar");
+		monthNames.add("März");
+		monthNames.add("April");
+		monthNames.add("Mai");
+		monthNames.add("Juni");
+		monthNames.add("Juli");
+		monthNames.add("August");
+		monthNames.add("September");
+		monthNames.add("Oktober");
+		monthNames.add("November");
+		monthNames.add("Dezember");
+		return monthNames;
+	}
+
+	public static ArrayList<String> getYearList()
+	{
+		ArrayList<String> years = new ArrayList<>();
+		for(int i = 2000; i < 2100; i++)
+		{
+			years.add(String.valueOf(i));
+		}
+		return years;
+	}
 }
\ No newline at end of file
diff --git a/src/de/deadlocker8/budgetmaster/main/Main.java b/src/de/deadlocker8/budgetmaster/main/Main.java
index 8f4ddb54f..a4c07a231 100644
--- a/src/de/deadlocker8/budgetmaster/main/Main.java
+++ b/src/de/deadlocker8/budgetmaster/main/Main.java
@@ -27,7 +27,7 @@ public class Main extends Application
 			FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("de/deadlocker8/budgetmaster/ui/GUI.fxml"));
 			Parent root = (Parent)loader.load();
 
-			Scene scene = new Scene(root, 600, 650);			
+			Scene scene = new Scene(root, 650, 650);			
 
 			((Controller)loader.getController()).init(stage);
 
diff --git a/src/de/deadlocker8/budgetmaster/ui/ChartController.java b/src/de/deadlocker8/budgetmaster/ui/ChartController.java
index d2fa8c944..5edb059a6 100644
--- a/src/de/deadlocker8/budgetmaster/ui/ChartController.java
+++ b/src/de/deadlocker8/budgetmaster/ui/ChartController.java
@@ -4,8 +4,10 @@ import java.time.LocalDate;
 import java.util.ArrayList;
 
 import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormat;
 
 import de.deadlocker8.budgetmaster.logic.CategoryInOutSum;
+import de.deadlocker8.budgetmaster.logic.Helpers;
 import de.deadlocker8.budgetmaster.logic.MonthInOutSum;
 import de.deadlocker8.budgetmaster.logic.ServerConnection;
 import de.deadlocker8.budgetmaster.logic.chartGenerators.BarChartGenerator;
@@ -13,18 +15,24 @@ import de.deadlocker8.budgetmaster.logic.chartGenerators.LineChartGenerator;
 import de.deadlocker8.budgetmaster.logic.chartGenerators.PieChartGenerator;
 import fontAwesome.FontIcon;
 import fontAwesome.FontIconType;
+import javafx.collections.FXCollections;
 import javafx.fxml.FXML;
 import javafx.scene.chart.BarChart;
 import javafx.scene.chart.LineChart;
 import javafx.scene.control.Accordion;
+import javafx.scene.control.Alert.AlertType;
 import javafx.scene.control.Button;
+import javafx.scene.control.ComboBox;
 import javafx.scene.control.DateCell;
 import javafx.scene.control.DatePicker;
+import javafx.scene.control.RadioButton;
+import javafx.scene.control.ToggleGroup;
 import javafx.scene.layout.AnchorPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.paint.Color;
 import javafx.util.Callback;
 import logger.Logger;
+import tools.AlertGenerator;
 
 public class ChartController implements Refreshable
 {
@@ -34,8 +42,14 @@ public class ChartController implements Refreshable
 	@FXML private HBox hboxChartCategories;
 	@FXML private DatePicker datePickerEnd;
 	@FXML private AnchorPane anchorPaneChartMonth;
-	@FXML private AnchorPane anchorPaneLineChart;
 	@FXML private Button buttonChartCategoriesShow;
+	@FXML private ComboBox<String> comboBoxStartMonth;
+	@FXML private ComboBox<String> comboBoxStartYear;
+	@FXML private ComboBox<String> comboBoxEndMonth;
+	@FXML private ComboBox<String> comboBoxEndYear;
+	@FXML private Button buttonChartMonthShow;
+	@FXML private RadioButton radioButtonBars;
+	@FXML private RadioButton radioButtonLines;
 
 	private Controller controller;
 
@@ -45,40 +59,63 @@ public class ChartController implements Refreshable
 
 		anchorPaneMain.setStyle("-fx-background-color: #F4F4F4;");
 		hboxChartCategories.setStyle("-fx-background-color: #F4F4F4;");
-		anchorPaneChartMonth.setStyle("-fx-background-color: #F4F4F4;");	
+		anchorPaneChartMonth.setStyle("-fx-background-color: #F4F4F4;");
 		FontIcon iconShow = new FontIcon(FontIconType.CHECK);
 		iconShow.setSize(16);
 		iconShow.setColor(Color.WHITE);
-		buttonChartCategoriesShow.setStyle("-fx-background-color: #2E79B9;");	
+		buttonChartCategoriesShow.setStyle("-fx-background-color: #2E79B9;");
 		buttonChartCategoriesShow.setGraphic(iconShow);
+
+		FontIcon iconShow2 = new FontIcon(FontIconType.CHECK);
+		iconShow2.setSize(16);
+		iconShow2.setColor(Color.WHITE);
+		buttonChartMonthShow.setStyle("-fx-background-color: #2E79B9;");
+		buttonChartMonthShow.setGraphic(iconShow2);		
 		
 		datePickerEnd.setDayCellFactory(new Callback<DatePicker, DateCell>()
-		{			
+		{
 			@Override
 			public DateCell call(DatePicker param)
 			{
-				 return new DateCell() 
-				 {
-                     @Override
-                     public void updateItem(LocalDate item, boolean empty) 
-                     {
-                         super.updateItem(item, empty);                        
-                         if (item.isBefore(datePickerStart.getValue().plusDays(1)))
-                         {
-                             setDisable(true);
-                             setStyle("-fx-background-color: #ffc0cb;");
-                         }   
-                     }
-                 };
+				return new DateCell()
+				{
+					@Override
+					public void updateItem(LocalDate item, boolean empty)
+					{
+						super.updateItem(item, empty);
+						if(item.isBefore(datePickerStart.getValue().plusDays(1)))
+						{
+							setDisable(true);
+							setStyle("-fx-background-color: #ffc0cb;");
+						}
+					}
+				};
 			}
 		});
+
+		comboBoxStartMonth.setItems(FXCollections.observableArrayList(Helpers.getMonthList()));
+		comboBoxStartMonth.setValue(controller.getCurrentDate().minusMonths(5).toString("MMMM"));
+		
+		comboBoxStartYear.setItems(FXCollections.observableArrayList(Helpers.getYearList()));
+		comboBoxStartYear.setValue(String.valueOf(controller.getCurrentDate().minusMonths(5).getYear()));
+		
+		comboBoxEndMonth.setItems(FXCollections.observableArrayList(Helpers.getMonthList()));
+		comboBoxEndMonth.setValue(controller.getCurrentDate().plusMonths(6).toString("MMMM"));
+		
+		comboBoxEndYear.setItems(FXCollections.observableArrayList(Helpers.getYearList()));
+		comboBoxEndYear.setValue(String.valueOf(controller.getCurrentDate().plusMonths(6).getYear()));
+
+		final ToggleGroup toggleGroup = new ToggleGroup();
+		radioButtonBars.setToggleGroup(toggleGroup);
+		radioButtonBars.setSelected(true);
+		radioButtonLines.setToggleGroup(toggleGroup);
 	}
-	
+
 	public void chartCategoriesShow()
 	{
 		DateTime startDate = DateTime.parse(datePickerStart.getValue().toString());
-		DateTime endDate = DateTime.parse(datePickerEnd.getValue().toString());		
-	
+		DateTime endDate = DateTime.parse(datePickerEnd.getValue().toString());
+
 		try
 		{
 			ServerConnection connection = new ServerConnection(controller.getSettings());
@@ -94,60 +131,45 @@ public class ChartController implements Refreshable
 		catch(Exception e)
 		{
 			Logger.error(e);
-			//TODO
-			//controller.showConnectionErrorAlert(e.getMessage());
+			// TODO
+			// controller.showConnectionErrorAlert(e.getMessage());
 		}
 	}
-	
+
 	public void chartMonthShow()
 	{
-		//DEBUG get date from comboboxes
-		DateTime startDate = controller.getCurrentDate().withMonthOfYear(1);
-		DateTime endDate =  controller.getCurrentDate().withMonthOfYear(12);	
+		anchorPaneChartMonth.getChildren().clear();
 		
-		try
-		{
-			ServerConnection connection = new ServerConnection(controller.getSettings());
-			//DEBUG
-			ArrayList<MonthInOutSum> sums = connection.getMonthInOutSum(startDate, endDate);
-
-			anchorPaneChartMonth.getChildren().clear();
-			BarChartGenerator generator = new BarChartGenerator(sums, controller.getSettings().getCurrency());
-			BarChart<String, Number> chartMonth = generator.generate();
-			anchorPaneChartMonth.getChildren().add(chartMonth);
-			AnchorPane.setTopAnchor(chartMonth, 0.0);
-			AnchorPane.setRightAnchor(chartMonth, 0.0);
-			AnchorPane.setBottomAnchor(chartMonth, 0.0);
-			AnchorPane.setLeftAnchor(chartMonth, 0.0);
-		}
-		catch(Exception e)
+		String startMonth = comboBoxStartMonth.getValue();
+		String startYear = comboBoxStartYear.getValue();
+		String endMonth = comboBoxEndMonth.getValue();
+		String endYear = comboBoxEndYear.getValue();		
+		
+		String startDateString = "01-" + startMonth + "-" + startYear;		
+		DateTime startDate = DateTime.parse(startDateString, DateTimeFormat.forPattern("dd-MMMM-YYYY"));
+		
+		String endDateString = "01-" + endMonth + "-" + endYear;		
+		DateTime endDate = DateTime.parse(endDateString, DateTimeFormat.forPattern("dd-MMMM-YYYY"));	
+			
+		if(endDate.isBefore(startDate))
 		{
-			Logger.error(e);
-			//TODO
-			//controller.showConnectionErrorAlert(e.getMessage());
+			AlertGenerator.showAlert(AlertType.WARNING, "Warnung", "", "Das Enddatum darf nicht vor dem Startdatum liegen.", controller.getIcon(), controller.getStage(), null, false);
+			return;
 		}
-	}
-	
-	public void chartLineChartShow()
-	{
-		//DEBUG get date from comboboxes
-		DateTime startDate = controller.getCurrentDate().withMonthOfYear(1);
-		DateTime endDate =  controller.getCurrentDate().withMonthOfYear(12);	
 		
 		try
 		{
 			ServerConnection connection = new ServerConnection(controller.getSettings());
-			//DEBUG
 			ArrayList<MonthInOutSum> sums = connection.getMonthInOutSum(startDate, endDate);
-
-			anchorPaneLineChart.getChildren().clear();
-			LineChartGenerator generator = new LineChartGenerator(sums, controller.getSettings().getCurrency());
-			LineChart<String, Number> chartMonth = generator.generate();
-			anchorPaneLineChart.getChildren().add(chartMonth);
-			AnchorPane.setTopAnchor(chartMonth, 0.0);
-			AnchorPane.setRightAnchor(chartMonth, 0.0);
-			AnchorPane.setBottomAnchor(chartMonth, 0.0);
-			AnchorPane.setLeftAnchor(chartMonth, 0.0);
+			
+			if(radioButtonBars.isSelected())
+			{
+				chartMonthBarShow(sums);
+			}
+			else
+			{
+				chartMonthLineShow(sums);
+			}		
 		}
 		catch(Exception e)
 		{
@@ -157,25 +179,47 @@ public class ChartController implements Refreshable
 		}
 	}
 
+	public void chartMonthBarShow(ArrayList<MonthInOutSum> sums)
+	{		
+		
+		BarChartGenerator generator = new BarChartGenerator(sums, controller.getSettings().getCurrency());
+		BarChart<String, Number> chartMonth = generator.generate();
+		anchorPaneChartMonth.getChildren().add(chartMonth);
+		AnchorPane.setTopAnchor(chartMonth, 0.0);
+		AnchorPane.setRightAnchor(chartMonth, 0.0);
+		AnchorPane.setBottomAnchor(chartMonth, 0.0);
+		AnchorPane.setLeftAnchor(chartMonth, 0.0);		
+	}
+
+	public void chartMonthLineShow(ArrayList<MonthInOutSum> sums)
+	{	
+		anchorPaneChartMonth.getChildren().clear();
+		LineChartGenerator generator = new LineChartGenerator(sums, controller.getSettings().getCurrency());
+		LineChart<String, Number> chartMonth = generator.generate();
+		anchorPaneChartMonth.getChildren().add(chartMonth);
+		AnchorPane.setTopAnchor(chartMonth, 0.0);
+		AnchorPane.setRightAnchor(chartMonth, 0.0);
+		AnchorPane.setBottomAnchor(chartMonth, 0.0);
+		AnchorPane.setLeftAnchor(chartMonth, 0.0);
+	}
+
 	@Override
 	public void refresh()
 	{
-		//chart categories
+		// chart categories
 		LocalDate startDate = LocalDate.parse(controller.getCurrentDate().withDayOfMonth(1).toString("yyyy-MM-dd"));
 		LocalDate endDate = LocalDate.parse(controller.getCurrentDate().dayOfMonth().withMaximumValue().toString("yyy-MM-dd"));
-		
+
 		datePickerStart.setValue(startDate);
 		datePickerEnd.setValue(endDate);
-		
+
 		chartCategoriesShow();
-		
-		//chart month
+
+		// chart month
 		chartMonthShow();
-		
-		chartLineChartShow();
-		
-		//TODO combine bar und line chart (radio buttons)
-		
+
+		// TODO combine bar und line chart (radio buttons)
+
 		accordion.setExpandedPane(accordion.getPanes().get(0));
 	}
 }
\ No newline at end of file
diff --git a/src/de/deadlocker8/budgetmaster/ui/ChartTab.fxml b/src/de/deadlocker8/budgetmaster/ui/ChartTab.fxml
index 36aa80c10..e2c153468 100644
--- a/src/de/deadlocker8/budgetmaster/ui/ChartTab.fxml
+++ b/src/de/deadlocker8/budgetmaster/ui/ChartTab.fxml
@@ -6,6 +6,7 @@
 <?import javafx.scene.control.ComboBox?>
 <?import javafx.scene.control.DatePicker?>
 <?import javafx.scene.control.Label?>
+<?import javafx.scene.control.RadioButton?>
 <?import javafx.scene.control.TitledPane?>
 <?import javafx.scene.layout.AnchorPane?>
 <?import javafx.scene.layout.HBox?>
@@ -73,43 +74,53 @@
                <content>
                   <VBox spacing="20.0">
                      <children>
-                        <HBox alignment="CENTER" prefHeight="8.0" prefWidth="750.0">
+                        <HBox alignment="CENTER_LEFT" prefHeight="8.0" prefWidth="750.0" spacing="50.0">
                            <children>
-                              <HBox alignment="CENTER_RIGHT" spacing="10.0" HBox.hgrow="ALWAYS">
+                              <VBox spacing="15.0">
                                  <children>
-                                    <Label text="Von:">
-                                       <font>
-                                          <Font name="System Bold" size="16.0" />
-                                       </font>
-                                    </Label>
-                                    <ComboBox prefHeight="25.0" prefWidth="100.0" />
-                                    <ComboBox prefWidth="70.0" />
+                                    <HBox alignment="CENTER_RIGHT" spacing="10.0">
+                                       <children>
+                                          <Label prefHeight="25.0" prefWidth="45.0" text="Von:">
+                                             <font>
+                                                <Font name="System Bold" size="16.0" />
+                                             </font>
+                                          </Label>
+                                          <ComboBox fx:id="comboBoxStartMonth" prefHeight="25.0" prefWidth="110.0" />
+                                          <ComboBox fx:id="comboBoxStartYear" prefWidth="70.0" />
+                                       </children>
+                                    </HBox>
+                                    <HBox alignment="CENTER_LEFT" spacing="10.0">
+                                       <children>
+                                          <Label prefWidth="45.0" text="Bis:">
+                                             <font>
+                                                <Font name="System Bold" size="16.0" />
+                                             </font>
+                                          </Label>
+                                          <ComboBox fx:id="comboBoxEndMonth" prefWidth="110.0" />
+                                          <ComboBox fx:id="comboBoxEndYear" prefWidth="70.0" />
+                                       </children>
+                                    </HBox>
                                  </children>
-                                 <HBox.margin>
-                                    <Insets right="15.0" />
-                                 </HBox.margin>
-                              </HBox>
-                              <HBox alignment="CENTER_LEFT" spacing="10.0" HBox.hgrow="ALWAYS">
+                              </VBox>
+                              <HBox alignment="CENTER_LEFT" spacing="15.0" HBox.hgrow="ALWAYS">
                                  <children>
-                                    <Label text="Bis:">
+                                    <RadioButton fx:id="radioButtonBars" mnemonicParsing="false" text="Balken">
                                        <font>
-                                          <Font name="System Bold" size="16.0" />
-                                       </font>
-                                    </Label>
-                                    <ComboBox prefWidth="100.0" />
-                                    <ComboBox prefWidth="70.0" />
-                                    <Button fx:id="buttonChartCategoriesShow1" mnemonicParsing="false" onAction="#chartCategoriesShow">
+                                          <Font size="14.0" />
+                                       </font></RadioButton>
+                                    <RadioButton fx:id="radioButtonLines" mnemonicParsing="false" text="Linien">
+                                       <font>
+                                          <Font size="14.0" />
+                                       </font></RadioButton>
+                                    <Button fx:id="buttonChartMonthShow" mnemonicParsing="false" onAction="#chartMonthShow">
                                        <font>
                                           <Font name="System Bold" size="12.0" />
                                        </font>
                                        <HBox.margin>
-                                          <Insets left="15.0" />
+                                          <Insets left="10.0" />
                                        </HBox.margin>
                                     </Button>
                                  </children>
-                                 <HBox.margin>
-                                    <Insets left="15.0" />
-                                 </HBox.margin>
                               </HBox>
                            </children>
                         </HBox>
@@ -118,58 +129,6 @@
                   </VBox>
                </content>
           </TitledPane>
-            <TitledPane animated="false" text="Einnahmen/Ausgaben pro Monat Verlauf">
-               <font>
-                  <Font name="System Bold" size="12.0" />
-               </font>
-               <content>
-                  <VBox spacing="20.0">
-                     <children>
-                        <HBox alignment="CENTER" prefHeight="8.0" prefWidth="750.0">
-                           <children>
-                              <HBox alignment="CENTER_RIGHT" spacing="10.0" HBox.hgrow="ALWAYS">
-                                 <children>
-                                    <Label text="Von:">
-                                       <font>
-                                          <Font name="System Bold" size="16.0" />
-                                       </font>
-                                    </Label>
-                                    <ComboBox prefHeight="25.0" prefWidth="100.0" />
-                                    <ComboBox prefWidth="70.0" />
-                                 </children>
-                                 <HBox.margin>
-                                    <Insets right="15.0" />
-                                 </HBox.margin>
-                              </HBox>
-                              <HBox alignment="CENTER_LEFT" spacing="10.0" HBox.hgrow="ALWAYS">
-                                 <children>
-                                    <Label text="Bis:">
-                                       <font>
-                                          <Font name="System Bold" size="16.0" />
-                                       </font>
-                                    </Label>
-                                    <ComboBox prefWidth="100.0" />
-                                    <ComboBox prefWidth="70.0" />
-                                    <Button fx:id="buttonChartCategoriesShow11" mnemonicParsing="false" onAction="#chartCategoriesShow">
-                                       <font>
-                                          <Font name="System Bold" size="12.0" />
-                                       </font>
-                                       <HBox.margin>
-                                          <Insets left="15.0" />
-                                       </HBox.margin>
-                                    </Button>
-                                 </children>
-                                 <HBox.margin>
-                                    <Insets left="15.0" />
-                                 </HBox.margin>
-                              </HBox>
-                           </children>
-                        </HBox>
-                        <AnchorPane fx:id="anchorPaneLineChart" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
-                     </children>
-                  </VBox>
-               </content>
-            </TitledPane>
         </panes>
       </Accordion>
    </children>
-- 
GitLab