将 DefaultCategoryDataset 转换为 CategoryDataset
我正在尝试创建一个基本的条形图,当我编译它时会出现类似的错误
<块引用>线程主"java.lang.ClassCastException 中的异常:org.jfree.data.category.DefaultCategoryDataset 不能转换为org.jfree.data.CategoryDataset
这是我的代码
包条形图;导入 java.awt.Color;导入 java.awt.Dimension;导入 org.jfree.chart.ChartFactory;导入 org.jfree.chart.ChartPanel;导入 org.jfree.chart.JFreeChart;导入 org.jfree.chart.plot.PlotOrientation;导入 org.jfree.data.category.CategoryDataset;导入 org.jfree.data.category.DefaultCategoryDataset;导入 org.jfree.ui.ApplicationFrame;公共类 BarChartDemo 扩展公共 BarChartDemo(最终字符串标题){超级(标题);最终 CategoryDataset 数据集 = createDataset();最终 JFreeChart 图表 = createChart(dataset);最终的 ChartPanel chartPanel = new ChartPanel(chart);chartPanel.setPreferredSize(新维度(500, 270));设置内容窗格(图表面板);}公共静态无效主要(字符串[]参数){BarChartDemo br = new BarChartDemo("title");br.createDataset();}私有类别数据集 createDataset() {//行键...最终字符串 series1 = 第一";最终字符串 series2 = "第二个";最终字符串 series3 = "第三";//列键...最终字符串 category1 = "类别 1";最终字符串 category2 = "类别 2";最终字符串 category3 = "类别 3";最终字符串 category4 = "类别 4";最终字符串 category5 = "Category 5";//创建数据集...最终 DefaultCategoryDataset 数据集 = 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);返回数据集;}私人 JFreeChart createChart(最终 CategoryDataset 数据集){//创建图表...最终 JFreeChart 图表 = ChartFactory.createBarChart("Bar Chart Demo",//图表标题"Category",//域轴标签值",(org.jfree.data.CategoryDataset)数据集,PlotOrientation.VERTICAL,//方向true,//包括图例true,//工具提示?假//网址?);chart.setBackgroundPaint(Color.white);返回图表;}}
为什么我们不能将 DefaultCategoryDataset 转换为 CategoryDataset,我可以纠正谁?
解决方案经过测试的代码:
导入java.awt.Color;导入 java.awt.Dimension;导入 java.awt.EventQueue;导入 org.jfree.chart.ChartFactory;导入 org.jfree.chart.ChartPanel;导入 org.jfree.chart.JFreeChart;导入 org.jfree.chart.plot.PlotOrientation;导入 org.jfree.data.category.CategoryDataset;导入 org.jfree.data.category.DefaultCategoryDataset;导入 org.jfree.ui.ApplicationFrame;公共类 BarChartDemo 扩展 ApplicationFrame {公共 BarChartDemo(最终字符串标题){超级(标题);最终 CategoryDataset 数据集 = createDataset();最终 JFreeChart 图表 = createChart(dataset);最终 ChartPanel chartPanel = new ChartPanel(chart) {@覆盖公共维度 getPreferredSize() {返回新维度(500, 270);}};设置内容窗格(图表面板);盒();设置可见(真);}公共静态无效主要(字符串[]参数){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){BarChartDemo br = new BarChartDemo("title");}});}私有类别数据集 createDataset() {//行键...最终字符串 series1 = 第一";最终字符串 series2 = "第二个";最终字符串 series3 = "第三";//列键...最终字符串 category1 = "类别 1";最终字符串 category2 = "类别 2";最终字符串 category3 = "类别 3";最终字符串 category4 = "类别 4";最终字符串 category5 = "Category 5";//创建数据集...最终 DefaultCategoryDataset 数据集 = 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);返回数据集;}私人 JFreeChart createChart(最终 CategoryDataset 数据集){//创建图表...最终 JFreeChart 图表 = ChartFactory.createBarChart("Bar Chart Demo",//图表标题"Category",//域轴标签价值",数据集,PlotOrientation.VERTICAL,//方向true,//包括图例true,//工具提示?假//网址?);chart.setBackgroundPaint(Color.white);返回图表;}}
I'm trying to create a basic barchart and when I compile it gives an error like
Exception in thread "main" java.lang.ClassCastException: org.jfree.data.category.DefaultCategoryDataset cannot be cast to org.jfree.data.CategoryDataset
here is my code
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;
}
}
why cann't we cast DefaultCategoryDataset to CategoryDataset and who can I correct this ?
解决方案The fully qualified name of CategoryDataset
is org.jfree.data.category.CategoryDataset
, but the cast is superfluous in this context.
Code, as tested:
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;
}
}
相关文章