From 7444d051919509b41847e4d7744218ad2281ce84 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=B6nnecke?= <simonkoennecke@gmail.com>
Date: Tue, 19 Jul 2016 13:12:09 +0200
Subject: [PATCH] Split supervisor and wallet histogram

---
 .../java/fucoin/gui/SuperVisorThreadGUI.java  |   2 +-
 .../java/fucoin/gui/WalletGuiControlImpl.java |   2 +-
 .../java/fucoin/gui/WalletHistogramGUI.java   | 122 ++++++++++++++++++
 3 files changed, 124 insertions(+), 2 deletions(-)
 create mode 100644 src/main/java/fucoin/gui/WalletHistogramGUI.java

diff --git a/src/main/java/fucoin/gui/SuperVisorThreadGUI.java b/src/main/java/fucoin/gui/SuperVisorThreadGUI.java
index f4cc75c..03e7bb1 100644
--- a/src/main/java/fucoin/gui/SuperVisorThreadGUI.java
+++ b/src/main/java/fucoin/gui/SuperVisorThreadGUI.java
@@ -98,7 +98,7 @@ public class SuperVisorThreadGUI {
             showHistogramBtn.addActionListener(e -> {
                 LocalDateTime time = (LocalDateTime) dropdown.getSelectedItem();
                 superVisorGuiControl.loadSnapshot(time);
-                new SuperVisorHistogramGUI(new HashSet<>(superVisorGuiControl.getSnapshots()), time).init();
+                new SuperVisorHistogramGUI(superVisorGuiControl, time).init();
             });
             btnPanel.add(showHistogramBtn);
 
diff --git a/src/main/java/fucoin/gui/WalletGuiControlImpl.java b/src/main/java/fucoin/gui/WalletGuiControlImpl.java
index fac84c0..2aed1bf 100644
--- a/src/main/java/fucoin/gui/WalletGuiControlImpl.java
+++ b/src/main/java/fucoin/gui/WalletGuiControlImpl.java
@@ -164,7 +164,7 @@ public class WalletGuiControlImpl implements WalletGuiControl {
         btnShowHistogram.addActionListener(e -> {
             LocalDateTime time = (LocalDateTime)dropdown.getSelectedItem();
             Set<Snapshot> snapshots = wallet.getSnapshots(time);
-            new SuperVisorHistogramGUI(snapshots, time).init();
+            new WalletHistogramGUI(snapshots, time).init();
         });
 
         dropdown.addActionListener(e -> {
diff --git a/src/main/java/fucoin/gui/WalletHistogramGUI.java b/src/main/java/fucoin/gui/WalletHistogramGUI.java
new file mode 100644
index 0000000..a45408f
--- /dev/null
+++ b/src/main/java/fucoin/gui/WalletHistogramGUI.java
@@ -0,0 +1,122 @@
+package fucoin.gui;
+
+import fucoin.QDigest;
+import fucoin.wallet.Snapshot;
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.ChartPanel;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.IntervalMarker;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.category.DefaultCategoryDataset;
+import org.jfree.ui.RectangleAnchor;
+import org.jfree.ui.TextAnchor;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import java.awt.*;
+import java.time.LocalDateTime;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+/**
+ *
+ */
+public class WalletHistogramGUI extends JFrame {
+
+    private final Set<Snapshot> snapshots;
+    private final LocalDateTime time;
+
+    private static final int width = 1200;
+    private static final int height = 800;
+
+
+    public WalletHistogramGUI(Set<Snapshot> snapshots, LocalDateTime time) {
+        super("Histogram of distributed granted commit (Snapshot " + time + ")");
+        this.time = time;
+        this.snapshots = new HashSet<>(snapshots);
+
+        setVisible(true);
+        setSize(width, height);
+    }
+
+    private JFreeChart createChart(DefaultCategoryDataset dataset) {
+        final JFreeChart chart = ChartFactory.createBarChart(
+                "",
+                "Transaction size [FUCoins]",
+                "Frequency",
+                dataset,
+                PlotOrientation.VERTICAL,
+                true,
+                true,
+                false
+        );
+        final IntervalMarker target = new IntervalMarker(400.0, 700.0);
+        target.setLabel("Target Range");
+        target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11));
+        target.setLabelAnchor(RectangleAnchor.LEFT);
+        target.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
+        target.setPaint(new Color(222, 222, 255, 128));
+
+        return chart;
+    }
+
+    public void init() {
+        QDigest qDigest = null;
+        for (Snapshot item : snapshots) {
+            if (qDigest == null) {
+                qDigest = item.getqDigest();
+            } else {
+                qDigest = QDigest.unionOf(qDigest, item.getqDigest());
+            }
+        }
+
+        // row keys...
+        final String series1 = "Transactions";
+
+        // create the dataset...
+        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
+
+        for (long[] item : qDigest.toAscRanges()) {
+            dataset.addValue(item[2], series1, item[0] + "-" + item[1]);
+        }
+
+        JFreeChart chart = createChart(dataset);
+        final ChartPanel chartPanel = new ChartPanel(chart);
+
+        JPanel myPanel = new JPanel(new BorderLayout());
+        myPanel.add(chartPanel, BorderLayout.CENTER);
+
+        JPanel quantilePanel = new JPanel();
+
+        final JSlider slider = new JSlider(0, 100);
+        Hashtable<Integer, JComponent> labelTable = new Hashtable<>();
+        labelTable.put(0, new JLabel("0.0") );
+        labelTable.put(5, new JLabel("0.5") );
+        labelTable.put(10, new JLabel("1.0") );
+        slider.setLabelTable( labelTable );
+        slider.setValue(50);
+
+        final JTextField quantileValueField = new JTextField();
+        quantileValueField.setEditable(false);
+        quantileValueField.setPreferredSize(new Dimension(40, 20));
+
+        QDigest finalQDigest = qDigest;
+        slider.addChangeListener(e -> {
+            double val = slider.getValue() / 100.;
+            assert val <= 1.0;
+            quantileValueField.setText(String.format("%1$d", finalQDigest.getQuantile(val)));
+        });
+
+        slider.getChangeListeners()[0].stateChanged(null);
+
+        quantilePanel.add(slider);
+        quantilePanel.add(quantileValueField);
+        quantilePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
+
+        myPanel.add(quantilePanel, BorderLayout.PAGE_END);
+
+        setContentPane(myPanel);
+    }
+
+}
-- 
GitLab