气泡图(JFreeChart- Bubble Chart)

优质
小牛编辑
144浏览
2023-12-01

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

业务数据

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

重量
AGE304050607080
104WORK
205
3010
408
509
606

基于AWT的应用 (AWT Based Application)

以下是从上面给出的信息创建气泡图的代码。 此代码可帮助您在任何基于AWT的应用程序中嵌入气泡图。

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 

如果一切正常,它将编译并运行以生成以下气泡图 -

JFreeChart气泡图

JPEG图像创建 (JPEG Image Creation)

让我们重新编写上面的例子,从命令行生成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图像文件。