一个趋势,柱状,饼状图结合的例子

import javax.swing.JApplet;
import javax.swing.JTabbedPane;

import org.jfree.data.CategoryDataset;
import org.jfree.data.DefaultCategoryDataset;
import org.jfree.data.DefaultPieDataset;
import org.jfree.data.PieDataset;
import org.jfree.data.XYDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardLegend;

import org.jfree.chart.labels.StandardPieItemLabelGenerator;

import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.BarRenderer;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.renderer.XYItemRenderer;
import org.jfree.ui.Spacer;
import org.jfree.util.Rotation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;

import java.awt.GradientPaint;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import webkiosk.util.*;

/*
 * Created on 2004-8-5
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SumStatusApplet extends JApplet {

 private static String[] keys;

 private static String[] series;

 private static String[] dkeys;
 
 private static String[] dseries;
 
 private static int[] values;

 private static int[] dvalues;
 
 private static Color[] colors;
 
 private static String title;
 private static String subject;
 private static String type;

 public void init() {

  colors = new Color[] { Color.red ,Color.green,Color.blue,Color.black,
    Color.magenta, Color.yellow};
        if (getParameter("title")!=null) title = getParameter("title");
        else title = "";
       
        if (getParameter("subject")!=null) subject = getParameter("subject");
        else subject = "";
       
        if (getParameter("xytype")!=null) type = getParameter("xytype");
        else type = "";
       
  if (getParameter("keys") != null)
   keys = ArrayUtil.stringToArray(getParameter("keys"), "|");
  if (getParameter("values") != null)
   values = ArrayUtil.intToArray(getParameter("values"), "|");
  if (getParameter("series") != null)
   series = ArrayUtil.stringToArray(getParameter("series"), "|");
  if (getParameter("dkeys") != null)
   dkeys = ArrayUtil.stringToArray(getParameter("dkeys"), "|");
  if (getParameter("dvalues") != null)
   dvalues = ArrayUtil.intToArray(getParameter("dvalues"), "|");
  if (getParameter("dseries") != null)
   dseries = ArrayUtil.stringToArray(getParameter("dseries"), "|");  
 }

 public void start() {

   JTabbedPane tabs = new JTabbedPane();
   XYDataset dataset2 = createXYDataset();
   JFreeChart chart2 = createXYChart(dataset2);
   ChartPanel chartPanel2 = new ChartPanel(chart2);
  tabs.add("趋势图",chartPanel2);
   CategoryDataset dataset = createBarDataset();
   JFreeChart chart = createBarChart(dataset);
   ChartPanel chartPanel = new ChartPanel(chart);
  tabs.add("柱状图", chartPanel);
   PieDataset dataset1 = createPieDataset();
   JFreeChart chart1 = createPieChart(dataset1);
   ChartPanel chartPanel1 = new ChartPanel(chart1);
  tabs.add("饼状图", chartPanel1);

  getContentPane().add(tabs);
 }

 private CategoryDataset createBarDataset() {

   DefaultCategoryDataset dataset = new DefaultCategoryDataset();

  for (int i = 0; i < series.length; i++) {

   dataset.addValue(format(values[i] / 100.0), series[i], keys[0]);

  }

  return dataset;

 }

 private PieDataset createPieDataset() {

   DefaultPieDataset result = new DefaultPieDataset();
  for (int j = 0; j < series.length; j++) {
   result.setValue(series[j], format(values[j] / 100.0));
  }
  return result;

 }

   private XYDataset createXYDataset() {

     TimeSeriesCollection dataset = new TimeSeriesCollection();
   
    if (dkeys[0].length()>11){
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date = null;
        for (int j = 0; j < dseries.length; j++){
      
            TimeSeries t1 = new TimeSeries(dseries[j],Hour.class);
         for (int i=0;i            
             try{
                         date = df2.parse(dkeys[i]);
             } catch(Exception e){
             }
      java.util.Calendar cal = new  java.util.GregorianCalendar();
      cal.setTime(date);
      Hour h = new Hour(date);
      t1.add(h,format(dvalues[i*dseries.length+j]/100.0));
         }
       dataset.addSeries(t1);  
     }   
    }else{
    for (int j = 0; j < dseries.length; j++){
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = null;
      TimeSeries t1 = new TimeSeries(dseries[j]);
        for (int i=0;i            try{
       date = df1.parse(dkeys[i]);
            } catch(Exception e){
              
            }
     Day day = new Day(date);
     t1.add(day,format(dvalues[i*dseries.length+j]/100.0));
        }
      dataset.addSeries(t1);  
    }
   }
  dataset.setDomainIsPointsInTime(true);
  return dataset;       
   }
   private JFreeChart createXYChart( XYDataset dataset){
     JFreeChart chart = ChartFactory.createTimeSeriesChart(
     title+"("+type+")",         // chart title
     subject,               // domain axis label
     "现金额(元)",                  // range axis label
     dataset,                  // data
     true,                     // include legend
     true,                     // tooltips?
     false                     // URLs?
    );
  chart.setBackgroundPaint(Color.white);
   StandardLegend sl = (StandardLegend) chart.getLegend();
  sl.setDisplaySeriesShapes(true);
  sl.setItemFont(new Font("/u5b8b/u4f53",Font.TRUETYPE_FONT,12));
   XYPlot plot = chart.getXYPlot();
  plot.setBackgroundPaint(Color.lightGray);
  plot.setDomainGridlinePaint(Color.white);
  plot.setRangeGridlinePaint(Color.white);
  plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
  plot.setDomainCrosshairVisible(true);
  plot.setRangeCrosshairVisible(true);
  // set the range axis to display integers only...
   NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  
  
   XYItemRenderer renderer = chart.getXYPlot().getRenderer();
        //  显示点上的数值
  //renderer.setBaseLabelGenerator(new StandardXYLabelGenerator());
  //renderer.setItemLabelsVisible(true);
  
     int size = dseries.length;
     int yu = size % colors.length;
     int loop = size / colors.length;
   
     for (int k = 0; k < loop; k++) {
   
      for (int i = 0; i < colors.length; i++) {
       GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, colors[i],
         0.0f, 0.0f, colors[i]);
       renderer.setSeriesPaint((k* colors.length + i), gp0);
      }
     }
   
     for (int i = 0; i < yu; i++) {
      GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, colors[i],
        0.0f, 0.0f, colors[i]);
      renderer.setSeriesPaint(loop*colors.length+i, gp1);
     }  

   StandardXYToolTipGenerator g = new StandardXYToolTipGenerator(
   StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
   new SimpleDateFormat("yyyy-MM-dd"), new DecimalFormat("0.00")
  );
  renderer.setItemLabelFont(new Font("/u5b8b/u4f53",Font.TRUETYPE_FONT,12));
  renderer.setToolTipGenerator(g);

  if (renderer instanceof StandardXYItemRenderer) {
    StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
   rr.setPlotShapes(true);
   rr.setShapesFilled(true);
   
   for (int k=0;k       renderer.setSeriesStroke(k, new BasicStroke(2.0f));
   }   
     }
  SimpleDateFormat sf = null;
  if (dkeys[0].length()>11) sf = new  SimpleDateFormat("HH:mm");
  else sf = new SimpleDateFormat("yyyy.MM.dd");
      DateAxis axis = (DateAxis) plot.getDomainAxis();
     axis.setDateFormatOverride(sf);
     axis.setLabelFont(new Font("/u5b8b/u4f53",Font.TRUETYPE_FONT,12));
    
  return chart;
          
   }  
    private JFreeChart createBarChart( CategoryDataset dataset) {
      JFreeChart chart = ChartFactory.createBarChart3D(
             title, // chart title
       subject, // domain axis label
       "现金额(元)", // range axis label
       dataset, // data
       PlotOrientation.VERTICAL, // orientation
       true, // include legend
       true, // tooltips?
       false // URLs?
       );
   
     // set the background color for the chart...
     chart.setBackgroundPaint(Color.white);
     
   StandardLegend sl = (StandardLegend) chart.getLegend();
  sl.setDisplaySeriesShapes(true);
  sl.setItemFont(new Font("/u5b8b/u4f53",Font.TRUETYPE_FONT,12));
     // get a reference to the plot for further customisation...
      CategoryPlot plot = chart.getCategoryPlot();
     plot.setBackgroundPaint(Color.lightGray);
     plot.setDomainGridlinePaint(Color.white);
     plot.setRangeGridlinePaint(Color.white);
   
     // set the range axis to display integers only...
      NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
     rangeAxis.setNumberFormatOverride(NumberFormat.getInstance(Locale.CHINA));
   
     // disable bar outlines...
      BarRenderer renderer = (BarRenderer) plot.getRenderer();
     //鼠标移过时的提示
      StandardCategoryToolTipGenerator g = new StandardCategoryToolTipGenerator("({0}, {1}) = {2}",new DecimalFormat("0.00"));
     renderer.setToolTipGenerator(g);
     //柱形上显示数值
     //带小数,两位
        NumberFormat nf = NumberFormat.getInstance(Locale.CHINA);
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern("0.00");
     StandardCategoryLabelGenerator scg = new StandardCategoryLabelGenerator(
             StandardCategoryLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,
             df);
     renderer.setBaseLabelGenerator(scg);
     renderer.setItemLabelsVisible(true);
     renderer.setDrawBarOutline(true);
     
     //renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));
     //renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
   
     int size = series.length;
     int yu = size % colors.length;
     int loop = size / colors.length;
   
     for (int k = 0; k < loop; k++) {
   
      for (int i = 0; i < colors.length; i++) {
       GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, colors[i],
         0.0f, 0.0f, colors[i]);
       renderer.setSeriesPaint((k * colors.length + i), gp0);
      }
     }
   
     for (int i = 0; i < yu; i++) {
      GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, colors[i],
        0.0f, 0.0f, colors[i]);
      renderer.setSeriesPaint(loop*colors.length+i, gp1);
     }
   
      CategoryAxis domainAxis = plot.getDomainAxis();
     domainAxis.setCategoryLabelPositions(CategoryLabelPositions
       .createUpRotationLabelPositions(Math.PI / 6.0));
     // OPTIONAL CUSTOMISATION COMPLETED.
     
     return chart;
   
    }

 private JFreeChart createPieChart( PieDataset dataset) {

   JFreeChart chart = ChartFactory.createPieChart3D(
          title, // title
    dataset, // data
    true, // include legend
    true, false);
  chart.setBackgroundPaint(Color.white);
  
   StandardLegend sl = (StandardLegend) chart.getLegend();
  sl.setItemFont(new Font("/u5b8b/u4f53",Font.TRUETYPE_FONT,12));
  
        NumberFormat nf = NumberFormat.getInstance(Locale.CHINA);
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern("0.00");
        DecimalFormat df2 = (DecimalFormat) nf;
        df2.applyPattern("0.00%");
       
   PiePlot3D plot = (PiePlot3D) chart.getPlot();
  plot.setLabelGenerator(new StandardPieItemLabelGenerator("{0} = {2}",
    df,df));
  plot.setBackgroundPaint(Color.lightGray);
  plot.setStartAngle(290);
  plot.setDirection(Rotation.CLOCKWISE);
  plot.setForegroundAlpha(0.5f);
  plot.setNoDataMessage("没有数据");
  plot.setIgnoreNullValues(false);
  
        int size = series.length;
        int yu = size % colors.length;
        int loop = size / colors.length;

        for (int k = 0; k < loop; k++) {

            for (int i = 0; i < colors.length; i++) {
                GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, colors[i],
                        0.0f, 0.0f, colors[i]);
                plot.setSectionPaint((k * colors.length + i),gp0);
            }
        }

        for (int i = 0; i < yu; i++) {
            GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, colors[i], 0.0f,
                    0.0f, colors[i]);
            plot.setSectionPaint((loop * colors.length + i),gp1);
        }
       
  return chart;
  

 }

 private static Double format(double value) {
  DecimalFormat df = new DecimalFormat("0.00");

  return Double.valueOf(df.format(value));
 }
}

你可能感兴趣的:(dataset,import,jfreechart,colors,null,string)