public void loadDataToGrafik() throws IOException {
    DefaultCategoryDataset dataset = (DefaultCategoryDataset) this.generateData();
    chart =
        ChartFactory.createBarChart(
            "Statistik Pendidikan Dosen",
            "Prodi",
            "Jumlah Dosen",
            dataset,
            PlotOrientation.HORIZONTAL,
            true,
            true,
            false);
    chart.setBackgroundPaint(new Color(0xCC, 0xFF, 0xCC));

    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
    plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    final CategoryItemRenderer renderer1 = plot.getRenderer();
    renderer1.setSeriesPaint(0, Color.red);
    renderer1.setSeriesPaint(1, Color.yellow);
    renderer1.setSeriesPaint(2, Color.green);
    BarRenderer br = (BarRenderer) renderer1;
    br.setShadowVisible(false);

    br.setShadowVisible(false);
    BufferedImage bi = chart.createBufferedImage(900, 1500, BufferedImage.TRANSLUCENT, null);
    byte[] bytes = EncoderUtil.encode(bi, ImageFormat.PNG, true);
    AImage image = new AImage("Bar Chart", bytes);
    chartImg.setContent(image);
    btnExport.setDisabled(false);
  }
示例#2
0
  public String creatchat(CategoryDataset cr, int ss) {
    JFreeChart chart =
        ChartFactory.createLineChart(
            "先来先服务", "进程", "时间", cr, PlotOrientation.VERTICAL, true, true, false);
    CategoryPlot ccc = (CategoryPlot) chart.getPlot();

    // Plot plo=chart.getPlot();
    // plo.setDrawingSupplier(getSupplier());

    ValueAxis valueaxis = ccc.getRangeAxis(); // getRangeAxis();
    // 数据为整型
    valueaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    // 设定显示范围,即总是显示1-10
    if (ss == 1) {
      Image image;
      try {
        image = ImageIO.read(new File("C:/wancheng.png"));
        chart.setBackgroundImage(image);
        chart.setBackgroundImageAlpha(1.0f);
        chart.setBorderPaint(Color.white);
        ccc.setBackgroundPaint(null);
      } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      valueaxis.setLowerBound(-1);
      valueaxis.setUpperBound(7);
    } else {
      valueaxis.setLowerBound(-1);
      valueaxis.setUpperBound(7);
    }
    CategoryItemRenderer renderer = ccc.getRenderer();
    renderer.setSeriesPaint(0, Color.YELLOW);
    renderer.setSeriesPaint(1, Color.red);
    renderer.setSeriesPaint(2, Color.green);
    renderer.setSeriesPaint(3, Color.gray);
    renderer.setSeriesPaint(4, Color.cyan);
    renderer.setSeriesPaint(5, Color.magenta);
    renderer.setSeriesPaint(6, Color.pink);

    FileOutputStream fos_jpg = null;
    try {
      if (ss == 1) {
        String chaername = "C:/jincheng.png";
        fos_jpg = new FileOutputStream(chaername);

        // 将报表保存为png文件
        ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 510);
        return chaername;
      } else {
        String chaername = "C:/wancheng.png";
        fos_jpg = new FileOutputStream(chaername);

        // 将报表保存为png文件
        ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 510);
        return chaername;
      }

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        fos_jpg.close();
        System.out.println("create time-createTimeXYChar.");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }