【问题标题】:How to create multiple ScatterPlot chart using createCombinedChart() on JFreeChart?如何在 JFreeChart 上使用 createCombinedChart() 创建多个 ScatterPlot 图表?
【发布时间】:2020-10-23 15:22:28
【问题描述】:

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

我找到了这个question,但尝试后我不知道如何让它工作(也许我只是不明白如何使用它)

这是我的代码示例(它与演示代码非常相似 + 我从数据库中检索并添加了时间轴)。

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 图表?

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

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

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

    请注意,StandardXYItemRenderer“由于历史原因已被保留,一般情况下,您应该改用 XYLineAndShapeRenderer 类。”可比较的构造函数参数和修改器被引用here

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

    【讨论】:

    • 非常感谢,我对你的例子理解得更好了 :) 这有点离题但是有没有办法只用点形状而不是正方形,点然后三角形?
    • 我会使用自定义 DrawingSupplier,但覆盖渲染器的 getSeriesShape() 可能更简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    相关资源
    最近更新 更多