2

Hello i have to make a program to display power curves, and therefore i need to display three different plots on one window. The different kind of plots are XY (just points), bar, and XY with lines.

My problem(s) : somehow i can get only two of the charts to get drawn AND i can't change the colors of the single chart correctly.

EDIT : When i put as comment the declaration of the third chart, the second one finally gets drawn. Is it impossible to draw three charts ?

Any help will be greatly appreciated, thanks ;)

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;


public class OverlaidPlot extends ApplicationFrame 
{
   final XYSeries series0 = new XYSeries("Graph0");
   final XYSeries series1 = new XYSeries("Graph1");
   final XYSeries series2 = new XYSeries("Graph2");

   public OverlaidXYPlotDemo(final String title) 
    {
       super(title);
       final JFreeChart chart = createOverlaidChart();
       final ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
       panel.setPreferredSize(new java.awt.Dimension(800, 600));
       setContentPane(panel);
     }
 public void addElem0(double x, double y)
{
    this.series0.add(x, y);
}

public void addElem1(double x, double y)
{
    this.series1.add(x, y);
}

public void addElem2(double x, double y)
{
    this.series2.add(x, y);
}


private JFreeChart createOverlaidChart() 
{
    final NumberAxis domainAxis = new NumberAxis("Speed (m/s)");
    final ValueAxis rangeAxis = new NumberAxis("Power (kw)");

    // create plot ...
    final IntervalXYDataset data0 = createDataset0();
    final XYItemRenderer renderer0 = new XYBarRenderer(0.20);
    // change "new XYBarRenderer(0.20)" to "StandardXYItemRenderer()" if you want to change  type of graph
    final XYPlot plot = new XYPlot(data0, domainAxis, rangeAxis, renderer0);

    // add a second dataset and renderer... 
    final IntervalXYDataset data1 = createDataset1();
    final XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer(false, true); 
    // arguments of new XYLineAndShapeRenderer are to activate or deactivate the display of points or line. Set first argument to true if you want to draw lines between the points for e.g.
    plot.setDataset(1, data1);
    plot.setRenderer(1, renderer1);

    // add a third dataset and renderer... 
    final IntervalXYDataset data2 = createDataset2();
    final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, true); 
    // arguments of new XYLineAndShapeRenderer are to activate or deactivate the display of points or line. Set first argument to true if you want to draw lines between the points for e.g.
    plot.setDataset(1, data2);
    plot.setRenderer(1, renderer2);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    NumberAxis domain = (NumberAxis) plot.getDomainAxis();/*
    domain.setRange(0.00, 30);*/

    domain.setTickUnit(new NumberTickUnit(0.5));     
    domain.setVerticalTickLabels(true);
    // return a new chart containing the overlaid plot...
    return new JFreeChart("Test", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

}

private IntervalXYDataset createDataset0() 
{
    // create dataset 0...
    final XYSeriesCollection coll0 = new XYSeriesCollection(series0);
    return coll0;
}

private IntervalXYDataset createDataset1() 
{
    // create dataset 1...
    final XYSeriesCollection coll1 = new XYSeriesCollection(series1);
    return coll1;
}

private IntervalXYDataset createDataset2() 
{
    // create dataset 2...
    final XYSeriesCollection coll2 = new XYSeriesCollection(series2);
    return coll2;
}
}
4

1 回答 1

5

您在图中的同一索引处有两个数据集 - 确保将每个数据集设置为唯一索引:

plot.setDataset(2, data2);
plot.setRenderer(2, renderer2);

更改此设置后,我使用一些测试数据运行您的示例,并且能够看到绘制的所有三个数据集。

于 2013-09-13T15:41:29.987 回答