How to create multiple ScatterPlot chart using createCombinedChart() on JFreeChart?

六眼飞鱼酱① 提交于 2021-01-28 07:33:23

问题


I want to create multiple ScatterPlot chart on JFreeChart and to do so I've used the function createCombinedChart(). With my current code I get multiple charts just like the CombinedXYPlotDemo1 that you can found, but those are line chart and I want them as ScatterPlot.

I've found this question but after trying I can't figure out how to make it work (maybe I just don't understand how to use it)

here is a sample of my code (it's very similar to the Demo code + I retrieve from my database and added a Time Axis).

public class DatabaseChart extends ApplicationFrame {
    private static final long serialVersionUID = 1L;
    public DatabaseChart(final String title) {
        super(title);
        final JFreeChart chart = createCombinedChart();
        final ChartPanel panel = new ChartPanel(chart, true, true, true, true, true);
        panel.setPreferredSize(new java.awt.Dimension(1000, 500));
        setContentPane(panel);
}
/**
 * Creates a combined chart.
 *
 * @return the combined chart.
 */
private JFreeChart createCombinedChart() {

    // create subplot 1 
    final XYDataset data1 = createDataset1();
    final XYItemRenderer renderer1 = new StandardXYItemRenderer();
    final NumberAxis rangeAxis1 = new NumberAxis("Axis");
    rangeAxis1.setLabelPaint(Color.RED);
    final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
    subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    
    // create subplot 2
    [...]
    
    // create subplot 3 
    [...]
    
    // parent plot...
    final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain"));
    //plot.setRenderer(new XYLineAndShapeRenderer(false,true)); ??
    plot.setGap(25.0);
    
    // add the subplots...
    ValueAxis domainAxis = new DateAxis("");
    plot.setDomainAxis(domainAxis);
    plot.add(subplot1, 1);
    plot.add(subplot2, 1);
    plot.add(subplot3, 1);
    plot.setOrientation(PlotOrientation.VERTICAL);

    // return a new chart containing the overlaid plot...
    return new JFreeChart("Name",JFreeChart.DEFAULT_TITLE_FONT, plot, false);
}

/**
 * Creates a sample dataset.
 *
 * @return Dataset1.
 */
private JDBCXYDataset createDataset1() {
    [...]
 }
//Same for Dataset2 and Dataset3

public static void main(final String[] args) {
  [...]
}}

So is there a simple way of converting the line chart that I get to ScatterPlot chart ?


回答1:


The simplest way is to specify StandardXYItemRenderer.SHAPES when instantiating the relevant StandardXYItemRenderer. The following changes to this example produce the result below:

private static void init() {
    XYItemRenderer renderer = new StandardXYItemRenderer(SHAPES);
    …
}

Note that StandardXYItemRenderer "has been retained for historical reasons and, in general, you should use the XYLineAndShapeRenderer class instead." Comparable constructor parameters and mutators are cited here.

With either renderer, you can alter the series shapes and colors using a custom DrawingSupplier, as shown here.



来源:https://stackoverflow.com/questions/64502868/how-to-create-multiple-scatterplot-chart-using-createcombinedchart-on-jfreecha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!