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); }
protected void configurePlot(Plot plot, JRChartPlot jrPlot) { super.configurePlot(plot, jrPlot); if (plot instanceof CategoryPlot) { CategoryPlot categoryPlot = (CategoryPlot) plot; CategoryItemRenderer categoryRenderer = categoryPlot.getRenderer(); CategoryDataset categoryDataset = categoryPlot.getDataset(); if (categoryDataset != null) { for (int i = 0; i < categoryDataset.getRowCount(); i++) { categoryRenderer.setSeriesOutlinePaint(i, ChartThemesConstants.TRANSPARENT_PAINT); } } categoryPlot.setRangeGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); categoryPlot.setRangeGridlineStroke(new BasicStroke(0.5f)); categoryPlot.setDomainGridlinesVisible(false); categoryPlot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45); } else if (plot instanceof XYPlot) { XYPlot xyPlot = (XYPlot) plot; XYItemRenderer xyItemRenderer = xyPlot.getRenderer(); XYDataset xyDataset = xyPlot.getDataset(); if (xyDataset != null) { for (int i = 0; i < xyDataset.getSeriesCount(); i++) { xyItemRenderer.setSeriesOutlinePaint(i, ChartThemesConstants.TRANSPARENT_PAINT); } } xyPlot.setRangeGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); xyPlot.setRangeGridlineStroke(new BasicStroke(0.5f)); xyPlot.setDomainGridlinesVisible(false); xyPlot.setRangeZeroBaselineVisible(true); } }
/** Check that setting a URL generator for a series does override the default generator. */ public void testSetSeriesURLGenerator() { CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); CategoryItemRenderer renderer = plot.getRenderer(); StandardCategoryURLGenerator url1 = new StandardCategoryURLGenerator(); renderer.setSeriesItemURLGenerator(0, url1); CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); assertTrue(url2 == url1); }
/** Check that setting a tool tip generator for a series does override the default generator. */ public void testSetSeriesToolTipGenerator() { CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); CategoryItemRenderer renderer = plot.getRenderer(); StandardCategoryToolTipGenerator tt = new StandardCategoryToolTipGenerator(); renderer.setSeriesToolTipGenerator(0, tt); CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); assertTrue(tt2 == tt); }
protected JFreeChart createGanttChart() throws JRException { JFreeChart jfreeChart = super.createGanttChart(); CategoryPlot categoryPlot = (CategoryPlot) jfreeChart.getPlot(); categoryPlot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.STANDARD); categoryPlot.setDomainGridlinesVisible(true); categoryPlot.setDomainGridlinePosition(CategoryAnchor.END); categoryPlot.setDomainGridlineStroke( new BasicStroke( 0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 50, new float[] {1}, 0)); categoryPlot.setDomainGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); categoryPlot.setRangeGridlinesVisible(true); categoryPlot.setRangeGridlineStroke( new BasicStroke( 0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 50, new float[] {1}, 0)); categoryPlot.setRangeGridlinePaint(ChartThemesConstants.GRAY_PAINT_217); // JRBarPlot barPlot = (BarPlot)categoryPlot; // categoryPlot.getDomainAxis().setTickLabelsVisible( // categoryPlot.getShowTickLabels() == null ? true : barPlot.getShowTickLabels(). // true // ); CategoryItemRenderer categoryRenderer = categoryPlot.getRenderer(); categoryRenderer.setBaseItemLabelsVisible(true); BarRenderer barRenderer = (BarRenderer) categoryRenderer; List seriesPaints = (List) getDefaultValue(defaultChartPropertiesMap, ChartThemesConstants.SERIES_COLORS); barRenderer.setSeriesPaint(0, (Paint) seriesPaints.get(3)); barRenderer.setSeriesPaint(1, (Paint) seriesPaints.get(0)); CategoryDataset categoryDataset = categoryPlot.getDataset(); if (categoryDataset != null) { for (int i = 0; i < categoryDataset.getRowCount(); i++) { barRenderer.setSeriesItemLabelFont(i, categoryPlot.getDomainAxis().getTickLabelFont()); barRenderer.setSeriesItemLabelsVisible(i, true); // barRenderer.setSeriesPaint(i, GRADIENT_PAINTS[i]); // CategoryMarker categoryMarker = new // CategoryMarker(categoryDataset.getColumnKey(i),MARKER_COLOR, new BasicStroke(1f)); // categoryMarker.setAlpha(0.5f); // categoryPlot.addDomainMarker(categoryMarker, Layer.BACKGROUND); } } categoryPlot.setOutlinePaint(Color.DARK_GRAY); categoryPlot.setOutlineStroke(new BasicStroke(1.5f)); categoryPlot.setOutlineVisible(true); return jfreeChart; }
/** * Creates a stacked vertical bar chart with default settings. * * @param title the chart title. * @param categoryAxisLabel the label for the category axis. * @param valueAxisLabel the label for the value axis. * @param data the dataset for the chart. * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * @return a stacked vertical bar chart. */ public static JFreeChart createStackedBarChart3D( String title, java.awt.Font titleFont, String categoryAxisLabel, String valueAxisLabel, CategoryDataset data, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls, CategoryURLGenerator urlGenerator) { // create the axes... CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel); ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel); // create the renderer... CategoryItemRenderer renderer = new StackedBarRenderer3D(); CategoryToolTipGenerator toolTipGenerator = null; if (tooltips) { toolTipGenerator = new StandardCategoryToolTipGenerator(); } if (urls) { renderer.setItemURLGenerator(urlGenerator); } renderer.setToolTipGenerator(toolTipGenerator); // create the plot... CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); plot.setOrientation(orientation); // create the chart... JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); return chart; }
/** Creates the chart below the statistics table */ private void createChart() { mAllocCountDataSet = new DefaultCategoryDataset(); mChart = ChartFactory.createBarChart( null, "Size", "Count", mAllocCountDataSet, PlotOrientation.VERTICAL, false, true, false); // get the font to make a proper title. We need to convert the swt font, // into an awt font. Font f = mStatisticsBase.getFont(); FontData[] fData = f.getFontData(); // event though on Mac OS there could be more than one fontData, we'll only use // the first one. FontData firstFontData = fData[0]; java.awt.Font awtFont = SWTUtils.toAwtFont(mStatisticsBase.getDisplay(), firstFontData, true /* ensureSameSize */); mChart.setTitle(new TextTitle("Allocation count per size", awtFont)); Plot plot = mChart.getPlot(); if (plot instanceof CategoryPlot) { // get the plot CategoryPlot categoryPlot = (CategoryPlot) plot; // set the domain axis to draw labels that are displayed even with many values. CategoryAxis domainAxis = categoryPlot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90); CategoryItemRenderer renderer = categoryPlot.getRenderer(); renderer.setBaseToolTipGenerator( new CategoryToolTipGenerator() { @Override public String generateToolTip(CategoryDataset dataset, int row, int column) { // get the key for the size of the allocation ByteLong columnKey = (ByteLong) dataset.getColumnKey(column); String rowKey = (String) dataset.getRowKey(row); Number value = dataset.getValue(rowKey, columnKey); return String.format( "%1$d %2$s of %3$d bytes", value.intValue(), rowKey, columnKey.getValue()); } }); } mChartComposite = new ChartComposite( mStatisticsBase, SWT.BORDER, mChart, ChartComposite.DEFAULT_WIDTH, ChartComposite.DEFAULT_HEIGHT, ChartComposite.DEFAULT_MINIMUM_DRAW_WIDTH, ChartComposite.DEFAULT_MINIMUM_DRAW_HEIGHT, 3000, // max draw width. We don't want it to zoom, so we put a big number 3000, // max draw height. We don't want it to zoom, so we put a big number true, // off-screen buffer true, // properties true, // save true, // print false, // zoom true); // tooltips mChartComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); }
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(); } } }