JfreeChart: Scroll XYBarChart Horizontally - (chart translation and navigation)

后端 未结 3 1364
南方客
南方客 2021-01-06 14:22

I am trying to scroll my XYBarChart horizontally, I am following one of the JfreeChart\'s Demo \"TranslateDemo1.java\" in which the source code you can find here:

h

相关标签:
3条回答
  • 2021-01-06 14:34

    Here is a source code that implement a simple solution.

    import java.awt.BorderLayout;
    import java.util.Date;
    
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.border.Border;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYBarRenderer;
    import org.jfree.data.time.DateRange;
    import org.jfree.data.time.Day;
    import org.jfree.data.time.RegularTimePeriod;
    import org.jfree.data.time.TimeSeries;
    import org.jfree.data.time.TimeSeriesCollection;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    public class DomainTranslateDemo extends ApplicationFrame {
    
        private static class DemoPanel extends JPanel implements ChangeListener {
    
            private static int SLIDER_INITIAL_VALUE = 50;
            private JSlider slider;
            private DateAxis domainAxis;
            private int lastValue = SLIDER_INITIAL_VALUE;
    
            // one month (milliseconds, seconds, minutes, hours, days)
            private int delta = 1000 * 60 * 60 * 24 * 30;
    
            public DemoPanel() {
                super(new BorderLayout());
                JFreeChart chart = createChart();
                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(new java.awt.Dimension(600, 270));
                chartPanel.setDomainZoomable(true);
                chartPanel.setRangeZoomable(true);
                Border border = BorderFactory.createCompoundBorder(
                    BorderFactory.createEmptyBorder(4, 4, 4, 4),
                    BorderFactory.createEtchedBorder()
                );
                chartPanel.setBorder(border);
                add(chartPanel);
    
                JPanel dashboard = new JPanel(new BorderLayout());
                dashboard.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));   
    
                this.slider = new JSlider(0, 100, SLIDER_INITIAL_VALUE);
                this.slider.addChangeListener(this);
                dashboard.add(this.slider);
                add(dashboard, BorderLayout.SOUTH);
            }
    
            private JFreeChart createChart() {
    
                TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
                TimeSeries series = createSerie(500,20);
                timeSeriesCollection.addSeries(series );
    
                this.domainAxis = new DateAxis("Time");
                NumberAxis rangeAxis = new NumberAxis("");
                XYBarRenderer renderer = new XYBarRenderer();
                renderer.setShadowVisible(false);
                XYPlot plot = new XYPlot(timeSeriesCollection, domainAxis, rangeAxis, renderer);
    
                JFreeChart chart = new JFreeChart(
                        "Title", 
                        JFreeChart.DEFAULT_TITLE_FONT,
                        plot, 
                        true);
                // performance
                chart.setAntiAlias(false);
                return chart;
            }
    
            private TimeSeries createSerie(int domainCount,int rangeCount) {
                TimeSeries timeSeries =  new TimeSeries("timeSeries1");
                Day d = new Day(new Date());
                RegularTimePeriod regularTimePeriod = d.next();
                for (int index = 0; index < domainCount; index++) {
                    if (index % 2 == 0) {
                        double value = (Math.random() * rangeCount);
                        timeSeries.add(regularTimePeriod,value);
                    }
                    regularTimePeriod = regularTimePeriod.next();
                }
                return timeSeries;
            }
    
            @Override
            public void stateChanged(ChangeEvent event) {
                int value = this.slider.getValue();
                long minimum = domainAxis.getMinimumDate().getTime();
                long maximum = domainAxis.getMaximumDate().getTime();
                if (value<lastValue) { // left
                    minimum = minimum - delta;
                    maximum = maximum - delta;
                } else { // right
                    minimum = minimum + delta;
                    maximum = maximum + delta;
                }
                DateRange range = new DateRange(minimum,maximum);
                domainAxis.setRange(range);
                lastValue = value;
            }
    
        }
    
        public DomainTranslateDemo(String title) {
            super(title);
            setContentPane(new DemoPanel());
        }
    
        public static JPanel createDemoPanel() {
            return new DemoPanel();
        }
    
        public static void main(String[] args) {
            DomainTranslateDemo demo = new DomainTranslateDemo("Translate Demo");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
        }
    
    } 
    
    0 讨论(0)
  • 2021-01-06 14:36

    TranslatingXYDataset presently implements XYDataset; instead, you'll need to implement IntervalXYDataset, as required by JFreeChart.createXYBarChart.

    0 讨论(0)
  • 2021-01-06 14:44

    Thanks trashgod :D But I gave up on this approach. I need to get some work done over here ;) So in order to navigate on the graph I used a different dataset org.jfree.data.time.DynamicTimeSeriesCollection.

    This class is aimed for real-time applications in which we have the ability to append new data and discard the oldest in a pretty fast way (depend on your input data). In summary, every time that someone scroll the bar I just need to change my underlying dataset, and that will fire a PlotChangeEvent which, in turn, gets passed on to the chart and results in a ChartChangeEvent being fired.

    This chain of events is used to ensure that charts are automatically updated whenever a change is made to any component of the chart.

    Once again, thank you very much

    0 讨论(0)
提交回复
热议问题