关闭

JFreeChart - BarChartTest.java

1260人阅读 评论(0) 收藏 举报
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
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.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.labels.StandardCategoryLabelGenerator;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.ui.TextAnchor;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class BarChartTest extends ApplicationFrame {
  static class LabelGenerator extends StandardCategoryLabelGenerator {
    public String generateItemLabel(CategoryDataset categorydataset, int i, int j) {
      return categorydataset.getRowKey(i).toString();
      //return "";
    }
    LabelGenerator() {
    }
  }

  public BarChartTest(String s) {
    super(s);
    CategoryDataset categoryDataset = createDataset();
    JFreeChart freeChart = createChart(categoryDataset);
    ChartPanel chartPanel = new ChartPanel(freeChart);
    chartPanel.setPreferredSize(new Dimension(640, 350));
    setContentPane(chartPanel);
  }
  private static CategoryDataset createDataset() {
    //图中显示的数据
    double data[][] = { {20D, 20D, 25D, 30D}, {25D, 30D, 35D, 40D} };
    String type[] = {"投诉人数", "投诉量"};
    String type2[] = {"8210", "7210", "6210", "3120"};
    return DatasetUtilities.createCategoryDataset(type, type2, data);
    //return DatasetUtilities.createCategoryDataset("投诉量", "8210", data);
  }
  private static JFreeChart createChart(CategoryDataset categoryDataset) {
    JFreeChart freeChart = ChartFactory.createBarChart("Nokia投诉统计", "型号",
      "", categoryDataset, PlotOrientation.HORIZONTAL, true, true, false);
    //JFreeChart freeChart = ChartFactory.createBarChart("Nokia投诉统计", "型号", "条(单位)", categoryDataset, PlotOrientation.HORIZONTAL, true, true, false);
    freeChart.setBackgroundPaint(Color.white);
    freeChart.setBorderVisible(true);
    Legend legend = freeChart.getLegend();
    legend.setAnchor(Legend.EAST);
    CategoryPlot categoryPlot = freeChart.getCategoryPlot();
    categoryPlot.setBackgroundPaint(Color.lightGray);
    categoryPlot.setRangeGridlinePaint(Color.black);
    //categoryPlot.setRangeGridlinePaint(Color.white);
    categoryPlot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
    //categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
    numberAxis.setRange(0.0D, 100D);
    numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    //numberAxis.setAutoTickIndex(5);
    BarRenderer barRenderer = (BarRenderer) categoryPlot.getRenderer();
    //设置每一项的数值可见
    barRenderer.setDrawBarOutline(false);
    barRenderer.setItemLabelsVisible(true);
    barRenderer.setLabelGenerator(new LabelGenerator());
    barRenderer.setSeriesPaint(0, new java.awt.Color(0xFCB90A));
    barRenderer.setSeriesPaint(1, new java.awt.Color(0x00D5E7));
    //设置每组数据中每一项中间的间隔
    barRenderer.setItemMargin(0.00000000000000001D);
    //设置每一项的数值显示的位置
    //ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, -1.5707963267948966D);
    //barRenderer.setPositiveItemLabelPosition(itemlabelposition);
    //ItemLabelPosition itemlabelposition1 = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, -1.5707963267948966D);
    //barRenderer.setPositiveItemLabelPositionFallback(itemlabelposition1);
    //设置 种类标签的 显示倾斜角度
    CategoryAxis categoryaxis = categoryPlot.getDomainAxis();
    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    return freeChart;
  }
  public static void main(String[] args) {
    BarChartTest barChartTest = new BarChartTest("BarChartTest");
    barChartTest.pack();
    RefineryUtilities.centerFrameOnScreen(barChartTest);
    barChartTest.setVisible(true);
  }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:171832次
    • 积分:1711
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:12篇
    • 译文:0篇
    • 评论:8条
    文章分类