/** * Exports the current chart as an image. * @param width The width of the image * @param height The height of the image * @param hideLegend If true, the image will not contain the legend * @return The image */ public BufferedImage exportAsImage(int width, int height, boolean hideLegend){ LegendTitle legend = null; if(hideLegend){ // Remove legend while exporting the image legend = this.chartPanel.getChart().getLegend(); this.chartPanel.getChart().removeLegend(); } BufferedImage thumb = this.chartPanel.getChart().createBufferedImage(width, height, 600, 400, null); if(hideLegend){ // If the legend was removed, add it after exporting the image this.chartPanel.getChart().addLegend(legend); } return thumb; }
private void legendPositionChanged(LegendPosition legendPosition) { JFreeChart chart = getCurrentChart(); if (chart != null) { LegendTitle legend = chart.getLegend(); RectangleEdge position = legendPosition.getPosition(); if (legend != null) { if (position != null) { legend.setPosition(position); } else { while (chart.getLegend() != null) { chart.removeLegend(); } } } else { if (position != null) { resetLegend(); } } } }
public void paintDeviationChart(Graphics graphics, int width, int height) { prepareData(); JFreeChart chart = createChart(this.dataset); // set the background color for the chart... chart.setBackgroundPaint(Color.white); // legend settings LegendTitle legend = chart.getLegend(); if (legend != null) { legend.setPosition(RectangleEdge.TOP); legend.setFrame(BlockBorder.NONE); legend.setHorizontalAlignment(HorizontalAlignment.LEFT); } Rectangle2D drawRect = new Rectangle2D.Double(0, 0, width, height); chart.draw((Graphics2D) graphics, drawRect); }
/** * Returns the nth legend for a chart, or <code>null</code>. * * @param index the legend index (zero-based). * * @return The legend (possibly <code>null</code>). * * @see #addLegend(LegendTitle) */ public LegendTitle getLegend(int index) { int seen = 0; Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title subtitle = (Title) iterator.next(); if (subtitle instanceof LegendTitle) { if (seen == index) { return (LegendTitle) subtitle; } else { seen++; } } } return null; }
/** * Update legend */ private void updateLegend() { _chart.removeLegend(); final LegendTitle legend = new LegendTitle(() -> { final LegendItemCollection collection = new LegendItemCollection(); if (_currentMode == DisplayMode.TIME) { collection.add(new LegendItem(Legend.LATENCY.label, null, null, null, new Rectangle(30, 20), Legend.LATENCY.color)); if (_dnsLookup && _services.isEmbeddedTRAvailable() && !Env.INSTANCE.isUseOSTraceroute()) { collection.add(new LegendItem(Legend.DNS.label, null, null, null, new Rectangle(30, 20), Legend.DNS.color)); } } else { collection.add(new LegendItem(Legend.DISTANCE.label, Legend.DISTANCE.color)); } return collection; }); legend.setPosition(RectangleEdge.BOTTOM); _chart.addLegend(legend); }
/** * Returns the nth legend for a chart, or {@code null}. * * @param index the legend index (zero-based). * * @return The legend (possibly {@code null}). * * @see #addLegend(LegendTitle) */ public LegendTitle getLegend(int index) { int seen = 0; Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title subtitle = (Title) iterator.next(); if (subtitle instanceof LegendTitle) { if (seen == index) { return (LegendTitle) subtitle; } else { seen++; } } } return null; }
private void fillChart(String title, List<String> names, List<String> colors, List<Float> values) throws Exception { DefaultPieDataset data=new DefaultPieDataset(); for (int ix=0; ix<names.size(); ix++) { data.setValue( names.get(ix), values.get(ix) ); } this.chart=ChartFactory.createPieChart3D( title, data, true, true, false); LegendTitle legend=this.chart.getLegend(); legend.setItemFont(new Font("", Font.TRUETYPE_FONT, 9) ); PiePlot plot=(PiePlot)this.chart.getPlot(); plot.setCircular(true); plot.setBackgroundAlpha(0.9f); plot.setForegroundAlpha(0.5f); plot.setLabelFont(new Font("", Font.TRUETYPE_FONT, 9) ); this.setPlotColor( plot, names, colors ); this.chart.setTitle(new TextTitle(title, new Font("", Font.TRUETYPE_FONT, 9) ) ); }
/** * TODO Documentation * * @param chart * @return */ protected JFreeChart styleChart(JFreeChart chart) { chart.setBackgroundPaint(Color.white); LegendTitle legend = chart.getLegend(); legend.setPosition(RectangleEdge.RIGHT); legend.setVisible(false); final CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.white); plot.setRangeGridlinePaint(Color.lightGray); final LineAndShapeRenderer renderer = new LineAndShapeRenderer(); renderer.setSeriesLinesVisible(0,true); renderer.setSeriesShapesVisible(1, true); renderer.setBaseLinesVisible(true); renderer.setBaseShapesFilled(true); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(Color.lightGray); plot.setRenderer(renderer); // customise the range axis... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setAutoRangeIncludesZero(false); rangeAxis.setStandardTickUnits(rangeAxis.getStandardTickUnits()); return chart; }
/** * TODO Documentation * * @param chart * @return */ public JFreeChart styleChart(JFreeChart chart) { chart.setBackgroundPaint(Color.white); LegendTitle legend = chart.getLegend(); legend.setPosition(RectangleEdge.RIGHT); legend.setVisible(false); XYPlot chartPlot = chart.getXYPlot(); chartPlot.setBackgroundPaint(Color.white); chartPlot.setForegroundAlpha(0.7F); XYItemRenderer brRenderer = chartPlot.getRenderer(); chartPlot.setDomainGridlinesVisible(true); chartPlot.setDomainGridlinePaint(Color.GRAY); chartPlot.setRangeGridlinePaint(Color.GRAY); brRenderer.setSeriesPaint(0, new Color(0,0,139)); // customise the range axis... final ValueAxis domainAxis = chartPlot.getDomainAxis(); domainAxis.setLowerMargin(0.0); return chart; }
/** * Creates a sample chart. * * @param dataset the dataset. * * @return The chart. */ protected JFreeChart createChart(CategoryDataset dataset) { // get a reference to the plot for further customisation... SOCRSpiderWebPlot plot = new SOCRSpiderWebPlot(dataset); JFreeChart chart = new JFreeChart( chartTitle, TextTitle.DEFAULT_FONT, plot, false ); LegendTitle legend = new LegendTitle(plot); legend.setPosition(RectangleEdge.BOTTOM); //renderer.setLegendItemLabelGenerator(new SOCRCategorySeriesLabelGenerator()); chart.addSubtitle(legend); setCategorySummary(dataset); if (legendPanelOn) chart.removeLegend(); return chart; }
/** * Returns the nth legend for a chart, or <code>null</code>. * * @param index the legend index (zero-based). * * @return The legend (possibly <code>null</code>). */ public LegendTitle getLegend(int index) { int seen = 0; Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title subtitle = (Title) iterator.next(); if (subtitle instanceof LegendTitle) { if (seen == index) { return (LegendTitle) subtitle; } else { seen++; } } } return null; }
protected void removeItemFromLegend(int itemToRemove, JFreeChart chart) { LegendItemCollection legendItems = chart.getPlot().getLegendItems(); final LegendItemCollection newLegendItems = new LegendItemCollection(); for (int i = 0; i < legendItems.getItemCount(); i++) { if (itemToRemove != i) newLegendItems.add(legendItems.get(i)); } LegendItemSource source = new LegendItemSource() { @Override public LegendItemCollection getLegendItems() { return newLegendItems; } }; chart.removeLegend(); chart.addLegend(new LegendTitle(source)); }
/** * Returns the nth legend for a chart, or <code>null</code>. * * @param index the legend index (zero-based). * @return The legend (possibly <code>null</code>). * @see #addLegend(LegendTitle) */ public LegendTitle getLegend(int index) { int seen = 0; Iterator iterator = this.subtitles.iterator(); while (iterator.hasNext()) { Title subtitle = (Title) iterator.next(); if (subtitle instanceof LegendTitle) { if (seen == index) { return (LegendTitle) subtitle; } else { seen++; } } } return null; }
/** * Creates {@link LegendTitle}s for all dimensions from the PlotConfiguration of this Plotter2D. * Expects that all {@link ValueSource} s in the provided PlotConfiguration use the same * {@link DimensionConfig} s. */ private List<LegendTitle> createLegendTitles() { List<LegendTitle> legendTitles = new LinkedList<LegendTitle>(); LegendConfiguration legendConfiguration = plotInstance.getCurrentPlotConfigurationClone().getLegendConfiguration(); LegendTitle legendTitle = new SmartLegendTitle(this, new FlowArrangement(HorizontalAlignment.CENTER, VerticalAlignment.CENTER, 30, 2), new ColumnArrangement( HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 2)); legendTitle.setItemPaint(legendConfiguration.getLegendFontColor()); RectangleEdge position = legendConfiguration.getLegendPosition().getPosition(); if (position == null) { return legendTitles; } legendTitle.setPosition(position); if (legendConfiguration.isShowLegendFrame()) { legendTitle.setFrame(new BlockBorder(legendConfiguration.getLegendFrameColor())); } ColoredBlockContainer wrapper = new ColoredBlockContainer(legendConfiguration.getLegendBackgroundColor()); wrapper.add(legendTitle.getItemContainer()); wrapper.setPadding(3, 3, 3, 3); legendTitle.setWrapper(wrapper); legendTitles.add(legendTitle); return legendTitles; }
public void paintDeviationChart(Graphics graphics, int width, int height) { prepareData(); JFreeChart chart = createChart(this.dataset); // set the background color for the chart... chart.setBackgroundPaint(Color.white); // legend settings LegendTitle legend = chart.getLegend(); if (legend != null) { legend.setPosition(RectangleEdge.TOP); legend.setFrame(BlockBorder.NONE); legend.setHorizontalAlignment(HorizontalAlignment.LEFT); } Rectangle2D drawRect = new Rectangle2D.Double(0, 0, width, height); chart.draw((Graphics2D)graphics, drawRect); }
public static LegendTitle createLegend() { final LegendItemCollection legendItems = new LegendItemCollection(); FontRenderContext frc = new FontRenderContext(null, true, true); Font legenfont = new Font(Font.SANS_SERIF, Font.PLAIN, 12); GlyphVector gv = legenfont.createGlyphVector(frc, new char[] {'I', 'I'}); Shape shape = gv.getVisualBounds(); Rectangle2D bounds = shape.getBounds2D(); { LegendItem li = new LegendItem("Standard error", null, null, null, new ErrorIndicator(bounds), new BasicStroke(), ErrorIndicator.ERROR_INDICATOR_COLOR); li.setLabelFont(legenfont); legendItems.add(li); } LegendTitle legend = new LegendTitle(new LegendItemSource() { @Override public LegendItemCollection getLegendItems() { return legendItems; }}); legend.setPosition(RectangleEdge.BOTTOM); legend.setMargin(new RectangleInsets(0,30,0,0)); legend.setPadding(RectangleInsets.ZERO_INSETS); legend.setLegendItemGraphicPadding(new RectangleInsets(0,20,0,0)); legend.setHorizontalAlignment(HorizontalAlignment.LEFT); return legend; }
private LegendTitle createLegend() { final LegendItemCollection legendItems = new LegendItemCollection(); FontRenderContext frc = new FontRenderContext(null, true, true); GlyphVector gv = LEGEND_FONT.createGlyphVector(frc, new char[] {'X'}); Shape shape = gv.getGlyphVisualBounds(0); for(Pair<String, Color> p : getLegend()) { LegendItem li = new LegendItem(p.getLeft(), null, null, null, shape, p.getRight()); li.setLabelFont(LEGEND_FONT); legendItems.add(li); } LegendTitle legend = new LegendTitle(new LegendItemSource() { @Override public LegendItemCollection getLegendItems() { return legendItems; }}); legend.setPosition(RectangleEdge.BOTTOM); return legend; }
protected final void addLegend() { if (chart == null) { throw new IllegalStateException("initChart() must be called first"); } LegendTitle legend = new LegendTitle(chart.getPlot()); legend.setItemFont(LEGEND_FONT); legend.setBorder(0, 0, 0, 0); legend.setBackgroundPaint(Color.WHITE); legend.setPosition(RectangleEdge.BOTTOM); RectangleInsets padding = new RectangleInsets(5, 5, 5, 5); legend.setItemLabelPadding(padding); chart.addLegend(legend); }
public void writeChart(String filename, int x, int y) throws Exception { JFreeChart chart = createChart(); if (param.backgroundPaint != null) chart.getPlot().setBackgroundPaint(param.backgroundPaint); //TODO: handle legends with series titles chart.removeLegend(); // plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12)); // plot.setNoDataMessage("No data available"); if (param.subtitle != null) chart.addSubtitle(new TextTitle(param.subtitle)); if (param.showLegend) chart.addLegend(new LegendTitle(chart.getPlot())); String fileExt = filenameExtension(filename); if (fileExt.equalsIgnoreCase("jpg") || fileExt.equalsIgnoreCase("jpeg")) ChartUtilities.saveChartAsJPEG(new File(filename), chart, x, y); else ChartUtilities.saveChartAsPNG(new File(filename), chart, x, y); }
private void setAttribute(JFreeChart chart) { // 圖案與文字的間隔 LegendTitle legend = chart.getLegend(); legend.setBorder(1, 1, 1, 1); CategoryPlot plot = chart.getCategoryPlot(); // 設定WorkItem的屬性 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); // 字體角度 domainAxis.setTickLabelFont(new Font("新細明體", Font.TRUETYPE_FONT, 12)); // 字體 // 設定Date的屬性 DateAxis da = (DateAxis) plot.getRangeAxis(0); setDateAxis(da); // 設定實體的顯示名稱 CategoryItemRenderer render = plot.getRenderer(0); DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); CategoryItemLabelGenerator generator = new IntervalCategoryItemLabelGenerator( "{3} ~ {4}", format); render.setBaseItemLabelGenerator(generator); render.setBaseItemLabelPaint(Color.BLUE); render.setBaseItemLabelsVisible(true); render.setBaseItemLabelFont(new Font("黑體", Font.TRUETYPE_FONT, 8)); render.setSeriesPaint(0, Color.RED); }
protected void formatChart() { format.getColors().reset(); chart.setBackgroundPaint(format.getBackgroundColor()); Plot plot = chart.getPlot(); formatPlot(plot); if(plot instanceof CategoryPlot) { CategoryPlot cPlot = (CategoryPlot) plot; formatAxis(cPlot.getDomainAxis()); formatAxis(cPlot.getRangeAxis()); } LegendTitle legend = chart.getLegend(); if(legend != null) formatLegend(legend); }
protected void formatChart() { format.getColors().reset(); chart.setBackgroundPaint(format.getBackgroundColor()); Plot plot = chart.getPlot(); formatPlot(plot); if(plot instanceof CategoryPlot) { CategoryPlot cPlot = (CategoryPlot) plot; formatAxisColors(cPlot.getDomainAxis()); formatAxisColors(cPlot.getRangeAxis()); } LegendTitle legend = chart.getLegend(); if(legend != null) formatLegend(legend); }
public static void createLegend(JFreeChart kmPlot, List<KMPlotPointSeriesSet> plotPointSeriesSetCollection) { LegendTitle legend = kmPlot.getLegend(); LegendItemSource[] sources = new LegendItemSource[1]; KMLegendItemSource legendSrc = new KMLegendItemSource(); Comparator<KMPlotPointSeriesSet> nameComparator = new Comparator<KMPlotPointSeriesSet>() { public int compare(KMPlotPointSeriesSet series1, KMPlotPointSeriesSet series2) { return series1.getName().compareToIgnoreCase(series2.getName()); } }; Collections.sort(plotPointSeriesSetCollection, nameComparator); LegendItem item ; for (KMPlotPointSeriesSet plotPointSeries : plotPointSeriesSetCollection) { Color color = plotPointSeries.getColor(); String title = plotPointSeries.getLegendTitle() + " (" + plotPointSeries.getGroupSize() + ")"; item = new LegendItem(title, null, null, null, new Rectangle2D.Double(2,2,10,10), color); legendSrc.addLegendItem(item); } sources[0] = legendSrc; legend.setSources(sources); }
/** * Creates {@link LegendTitle}s for all dimensions from the PlotConfiguration of this Plotter2D. * Expects that all {@link ValueSource} s in the provided PlotConfiguration use the same * {@link DimensionConfig} s. */ private List<LegendTitle> createLegendTitles() { List<LegendTitle> legendTitles = new LinkedList<LegendTitle>(); LegendConfiguration legendConfiguration = plotInstance.getCurrentPlotConfigurationClone().getLegendConfiguration(); LegendTitle legendTitle = new SmartLegendTitle(this, new FlowArrangement(HorizontalAlignment.CENTER, VerticalAlignment.CENTER, 30, 2), new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 2)); legendTitle.setItemPaint(legendConfiguration.getLegendFontColor()); RectangleEdge position = legendConfiguration.getLegendPosition().getPosition(); if (position == null) { return legendTitles; } legendTitle.setPosition(position); if (legendConfiguration.isShowLegendFrame()) { legendTitle.setFrame(new BlockBorder(legendConfiguration.getLegendFrameColor())); } ColoredBlockContainer wrapper = new ColoredBlockContainer(legendConfiguration.getLegendBackgroundColor()); wrapper.add(legendTitle.getItemContainer()); wrapper.setPadding(3, 3, 3, 3); legendTitle.setWrapper(wrapper); legendTitles.add(legendTitle); return legendTitles; }