11package ie .tcd .pavel ;
22
33import java .util .ArrayList ;
4+ import java .util .HashMap ;
45import java .util .List ;
56
67import com .vaadin .flow .component .Text ;
78import com .vaadin .flow .component .charts .Chart ;
9+ import com .vaadin .flow .component .charts .model .AbstractSeries ;
810import com .vaadin .flow .component .charts .model .AxisTitle ;
911import com .vaadin .flow .component .charts .model .ChartType ;
1012import com .vaadin .flow .component .charts .model .Configuration ;
2527import com .vaadin .flow .component .orderedlayout .VerticalLayout ;
2628import com .vaadin .flow .router .Route ;
2729
28- import ie .tcd .pavel .documents .Group ;
29- import ie .tcd .pavel .documents .User ;
30+ import ie .tcd .pavel .documents .*;
3031import ie .tcd .pavel .security .SecurityUtils ;
3132import ie .tcd .pavel .utility .ExerciseTypes ;
3233
@@ -41,106 +42,63 @@ public class ExerciseChartsPage extends VerticalLayout {
4142 public ExerciseChartsPage () {
4243 database = BeanUtil .getBean (MongoDBOperations .class );
4344 exerciseTypes = BeanUtil .getBean (ExerciseTypes .class );
45+
4446 String [] exercises = exerciseTypes .getExerciseTypes ();
4547 List <Group > groups = database .getGroupsByUser (SecurityUtils .getUsername ());
4648 User currentUser = database .getUserByLogin (SecurityUtils .getUsername ());
47- String currentUserString = currentUser .getForename ()+ " " + currentUser .getSurname ();
48- Group currentGroup = groups .get (0 );
49- List <User > users = database .getUsersByGroup (currentGroup .getName ());
50- ArrayList <String > userList = new ArrayList <String >();
51- for (int i = 0 ; i <users .size (); i ++)
52- {
53- userList .add (users .get (i ).getForename ()+ " " + users .get (i ).getSurname ());
54- }
55- String allUsersString = "" ;
56- for (int i = 0 ; i <userList .size (); i ++)
49+
50+ String [] groupsString = new String [groups .size ()];
51+ for (int i = 0 ; i <groups .size (); i ++)
5752 {
58- allUsersString += userList .get (i );
59- allUsersString += "\n " ;
53+ groupsString [i ]= groups .get (i ).getName ();
6054 }
6155
62- ComboBox <String > comboBox = new ComboBox <>();
63- comboBox .setLabel ("Exercise" );
64- comboBox .setItems (exercises );
65- comboBox .addValueChangeListener (event ->{
66- Chart pieChart = new Chart (ChartType .PIE );
67- Configuration pieChartConfig = pieChart .getConfiguration ();
68- pieChartConfig .setTitle ("Member Contribution" );
69- pieChartConfig .setSubTitle (comboBox .getValue ());
70-
71- Tooltip tooltip = new Tooltip ();
72- tooltip .setValueDecimals (1 );
73- pieChartConfig .setTooltip (tooltip );
74-
75- PlotOptionsPie plotOptions = new PlotOptionsPie ();
76- plotOptions .setAllowPointSelect (true );
77- plotOptions .setCursor (Cursor .POINTER );
78- plotOptions .setShowInLegend (true );
79- pieChartConfig .setPlotOptions (plotOptions );
80-
81- DataSeries series = new DataSeries ();
82- DataSeriesItem currentUserSeries = new DataSeriesItem (currentUserString , 0 );
56+ ComboBox <String > comboBoxGroup = new ComboBox <>();
57+ comboBoxGroup .setLabel ("Group" );
58+ comboBoxGroup .setItems (groupsString );
59+ comboBoxGroup .addValueChangeListener (groupEvent ->{
60+
61+ ComboBox <String > comboBoxExercise = new ComboBox <>();
62+ verticalLayout .remove (comboBoxExercise );
63+ comboBoxExercise .setLabel ("Exercise" );
64+ comboBoxExercise .setItems (exercises );
65+ comboBoxExercise .addValueChangeListener (exerciseEvent ->{
66+ HashMap <User , Double > data = database .inGroupGetCumulativeValuesByUserAndType (comboBoxGroup .getValue (), comboBoxExercise .getValue ());
67+
68+ Chart pieChart = new Chart (ChartType .PIE );
69+ Configuration pieChartConfig = pieChart .getConfiguration ();
70+ pieChartConfig .setTitle ("Member Contribution" );
71+ pieChartConfig .setSubTitle (comboBoxExercise .getValue ());
72+
73+ Tooltip tooltip = new Tooltip ();
74+ tooltip .setValueDecimals (1 );
75+ pieChartConfig .setTooltip (tooltip );
76+
77+ PlotOptionsPie plotOptions = new PlotOptionsPie ();
78+ plotOptions .setAllowPointSelect (true );
79+ plotOptions .setCursor (Cursor .POINTER );
80+ plotOptions .setShowInLegend (true );
81+ pieChartConfig .setPlotOptions (plotOptions );
82+
83+ DataSeries dataSeries = new DataSeries ();
84+ dataSeries .clear ();
85+ DataSeriesItem currentUserSeries = new DataSeriesItem (currentUser .getLogin (), data .get (currentUser ));
86+ currentUserSeries .setSliced (true );
87+ currentUserSeries .setSelected (true );
88+ dataSeries .add (currentUserSeries );
89+ for (int i = 0 ; i <database .getUsersByGroup (comboBoxGroup .getValue ()).size (); i ++)
90+ {
91+ User user = database .getUsersByGroup (comboBoxGroup .getValue ()).get (i );
92+ dataSeries .add (new DataSeriesItem (user .getLogin (), data .get (user )));
93+ }
94+ pieChartConfig .setSeries (dataSeries );
95+ pieChart .setVisibilityTogglingDisabled (true );
96+ horizontalLayout .add (pieChart );
97+ });
98+ verticalLayout .add (comboBoxExercise );
8399 });
84-
85- Details memberDetails = new Details ();
86- memberDetails .setSummaryText ("Members" );
87- memberDetails .addContent (new Text (allUsersString ));
88-
89- verticalLayout .add (comboBox , memberDetails );
90-
91- //Template
92- /*int[] dataMember1= new int[] {200, 300, 22, 4, 400, 340, 6};
93- int[] dataMember2= new int[] {0, 30, 20, 1, 40, 170, 260};
94- int[] dataMember3= new int[] {100, 0, 20, 340, 200, 300, 57};
95- int[] dataMember4= new int[] {400, 3, 20, 23, 73, 250, 168};
96- int[] dataMember5= new int[] {150, 100, 30, 73, 45, 27, 221};
97- int[] dataMember6= new int[] {70, 75, 21, 3, 47, 25, 72};
98-
99- //Creating a drop down menu of all the exercises
100- ComboBox<String> comboBox= new ComboBox<>();
101- comboBox.setLabel("Exercise");
102- comboBox.setItems(exercises);
103- comboBox.addValueChangeListener(event ->{
104- //Adding bar chart
105- Chart barChart= new Chart();
106- verticalLayout.removeAll();
107- Configuration configBarChart= barChart.getConfiguration();
108- configBarChart.setTitle("Group Leaderboard");
109- configBarChart.setSubTitle(comboBox.getValue());
110- barChart.getConfiguration().getChart().setType(ChartType.PIE);
111-
112- DataSeries dataSeries = new DataSeries();
113- dataSeries.clear();
114- for(int index= 0; index<=groupSize; index++)
115- {
116- dataSeries.setData(dataMember1[index], dataMember2[index], dataMember3[index], dataMember4[index], dataMember5[index], dataMember6[index]);
117- configBarChart.addSeries(dataSeries);
118- }
119-
120- XAxis xAxis= new XAxis();
121- xAxis.setCategories("Member 1", "Member 2", "Member 3", "Member 4", "Member 5", "Member 6");
122- configBarChart.addxAxis(xAxis);
123-
124- YAxis yAxis= new YAxis();
125- yAxis.setMin(0);
126- AxisTitle yTitle= new AxisTitle();
127- yTitle.setText("Future parameter");
128- yTitle.setAlign(VerticalAlign.HIGH);
129- yAxis.setTitle(yTitle);
130- configBarChart.addyAxis(yAxis);
131-
132- Tooltip tooltip = new Tooltip();
133- tooltip.setValueSuffix(" future parameter");
134- tooltip.setShared(true);
135- configBarChart.setTooltip(tooltip);
136-
137- PlotOptionsBar plotOptions= new PlotOptionsBar();
138- DataLabels dataLabels= new DataLabels();
139- dataLabels.setEnabled(true);
140- plotOptions.setDataLabels(dataLabels);
141- configBarChart.setPlotOptions(plotOptions);
142- verticalLayout.add(barChart);
143- });
144- add(comboBox, verticalLayout);*/
100+ verticalLayout .add (comboBoxGroup );
101+ horizontalLayout .add (verticalLayout );
102+ add (verticalLayout , horizontalLayout );
145103 }
146104}
0 commit comments