JFreeChart气泡图


本章演示如何使用JFreeChart 从给定的一组业务数据创建 Bubble Chart 。气泡图以三维方式显示信息。气泡绘制在(x,y)坐标相交的地方。气泡的大小被认为是X轴和Y轴的范围或数量。

业务数据

让我们考虑不同的人以及他们的年龄,体重和工作能力。炒锅的容量可以视为在图表中作为气泡绘制的小时数。

重量
**年龄** 三十 40 50 60 70 80
10 4 **工作**
20
三十 10
40 8
50 9
60 6

基于AWT的应用程序

以下是根据上述信息创建气泡图的代码。此代码可帮助您在任何基于AWT的应用程序中嵌入Bubble图表。

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BubbleChart_AWT extends ApplicationFrame {

   public BubbleChart_AWT( String s ) {
      super( s );                 
      JPanel jpanel = createDemoPanel( );                 
      jpanel.setPreferredSize(new Dimension( 560 , 370 ) );                 
      setContentPane( jpanel );
   }

   private static JFreeChart createChart( XYZDataset xyzdataset ) {
      JFreeChart jfreechart = ChartFactory.createBubbleChart(
         "AGE vs WEIGHT vs WORK",                    
         "Weight",                    
         "AGE",                    
         xyzdataset,                    
         PlotOrientation.HORIZONTAL,                    
         true, true, false);

      XYPlot xyplot = ( XYPlot )jfreechart.getPlot( );                 
      xyplot.setForegroundAlpha( 0.65F );                 
      XYItemRenderer xyitemrenderer = xyplot.getRenderer( );
      xyitemrenderer.setSeriesPaint( 0 , Color.blue );                 
      NumberAxis numberaxis = ( NumberAxis )xyplot.getDomainAxis( );                 
      numberaxis.setLowerMargin( 0.2 );                 
      numberaxis.setUpperMargin( 0.5 );                 
      NumberAxis numberaxis1 = ( NumberAxis )xyplot.getRangeAxis( );                 
      numberaxis1.setLowerMargin( 0.8 );                 
      numberaxis1.setUpperMargin( 0.9 );

      return jfreechart;
   }

   public static XYZDataset createDataset( ) {
      DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset();
      double ad[ ] = { 30 , 40 , 50 , 60 , 70 , 80 };                 
      double ad1[ ] = { 10 , 20 , 30 , 40 , 50 , 60 };                 
      double ad2[ ] = { 4 , 5 , 10 , 8 , 9 , 6 };                 
      double ad3[][] = { ad , ad1 , ad2 };                 
      defaultxyzdataset.addSeries( "Series 1" , ad3 );

      return defaultxyzdataset;
   }

   public static JPanel createDemoPanel( ) {
      JFreeChart jfreechart = createChart( createDataset( ) );                 
      ChartPanel chartpanel = new ChartPanel( jfreechart );

      chartpanel.setDomainZoomable( true );                 
      chartpanel.setRangeZoomable( true );

      return chartpanel;
   }

   public static void main( String args[ ] ) {
      BubbleChart_AWT bubblechart = new BubbleChart_AWT( "Bubble Chart_frame" );   
      bubblechart.pack( );                 
      RefineryUtilities.centerFrameOnScreen( bubblechart );                 
      bubblechart.setVisible( true );
   }
}

让我们将上面的Java代码保存在 BubbleChart_AWT.java 文件中,然后从提示的命令中编译并运行它 -

$javac BubbleChart_AWT.java  
$java BubbleChart_AW

如果一切正常,它将编译并运行以生成以下Bubble Graph -

JFreeChart气泡图

JPEG图像创建

让我们重写上面的例子,从命令行生成一个JPEG图像。

import java.io.*;

import java.awt.Color;

import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYZDataset;
import org.jfree.chart.ChartUtilities;

public class BubbleChart_image {

   public static void main( String args[ ] )throws Exception {
      DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset( );
      double ad[ ] = { 30 , 40 , 50 , 60 , 70 , 80 };
      double ad1[ ] = { 10 , 20 , 30 , 40 , 50 , 60 };
      double ad2[ ] = { 4 , 5 , 10 , 8 , 9 , 6 };
      double ad3[ ][ ] = { ad , ad1 , ad2 };
      defaultxyzdataset.addSeries( "Series 1" , ad3 );

      JFreeChart jfreechart = ChartFactory.createBubbleChart(
         "AGE vs WEIGHT vs WORK",
         "Weight",
         "AGE",
         defaultxyzdataset,
         PlotOrientation.HORIZONTAL,
         true, true, false);

      XYPlot xyplot = ( XYPlot )jfreechart.getPlot( );
      xyplot.setForegroundAlpha( 0.65F );
      XYItemRenderer xyitemrenderer = xyplot.getRenderer( );
      xyitemrenderer.setSeriesPaint( 0 , Color.blue );
      NumberAxis numberaxis = ( NumberAxis )xyplot.getDomainAxis( );
      numberaxis.setLowerMargin( 0.2 );
      numberaxis.setUpperMargin( 0.5 );
      NumberAxis numberaxis1 = ( NumberAxis )xyplot.getRangeAxis( );
      numberaxis1.setLowerMargin( 0.8 );
      numberaxis1.setUpperMargin( 0.9 );

      int width = 560;   /* Width of the image */
      int height = 370;  /* Height of the image */
      File bubbleChart = new File("BubbleChart.jpeg");
      ChartUtilities.saveChartAsJPEG(bubbleChart,jfreechart,width,height);
   }
}

让我们将上面的Java代码保存在 BubbleChart_image.java 文件中,然后从提示的命令中编译并运行它 -

$javac BubbleChart_image.java  
$java BubbleChart_image

如果一切正常,它将编译并运行在当前目录中创建一个名为 BubbleChart.jpeg 的JPEG图像文件。