本文介绍了将DefaultCategoryDataset转换为CategoryDataset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个基本的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(){
//行键...
final String series1 =First;
final String series2 =Second;
final String series3 =Third;
//列键...
final String category1 =类别1;
final String category2 =Category 2;
final String category3 =Category 3;
final String category4 =Category 4;
final String category5 =Category 5;
//创建数据集...
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);
返回数据集;
}
private JFreeChart createChart(final CategoryDataset dataset){
//创建图表...
最后JFreeChart图表= ChartFactory.createBarChart(
条形图演示,//图表标题
类别,//域轴标签
Value,(org.jfree.data。 Category dataset)dataset,
PlotOrientation.VERTICAL,// orientation
true,// include legend
true,// tooltips?
false // URLs?
);
chart.setBackgroundPaint(Color.white);
return chart;
}
}
为什么我们不能强制转换DefaultCategoryDataset到CategoryDataset,谁能解决这个问题?
解决方案
测试代码:
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;
//列键...
final String category1 =类别1;
final String category2 =Category 2;
final String category3 =Category 3;
final String category4 =Category 4;
final String category5 =Category 5;
//创建数据集...
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);
返回数据集;
}
private JFreeChart createChart(final CategoryDataset dataset){
//创建图表...
final JFreeChart chart = ChartFactory.createBarChart(
Bar Chart Demo,//图表标题
Category,//域轴标签
Value,数据集,
PlotOrientation.VERTICAL,// orientation
true, // include legend
true,// tooltips?
false // URLs?
);
chart.setBackgroundPaint(Color.white);
return chart;
}
}
I'm trying to create a basic barchart and when I compile it gives an error like
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;
}
}
这篇关于将DefaultCategoryDataset转换为CategoryDataset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!