程式扎記: [ Java 代碼範本 ] JFreeChart : 3D BarChart with Blends - Demo 3 (多維度的3D 漸層長條圖)

標籤

2011年11月30日 星期三

[ Java 代碼範本 ] JFreeChart : 3D BarChart with Blends - Demo 3 (多維度的3D 漸層長條圖)


前言 :
這裡透過繼承 BarRenderer3D 新建類別 CylinderRenderer 來達成 3D 漸層的效果. 執行效果如下 :


範例代碼 :
在使用下面代碼時, 請在 Classpath 引入 jcommon-1.0.16.jar 與 jfreechart-1.0.13.jar (或更高版本).
- CylinderRenderer.java :
  1. package test;  
  2.   
  3. import java.awt.GradientPaint;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.geom.*;  
  6. import org.jfree.chart.axis.CategoryAxis;  
  7. import org.jfree.chart.axis.ValueAxis;  
  8. import org.jfree.chart.entity.CategoryItemEntity;  
  9. import org.jfree.chart.entity.EntityCollection;  
  10. import org.jfree.chart.labels.CategoryToolTipGenerator;  
  11. import org.jfree.chart.plot.CategoryPlot;  
  12. import org.jfree.chart.plot.PlotOrientation;  
  13. import org.jfree.chart.renderer.category.BarRenderer3D;  
  14. import org.jfree.chart.renderer.category.CategoryItemRendererState;  
  15. import org.jfree.chart.urls.CategoryURLGenerator;  
  16. import org.jfree.data.category.CategoryDataset;  
  17. import org.jfree.ui.GradientPaintTransformer;  
  18.   
  19. public class CylinderRenderer extends BarRenderer3D  
  20. {  
  21.   
  22.     public CylinderRenderer()  
  23.     {  
  24.     }  
  25.   
  26.     public CylinderRenderer(double d, double d1)  
  27.     {  
  28.         super(d, d1);  
  29.     }  
  30.   
  31.     public void drawItem(Graphics2D graphics2d, CategoryItemRendererState categoryitemrendererstate, Rectangle2D rectangle2d, CategoryPlot categoryplot, CategoryAxis categoryaxis, ValueAxis valueaxis, CategoryDataset categorydataset,   
  32.             int i, int j, int k)  
  33.     {  
  34.         Number number = categorydataset.getValue(i, j);  
  35.         if(number == null)  
  36.             return;  
  37.         double d = number.doubleValue();  
  38.         java.awt.geom.Rectangle2D.Double double1 = new java.awt.geom.Rectangle2D.Double(rectangle2d.getX(), rectangle2d.getY() + getYOffset(), rectangle2d.getWidth() - getXOffset(), rectangle2d.getHeight() - getYOffset());  
  39.         PlotOrientation plotorientation = categoryplot.getOrientation();  
  40.         double d1 = calculateBarW0(categoryplot, plotorientation, double1, categoryaxis, categoryitemrendererstate, i, j);  
  41.         double ad[] = calculateBarL0L1(d);  
  42.         if(ad == null)  
  43.             return;  
  44.         org.jfree.ui.RectangleEdge rectangleedge = categoryplot.getRangeAxisEdge();  
  45.         float f = (float)valueaxis.valueToJava2D(ad[0], double1, rectangleedge);  
  46.         float f1 = (float)valueaxis.valueToJava2D(ad[1], double1, rectangleedge);  
  47.         float f2 = Math.min(f, f1);  
  48.         float f3 = Math.abs(f1 - f);  
  49.         GeneralPath generalpath = new GeneralPath();  
  50.         if(plotorientation == PlotOrientation.HORIZONTAL)  
  51.         {  
  52.             generalpath.moveTo(f2, (float)d1);  
  53.             generalpath.lineTo(f2, (float)(d1 + categoryitemrendererstate.getBarWidth()));  
  54.             generalpath.lineTo(f2 + f3, (float)(d1 + categoryitemrendererstate.getBarWidth()));  
  55.             generalpath.lineTo(f2 + f3, (float)d1);  
  56.             generalpath.closePath();  
  57.         } else  
  58.         {  
  59.             generalpath.moveTo((float)d1, (float)((double)f2 - getYOffset() / 2D));  
  60.             generalpath.lineTo((float)d1, (float)((double)(f2 + f3) - getYOffset() / 2D));  
  61.             java.awt.geom.Arc2D.Double double2 = new java.awt.geom.Arc2D.Double(d1, (double)(f2 + f3) - getYOffset(), categoryitemrendererstate.getBarWidth(), getYOffset(), 180D, 180D, 0);  
  62.             generalpath.append(double2, true);  
  63.             generalpath.lineTo((float)(d1 + categoryitemrendererstate.getBarWidth()), (float)((double)f2 - getYOffset() / 2D));  
  64.             double2 = new java.awt.geom.Arc2D.Double(d1, (double)f2 - getYOffset(), categoryitemrendererstate.getBarWidth(), getYOffset(), 0.0D, -180D, 0);  
  65.             generalpath.append(double2, true);  
  66.             generalpath.closePath();  
  67.         }  
  68.         Object obj = getItemPaint(i, j);  
  69.         if(getGradientPaintTransformer() != null && (obj instanceof GradientPaint))  
  70.         {  
  71.             GradientPaint gradientpaint = (GradientPaint)obj;  
  72.             obj = getGradientPaintTransformer().transform(gradientpaint, generalpath);  
  73.         }  
  74.         graphics2d.setPaint(((java.awt.Paint) (obj)));  
  75.         graphics2d.fill(generalpath);  
  76.         java.awt.geom.Ellipse2D.Double double3 = new java.awt.geom.Ellipse2D.Double(d1, (double)f2 - getYOffset(), categoryitemrendererstate.getBarWidth(), getYOffset());  
  77.         if(obj instanceof GradientPaint)  
  78.             graphics2d.setPaint(((GradientPaint)obj).getColor2());  
  79.         graphics2d.fill(double3);  
  80.         if(isDrawBarOutline() && categoryitemrendererstate.getBarWidth() > 3D)  
  81.         {  
  82.             graphics2d.setStroke(getItemOutlineStroke(i, j));  
  83.             graphics2d.setPaint(getItemOutlinePaint(i, j));  
  84.             graphics2d.draw(generalpath);  
  85.             if(double3 != null)  
  86.                 graphics2d.draw(double3);  
  87.         }  
  88.         org.jfree.chart.labels.CategoryItemLabelGenerator categoryitemlabelgenerator = getItemLabelGenerator(i, j);  
  89.         if(categoryitemlabelgenerator != null && isItemLabelVisible(i, j))  
  90.             drawItemLabel(graphics2d, categorydataset, i, j, categoryplot, categoryitemlabelgenerator, generalpath.getBounds2D(), d < 0.0D);  
  91.         if(categoryitemrendererstate.getInfo() != null)  
  92.         {  
  93.             EntityCollection entitycollection = categoryitemrendererstate.getEntityCollection();  
  94.             if(entitycollection != null)  
  95.             {  
  96.                 String s = null;  
  97.                 CategoryToolTipGenerator categorytooltipgenerator = getToolTipGenerator(i, j);  
  98.                 if(categorytooltipgenerator != null)  
  99.                     s = categorytooltipgenerator.generateToolTip(categorydataset, i, j);  
  100.                 String s1 = null;  
  101.                 if(getItemURLGenerator(i, j) != null)  
  102.                     s1 = getItemURLGenerator(i, j).generateURL(categorydataset, i, j);  
  103.                 CategoryItemEntity categoryitementity = new CategoryItemEntity(generalpath.getBounds2D(), s, s1, categorydataset, i, categorydataset.getColumnKey(j), j);  
  104.                 entitycollection.add(categoryitementity);  
  105.             }  
  106.         }  
  107.     }  
  108. }  

- CylinderChartdemo1.java : 主程式
  1. package test;  
  2.   
  3. import java.awt.*;  
  4. import javax.swing.JPanel;  
  5. import org.jfree.chart.*;  
  6. import org.jfree.chart.plot.CategoryPlot;  
  7. import org.jfree.chart.plot.PlotOrientation;  
  8. import org.jfree.data.category.CategoryDataset;  
  9. import org.jfree.data.category.DefaultCategoryDataset;  
  10. import org.jfree.ui.*;  
  11.   
  12. /** 
  13. * Reference  
  14. *  - http://read.pudn.com/downloads94/sourcecode/java/378182/demo1.0.6%20src/demo/CylinderChartDemo1.java__.htm 
  15. *  - http://www.koders.com/java/fid906FA2E5BC738E0DF4BC3A8C9A7916854086DE18.aspx?s=160 
  16. * @author John-Lee 
  17. * 
  18. */  
  19. public class CylinderChartdemo1 extends ApplicationFrame{  
  20.     static class CustomCylinderRenderer extends CylinderRenderer  
  21.     {  
  22.   
  23.         public Paint getItemPaint(int i, int j)  
  24.         {  
  25.             return colors[j % colors.length];  
  26.         }  
  27.   
  28.         private Paint colors[];  
  29.   
  30.         public CustomCylinderRenderer(Paint apaint[])  
  31.         {  
  32.             colors = apaint;  
  33.         }  
  34.     }  
  35.   
  36.   
  37.     public CylinderChartdemo1(String s)  
  38.     {  
  39.         super(s);  
  40.         CategoryDataset categorydataset = createDataset();  
  41.         JFreeChart jfreechart = createChart(categorydataset);  
  42.         ChartPanel chartpanel = new ChartPanel(jfreechart);  
  43.         chartpanel.setPreferredSize(new Dimension(500270));  
  44.         setContentPane(chartpanel);  
  45.     }  
  46.   
  47.     private static CategoryDataset createDataset()  
  48.     {  
  49.         DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();  
  50.         defaultcategorydataset.addValue(4D, "S1""Monday");  
  51.         defaultcategorydataset.addValue(5D, "S1""Tuesday");  
  52.         defaultcategorydataset.addValue(7D, "S1""Wednesday");  
  53.         defaultcategorydataset.addValue(6D, "S1""Thursday");  
  54.         defaultcategorydataset.addValue(4D, "S1""Friday");  
  55.         return defaultcategorydataset;  
  56.     }  
  57.   
  58.     private static JFreeChart createChart(CategoryDataset categorydataset)  
  59.     {  
  60.         JFreeChart jfreechart = ChartFactory.createBarChart3D("Cylinder Chart demo""Category""Value", categorydataset, PlotOrientation.VERTICAL, falsetruefalse);  
  61.         CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();  
  62.         Paint apaint[] = createPaint();  
  63.         CustomCylinderRenderer customcylinderrenderer = new CustomCylinderRenderer(apaint);  
  64.         customcylinderrenderer.setGradientPaintTransformer(newStandardGradientPaintTransformer(GradientPaintTransformType.CENTER_HORIZONTAL));  
  65.         customcylinderrenderer.setOutlinePaint(Color.gray);  
  66.         customcylinderrenderer.setOutlineStroke(new BasicStroke(0.3F));  
  67.         categoryplot.setRenderer(customcylinderrenderer);  
  68.         return jfreechart;  
  69.     }  
  70.   
  71.     private static Paint[] createPaint()  
  72.     {  
  73.         Paint apaint[] = new Paint[5];  
  74.         apaint[0] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.red);  
  75.         apaint[1] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.green);  
  76.         apaint[2] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.blue);  
  77.         apaint[3] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.orange);  
  78.         apaint[4] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.magenta);  
  79.         return apaint;  
  80.     }  
  81.   
  82.     public static JPanel createdemoPanel()  
  83.     {  
  84.         JFreeChart jfreechart = createChart(createDataset());  
  85.         return new ChartPanel(jfreechart);  
  86.     }  
  87.   
  88.     public static void main(String args[])  
  89.     {  
  90.         CylinderChartdemo1 cylinderchartdemo1 = new CylinderChartdemo1("Cylinder Chart demo 1");  
  91.         cylinderchartdemo1.pack();  
  92.         RefineryUtilities.centerFrameOnScreen(cylinderchartdemo1);  
  93.         cylinderchartdemo1.setVisible(true);  
  94.     }  
  95. }  
This message was edited 1 time. Last update was at 01/12/2011 09:16:14

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!