【java】实现柱状图 饼状图 博客分类: java知识总结 java
一、前提: 下载增加jar包 : jfreechart-1.0.13.jar , jcommon-1.0.14.jar
二、柱状图:
2.1 效果
2.2 代码
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); categoryaxis.setUpperMargin(0.02D); categoryaxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// x轴标题文字 categoryaxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// x轴坐标上文字 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();// Y轴 numberaxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// x轴标题文字 numberaxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// x轴坐标上文字 numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //numberaxis.setRange(0.0D, 100D); BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer(); barrenderer.setDrawBarOutline(false); barrenderer.setItemLabelAnchorOffset(11D); barrenderer.setBaseItemLabelsVisible(true); barrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); //barrenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator("{0}, {1}) = {2} per 100,000", new DecimalFormat("0"))); GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F,Color.blue, 0.0F, 0.0F, new Color(0, 0, 64)); barrenderer.setSeriesPaint(0, gradientpaint); return jfreechart; } public void stateChanged(ChangeEvent changeevent) { dataset.setFirstCategoryIndex(scroller.getValue()); } private PanelByHistogram() throws Exception { super(new BorderLayout()); dataset = new SlidingCategoryDataset(createDataset(), 0, elem.size()); JFreeChart jfreechart = createChart(dataset); addChart(jfreechart); ChartPanel chartpanel = new ChartPanel(jfreechart); //// chartpanel.setPreferredSize(new Dimension(800, 600)); // scroller = new JScrollBar(0, 0, elem.size(), 0, elem.size()); add(chartpanel); // scroller.getModel().addChangeListener(this); // JPanel jpanel = new JPanel(new BorderLayout()); // jpanel.add(scroller); // jpanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); // jpanel.setBackground(Color.white); // add(jpanel, "South"); } } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getVerticallyTitle() { return verticallyTitle; } public void setVerticallyTitle(String verticallyTitle) { this.verticallyTitle = verticallyTitle; } public String getHorizontalTitle() { return horizontalTitle; } public void setHorizontalTitle(String horizontalTitle) { this.horizontalTitle = horizontalTitle; } public List<String> getElem() { return elem; } public void setElem(List<String> elem) { this.elem = elem; } public List<Float> getValue() { return value; } public void setValue(List<Float> value) { this.value = value; } public JPanel createHistogramPanel() throws Exception { return new PanelByHistogram(); } }
2.3 调用方式
HistogramJPanel histogramJPanel = new HistogramJPanel();// 柱状图的panel
histogramJPanel.setTitle("柱状图");
histogramJPanel.setHorizontalTitle("序号");
histogramJPanel.setVerticallyTitle("数据量");
histogramJPanel.setElem(elem);
histogramJPanel.setValue(value);
view.jScrollPane.setViewportView(histogramJPanel.createHistogramPanel());
view.jScrollPane
.updateUI();
三、饼状图
3.1 效果
3.2 代码
import java.awt.BorderLayout; import java.awt.Font; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; public class PieChartJPanel { private String title; private List<String> elem = new ArrayList<String>();// 内容初始值 (每个类型显示文字) private List<Float> value = new ArrayList<Float>();// 内容初始值(每个类型显示精度) public PieChartJPanel(){ } public PieChartJPanel(String title,List<String> elem,List<Float> value){ this.title=title; this.elem=elem; this.value=value; } private class PieChart extends JPanel{ /** * */ private static final long serialVersionUID = 1L; private PieDataset createDataset()throws Exception{ 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行)"); } DefaultPieDataset defaultpiedataset = new DefaultPieDataset(); for (int i = 0; i < elem.size(); i++) { defaultpiedataset.setValue(elem.get(i), value.get(i)); } return defaultpiedataset; } private JFreeChart createChart(PieDataset piedataset){ JFreeChart jfreechart = ChartFactory.createPieChart(title, piedataset, true, true, false); TextTitle t = jfreechart.getTitle(); t.setFont(new Font("宋体", Font.BOLD, 30));// 标题文字 // TextTitle texttitle = jfreechart.getTitle(); // texttitle.setToolTipText("A title tooltip!"); PiePlot pieplot = (PiePlot)jfreechart.getPlot(); pieplot.setLabelFont(new Font("宋体", 0, 12)); pieplot.setNoDataMessage("No data available"); pieplot.setCircular(false); pieplot.setLabelGap(0.02D); jfreechart.getLegend().setItemFont(new Font("宋体", 0,10)); return jfreechart; } private PieChart()throws Exception{ super(new BorderLayout()); JFreeChart jfreechart=createChart(createDataset()); ChartPanel chartpanel = new ChartPanel(jfreechart); // chartpanel.setPreferredSize(new Dimension(600, 400)); add(chartpanel); } } public JPanel createPieChartPanel() throws Exception{ return new PieChart(); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<String> getElem() { return elem; } public void setElem(List<String> elem) { this.elem = elem; } public List<Float> getValue() { return value; } public void setValue(List<Float> value) { this.value = value; } }
3.3 调用方式
PieChartJPanel PieChart = new PieChartJPanel();// 饼状图panel
PieChart.setTitle("饼状图");
PieChart.setElem(elem);
PieChart.setValue(value);
view.jScrollPane.setViewportView(PieChart.createPieChartPanel());
view.jScrollPane
.updateUI();
上一篇: SQL里各种联合查询总结 博客分类: Oracle sqlOracleMongoDBSQL Server
下一篇: Howto: 如何让tomcat bundle liferay使用不同的数据库 博客分类: 定置 TomcatOracle脚本HSQLDBJDBC
推荐阅读
-
算法--排序(冒泡,选择,插入,快速) 博客分类: java知识总结 算法java
-
【java】实现日历对话框 博客分类: java知识总结 javaCalendar
-
【java】反射可执行的实例 博客分类: java知识总结 java
-
【java】实现柱状图 饼状图 博客分类: java知识总结 java
-
【java】加密方式 博客分类: java知识总结 java算法
-
java实现计算算术表达式的值(后缀表达式方式) 博客分类: JAVA零散小知识 算术表达式java计算后缀表达式
-
java利用JFreeChart实现各种数据统计图 博客分类: Java jfreechartjava
-
jfreechar 柱状图 博客分类: java语言 jfreechart柱状图
-
Java桌面程序打包成exe可执行文件 博客分类: 【1】、Java基础知识总结 java
-
用JNA开发身份证阅读程序 博客分类: 【2】、java小工具【1】、Java基础知识总结 jna