Skip to content

Commit 8aea9b2

Browse files
committed
externalize title of plot, use max for plot height
1 parent c830f7f commit 8aea9b2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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");

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)