CombinedDomainXYPlot 不重新缩放域轴 [英] CombinedDomainXYPlot not rescaling domain axis

查看:15
本文介绍了CombinedDomainXYPlot 不重新缩放域轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从使用 CombinedDomainXYPlot 的图表中隐藏系列数时,所有范围轴都会很好地自动重新缩放.但是,域轴不会重新缩放.有什么方法可以手动刷新缩放,或者我可能缺少一个设置来在此设置中启用域轴的自动缩放?

When I hide number of series from a chart that is using a CombinedDomainXYPlot, all range axes are auto rescaled nicely. Howver, the domain axis does not get rescaled. Is there any way to manually refresh scaling, or perhaps there is a setting I am missing to enable auto scaling of a domain axis in this setting?

推荐答案

回答我自己的问题:

我设法使用一个小技巧刷新了轴:

I managed to refresh the axis using a little hack:

 mainPlot.getDomainAxis().setAutoRange(false);
 mainPlot.getDomainAxis().setAutoRange(true);

这不是很好,但它确实有效.不过,我希望有人可以发布一个更好的解决方案...

It is not nice but it does the trick. Nevertheless, I wish someone could post a nicer solution...

这是使用不起作用的非自定义数据集的代码.请运行它,然后单击一个名为 click 的大按钮多次以隐藏几个系列.域轴未重新缩放.

Here is the code using non-custom data set that does not work. Please run it and then click on a big button called click multiple times to hide a few series. The domain axis is not rescaled.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;
import javax.swing.*;
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.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleEdge;

public class Runner {

    private static Random rand = new Random();

    public static void main(String[] args) {
        XYSeriesCollection data = new XYSeriesCollection();
        int max = rand.nextInt(2) + 2;
        for (int i = 0; i < max; i++) {
            data.addSeries(generateSeries("Series" + (i + 1)));
        }
        final XYItemRenderer renderer1 = new StandardXYItemRenderer();
        final XYPlot plot1 = new XYPlot(data, null, new DateAxis("Dates"), renderer1);

        data = new XYSeriesCollection();
        for (int i = 0; i < max; i++) {
            data.addSeries(generateSeries("Series" + (i + 1)));
        }
        final XYPlot plot2 = new XYPlot(data, null, new NumberAxis("Numbers"), renderer1);

        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
        plot.setGap(10.0);

        // add the subplots...
        plot.add(plot1, 1);
        plot.add(plot2, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        // return a new chart containing the overlaid plot...
        final JFreeChart chart = new JFreeChart("CombinedDomainXYPlot Demo",
            JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        chart.getLegend().setPosition(RectangleEdge.RIGHT);

        chart.addChangeListener(new ChartChangeListener() {

            boolean changed = false;

            @Override
            public void chartChanged(ChartChangeEvent event) {
                if (!changed) {
                } else {
                    changed = false;
                }
            }
        });

        ChartPanel panel = new ChartPanel(chart);
        JPanel panel2 = new JPanel(new BorderLayout(0, 10));
        panel2.add(panel, BorderLayout.CENTER);
        JButton b = new JButton("Click");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                CombinedDomainXYPlot plot = (CombinedDomainXYPlot) chart.getXYPlot();
                List l = plot.getSubplots();
                int index = rand.nextInt(plot1.getSeriesCount() + plot2.getSeriesCount());
                boolean b = renderer1.isSeriesVisible(index);
                renderer1.setSeriesVisible(index, false);
            }
        });
        panel2.add(b, BorderLayout.NORTH);
        panel2.setVisible(true);

        JFrame frame = new JFrame("dsadsa");
        frame.add(panel2);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    private static XYSeries generateSeries(String key) {
        XYSeries series = new XYSeries(key);
        int points = 15;
        double val = 0.0;
        double x = 0.0;
        for (int i = 0; i < points; i++) {
            val += rand.nextDouble() * 6 - 3;
            x += rand.nextDouble() * 4;
            series.add(x, val);
        }
        return series;
    }
}

这篇关于CombinedDomainXYPlot 不重新缩放域轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆