gpt4 book ai didi

java - 将 DefaultCategoryDataset 转换为 CategoryDataset

转载 作者:行者123 更新时间:2023-12-02 03:47:30 26 4
gpt4 key购买 nike

我正在尝试创建一个基本的条形图,当我编译它时,它会出现类似的错误

Exception in thread "main" java.lang.ClassCastException: org.jfree.data.category.DefaultCategoryDataset cannot be cast to org.jfree.data.CategoryDataset

这是我的代码

 package barchart;

import java.awt.Color;
import java.awt.Dimension;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;


public class BarChartDemo extends
public BarChartDemo(final String title) {

super(title);

final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);

}
public static void main(String[] args) {
BarChartDemo br = new BarChartDemo("title");
br.createDataset();
}


private CategoryDataset createDataset() {

// row keys...
final String series1 = "First";
final String series2 = "Second";
final String series3 = "Third";

// column keys...
final String category1 = "Category 1";
final String category2 = "Category 2";
final String category3 = "Category 3";
final String category4 = "Category 4";
final String category5 = "Category 5";

// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);
dataset.addValue(5.0, series1, category4);
dataset.addValue(5.0, series1, category5);

dataset.addValue(5.0, series2, category1);
dataset.addValue(7.0, series2, category2);
dataset.addValue(6.0, series2, category3);
dataset.addValue(8.0, series2, category4);
dataset.addValue(4.0, series2, category5);

dataset.addValue(4.0, series3, category1);
dataset.addValue(3.0, series3, category2);
dataset.addValue(2.0, series3, category3);
dataset.addValue(3.0, series3, category4);
dataset.addValue(6.0, series3, category5);

return dataset;

}


private JFreeChart createChart(final CategoryDataset dataset) {

// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo", // chart title
"Category", // domain axis label
"Value", (org.jfree.data.CategoryDataset) dataset,
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);

chart.setBackgroundPaint(Color.white);
return chart;

}
}

为什么我们不能将 DefaultCategoryDataset 转换为 CategoryDataset?我可以纠正这个问题吗?

最佳答案

CategoryDataset 的完全限定名称是org.jfree.data.category.CategoryDataset,但在这种情况下强制转换是多余的。

image

代码,经测试:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

public class BarChartDemo extends ApplicationFrame {

public BarChartDemo(final String title) {
super(title);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart) {

@Override
public Dimension getPreferredSize() {
return new Dimension(500, 270);
}
};
setContentPane(chartPanel);
pack();
setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
BarChartDemo br = new BarChartDemo("title");
}
});
}

private CategoryDataset createDataset() {
// row keys...
final String series1 = "First";
final String series2 = "Second";
final String series3 = "Third";

// column keys...
final String category1 = "Category 1";
final String category2 = "Category 2";
final String category3 = "Category 3";
final String category4 = "Category 4";
final String category5 = "Category 5";

// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(1.0, series1, category1);
dataset.addValue(4.0, series1, category2);
dataset.addValue(3.0, series1, category3);
dataset.addValue(5.0, series1, category4);
dataset.addValue(5.0, series1, category5);

dataset.addValue(5.0, series2, category1);
dataset.addValue(7.0, series2, category2);
dataset.addValue(6.0, series2, category3);
dataset.addValue(8.0, series2, category4);
dataset.addValue(4.0, series2, category5);

dataset.addValue(4.0, series3, category1);
dataset.addValue(3.0, series3, category2);
dataset.addValue(2.0, series3, category3);
dataset.addValue(3.0, series3, category4);
dataset.addValue(6.0, series3, category5);

return dataset;
}

private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo", // chart title
"Category", // domain axis label
"Value", dataset,
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
chart.setBackgroundPaint(Color.white);
return chart;
}
}

关于java - 将 DefaultCategoryDataset 转换为 CategoryDataset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36171659/

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