结合条形图和线图

时间:2014-01-21 06:31:39

标签: java jfreechart

是否可以绘制使用直线显示范围的条形图。以下链接显示条形图 http://i41.tinypic.com/28atkr6.jpg 我使用jfreechart开发了一个条形图,但我无法在图像上看到它。 任何人都可以给我一个解决方案。

这是我编写的用于开发条形图的代码

公共类BarChartDemo_1扩展了ApplicationFrame {

 public BarChartDemo_1(String title) {
     super(title);
     CategoryDataset dataset = createDataset();
     JFreeChart chart = createChart(dataset);
     ChartPanel chartPanel = new ChartPanel(chart, false);
     chartPanel.setPreferredSize(new Dimension(600, 380));
     setContentPane(chartPanel);
 }

静态类LabelGenerator扩展了StandardCategoryItemLabelGenerator {                 private static final long serialVersionUID = 1L;

            @Override
            public String generateLabel(CategoryDataset categorydataset, int i, int j) {
                    return categorydataset.getRowKey(i).toString();
            }
    }
 /**
  * Returns a sample dataset.
  * 
   * @return The dataset.
   */
private static CategoryDataset createDataset() {


    String series1 = "Series";
    String category1 = "2012";
    String category2 = "2013";
    String category3 = "2014";
    String category4 = "2015";
    String category5 = "2016";
    String category6 = "2017";
    String category7 = "2018";
    String category8 = "2019";
    String category9 = "2020";

     DefaultCategoryDataset  dataset = new DefaultCategoryDataset ();
     dataset.addValue(916.6 ,"Results", category1);
     dataset.addValue(1099.9 ,"Results", category2);
     dataset.addValue(1404.9 ,"Results", category3);
     dataset.addValue(1819.4,"Results", category4);
     dataset.addValue(2388.7 ,"Results", category5);
     dataset.addValue(3179.7,"Results", category6);
     dataset.addValue(4291.5,"Results", category7);
     dataset.addValue(5872.9,"Results", category8);
     dataset.addValue(8149.5,"Results", category9);
    return dataset;

 }



 /**
 * Creates a sample chart.
  * 
 * @param dataset  the dataset.
  * 
  * @return The chart.
 */
private static JFreeChart createChart(CategoryDataset dataset) {

    ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
       BarRenderer.setDefaultBarPainter(new StandardBarPainter()); 
   // create the chart...
     JFreeChart chart = ChartFactory.createBarChart(
        "",       // chart title
        "Year",               // domain axis label
         "Revenue in $ million",                  // range axis label
         dataset,                  // data
        PlotOrientation.VERTICAL, // orientation
         true,                     // include legend
         true,                     // tooltips?
         false                     // URLs?
    );


    chart.setBackgroundPaint(Color.white);

     CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();

     plot.setBackgroundPaint(Color.white);
     plot.setDomainGridlinePaint(Color.white);
     plot.setDomainGridlinesVisible(true);
     plot.setRangeGridlinePaint(Color.white);

     final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());


    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setMaximumBarWidth(0.0380000000000000001D);


    ((BarRenderer)plot.getRenderer()).setBarPainter(new StandardBarPainter());
    renderer.setShadowVisible(false);
    renderer.setDrawBarOutline(false);
    renderer.setSeriesPaint(0,new Color(46,110,172));



    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());


   renderer.setBaseItemLabelsVisible(true);
   ItemLabelPosition itemLabelPosition=new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT,-1.5707963267948966D);
   renderer.setBaseItemLabelPaint(Color.WHITE);
   renderer.setBasePositiveItemLabelPosition(itemLabelPosition);
   CategoryAxis domainAxis = plot.getDomainAxis();
   domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);

返回图表;

 }

public static void main(String[] args) {
  BarChartDemo1 demo = new BarChartDemo1("Bar Chart Demo_1");
  demo.pack();
  RefineryUtilities.centerFrameOnScreen(demo);
  demo.setVisible(true);
 }

}

这就是我创建条形图的方法。但我不明白如何在其中组合线图。无法开发上述图形。请求大家帮助我。

0 个答案:

没有答案