ChartRenderingInfo为图表区域返回0

时间:2013-07-24 18:54:53

标签: java jfreechart

我正在尝试获取XYPlot的dataArea,但我放置的图表面板似乎没有记录它。每当我要求时,我总是得到0的高度,宽度,x和y值。

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

1 个答案:

答案 0 :(得分: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]