added multiple show types for scrobble chart

This commit is contained in:
aj 2019-05-23 11:13:20 +01:00
parent 0dd9e5dda6
commit c35be3b731
4 changed files with 268 additions and 29 deletions

View File

@ -4,12 +4,15 @@ import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import javafx.fxml.FXML;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.GridPane;
@ -18,6 +21,7 @@ import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.util.FMObjCalendarWrapper;
import sarsoo.fmframework.util.MonthScrobbles;
import sarsoo.fmframework.util.ScrobbleCountCalendar;
@ -31,21 +35,40 @@ public class ScrobblesViewPaneController {
@FXML
private AreaChart<String, Integer> areaChart;
@FXML
private Button buttonTracks;
@FXML
private Button buttonAlbums;
@FXML
private Button buttonTracksAlbums;
private FMObj obj;
private LocalDate firstDate;
@FXML
public void initialize() {
buttonTracks.setDisable(true);
buttonAlbums.setDisable(true);
buttonTracksAlbums.setDisable(true);
}
public void populate(FMObj obj) {
this.obj = obj;
ArrayList<Scrobble> scrobbles = obj.getScrobbles();
if (scrobbles != null) {
if (scrobbles.size() > 0) {
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK);
gridPane.getChildren().clear();
int counter;
@ -71,28 +94,202 @@ public class ScrobblesViewPaneController {
Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
LocalDate date = net.getFirstScrobbleDateTime().toLocalDate();
firstDate = net.getFirstScrobbleDateTime().toLocalDate();
ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(date);
XYChart.Series<String, Integer> series = new XYChart.Series<>();
for (Scrobble scrobble : scrobbles) {
LocalDateTime scrobbleDate = scrobble.getDateTime();
calendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
int cumulative = 0;
for(MonthScrobbles month: calendar.getMonthScrobbles()) {
cumulative += month.getCount();
series.getData().add(new Data<String, Integer>(month.toString(), cumulative));
}
areaChart.getData().add(series);
buttonAlbums.setDisable(false);
buttonTracksAlbums.setDisable(false);
graphShowTracks();
}
}
}
public void graphShowTracks() {
ArrayList<FMObjCalendarWrapper> list = new ArrayList<>();
ScrobbleCountCalendar totalCalendar = new ScrobbleCountCalendar(firstDate, "total");
for (Scrobble scrobble : obj.getScrobbles()) {
Boolean needNew = true;
LocalDateTime scrobbleDate = scrobble.getDateTime();
for (FMObjCalendarWrapper wrapper : list) {
if (scrobble.getTrack().equals(wrapper.getScrobble().getTrack())) {
needNew = false;
wrapper.getCalendar().addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
}
if (needNew) {
ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(firstDate, scrobble.getTrack().getName());
calendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
list.add(new FMObjCalendarWrapper(scrobble, calendar));
}
totalCalendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
List<ScrobbleCountCalendar> toShow = list.stream().map(FMObjCalendarWrapper::getCalendar)
.collect(Collectors.toList());
if (list.size() > 1) {
toShow.add(totalCalendar);
}
refreshGraph(toShow);
}
public void graphShowAlbums() {
ArrayList<FMObjCalendarWrapper> list = new ArrayList<>();
ScrobbleCountCalendar totalCalendar = new ScrobbleCountCalendar(firstDate, "total");
for (Scrobble scrobble : obj.getScrobbles()) {
Boolean needNew = true;
LocalDateTime scrobbleDate = scrobble.getDateTime();
for (FMObjCalendarWrapper wrapper : list) {
if (scrobble.getAlbum().equals(wrapper.getScrobble().getAlbum())) {
needNew = false;
wrapper.getCalendar().addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
}
if (needNew) {
ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(firstDate, scrobble.getAlbum().getName());
calendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
list.add(new FMObjCalendarWrapper(scrobble, calendar));
}
totalCalendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
List<ScrobbleCountCalendar> toShow = list.stream().map(FMObjCalendarWrapper::getCalendar)
.collect(Collectors.toList());
if (list.size() > 1) {
toShow.add(totalCalendar);
}
refreshGraph(toShow);
}
public void graphShowTracksByAlbum() {
ArrayList<FMObjCalendarWrapper> list = new ArrayList<>();
ScrobbleCountCalendar totalCalendar = new ScrobbleCountCalendar(firstDate, "total");
for (Scrobble scrobble : obj.getScrobbles()) {
Boolean needNew = true;
LocalDateTime scrobbleDate = scrobble.getDateTime();
for (FMObjCalendarWrapper wrapper : list) {
if (scrobble.getTrack().equals(wrapper.getScrobble().getTrack())
&& scrobble.getAlbum().equals(wrapper.getScrobble().getAlbum())) {
needNew = false;
wrapper.getCalendar().addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
}
if (needNew) {
ScrobbleCountCalendar calendar = new ScrobbleCountCalendar(firstDate, scrobble.getAlbum().getName());
calendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
list.add(new FMObjCalendarWrapper(scrobble, calendar));
}
totalCalendar.addCount(scrobbleDate.getMonth(), scrobbleDate.getYear());
}
List<ScrobbleCountCalendar> toShow = list.stream().map(FMObjCalendarWrapper::getCalendar)
.collect(Collectors.toList());
if (list.size() > 1) {
toShow.add(totalCalendar);
}
refreshGraph(toShow);
}
public void refreshGraph(List<ScrobbleCountCalendar> calendars) {
areaChart.getData().clear();
for (ScrobbleCountCalendar calendar : calendars) {
XYChart.Series<String, Integer> series = new XYChart.Series<>();
series.setName(calendar.getName());
int cumulative = 0;
for (MonthScrobbles month : calendar.getMonthScrobbles()) {
cumulative += month.getCount();
series.getData().add(new Data<String, Integer>(month.toString(), cumulative));
}
areaChart.getData().add(series);
}
}
@FXML
private void handleShowTracks() {
buttonTracks.setDisable(true);
buttonAlbums.setDisable(false);
buttonTracksAlbums.setDisable(false);
graphShowTracks();
}
@FXML
private void handleShowAlbums() {
buttonTracks.setDisable(false);
buttonAlbums.setDisable(true);
buttonTracksAlbums.setDisable(false);
graphShowAlbums();
}
@FXML
private void handleShowTracksByAlbums() {
buttonTracks.setDisable(false);
buttonAlbums.setDisable(false);
buttonTracksAlbums.setDisable(true);
graphShowTracksByAlbum();
}
}

View File

@ -0,0 +1,23 @@
package sarsoo.fmframework.util;
import sarsoo.fmframework.music.Scrobble;
public class FMObjCalendarWrapper {
private Scrobble obj;
private ScrobbleCountCalendar calendar;
public FMObjCalendarWrapper(Scrobble scrobble, ScrobbleCountCalendar calendar) {
this.obj = scrobble;
this.calendar = calendar;
}
public ScrobbleCountCalendar getCalendar() {
return calendar;
}
public Scrobble getScrobble() {
return obj;
}
}

View File

@ -10,11 +10,13 @@ import sarsoo.fmframework.log.entry.ErrorEntry;
public class ScrobbleCountCalendar {
public ArrayList<MonthScrobbles> months;
private ArrayList<MonthScrobbles> months;
private String name;
public ScrobbleCountCalendar(LocalDate date) {
public ScrobbleCountCalendar(LocalDate date, String name) {
months = new ArrayList<>();
this.name = name;
LocalDate now = LocalDate.now();
@ -45,4 +47,8 @@ public class ScrobbleCountCalendar {
return months;
}
public String getName() {
return name;
}
}

View File

@ -26,14 +26,27 @@
</ScrollPane>
<AnchorPane fx:id="areaChartAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<AreaChart fx:id="areaChart" layoutX="-93.0" layoutY="69.0" legendVisible="false" prefHeight="798.0" prefWidth="504.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</AreaChart>
<BorderPane prefHeight="384.0" prefWidth="256.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<AreaChart fx:id="areaChart" prefHeight="798.0" prefWidth="504.0" BorderPane.alignment="CENTER">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</AreaChart>
</center>
<bottom>
<ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Button fx:id="buttonTracks" mnemonicParsing="false" onAction="#handleShowTracks" text="tracks" />
<Button fx:id="buttonAlbums" mnemonicParsing="false" onAction="#handleShowAlbums" text="albums" />
<Button fx:id="buttonTracksAlbums" mnemonicParsing="false" onAction="#handleShowTracksByAlbums" text="tracks &amp; albums" />
</items>
</ToolBar>
</bottom>
</BorderPane>
</children>
</AnchorPane>
</items>