6.6 示例代码解读
6.6.1 体会
与饼图的数据集不同之处在于:
饼图数据集是key/value二维数据(PieDataset)。而直方条形图需要三维数据(CategoryDataset)。
6.6.2 类BarChartDemo1.java
功能:
一个简单的直方条形图。使用GradientPaint实例对象为每一个系列修改renderer
效果:
代码:
public class BarChartDemo1 extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public BarChartDemo1(String string) {
super(string);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}
private static CategoryDataset createDataset() {
String string = "First";
String string_0_ = "Second";
String string_1_ = "Third";
String string_2_ = "Category 1";
String string_3_ = "Category 2";
String string_4_ = "Category 3";
String string_5_ = "Category 4";
String string_6_ = "Category 5";
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0, string, string_2_);
defaultcategorydataset.addValue(4.0, string, string_3_);
defaultcategorydataset.addValue(3.0, string, string_4_);
defaultcategorydataset.addValue(5.0, string, string_5_);
defaultcategorydataset.addValue(5.0, string, string_6_);
defaultcategorydataset.addValue(5.0, string_0_, string_2_);
defaultcategorydataset.addValue(7.0, string_0_, string_3_);
defaultcategorydataset.addValue(6.0, string_0_, string_4_);
defaultcategorydataset.addValue(8.0, string_0_, string_5_);
defaultcategorydataset.addValue(4.0, string_0_, string_6_);
defaultcategorydataset.addValue(4.0, string_1_, string_2_);
defaultcategorydataset.addValue(3.0, string_1_, string_3_);
defaultcategorydataset.addValue(2.0, string_1_, string_4_);
defaultcategorydataset.addValue(3.0, string_1_, string_5_);
defaultcategorydataset.addValue(6.0, string_1_, string_6_);
return defaultcategorydataset;
}
private static JFreeChart createChart(CategoryDataset categorydataset) {
JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo 1",
"Category", "Value", categorydataset, PlotOrientation.VERTICAL,
true, true, false);
jfreechart.setBackgroundPaint(Color.WHITE);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.white);
//刻度轴刻度设置
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//renderer设置
BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
barrenderer.setDrawBarOutline(false);//设置外廓线不可见
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint gradientpaint_7_ = new GradientPaint(0.0F, 0.0F,
Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint gradientpaint_8_ = new GradientPaint(0.0F, 0.0F,
Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
barrenderer.setSeriesPaint(0, gradientpaint);
barrenderer.setSeriesPaint(1, gradientpaint_7_);
barrenderer.setSeriesPaint(2, gradientpaint_8_);
//设置种类标签旋转的角度,逆时针旋转
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(Math.PI / 6));
return jfreechart;
}
public static JPanel createDemoPanel() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String[] strings) {
BarChartDemo1 barchartdemo1 = new BarChartDemo1("Bar Chart Demo");
barchartdemo1.pack();
RefineryUtilities.centerFrameOnScreen(barchartdemo1);
barchartdemo1.setVisible(true);
}
}
程序代码说明:
Main()
方法执行直方条形图。编写方法与饼图一样。BarChartDemo1
构造函数中创了一个JPanel,并设置大小。createDemoPanel()
方法创建了一个JPanel,并且在该panel上创建了直方条形图。createDataset()
方法创建了数据集。类型为CategoryDataset。注意数据集为三维数据。与饼图不同。- 使用
ChartFactory.createBarChart()
方法创建直方条形图 jfreechart.setBackgroundPaint(Color.WHITE)
:设置图表的背景颜色。categoryplot.setBackgroundPaint(Color.lightGray)
:设置直方条形图的背景颜色。setDomainGridlinePaint(Color.whites)
:设置垂直格线的颜色。默认不可见。setRangeGridlinePaint(Color.white)
:设置水平格线的颜色。默认可见。setStandardTickUnits(NumberAxis.createIntegerTickUnits())
:设置数据轴的刻度递进范围。GradientPaint
类用来设置渐变色。categoryaxis.setCategoryLabelPositions()
:设置标签文字旋转的角度。
6.6.3 类BarChartDemo2.java
功能:
显示水平的直方条形图。
效果:
代码:
private static CategoryDataset createDataset() {
double[][] ds = { { 1.0, 43.0, 35.0, 58.0, 54.0, 77.0, 71.0, 89.0 },
{ 54.0, 75.0, 63.0, 83.0, 43.0, 46.0, 27.0, 13.0 },
{ 41.0, 33.0, 22.0, 34.0, 62.0, 32.0, 42.0, 34.0 } };
return DatasetUtilities.createCategoryDataset("Series ", "Factor ", ds);
}
private static JFreeChart createChart(CategoryDataset categorydataset) {
JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo 2",
"Category", "Score (%)", categorydataset,
PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setRange(0.0, 100.0);
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
barrenderer.setDrawBarOutline(false);
barrenderer
.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator(
"Tooltip: {0}"));
return jfreechart;
}
程序代码说明:
- 数据集的创建另一种方式,使用二维数组。