[java]实现柱状图 饼状图
一、前提:下载增加jar包 :jfreechart-1.0.13.jar ,jcommon-1.0.14.jar
二、柱状图:
2.1 效果
2.2 代码
[java]
public class HistogramJPanel {
private String title; // 图标题
private String verticallyTitle;// 纵向坐标显示名称
private String horizontalTitle;// 横向坐标显示名称
private List<String> elem = new ArrayList<String>();// 内容初始值 (每个类型显示文字)
private List<Float> value = new ArrayList<Float>();// 内容初始值(每个类型显示精度)
public HistogramJPanel(String title, String horizontalTitle,
String verticallyTitle, List<String> elem, List<Float> value) {
this.title = title;
this.horizontalTitle = horizontalTitle;
this.verticallyTitle = verticallyTitle;
this.elem = elem;
this.value = value;
}
public HistogramJPanel() {
}
private class PanelByHistogram extends DemoPanel implements ChangeListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JScrollBar scroller;
private SlidingCategoryDataset dataset;// 一种滑动类型(图形)数据设置
private CategoryDataset createDataset() throws Exception {
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();// 缺省类型数据设置
if (elem.size() == 0) {
JOptionPane.showMessageDialog(null, "柱状图显示数据不符合要求请重新设置", "",
JOptionPane.INFORMATION_MESSAGE);
throw new Exception("柱状图显示数据不符合要求请重新设置(PanelByHistogram 类 61行)");
}
if (elem.size() != value.size()) {
JOptionPane.showMessageDialog(null, "柱状图显示数据不符合要求请重新设置", "",
JOptionPane.INFORMATION_MESSAGE);
throw new Exception("柱状图显示数据不符合要求请重新设置(PanelByHistogram 类 67行)");
}
for (int i = 0; i < elem.size(); i++) {
defaultcategorydataset.addValue(value.get(i),"PanelByHistogram", elem.get(i));
}
return defaultcategorydataset;
}
private JFreeChart createChart(CategoryDataset categorydataset)
throws Exception {
if ((verticallyTitle == null || verticallyTitle.trim().length() == 0)) {
JOptionPane.showMessageDialog(null, "纵向坐标显示中文?", "",JOptionPane.INFORMATION_MESSAGE);
throw new Exception("柱状图显示数据不符合要求请重新设置(PanelByHistogram 类 81行)");
}
if ((horizontalTitle == null || horizontalTitle.trim().length() == 0)) {
JOptionPane.showMessageDialog(null, "横向坐标显示中文?", "", JOptionPane.INFORMATION_MESSAGE);
throw new Exception("柱状图显示数据不符合要求请重新设置(PanelByHistogram 类 85行)");
}
JFreeChart jfreechart = ChartFactory.createBarChart(title,
horizontalTitle, verticallyTitle, categorydataset,
PlotOrientation.VERTICAL, false, true, false);
TextTitle t = jfreechart.getTitle();
t.setFont(new Font("宋体", Font.BOLD, 30));// 标题文字
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
// categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
//categoryplot.setRangePannable(true);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();// X轴
categoryaxis.setMaximumCategoryLabelWidthRatio(0.8F);
categoryaxis.setLowerMargin(0.02D);
categor
补充:软件开发 , Java ,