Skip to content

Commit b88c4b4

Browse files
authored
Merge pull request #419 from danthe1st/improve-plotting
externalize title of plot, use max for plot height
2 parents c830f7f + 6c4cf74 commit b88c4b4

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/main/java/net/javadiscord/javabot/systems/help/commands/HelpAccountSubcommand.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
9696

9797
try {
9898
HelpAccount account = helpExperienceService.getOrCreateAccount(user.getIdLong());
99-
WebhookMessageCreateAction<Message> reply = event.getHook().sendMessageEmbeds(buildHelpAccountEmbed(account, user, event.getGuild(), totalThanks, weekThanks));
99+
WebhookMessageCreateAction<Message> reply = event.getHook().sendMessageEmbeds(buildHelpAccountEmbed(account, user, event.getGuild(), totalThanks, weekThanks, upload));
100100
if (upload!=null) {
101101
reply.addFiles(upload);
102102
}
@@ -129,7 +129,7 @@ private FileUpload generatePlot(User user) {
129129
plotData.add(new Pair<>(position.getMonth() + " " + position.getYear(), value));
130130
}
131131

132-
BufferedImage plt = new Plotter(plotData).plot();
132+
BufferedImage plt = new Plotter(plotData, "gained help XP per month").plot();
133133
try(ByteArrayOutputStream os = new ByteArrayOutputStream()){
134134
ImageIO.write(plt, "png", os);
135135
return FileUpload.fromData(os.toByteArray(), "image.png");
@@ -139,16 +139,19 @@ private FileUpload generatePlot(User user) {
139139
return null;
140140
}
141141

142-
private @NotNull MessageEmbed buildHelpAccountEmbed(HelpAccount account, @NotNull User user, Guild guild, long totalThanks, long weekThanks) {
143-
return new EmbedBuilder()
142+
private @NotNull MessageEmbed buildHelpAccountEmbed(HelpAccount account, @NotNull User user, Guild guild, long totalThanks, long weekThanks, FileUpload upload) {
143+
EmbedBuilder eb = new EmbedBuilder()
144144
.setAuthor(user.getAsTag(), null, user.getEffectiveAvatarUrl())
145145
.setTitle("Help Account")
146146
.setThumbnail(user.getEffectiveAvatarUrl())
147147
.setDescription("Here are some statistics about how you've helped others here.")
148148
.addField("Experience (BETA)", formatExperience(guild, account), false)
149149
.addField("Total Times Thanked", String.format("**%s**", totalThanks), true)
150-
.addField("Times Thanked This Week", String.format("**%s**", weekThanks), true)
151-
.build();
150+
.addField("Times Thanked This Week", String.format("**%s**", weekThanks), true);
151+
if (upload != null) {
152+
eb.setImage("attachment://"+upload.getName());
153+
}
154+
return eb.build();
152155
}
153156

154157
private @NotNull String formatExperience(Guild guild, @NotNull HelpAccount account) {

src/main/java/net/javadiscord/javabot/util/Plotter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
* Creates diagrams.
1010
*/
1111
public class Plotter {
12+
private String title;
1213
private final List<Pair<String, Double>> entries;
1314
private int width=3000;
1415
private int height=1500;
1516

1617
/**
1718
* Creates the plotter.
1819
* @param entries a list of all data points to plot, each represented as a {@link Pair} consisting of the name and value of the data point
20+
* @param title the title of the plot
1921
*/
20-
public Plotter(List<Pair<String, Double>> entries) {
21-
this.entries=entries;
22+
public Plotter(List<Pair<String, Double>> entries, String title) {
23+
this.entries = entries;
24+
this.title = title;
2225
}
2326

2427
/**
@@ -35,15 +38,15 @@ public BufferedImage plot() {
3538
g2d.fillRect(0, 0, width, height);
3639
g2d.setColor(Color.BLACK);
3740

38-
centeredText(g2d, "gained help XP per month", width/2, 50);
41+
centeredText(g2d, title, width/2, 50);
3942

4043
plotEntries(g2d, 100, 100, width-200, height-200);
4144

4245
return img;
4346
}
4447

4548
private void plotEntries(Graphics2D g2d, int x, int y, int width, int height) {
46-
double maxValue = entries.stream().mapToDouble(Pair::second).sum();
49+
double maxValue = entries.stream().mapToDouble(Pair::second).max().orElse(0);
4750
int stepSize = 2*(int)Math.pow(10,(int)Math.log10(maxValue)-1);
4851
if (stepSize==0) {
4952
stepSize=1;

0 commit comments

Comments
 (0)