Commit c4b38381 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

better charts design

parent 78dbc5b5
No related merge requests found
Showing with 14 additions and 10 deletions
+14 -10
...@@ -28,6 +28,7 @@ import javax.swing.JPanel; ...@@ -28,6 +28,7 @@ import javax.swing.JPanel;
import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart; import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot; import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter; import org.jfree.chart.renderer.xy.StandardXYBarPainter;
...@@ -52,7 +53,7 @@ public class TimeSerieChart { ...@@ -52,7 +53,7 @@ public class TimeSerieChart {
private final boolean histogram, series; private final boolean histogram, series;
private final ReflectUtils.MethodOnInstance methodOnInstance; private final ReflectUtils.MethodOnInstance methodOnInstance;
private final List<double[]> timeserie; private final List<double[]> timeserie;
private final ChartPanel histogramPanel; private ChartPanel histogramPanel;
private final XYSeriesCollection xySeries; private final XYSeriesCollection xySeries;
public Timeserie(Model model, String fetcherPath, String name, boolean histogram, boolean series) throws ProcessingException { public Timeserie(Model model, String fetcherPath, String name, boolean histogram, boolean series) throws ProcessingException {
...@@ -63,7 +64,7 @@ public class TimeSerieChart { ...@@ -63,7 +64,7 @@ public class TimeSerieChart {
this.methodOnInstance = ReflectUtils.getMethodOnInstance(model, fetcherPath); this.methodOnInstance = ReflectUtils.getMethodOnInstance(model, fetcherPath);
this.timeserie = new ArrayList<>(); this.timeserie = new ArrayList<>();
xySeries = new XYSeriesCollection(); xySeries = new XYSeriesCollection();
histogramPanel = new ChartPanel(createHistogram(name, (double[]) methodOnInstance.invoke())); histogramPanel = new ChartPanel(createHistogram(name, 0, (double[]) methodOnInstance.invoke()));
} catch (NoSuchFieldException | IllegalAccessException ex) { } catch (NoSuchFieldException | IllegalAccessException ex) {
throw new ProcessingException("Error during method retrieval", ex); throw new ProcessingException("Error during method retrieval", ex);
} }
...@@ -81,9 +82,9 @@ public class TimeSerieChart { ...@@ -81,9 +82,9 @@ public class TimeSerieChart {
timeserie.add(data); timeserie.add(data);
} }
public void update() throws IllegalAccessException { public void update(int timestep) throws IllegalAccessException {
final double[] data = (double[]) methodOnInstance.invoke(); final double[] data = (double[]) methodOnInstance.invoke();
histogramPanel.setChart(createHistogram(name, data)); histogramPanel.setChart(createHistogram(name, timestep, data));
timeserie.add(data); timeserie.add(data);
if (xySeries.getSeriesCount() == 0) { if (xySeries.getSeriesCount() == 0) {
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
...@@ -117,10 +118,10 @@ public class TimeSerieChart { ...@@ -117,10 +118,10 @@ public class TimeSerieChart {
} }
} }
public void updateCharts() { public void updateCharts(int timestep) {
for (Timeserie timeserie : timeseries) { for (Timeserie timeserie : timeseries) {
try { try {
timeserie.update(); timeserie.update(timestep);
} catch (IllegalAccessException ex) { } catch (IllegalAccessException ex) {
Logger.getLogger(TimeSerieChart.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(TimeSerieChart.class.getName()).log(Level.SEVERE, null, ex);
} }
...@@ -128,20 +129,23 @@ public class TimeSerieChart { ...@@ -128,20 +129,23 @@ public class TimeSerieChart {
} }
} }
public static JFreeChart createHistogram(String title, double[] data) { public static JFreeChart createHistogram(String title, int timestep, double[] data) {
HistogramDataset dataset = new HistogramDataset(); HistogramDataset dataset = new HistogramDataset();
dataset.setType(HistogramType.SCALE_AREA_TO_1); dataset.setType(HistogramType.RELATIVE_FREQUENCY);
dataset.addSeries(title, data, 30); dataset.addSeries(title, data, 30);
String plotTitle = title; String plotTitle = title + " " + timestep;
String xaxis = "opinions"; String xaxis = "opinions";
String yaxis = "amount"; String yaxis = "amount";
PlotOrientation orientation = PlotOrientation.VERTICAL; PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean show = false; boolean show = false;
boolean toolTips = false; boolean toolTips = true;
boolean urls = false; boolean urls = false;
JFreeChart chart = ChartFactory.createHistogram(plotTitle, xaxis, yaxis, JFreeChart chart = ChartFactory.createHistogram(plotTitle, xaxis, yaxis,
dataset, orientation, show, toolTips, urls); dataset, orientation, show, toolTips, urls);
XYPlot plot = (XYPlot) chart.getPlot(); XYPlot plot = (XYPlot) chart.getPlot();
NumberAxis numberaxis = (NumberAxis)plot.getRangeAxis();
numberaxis.setUpperBound(1.0);
// raw style
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer(); XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
renderer.setBarPainter(new StandardXYBarPainter()); renderer.setBarPainter(new StandardXYBarPainter());
renderer.setShadowVisible(false); renderer.setShadowVisible(false);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment