gpt4 book ai didi

java - 更改散点图中点的形状

转载 作者:搜寻专家 更新时间:2023-10-30 21:41:48 25 4
gpt4 key购买 nike

我有数千个点要绘制在 JFreeChart 散点图上。现在的问题是我的程序正在用“正方形”绘制点,但我需要一些帮助来了解如何将点的 Shape 从“正方形”更改为“点/圆”。任何帮助将不胜感激。

//*我正在使用 ShapeUtilities,但当我使用 XYItemRenderer/XYDotRenderer 时,它不会将点的形状更改为“DaigonalCross”——如果代码中有任何错误,请进行任何更正。 *///

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.util.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.ShapeUtilities;

public class SPlotfinal extends ApplicationFrame {

public SPlotfinal(String s) {
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}

public static JPanel createDemoPanel() {

JFreeChart jfreechart = ChartFactory.createScatterPlot("Scatter Plot Demo",
"X", "Y", samplexydataset2(), PlotOrientation.VERTICAL, true, true, false);
Shape cross = ShapeUtilities.createDiagonalCross(3, 1);

XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setBaseShape(cross);
renderer.setBasePaint(Color.red);
//changing the Renderer to XYDotRenderer
//xyPlot.setRenderer(new XYDotRenderer());
XYDotRenderer xydotrenderer = new XYDotRenderer();
xyPlot.setRenderer(xydotrenderer);
xydotrenderer.setSeriesShape(0, cross);

xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);

return new ChartPanel(jfreechart);
}

private static XYDataset samplexydataset2() {
int cols = 20;
int rows = 20;
double[][] values = new double[cols][rows];

XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
Random rand = new Random();
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
double x = Math.round(rand.nextDouble() * 500);
double y = Math.round(rand.nextDouble() * 500);

series.add(x, y);
}
}
xySeriesCollection.addSeries(series);
return xySeriesCollection;
}

public static void main(String args[]) {
SPlotfinal scatterplotdemo4 = new SPlotfinal("Scatter Plot Demo 4");
scatterplotdemo4.pack();
RefineryUtilities.centerFrameOnScreen(scatterplotdemo4);
scatterplotdemo4.setVisible(true);
}
}

最佳答案

A ScatterRenderer继承getItemShape()来自 AbstractRenderer .您可以覆盖 getItemShape()提供您自己的形状。

附录:这种方法的一个优点是您可以为每个系列中的每个项目指定一个 Shape

附录:要使用 ShapeUtilities.createDiagonalCross(),请执行以下操作:

Shape cross = ShapeUtilities.createDiagonalCross(3, 1);
plot = (XYPlot) chart.getPlot();
renderer = plot.getRenderer();
renderer.setSeriesShape(0, cross);

附录:只需切换到 setSeriesShape()。此外,跳过 XYDotRendererMath.round()

Scatter Plot Demo

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.util.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.ShapeUtilities;

public class SPlotfinal extends ApplicationFrame {

public SPlotfinal(String s) {
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(640, 480));
add(jpanel);
}

public static JPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
"Scatter Plot Demo", "X", "Y", samplexydataset2(),
PlotOrientation.VERTICAL, true, true, false);
Shape cross = ShapeUtilities.createDiagonalCross(3, 1);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesShape(0, cross);
renderer.setSeriesPaint(0, Color.red);
return new ChartPanel(jfreechart);
}

private static XYDataset samplexydataset2() {
int cols = 20;
int rows = 20;
double[][] values = new double[cols][rows];
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
Random rand = new Random();
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
double x = rand.nextGaussian();
double y = rand.nextGaussian();
series.add(x, y);
}
}
xySeriesCollection.addSeries(series);
return xySeriesCollection;
}

public static void main(String args[]) {
SPlotfinal scatterplotdemo4 = new SPlotfinal("Scatter Plot Demo 4");
scatterplotdemo4.pack();
RefineryUtilities.centerFrameOnScreen(scatterplotdemo4);
scatterplotdemo4.setVisible(true);
}
}

关于java - 更改散点图中点的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6665354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com