如何在JFreeChart上使用createCombinedChart()创建多个ScatterPlot图表? [英] How to create multiple ScatterPlot chart using createCombinedChart() on JFreeChart?

查看:64
本文介绍了如何在JFreeChart上使用createCombinedChart()创建多个ScatterPlot图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JFreeChart上创建多个ScatterPlot图表,为此,我使用了函数createCombinedChart().在我当前的代码中,我得到了多个图表,就像可以找到的CombinedXYPlotDemo1一样,但是它们是折线图,我希望将它们作为ScatterPlot.

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) {
  [...]
}}

那么有一种简单的方法可以将我得到的折线图转换为ScatterPlot图表吗?

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

推荐答案

最简单的方法是在实例化相关的StandardXYItemRenderer时指定StandardXYItemRenderer.SHAPES.此示例的以下更改产生以下结果:

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

请注意,StandardXYItemRenderer是出于历史原因而保留的,通常,应改为使用XYLineAndShapeRenderer类." 此处.

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.

使用任一渲染器,您都可以使用自定义的DrawingSupplier更改系列形状和颜色,如

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

这篇关于如何在JFreeChart上使用createCombinedChart()创建多个ScatterPlot图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆