前言 :
這裡透過繼承 BarRenderer3D 新建類別 CylinderRenderer 來達成 3D 漸層的效果. 執行效果如下 :
範例代碼 :
在使用下面代碼時, 請在 Classpath 引入 jcommon-1.0.16.jar 與 jfreechart-1.0.13.jar (或更高版本).
- CylinderRenderer.java :
- CylinderChartdemo1.java : 主程式
這裡透過繼承 BarRenderer3D 新建類別 CylinderRenderer 來達成 3D 漸層的效果. 執行效果如下 :
範例代碼 :
在使用下面代碼時, 請在 Classpath 引入 jcommon-1.0.16.jar 與 jfreechart-1.0.13.jar (或更高版本).
- CylinderRenderer.java :
- package test;
- import java.awt.GradientPaint;
- import java.awt.Graphics2D;
- import java.awt.geom.*;
- import org.jfree.chart.axis.CategoryAxis;
- import org.jfree.chart.axis.ValueAxis;
- import org.jfree.chart.entity.CategoryItemEntity;
- import org.jfree.chart.entity.EntityCollection;
- import org.jfree.chart.labels.CategoryToolTipGenerator;
- import org.jfree.chart.plot.CategoryPlot;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.renderer.category.BarRenderer3D;
- import org.jfree.chart.renderer.category.CategoryItemRendererState;
- import org.jfree.chart.urls.CategoryURLGenerator;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.ui.GradientPaintTransformer;
- public class CylinderRenderer extends BarRenderer3D
- {
- public CylinderRenderer()
- {
- }
- public CylinderRenderer(double d, double d1)
- {
- super(d, d1);
- }
- public void drawItem(Graphics2D graphics2d, CategoryItemRendererState categoryitemrendererstate, Rectangle2D rectangle2d, CategoryPlot categoryplot, CategoryAxis categoryaxis, ValueAxis valueaxis, CategoryDataset categorydataset,
- int i, int j, int k)
- {
- Number number = categorydataset.getValue(i, j);
- if(number == null)
- return;
- double d = number.doubleValue();
- java.awt.geom.Rectangle2D.Double double1 = new java.awt.geom.Rectangle2D.Double(rectangle2d.getX(), rectangle2d.getY() + getYOffset(), rectangle2d.getWidth() - getXOffset(), rectangle2d.getHeight() - getYOffset());
- PlotOrientation plotorientation = categoryplot.getOrientation();
- double d1 = calculateBarW0(categoryplot, plotorientation, double1, categoryaxis, categoryitemrendererstate, i, j);
- double ad[] = calculateBarL0L1(d);
- if(ad == null)
- return;
- org.jfree.ui.RectangleEdge rectangleedge = categoryplot.getRangeAxisEdge();
- float f = (float)valueaxis.valueToJava2D(ad[0], double1, rectangleedge);
- float f1 = (float)valueaxis.valueToJava2D(ad[1], double1, rectangleedge);
- float f2 = Math.min(f, f1);
- float f3 = Math.abs(f1 - f);
- GeneralPath generalpath = new GeneralPath();
- if(plotorientation == PlotOrientation.HORIZONTAL)
- {
- generalpath.moveTo(f2, (float)d1);
- generalpath.lineTo(f2, (float)(d1 + categoryitemrendererstate.getBarWidth()));
- generalpath.lineTo(f2 + f3, (float)(d1 + categoryitemrendererstate.getBarWidth()));
- generalpath.lineTo(f2 + f3, (float)d1);
- generalpath.closePath();
- } else
- {
- generalpath.moveTo((float)d1, (float)((double)f2 - getYOffset() / 2D));
- generalpath.lineTo((float)d1, (float)((double)(f2 + f3) - getYOffset() / 2D));
- java.awt.geom.Arc2D.Double double2 = new java.awt.geom.Arc2D.Double(d1, (double)(f2 + f3) - getYOffset(), categoryitemrendererstate.getBarWidth(), getYOffset(), 180D, 180D, 0);
- generalpath.append(double2, true);
- generalpath.lineTo((float)(d1 + categoryitemrendererstate.getBarWidth()), (float)((double)f2 - getYOffset() / 2D));
- double2 = new java.awt.geom.Arc2D.Double(d1, (double)f2 - getYOffset(), categoryitemrendererstate.getBarWidth(), getYOffset(), 0.0D, -180D, 0);
- generalpath.append(double2, true);
- generalpath.closePath();
- }
- Object obj = getItemPaint(i, j);
- if(getGradientPaintTransformer() != null && (obj instanceof GradientPaint))
- {
- GradientPaint gradientpaint = (GradientPaint)obj;
- obj = getGradientPaintTransformer().transform(gradientpaint, generalpath);
- }
- graphics2d.setPaint(((java.awt.Paint) (obj)));
- graphics2d.fill(generalpath);
- java.awt.geom.Ellipse2D.Double double3 = new java.awt.geom.Ellipse2D.Double(d1, (double)f2 - getYOffset(), categoryitemrendererstate.getBarWidth(), getYOffset());
- if(obj instanceof GradientPaint)
- graphics2d.setPaint(((GradientPaint)obj).getColor2());
- graphics2d.fill(double3);
- if(isDrawBarOutline() && categoryitemrendererstate.getBarWidth() > 3D)
- {
- graphics2d.setStroke(getItemOutlineStroke(i, j));
- graphics2d.setPaint(getItemOutlinePaint(i, j));
- graphics2d.draw(generalpath);
- if(double3 != null)
- graphics2d.draw(double3);
- }
- org.jfree.chart.labels.CategoryItemLabelGenerator categoryitemlabelgenerator = getItemLabelGenerator(i, j);
- if(categoryitemlabelgenerator != null && isItemLabelVisible(i, j))
- drawItemLabel(graphics2d, categorydataset, i, j, categoryplot, categoryitemlabelgenerator, generalpath.getBounds2D(), d < 0.0D);
- if(categoryitemrendererstate.getInfo() != null)
- {
- EntityCollection entitycollection = categoryitemrendererstate.getEntityCollection();
- if(entitycollection != null)
- {
- String s = null;
- CategoryToolTipGenerator categorytooltipgenerator = getToolTipGenerator(i, j);
- if(categorytooltipgenerator != null)
- s = categorytooltipgenerator.generateToolTip(categorydataset, i, j);
- String s1 = null;
- if(getItemURLGenerator(i, j) != null)
- s1 = getItemURLGenerator(i, j).generateURL(categorydataset, i, j);
- CategoryItemEntity categoryitementity = new CategoryItemEntity(generalpath.getBounds2D(), s, s1, categorydataset, i, categorydataset.getColumnKey(j), j);
- entitycollection.add(categoryitementity);
- }
- }
- }
- }
- CylinderChartdemo1.java : 主程式
- package test;
- import java.awt.*;
- import javax.swing.JPanel;
- import org.jfree.chart.*;
- import org.jfree.chart.plot.CategoryPlot;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- import org.jfree.ui.*;
- /**
- * Reference
- * - http://read.pudn.com/downloads94/sourcecode/java/378182/demo1.0.6%20src/demo/CylinderChartDemo1.java__.htm
- * - http://www.koders.com/java/fid906FA2E5BC738E0DF4BC3A8C9A7916854086DE18.aspx?s=160
- * @author John-Lee
- *
- */
- public class CylinderChartdemo1 extends ApplicationFrame{
- static class CustomCylinderRenderer extends CylinderRenderer
- {
- public Paint getItemPaint(int i, int j)
- {
- return colors[j % colors.length];
- }
- private Paint colors[];
- public CustomCylinderRenderer(Paint apaint[])
- {
- colors = apaint;
- }
- }
- public CylinderChartdemo1(String s)
- {
- super(s);
- CategoryDataset categorydataset = createDataset();
- JFreeChart jfreechart = createChart(categorydataset);
- ChartPanel chartpanel = new ChartPanel(jfreechart);
- chartpanel.setPreferredSize(new Dimension(500, 270));
- setContentPane(chartpanel);
- }
- private static CategoryDataset createDataset()
- {
- DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
- defaultcategorydataset.addValue(4D, "S1", "Monday");
- defaultcategorydataset.addValue(5D, "S1", "Tuesday");
- defaultcategorydataset.addValue(7D, "S1", "Wednesday");
- defaultcategorydataset.addValue(6D, "S1", "Thursday");
- defaultcategorydataset.addValue(4D, "S1", "Friday");
- return defaultcategorydataset;
- }
- private static JFreeChart createChart(CategoryDataset categorydataset)
- {
- JFreeChart jfreechart = ChartFactory.createBarChart3D("Cylinder Chart demo", "Category", "Value", categorydataset, PlotOrientation.VERTICAL, false, true, false);
- CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
- Paint apaint[] = createPaint();
- CustomCylinderRenderer customcylinderrenderer = new CustomCylinderRenderer(apaint);
- customcylinderrenderer.setGradientPaintTransformer(newStandardGradientPaintTransformer(GradientPaintTransformType.CENTER_HORIZONTAL));
- customcylinderrenderer.setOutlinePaint(Color.gray);
- customcylinderrenderer.setOutlineStroke(new BasicStroke(0.3F));
- categoryplot.setRenderer(customcylinderrenderer);
- return jfreechart;
- }
- private static Paint[] createPaint()
- {
- Paint apaint[] = new Paint[5];
- apaint[0] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.red);
- apaint[1] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.green);
- apaint[2] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.blue);
- apaint[3] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.orange);
- apaint[4] = new GradientPaint(0.0F, 0.0F, Color.white, 0.0F, 0.0F, Color.magenta);
- return apaint;
- }
- public static JPanel createdemoPanel()
- {
- JFreeChart jfreechart = createChart(createDataset());
- return new ChartPanel(jfreechart);
- }
- public static void main(String args[])
- {
- CylinderChartdemo1 cylinderchartdemo1 = new CylinderChartdemo1("Cylinder Chart demo 1");
- cylinderchartdemo1.pack();
- RefineryUtilities.centerFrameOnScreen(cylinderchartdemo1);
- cylinderchartdemo1.setVisible(true);
- }
- }
This message was edited 1 time. Last update was at 01/12/2011 09:16:14