如何在 JFreeChart 上使用 createCombinedChart() 创建多个 ScatterPlot 图表?
How to create multiple ScatterPlot chart using createCombinedChart() on JFreeChart?
我想在 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) {
[...]
}}
那么有没有一种简单的方法可以将我得到的折线图转换为散点图?
最简单的方法是在实例化相关StandardXYItemRenderer
时指定StandardXYItemRenderer.SHAPES
。对此 example 的以下更改产生以下结果:
private static void init() {
XYItemRenderer renderer = new StandardXYItemRenderer(SHAPES);
…
}
请注意,StandardXYItemRenderer
“由于历史原因已被保留,一般来说,您应该使用 XYLineAndShapeRenderer
class。”引用了可比较的构造函数参数和修改器 here.
使用任一渲染器,您可以使用自定义 DrawingSupplier
改变系列形状和颜色,如图 here。
我想在 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) {
[...]
}}
那么有没有一种简单的方法可以将我得到的折线图转换为散点图?
最简单的方法是在实例化相关StandardXYItemRenderer
时指定StandardXYItemRenderer.SHAPES
。对此 example 的以下更改产生以下结果:
private static void init() {
XYItemRenderer renderer = new StandardXYItemRenderer(SHAPES);
…
}
请注意,StandardXYItemRenderer
“由于历史原因已被保留,一般来说,您应该使用 XYLineAndShapeRenderer
class。”引用了可比较的构造函数参数和修改器 here.
使用任一渲染器,您可以使用自定义 DrawingSupplier
改变系列形状和颜色,如图 here。