JFreeChart圆柱颜色渐变

来源:互联网 发布:淘宝引流方法 编辑:程序博客网 时间:2024/04/28 18:37
package Demo01;import java.awt.GradientPaint;import java.awt.Graphics2D;import java.awt.Paint;import java.awt.Shape;import java.awt.geom.Arc2D;import java.awt.geom.Ellipse2D;import java.awt.geom.GeneralPath;import java.awt.geom.Rectangle2D;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.CategoryItemLabelGenerator;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.data.category.CategoryDataset;import org.jfree.ui.RectangleEdge;/** * 柱状图3D绘制,并用颜色渲染 *  * @author 周洪福 *  */public class CylinderRenderer extends BarRenderer3D {private static final long serialVersionUID = 6642524450330523696L;/** * 无参构造方法 */public CylinderRenderer() {super();}/** * 有参构造方法 *  * @param xOffset *            横轴 * @param yOffset *            纵轴 */public CylinderRenderer(double xOffset, double yOffset) {super(xOffset, yOffset);}/** * 画一个柱状图代表一个数据项 *  * @param g2 *            2D图形 * @param state *            渲染状态 * @param dataArea *            绘图区域 * @param plot *            具体配置 * @param domainAxis *            轴线 * @param rangeAxis *            边框 * @param dataset *            数据源 * @param row *            行数 * @param column *            列数 * @param pass *            通过指数 */public void drawItem(Graphics2D g2, CategoryItemRendererState state,Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,ValueAxis rangeAxis, CategoryDataset dataset, int row, int column,int pass) {// 获取正在绘图区域的值Number dataValue = dataset.getValue(row, column);if (dataValue == null) {return;}// 将获取的值转为double类型double value = dataValue.doubleValue();// 定义一个指定坐标的矩形Rectangle2D adjusted = new Rectangle2D.Double(dataArea.getX(),dataArea.getY() + getYOffset(), dataArea.getWidth()- getXOffset(), dataArea.getHeight() - getYOffset());// 定义图形的方向,horizontal or verticalPlotOrientation orientation = plot.getOrientation();// 计算第一列柱状图的X轴坐标和Y轴坐标double barW0 = calculateBarW0(plot, orientation, adjusted, domainAxis,state, row, column);// 计算坐标的长度double[] barL0L1 = calculateBarL0L1(value);if (barL0L1 == null) {return; // the bar is not visible}// 主轴线RectangleEdge edge = plot.getRangeAxisEdge();float transL0 = (float) rangeAxis.valueToJava2D(barL0L1[0], adjusted,edge);float transL1 = (float) rangeAxis.valueToJava2D(barL0L1[1], adjusted,edge);float barL0 = Math.min(transL0, transL1);float barLength = Math.abs(transL1 - transL0);// draw the bar...GeneralPath bar = new GeneralPath();Shape top = null;if (orientation == PlotOrientation.HORIZONTAL) {bar.moveTo((float) (barL0 + getXOffset() / 2), (float) barW0);bar.lineTo((float) (barL0 + barLength + getXOffset() / 2),(float) barW0);Arc2D arc = new Arc2D.Double(barL0 + barLength, barW0,getXOffset(), state.getBarWidth(), 90, 180, Arc2D.OPEN);bar.append(arc, true);bar.lineTo((float) (barL0 + getXOffset() / 2),(float) (barW0 + state.getBarWidth()));arc = new Arc2D.Double(barL0, barW0, getXOffset(),state.getBarWidth(), 270, -180, Arc2D.OPEN);bar.append(arc, true);bar.closePath();top = new Ellipse2D.Double(barL0 + barLength, barW0, getXOffset(),state.getBarWidth());} else {bar.moveTo((float) barW0, (float) (barL0 - getYOffset() / 2));bar.lineTo((float) barW0,(float) (barL0 + barLength - getYOffset() / 2));Arc2D arc = new Arc2D.Double(barW0,(barL0 + barLength - getYOffset()), state.getBarWidth(),getYOffset(), 180, 180, Arc2D.OPEN);bar.append(arc, true);bar.lineTo((float) (barW0 + state.getBarWidth()),(float) (barL0 - getYOffset() / 2));arc = new Arc2D.Double(barW0, (barL0 - getYOffset()),state.getBarWidth(), getYOffset(), 0, -180, Arc2D.OPEN);bar.append(arc, true);bar.closePath();top = new Ellipse2D.Double(barW0, barL0 - getYOffset(),state.getBarWidth(), getYOffset());}Paint itemPaint = getItemPaint(row, column);if (getGradientPaintTransformer() != null&& itemPaint instanceof GradientPaint) {GradientPaint gp = (GradientPaint) itemPaint;itemPaint = getGradientPaintTransformer().transform(gp, bar);}g2.setPaint(itemPaint);g2.fill(bar);if (itemPaint instanceof GradientPaint) {g2.setPaint(((GradientPaint) itemPaint).getColor2());}if (top != null) {g2.fill(top);}if (isDrawBarOutline()&& state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {g2.setStroke(getItemOutlineStroke(row, column));g2.setPaint(getItemOutlinePaint(row, column));g2.draw(bar);if (top != null) {g2.draw(top);}}CategoryItemLabelGenerator generator = getItemLabelGenerator(row,column);if (generator != null && isItemLabelVisible(row, column)) {drawItemLabel(g2, dataset, row, column, plot, generator,bar.getBounds2D(), (value < 0.0));}// collect entity and tool tip information...if (state.getInfo() != null) {EntityCollection entities = state.getEntityCollection();if (entities != null) {String tip = null;CategoryToolTipGenerator tipster = getToolTipGenerator(row,column);if (tipster != null) {tip = tipster.generateToolTip(dataset, row, column);}String url = null;if (getItemURLGenerator(row, column) != null) {url = getItemURLGenerator(row, column).generateURL(dataset,row, column);}CategoryItemEntity entity = new CategoryItemEntity(bar.getBounds2D(), tip, url, dataset,dataset.getRowKey(row), dataset.getColumnKey(column));entities.add(entity);}}}}



package Demo01;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Dimension;import java.awt.GradientPaint;import java.awt.Paint;import javax.swing.JPanel;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartPanel;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.ItemLabelAnchor;import org.jfree.chart.labels.ItemLabelPosition;import org.jfree.chart.labels.StandardCategoryToolTipGenerator;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRenderer;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.ui.ApplicationFrame;import org.jfree.ui.GradientPaintTransformType;import org.jfree.ui.RefineryUtilities;import org.jfree.ui.StandardGradientPaintTransformer;import org.jfree.ui.TextAnchor;/** * 柱状图 *  * @author 周洪福 *  */public class BargraphDemo02 extends ApplicationFrame {private static final long serialVersionUID = 6220983878162731937L;/** * 柱状图颜色的渲染 * @author 周洪福 * */static class Renderer extends StackedBarRenderer3D{private static final long serialVersionUID = -7047513243372381048L;/** 颜色数组 */private Paint[] colors;/** * 创建一个渲染 *  * @param colors *            颜色数据 */public Renderer(Paint[] colors) {this.colors = colors;}/** * 每项颜色的绘制 */public Paint getItemPaint(int row, int column) {return this.colors[column % this.colors.length];}}public BargraphDemo02(String title) {super(title);// 创建面板JPanel chartPanel = createPanel();// 设置面板大小chartPanel.setPreferredSize(new Dimension(800, 300));// 为窗体添加一个面板setContentPane(chartPanel);}/** * 创建数据源 *  * @return */private static CategoryDataset createDataset() {DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.setValue(3000, "地区:", "临沂");//dataset.setValue(6000, "1:", "临沂");dataset.setValue(4000, "地区:", "日照");dataset.setValue(3500, "地区:", "潍坊");dataset.setValue(5000, "地区:", "寿光");dataset.setValue(5500, "地区:", "胜利");dataset.setValue(3000, "地区:", "辛店");dataset.setValue(3700, "地区:", "张店");dataset.setValue(3900, "地区:", "南定");return dataset;}/** * 设置每个柱状图的颜色 *  * @return */private static Paint[] createPaint() {// 为每个柱状图设置颜色Paint[] colors = new Paint[8];colors[0] = new GradientPaint(0f, 0f, Color.GREEN, 0f, 0f, Color.WHITE);colors[1] = new GradientPaint(0f, 0f, Color.RED, 0f, 0f, Color.WHITE);colors[2] = new GradientPaint(0f, 0f, Color.BLACK, 0f, 0f, Color.WHITE);colors[3] = new GradientPaint(0f, 0f, Color.BLUE, 0f, 0f, Color.WHITE);colors[4] = new GradientPaint(0f, 0f, Color.CYAN, 0f, 0f, Color.WHITE);colors[5] = new GradientPaint(0f, 0f, Color.DARK_GRAY, 0f, 0f,Color.WHITE);colors[6] = new GradientPaint(0f, 0f, Color.GRAY, 0f, 0f, Color.WHITE);colors[7] = new GradientPaint(0f, 0f, Color.LIGHT_GRAY, 0f, 0f,Color.WHITE);return colors;}/** * 创建图表 *  * @param dataset *            数据源 * @return */private static JFreeChart createChart(CategoryDataset dataset) {// 定义图表属性JFreeChart chart = ChartFactory.createStackedBarChart3D("图表标题", "横坐标轴标题","纵坐标轴标题", dataset, PlotOrientation.VERTICAL, false, true, false);// 获得图表的每个组件CategoryPlot plot = (CategoryPlot) chart.getPlot();// 获得颜色数据Paint[] colors = createPaint(); // 将颜色渲染到具体数据Renderer renderer = new Renderer(colors);renderer.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.CENTER_HORIZONTAL));// 边框颜色renderer.setBaseOutlinePaint(Color.gray);// 边框宽度renderer.setBaseOutlineStroke(new BasicStroke(0.3f));renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());//BarRenderer render = (BarRenderer) plot.getRenderer();//render.setBasePositiveItemLabelPosition(new ItemLabelPosition(//                ItemLabelAnchor.CENTER, TextAnchor.CENTER));//render.setBaseNegativeItemLabelPosition(new ItemLabelPosition(//                ItemLabelAnchor.CENTER, TextAnchor.CENTER));plot.setRenderer(renderer);return chart;}/** * 创建面板 *  * @return */public static JPanel createPanel() {// 创建面板并赋与数据JFreeChart chart = createChart(createDataset());return new ChartPanel(chart);}/** * 程序运行主方法 *  * @param args */public static void main(String[] args) {BargraphDemo02 bargraphDemo02 = new BargraphDemo02("柱状图");bargraphDemo02.pack();RefineryUtilities.centerFrameOnScreen(bargraphDemo02);bargraphDemo02.setVisible(true);}}




0 0