JFreeChart Get Legend Graphic - java

In my application, I am showing data in a table as well as through a Dual Axis Bar/Line JFreeChart. To save some space (as the charts are being saved as PNG and put on PDF with iText PDF), I wanted to take the Graphics from the Legend, use them in the tabular view, and remove the legend.
Is there a way to grab the icons that lie with the legend item? I have found the LegendGraphic class, which seemed like would be the method to retrieve the icon from the LegendItem, but have not found anything in the documentation for LegendItem that would indicate it does.
It would be preferable if they were returned in an object that could easily be used to create a com.itextpdf.text.Image, such as byte[] or java.awt.Image.

You can get a series' LegendItem using the chart renderer's getLegendItem() method. You can change a series' Shape using the methods of ShapeUtilities, as shown in this example. See also DefaultDrawingSupplier for details of how createStandardSeriesShapes() works.
Addendum: Note that the renderer's getLegendItem() method works even if you create the chart with no legend or later use chart.removeLegend(). Once you have the LegendItem, you can use it's attributes as required.
System.out.println(renderer.getLegendItem(0, 0).getShape());
System.out.println(renderer.getLegendItem(0, 0).getFillPaint());

Related

Is there a way to set individual label link colors in JFreeChart's PiePlot?

I see that I can use setLabelLinkPaint() to universally apply a color to all label links, but I'm looking to set each individual label link to a different color. Is there a way to do this?
It looks like for now the most straightforward way to approach this problem is to subclass the PiePlot class and override the drawRightLabel and drawLeftLabel methods. In my case, I needed to have the label link color match the corresponding pie chart section, so in my overridden methods I set g2.setPaint(getSectionPaint(record.getKey())).
My advice to anyone trying to do something similar is to watch out for casting errors if casting from a JFreeChart chart object. You will likely have to additionally subclass ChartFactory and modify it so that it uses your subclassed PiePlot and not the original one.

Android Customized Barchart

I need to create a chart like this picture bellow in Android.
and I am already aware of MPAndroidChart and AndroidHelloCharts libraries. but none of them are capable of drawing such thing.
do you know any way to create a chart like this?
please explain and at least suggest me some tutorials or articles that are related to what I am about to do.
thank you
Its actually pretty simple to create your own bar chart.
First you need to use drawRect to draw a rectangle.
And then drawText to draw text below the rectangle.
You may have noticed that both methods take a paint object as the parameter , you can think of the paint object like a brush. So you set the color to the brush and draw different elements.
I have written about it
here

JFreechart, Line Chart with filled Areas

I am trying to create chart like below:
While I've almost achieved everything by simply creating a line chart and customizing shape/paint for Renderer, I can't seem to find a way to fill the areas under the series line.
Any clues, how can I do this?
You could create your chart with a StackedXYAreaRenderer. Specify AREA_AND_SHAPES in the constructor and enable outlines. See the ChartFactory code for createStackedXYAreaChart() as an example.
StackedXYAreaRenderer r = new StackedXYAreaRenderer(XYAreaRenderer.AREA_AND_SHAPES);
r.setOutline(true);
Given a renderer, you can set the outline paint and stroke as desired.

Java JFreeChart: customize tooltip screen position

I implemented my own JFreeChart XYToolTipGenerator and, as the chart it is used on is almost full screen, sometimes the tooltip position (on screen) hides the point it is related to (e.g. in the bottom right corner, since it seems that tooltip is configured to be positioned South-East of the mouse / data point). This is a problem because the user needs to be able to click on the chart's data points (as it generates a specific action).
Is there a way to either define dynamically the position of the tooltip (e.g. for data points bottom right I would ask the tooltip to be shown North-West) or, alternatively, to define a systematic position (e.g. North-West instead of South-East as it is by default)?
This problem has given me headaches for the last few days - any help or hint is more than welcome.
Many thanks!
Thomas
Here's the answer I posted on the JFreeChart forum:
JFreeChart is using the standard Swing tool tip mechanism. In the ChartPanel class, the getToolTipText(mouseEvent) method is overridden to return the appropriate text for the tooltip, and that's it.
Swing also gives you the option to override the getToolTipLocation(mouseEvent) method, and that's probably what you need here.

JFreeChart : how do you put multiple charts into a composite chart

In JFreeChart is there a notion of a composite Chart.
I need to layout several charts in a grid like arrangement.
Each chart in the grid needs to have its own separate title.
I would like to be able to save this composite chart into a png file
I would get a code snippet that explains how to do this.
As shown here, ChartPanel can be placed in any desired Swing layout.
The example cited uses GridLayout. ChartUtilities has methods for rendering a chart as a .png. I see several approaches to getting a composite image:
Use Robot#createScreenCapture() to image the layout, as shown here and here; the resulting BufferedImage can be saved using ImageIO.write().
Use JFreeChart#createBufferedImage() to render each chart and impose the individual images into a BufferedImage to create a single image, as suggested here.
Implement the Printable interface to render the image in a graphics context, as shown here.

Categories

Resources