From c4b3838110f6892c6e77e672e95ea22cf5e34886 Mon Sep 17 00:00:00 2001 From: Dumoulin Nicolas <nicolas.dumoulin@irstea.fr> Date: Tue, 24 May 2016 14:59:18 +0200 Subject: [PATCH] better charts design --- .../model/gui/TimeSerieChart.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/fr/irstea/associatione/model/gui/TimeSerieChart.java b/src/main/java/fr/irstea/associatione/model/gui/TimeSerieChart.java index b353aed..7f50b1c 100644 --- a/src/main/java/fr/irstea/associatione/model/gui/TimeSerieChart.java +++ b/src/main/java/fr/irstea/associatione/model/gui/TimeSerieChart.java @@ -28,6 +28,7 @@ import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; +import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.StandardXYBarPainter; @@ -52,7 +53,7 @@ public class TimeSerieChart { private final boolean histogram, series; private final ReflectUtils.MethodOnInstance methodOnInstance; private final List<double[]> timeserie; - private final ChartPanel histogramPanel; + private ChartPanel histogramPanel; private final XYSeriesCollection xySeries; public Timeserie(Model model, String fetcherPath, String name, boolean histogram, boolean series) throws ProcessingException { @@ -63,7 +64,7 @@ public class TimeSerieChart { this.methodOnInstance = ReflectUtils.getMethodOnInstance(model, fetcherPath); this.timeserie = new ArrayList<>(); xySeries = new XYSeriesCollection(); - histogramPanel = new ChartPanel(createHistogram(name, (double[]) methodOnInstance.invoke())); + histogramPanel = new ChartPanel(createHistogram(name, 0, (double[]) methodOnInstance.invoke())); } catch (NoSuchFieldException | IllegalAccessException ex) { throw new ProcessingException("Error during method retrieval", ex); } @@ -81,9 +82,9 @@ public class TimeSerieChart { timeserie.add(data); } - public void update() throws IllegalAccessException { + public void update(int timestep) throws IllegalAccessException { final double[] data = (double[]) methodOnInstance.invoke(); - histogramPanel.setChart(createHistogram(name, data)); + histogramPanel.setChart(createHistogram(name, timestep, data)); timeserie.add(data); if (xySeries.getSeriesCount() == 0) { for (int i = 0; i < data.length; i++) { @@ -117,10 +118,10 @@ public class TimeSerieChart { } } - public void updateCharts() { + public void updateCharts(int timestep) { for (Timeserie timeserie : timeseries) { try { - timeserie.update(); + timeserie.update(timestep); } catch (IllegalAccessException ex) { Logger.getLogger(TimeSerieChart.class.getName()).log(Level.SEVERE, null, ex); } @@ -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(); - dataset.setType(HistogramType.SCALE_AREA_TO_1); + dataset.setType(HistogramType.RELATIVE_FREQUENCY); dataset.addSeries(title, data, 30); - String plotTitle = title; + String plotTitle = title + " " + timestep; String xaxis = "opinions"; String yaxis = "amount"; PlotOrientation orientation = PlotOrientation.VERTICAL; boolean show = false; - boolean toolTips = false; + boolean toolTips = true; boolean urls = false; JFreeChart chart = ChartFactory.createHistogram(plotTitle, xaxis, yaxis, dataset, orientation, show, toolTips, urls); XYPlot plot = (XYPlot) chart.getPlot(); + NumberAxis numberaxis = (NumberAxis)plot.getRangeAxis(); + numberaxis.setUpperBound(1.0); + // raw style XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer(); renderer.setBarPainter(new StandardXYBarPainter()); renderer.setShadowVisible(false); -- GitLab