Home >Java >javaTutorial >How to Dynamically Rescale the Domain Axis in JFreeChart's CombinedDomainXYPlot?

How to Dynamically Rescale the Domain Axis in JFreeChart's CombinedDomainXYPlot?

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 12:55:12466browse

How to Dynamically Rescale the Domain Axis in JFreeChart's CombinedDomainXYPlot?

Domain Axis Rescaling in CombinedDomainXYPlot

When utilizing a CombinedDomainXYPlot to share a domain axis between multiple subplots, it's observed that the range axes adjust to data changes while the domain axis remains unchanged. Understanding and addressing this issue requires a closer look at the underlying mechanisms of the plot.

Combined Ranges for Domain Axis

CombinedDomainXYPlot establishes a maximum range for the shared domain axis, which enables axis sharing. Series visibility changes do not directly affect the shared domain axis. However, altering the dataset updates the domain axis via the configure() method. This allows the range axes of the subplots to be updated independently.

Auto-Updating Domain Axis

To automatically adjust the shared domain axis, use addSeries() or removeSeries() rather than setSeriesVisible(). These methods trigger the configuration of the domain axis.

Example Customization

The code example below demonstrates a CombinedDomainXYPlot where the domain axis is updated by calling configure() when a subplot is updated or a series is hidden.

mainPlot.getDomainAxis().configure();

Considerations

  • Ensure that initial threads are accounted for and the frame is properly packed.
  • The suggested approach of toggling setAutoRange() can be substituted with a single configure() call, but the effect may not be noticeable as the data and maximum range remain unchanged.

      public static void init() {
          XYItemRenderer renderer = new StandardXYItemRenderer(SHAPES_AND_LINES);
          XYPlot plot1 = new XYPlot(
              generateData(), null, new NumberAxis("Range 1"), renderer);
          XYPlot plot2 = new XYPlot(
              generateData(), null, new NumberAxis("Range 2"), renderer);
          final CombinedDomainXYPlot plot =
              new CombinedDomainXYPlot(new NumberAxis("Domain"));
          plot.setDomainPannable(true);
          plot.setRangePannable(true);
          plot.add(plot1);
          plot.add(plot2);
          plot.setOrientation(PlotOrientation.VERTICAL);
          JFreeChart chart = new JFreeChart(
              "Combined Plots", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
          ChartPanel chartPanel = new ChartPanel(chart);
          chartPanel.setPreferredSize(new Dimension(800, 500));
    
          JPanel controlPanel = new JPanel();
          controlPanel.add(new JButton(new UpdateAction(plot, 0)));
          controlPanel.add(new JButton(new UpdateAction(plot, 1)));
          for (int i = 0; i < MAX; i++) {
              JCheckBox jcb = new JCheckBox(new VisibleAction(renderer, i));
              jcb.setSelected(true);
              renderer.setSeriesVisible(i, true);
              controlPanel.add(jcb);
          }
    
          JFrame frame = new JFrame("Combined Plot Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(chartPanel, BorderLayout.CENTER);
          frame.add(controlPanel, BorderLayout.SOUTH);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }

The above is the detailed content of How to Dynamically Rescale the Domain Axis in JFreeChart's CombinedDomainXYPlot?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn