欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【java】实现柱状图 饼状图 博客分类: java知识总结 java 

程序员文章站 2024-03-22 21:26:28
...

一、前提: 下载增加jar包 : jfreechart-1.0.13.jar , jcommon-1.0.14.jar

二、柱状图:

2.1 效果

【java】实现柱状图 饼状图
            
    
    博客分类: java知识总结 java 

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 效果

【java】实现柱状图 饼状图
            
    
    博客分类: java知识总结 java 

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();

 

 

 

相关标签: java