- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在向 XYSeries 添加点时遇到问题。我有两个类(class)。一个是Sample
(它有一个main
方法),另一个是JfreeChart
(它有JfreeChart
代码) .在我的 Sample
类中,我有一个二维数组 sample[row][2]
最初有 10 行,然后我需要调用 JfreeChart
类并将它们添加到 XYSeries 并显示散点图。我设法做到了这一点,但下次我调用 Jfreechart
类时,我的数组有 25 行。
我需要将这些值添加到 XYSeries 并将它们绘制在散点图上,该散点图应该以不同的颜色显示较早的 10 行值,现在以不同的颜色显示 25 行的值……这样继续下去。任何人都可以提供一些建议或示例吗?
class Sample {
public static void main(String args[]) {
System.out.print("(X,Y) Paired Values");
double[][] sample = new double[row][2];
for (int g = 0; g < sampe.length; g++) {
for (int h = 0; h < 2; h++) {
System.out.print("" + sample[g][h] + ",");
}
}
JfreeChart sample = new JfreeChart("Demo", sample);
}
static XYDataset samplexydataset2(double[][] sample) {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("DataSet");
for (int x = 0; x < sample.length; x++) {
series.add(sample[x][0], sample[x][1]);
}
xySeriesCollection.addSeries(series);
return xySeriesCollection;
}
}
1) 当我调用“第一次”JfreeChart 类时,我将在我的示例数组中包含这些对
(0.78,0.80)(0.21,0.19)(0.181,0.187)
2) 当我“第二次”调用 JfreeChart 类时,我的示例数组中将有不同的值(0.20,0.19)(0.8,0.79)(0.41,0.45)(0.77,0.79)(0.54,0.65)
这有几次(10 次)所以我需要将它添加到“XYSeries”和“XYSeriesCollection”,并在我第二次调用 JFreeChart 类时显示“第一次”值和“第二次”值
最佳答案
您可以向 XYSeries
添加新值使用可用的 add()
方法之一,如 example 所示.如果你得到不定行,你需要发布一个 sscce .
附录:更仔细地观察(最近更新)genesis在您的示例中,有些困惑是可以理解的:根本不需要数组。下面的示例包含一个按钮,用于将新样本添加到第二个系列。
Can I change the Color of Points when I click the "Add" Button?
每个新系列都是一种新颜色,如图example .要更改个别颜色,推荐的方法是重写渲染器的 getItemPaint()
方法,如图所示 here .
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
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;
/**
* @see https://stackoverflow.com/questions/7205742
* @see https://stackoverflow.com/questions/7208657
* @see https://stackoverflow.com/questions/7071057
*/
public class ScatterAdd extends JFrame {
private static final int N = 8;
private static final String title = "Scatter Add Demo";
private static final Random rand = new Random();
private XYSeries added = new XYSeries("Added");
public ScatterAdd(String s) {
super(s);
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
JPanel control = new JPanel();
control.add(new JButton(new AbstractAction("Add") {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < N; i++) {
added.add(rand.nextGaussian(), rand.nextGaussian());
}
}
}));
this.add(control, BorderLayout.SOUTH);
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
title, "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setVerticalTickLabels(true);
return new ChartPanel(jfreechart);
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
XYSeries series = new XYSeries("Random");
for (int i = 0; i < N * N; i++) {
double x = rand.nextGaussian();
double y = rand.nextGaussian();
series.add(x, y);
}
xySeriesCollection.addSeries(series);
xySeriesCollection.addSeries(added);
return xySeriesCollection;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ScatterAdd demo = new ScatterAdd(title);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}
关于java - 使用 JfreeChart 向 XYSeries 动态添加点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7205742/
我正在尝试创建一个 XyDataSeries对于我的 Datatable 的每一列以编程方式用于图表 (SciChart)。我需要这样做是因为事先不知道列的数量和名称。 对于任何给定的系列,x 值始终
我正在使用 jfreechart 并尝试制作一个运行良好的 XYLineChart。 我的问题是,y 标签是 double 值,我需要字符串。 我的代码: DefaultXYDataset resul
XYSeries add 方法只接受 add(double, double)。为什么它不能接受类似于 TimeSeries 的 add(java.util Date, double)。 我想绘制 X
谁能帮我知道如何在 JFreeChart 的 x-y 图中设置条形宽度。非常感谢。 final XYSeries series = new XYSeries("Analysis");
我知道我可以在创建图表时设置标题: XYSeries series = new XYSeries("Random Data"); 但是如果在我的代码中我想在运行时将标题 “Random Data”
我正在尝试遍历 Qt 5.5 QtCharts 中的点 LineSeries 类型,继承自 XYSeries . 我尝试了一个简单的 for -loop,但我想不出一种方法来确定系列中的点数: for
有没有办法隐藏 JFreeChart XYSeries yaxis?yaxis在逻辑分析仪显示上没有意义。 最佳答案 您可以使用 setVisible() ,如下图。 XYPlot plot = (X
“我在将双变量“gdataset”值添加到“series.add()”时遇到问题。请就此提供任何帮助 private static XYDataset samplexydataset2() {
series.getData().add(new XYChart.Data(x ,function(x)); 我尝试获得函数(x)的最大值;我知道我可以使用“series.getData().sort
假设我们需要在单个 XYSeriesCollection 中显示多个 XYSeries。我的问题是,每次添加 XYSeries 时,JFreeChart 都会更新图表,从而减慢显示多个 XYSerie
我在向 XYSeries 添加点时遇到问题。我有两个类(class)。一个是Sample(它有一个main方法),另一个是JfreeChart(它有JfreeChart代码) .在我的 Sample
嗨,我正在尝试实现 aChartEngine 库,当我在代码中添加值时(如本教程 by JavaCodeGeeks ),它工作得很好,但如果我使用一个函数,应用程序崩溃并给出 NullPointerE
我对 PySide2 和 QML 有点陌生,我真的需要一种方法来一次替换 XYSeries 中的所有点。由于 QML 项没有执行此操作的函数,因此我认为必须创建一个自定义类(继承自 QtCharts.
我正在编写一段代码,其中必须通过重复调用 aSyncTask 来不断更新图形。但为确保图表不会过于压缩,我计划移除/删除最旧的条目超过某个点。目前我的代码如下: int counter = 0; pr
我是一名优秀的程序员,十分优秀!