本文介绍了JfreeChart:滚动XYBarChart水平 - (图表翻译和导航)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图水平滚动我的XYBarChart,我下面的一个JfreeChart的演示TranslateDemo1.java,其中的源代码,你可以在这里找到:

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:

源代码适用于TimeSeriesChart。

The source code works fine for a "TimeSeriesChart".

但是我试着用XYBarChart和WHEN I SLIDE THE BAR的行为是不一样的。

However I tried with "XYBarChart" and WHEN I SLIDE THE BAR the behavior is not the same.

为了清楚,我用以下源代码替换了行#157:

Just to be clear I replaced the line #157 with the following source code:

JFreeChart chart1= ChartFactory.createXYBarChart(
                "Title",
                null,
                true,
                "Ylabel",
                xyDatasetTranslating,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);

有没有人知道为什么应用程序以这种方式运行?

Does anyone know why the application is behaving in this way? and how I could possibly fix it.

感谢,

推荐答案

这是一个实现一个简单解决方案的源代码。

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);
    }

}

这篇关于JfreeChart:滚动XYBarChart水平 - (图表翻译和导航)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:36