Java tutorial
/** * PureInfo Quake * @(#)CategoryChartBuilder.java 1.0 Sep 22, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.reports.impl; import java.awt.Color; import java.awt.Point; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Iterator; import java.util.List; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.AbstractCategoryItemLabelGenerator; import org.jfree.chart.labels.CategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.DefaultDrawingSupplier; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import com.pureinfo.srm.reports.IChartBuilder; import com.pureinfo.srm.reports.impl.ChartBuilderBase.ChartInfo; import com.pureinfo.srm.reports.model.ReportResult; /** * <P> * Created on Sep 22, 2005 10:27:32 AM <BR> * Last modified on Sep 22, 2005 * </P> * * @author Freeman.Hu * @version 1.0, Sep 22, 2005 * @since Quake 1.0 */ public class CategoryChartBuilder extends ChartBuilderBase implements IChartBuilder { static class LabelGenerator extends AbstractCategoryItemLabelGenerator implements CategoryItemLabelGenerator { public String generateLabel(CategoryDataset categorydataset, int i, int j) { String s = null; Number number = categorydataset.getValue(i, j); if (number != null) { double d = number.doubleValue(); if (d > threshold) s = number.toString(); } return s; } private double threshold; public LabelGenerator(double d) { super("", NumberFormat.getInstance()); threshold = d; } } private String m_sTitle; private String m_sYXxisName; private List m_sDatas; /** * @see com.pureinfo.srm.reports.IChartBuilder#buildChart(java.util.List, * int, java.lang.String) */ public JFreeChart buildChart() { CategoryDataset dataset = createDataset(); JFreeChart jfreechart = MyChartFactory.createBarChart3D(null, m_sXAxisName, m_sYXxisName, dataset, PlotOrientation.VERTICAL, false, false, false); CategoryPlot categoryplot = jfreechart.getCategoryPlot(); jfreechart.setBackgroundPaint(Color.white); categoryplot.setDomainGridlinesVisible(false); categoryplot.setRangeGridlinesVisible(true); categoryplot.setForegroundAlpha(0.8f); categoryplot.setDrawingSupplier(new DefaultDrawingSupplier(MyPaintMgr.getPaints(), DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE)); CategoryAxis categoryAxisX = categoryplot.getDomainAxis(); categoryAxisX.setTickLabelsVisible(true); categoryAxisX.setLabel(null); categoryAxisX.setMaximumCategoryLabelWidthRatio(.8f); ValueAxis categoryAxisY = categoryplot.getRangeAxis(); categoryAxisY.setVerticalTickLabels(false); // categoryplot.setDrawSharedDomainAxis(false); categoryAxisX.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(0.39269908169872414D)); BarRenderer3D barrenderer3d = (BarRenderer3D) categoryplot.getRenderer(); barrenderer3d.setDrawBarOutline(false); barrenderer3d.setItemLabelsVisible(true); barrenderer3d.setBaseItemLabelsVisible(false); barrenderer3d.setSeriesVisible(Boolean.FALSE); //CategoryPlot categoryPlot = jfreechart.getCategoryPlot(); // categoryPlot.setBackgroundPaint(Color.lightGray); // categoryPlot.setDomainGridlinePaint(Color.white); // categoryPlot.setRangeGridlinePaint(Color.white); // // BarRenderer barRenderer = (BarRenderer) categoryPlot.getRenderer(); // barRenderer.setDrawBarOutline(false); // barRenderer.setMaxBarWidth(0.03); // GradientPaint gradientPaint = new GradientPaint(0.0F, 0.0F, // Color.red, 0.0F, 0.0F, new Color(64, 0, 0)); // barRenderer.setSeriesPaint(0, gradientPaint); DecimalFormat format = null; if (m_dblMaxData > 1) { format = new DecimalFormat("#,###"); } else { StringBuffer sb = new StringBuffer(); sb.append("0.00#"); for (double i = m_dblMaxData; i != 0 && i < 1; i *= 10f) { sb.append("#"); } format = new DecimalFormat(sb.toString()); } fillChartInfo(dataset, format); barrenderer3d.setMaxBarWidth(8.0 / m_ChartInfo.getChartSize().x); NumberAxis numberAxis = (NumberAxis) categoryplot.getRangeAxis(0); numberAxis.setUpperMargin(0.149999999999D); numberAxis.setNumberFormatOverride(format); categoryAxisX.setCategoryLabelPositions(CategoryLabelPositions.UP_45); NumberAxis dataNumberAxis = (NumberAxis) categoryplot.getRangeAxisForDataset(0); dataNumberAxis.setUpperMargin(0.149999999999D); dataNumberAxis.setNumberFormatOverride(format); // CategoryItemRenderer renderer = categoryPlot.getRenderer(); // CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", format); // renderer.setItemLabelGenerator(generator); // renderer.setItemLabelFont(new Font("Serif", 0, 9)); // renderer.setItemLabelsVisible(true); return jfreechart; } /** * @param _format * @param _dataset */ private void fillChartInfo(CategoryDataset _dataset, DecimalFormat _format) { int n = _dataset.getColumnCount(); m_ChartInfo = new ChartInfo(); m_ChartInfo.setChartTitle(m_sTitle); String[] labels = new String[n]; for (int i = 0; i < n; i++) { labels[i] = "" + _dataset.getColumnKey(i); labels[i] += " = "; labels[i] += _format.format(_dataset.getValue(0, i)); } m_ChartInfo.setLabels(labels); Point size = new Point(n > 20 ? ChartInfo.SIZE_WIDE : ChartInfo.SIZE_NORROW); if (n > 20) size.x += 40 * (n - 20); m_ChartInfo.setChartSize(size); m_ChartInfo.setLengedPosition(n > 20 ? ChartInfo.LENGEND_POSITION_BUTTOM : ChartInfo.LENGEND_POSITION_LEFT); } private double m_dblMaxData = 0; private String m_sXAxisName; private CategoryDataset createDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Iterator iter = m_sDatas.iterator(); iter.hasNext();) { ReportResult result = (ReportResult) iter.next(); String sName = result.getName(); double dblValue = result.getValue(); dataset.addValue(dblValue, m_sYXxisName, sName); m_dblMaxData = dblValue > m_dblMaxData ? dblValue : m_dblMaxData; } return dataset; } public String getTitle() { return m_sTitle; } public void setTitle(String title) { m_sTitle = title; } public void setXAxisName(String axisName) { m_sXAxisName = axisName; } public void setYXxisName(String xxisName) { m_sYXxisName = xxisName; } /** * List format: <br> * <result(ReportResult)> <br> * * @param datas */ public void setDatas(List datas) { m_sDatas = datas; } }