0

XYPlot の dataArea を取得しようとしていますが、配置したグラフ パネルはそれを記録していないようです。要求するたびに、高さ、幅、x、y の値が常に 0 になります。

private JPanel createChartPanel()
    {
    JPanel chartAreaPanel = new JPanel();
    JFreeChart originalDataChart = createOriginalDataChart();
    ChartPanel originalDataPanel = new ChartPanel(originalDataChart);
    drawDataDirections(originalDataChart, originalDataPanel);       
    //This is where I need originalDataPanel's ChartRenderingInfo,
    //but all of the relevant fields in it are zero. Why?
    originalDataPanel.setPreferredSize(new Dimension(960, 500));        
    chartAreaPanel.setLayout(new GridBagLayout());
    chartAreaPanel.add(originalDataPanel);

    JPanel centerPanel = new JPanel();

    JPanel chartPanel = new JPanel(new BorderLayout());
    Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
    TitledBorder chartBorder = BorderFactory.createTitledBorder(etchedBorder, "Timeseries Chart");
    chartBorder.setTitlePosition(TitledBorder.TOP);
    chartPanel.setBorder(chartBorder);

    centerPanel.add(chartAreaPanel);
    chartPanel.add(centerPanel, BorderLayout.EAST);

    return chartPanel;
    }

以下は drawDataDirections() です

private void drawDataDirections(JFreeChart jfc, ChartPanel cp)
    {
    XYPlot original = ((XYPlot) ((CombinedDomainXYPlot) jfc.getXYPlot()).getSubplots().get(0));
    XYDataset data  = original.getDataset(0);
    Rectangle2D dataArea = cp.getChartRenderingInfo().getPlotInfo().getDataArea();
    ValueAxis domAxis = original.getDomainAxis();
    ValueAxis rngAxis = original.getRangeAxis();

    float[] xCoords = new float[data.getItemCount(0)];
    float[] yCoords = new float[data.getItemCount(0)];
    double[] angles = new double[data.getItemCount(0)];

    //These three values need to be set in advance, as they are never reached.
    xCoords[0] = (float) domAxis.valueToJava2D(data.getXValue(0, 0), dataArea, original.getDomainAxisEdge());
    yCoords[0] = (float) rngAxis.valueToJava2D(data.getYValue(0, 0), dataArea, original.getRangeAxisEdge());
    angles[angles.length - 1] = 0.0;

    for (int i = 1;i < data.getItemCount(0);i++)
        {
        xCoords[i] = (float) domAxis.valueToJava2D(data.getXValue(0, i), dataArea, original.getDomainAxisEdge());
        yCoords[i] = (float) rngAxis.valueToJava2D(data.getYValue(0, i), dataArea, original.getRangeAxisEdge());
        angles[i] = Math.atan2(yCoords[i] - yCoords[i - 1], xCoords[i] - xCoords[i - 1]);
        }

    ((XYLineAndShapeRendererExtended) original.getRenderer(0)).setAngles(angles);
    ((XYLineAndShapeRendererExtended) original.getRenderer(0)).setCoords(xCoords, yCoords);
    //Finally, start drawing the shapes.
    ((XYLineAndShapeRendererExtended) original.getRenderer(0)).setSeriesShapesVisible(0,true);
    }
4

1 に答える 1

1

グラフが実際にサイズ変更されてレンダリングされる前に、レンダリング情報にアクセスしようとしています。値は不明です。たとえば、ここでは、フレームがパックされて表示される前後のレンダリング情報にアクセスしようとしています。

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

class Sandbox {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1, "Test", "A");
        dataset.addValue(4, "Test", "B");
        dataset.addValue(3, "Test", "C");

        final JFreeChart chart = ChartFactory.createLineChart(
            "Test", "Domain", "Range", dataset,
            PlotOrientation.VERTICAL, true, true, false);

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout(5, 5));
        final ChartPanel cp = new ChartPanel(chart);
        frame.add(cp, BorderLayout.CENTER);

        Rectangle2D dataArea = cp.getChartRenderingInfo().getPlotInfo().getDataArea();
        System.out.println("dataArea = " + dataArea);

        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            Rectangle2D dataArea = cp.getChartRenderingInfo().getPlotInfo().getDataArea();
            System.out.println("dataArea = " + dataArea);
          }
        });
      }
    });
  }
}

出力:

dataArea = java.awt.geom.Rectangle2D$Double[x=0.0,y=0.0,w=0.0,h=0.0]
dataArea = java.awt.geom.Rectangle2D$Double[x=67.8984375,y=34.140625,w=600.1015625,h=307.9921875]
于 2013-07-24T19:51:44.943 に答える