gpt4 book ai didi

java - Jfreechart Boxplot DefaultBoxAndWhiskerCategoryDataset 初始化

转载 作者:行者123 更新时间:2023-11-30 03:01:39 27 4
gpt4 key购买 nike

我想用来自用户的一些数据绘制一个简单的箱线图(箱线图和须线图)(1 个箱线图不多),但我在 jfreechart 中的 DefaultBoxAndWhiskerCategoryDataset 变量上遇到了问题。似乎我输入的任何数据都会消失。绘图始终是三角形而不是箱线图

package boxplot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class Boxplot extends ApplicationFrame{

@SuppressWarnings("deprecation")
public Boxplot(String title) {
super(title);

final BoxAndWhiskerCategoryDataset dataset = createDataset();

final CategoryAxis xAxis = new CategoryAxis("");
final NumberAxis yAxis = new NumberAxis("Value");
yAxis.setAutoRangeIncludesZero(false);
final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
renderer.setFillBox(false);
renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

final JFreeChart chart = new JFreeChart(
"Box-and-Whisker Demo",
plot
);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(450, 270));
setContentPane(chartPanel);

}

public static void main(String[] args) {

final Boxplot plot = new Boxplot("");
plot.pack();
RefineryUtilities.centerFrameOnScreen(plot);
plot.setVisible(true);
}

private static DefaultBoxAndWhiskerCategoryDataset createDataset() {
System.out.print("Input the data (use space after every input)");

double[] inputData = getInputData();

ArrayList<Double> inputDataList = new ArrayList<Double>();
for (int i=0;i<100;i++)
inputDataList.add(i, inputData[i]);

final DefaultBoxAndWhiskerCategoryDataset dataset
= new DefaultBoxAndWhiskerCategoryDataset();
dataset.add(inputDataList, "1", "2");

return dataset;
}

private static double[] getInputData() {
Scanner scanner = new Scanner(System.in);

double[] data = new double[100];
Arrays.fill(data, -1);

int index =0;
do
{
double temp = scanner.nextDouble();
if (temp==-1)
break;
data[index++]= temp;
}while (scanner.hasNext());
scanner.close();

return data;
}

}

最佳答案

目前尚不清楚您的示例在哪里失败。您对相关工厂方法的概括, ChartFactory.createBoxAndWhiskerChart() 显示here ,显示正确。从一个更简单的工作示例开始可能会更容易。一些注意事项:

  • 为了方便起见,我构建了一个 Scanner使用String ,而不是 System.in .

  • 我还简化了 getInputData()返回List<Number> ,适用于数据集的 add()方法。

  • Swing GUI 对象应该event dispatch thread 上构建和操作。 .

  • 不要使用setPreferredSize()当你真的想覆盖 getPreferredSize() 时。正如所讨论的here .

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;

/**
* @see https://stackoverflow.com/a/35814571/230513
*/
public class BoxPlot {

private static final String ROW_KEY = "City";

private void display() {
JFrame f = new JFrame("BoxPlot");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultBoxAndWhiskerCategoryDataset data = new DefaultBoxAndWhiskerCategoryDataset();
data.add(getInputData(), ROW_KEY, "Coruscant");
JFreeChart chart = ChartFactory.createBoxAndWhiskerChart(
"Box and Whisker Chart", ROW_KEY, "Temperature", data, false);
f.add(new ChartPanel(chart) {

@Override
public Dimension getPreferredSize() {
return new Dimension(320, 480);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private List<Number> getInputData() {
Scanner s = new Scanner("30 36 46 55 65 76 81 80 71 59 44 34");
List<Number> list = new ArrayList<>();
do {
list.add(s.nextDouble());
} while (s.hasNext());
return list;
}

public static void main(String[] args) {
EventQueue.invokeLater(new BoxPlot()::display);
}
}

关于java - Jfreechart Boxplot DefaultBoxAndWhiskerCategoryDataset 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803022/

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