I have come up with my final hysteresis plot, which looks like this:
My earlier post:
Plotting a hysteresis loop with jFreeChart
I have used 4 XYSeries for my first hysteresis loop and another four for the second hysteresis loop.
If i turn on the legend
JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title
"Pounds(lb)", // domain axis label
"Movement(inch)", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
it appears like this :
I just want to show that blue is my first hysteresis loop and red is my second hysteresis loop, which I have added as subtitle in the first pic as workaround. Can someone guide how I can add manual legends which indicate blue as first loop and red as second.
Thanks
Because ChartFactory.createXYLineChart() creates an XYPlot, you could try setFixedLegendItems(), mentioned here and here.
A more ambitious scheme is to suppress the native legend and render the legend items in a separate component, as shown here.
Related
I am rendering a line chart using JFreechart. I need help to modify legend which is being rendered by the JFree. I need to increase the length of line in legend area. Below image shows the current output.
Below images shows what I want to achieve
Thanks
It can be done by using the code below:
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setLegendLine(new Line2D.Double(-20.0D, 0.0D, 20.0D, 0.0D));
I'm facing an issue using PolarPlot with jFreeChart. My code plots a single dot with an arbitrary angle and a radius of maximum 1. Everytime I redraw the plot, the number of radial grid lines changes. This makes the program very ugly.
How can I constrain the number of lines to a predefined number?
So I found a satisfying solution:
final JFreeChart chart = ChartFactory.createPolarChart("", dataset, true, true, false);
final PolarPlot plot = (PolarPlot) chart.getPlot();
((NumberAxis)plot.getAxis()).setTickUnit(new NumberTickUnit(0.25));
plot.getAxis().setRange(-1, 1);
Important is the last line, where you set the Range, else if you only plot one single dot the spacing is correct, but the plot will be scaled and the plotted dot lies on the border.
Is it possible to remove the section label boxes from the below pie chart.I searched alot but was not able to get an answer.
![enter image description here]
[1] http://tinypic.com/view.php?pic=2vkhuyp&s=5
the above link is for pie chart drawn using java.I mean i dont want the percent shown inside the pie chart surrounded with a box it should just show the percent for ex.14%
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelBackgroundPaint(null);
plot.setLabelOutlinePaint(null);
plot.setLabelShadowPaint(null);
I need to add break line in some legend in JFree Chart.
I have some legends with 316 characters and need to break every 80.
Finally, I'll have 4 lines.
Anyway, I tried with "\n", "\u2424" and "
". It did nothing.
(From http://www.jfree.org/forum/viewtopic.php?f=3&t=10226 & http://www.jfree.org/forum/viewtopic.php?f=3&t=22417)
The only solution I could find (but I wished it could be avoided, since I want it to be dynamically done) is to fix a width for each legend, so it should break as I need to.
Edit : that even didn't work.
I'm using jFree Chart 0.9.20
EDIT
For the moment, with a small legend, that's what I have :
It's fine but when I have my long legends :
For that last picture, I logged my legend and break lines are here, but they don't show up with jFree Chart.
Two alternatives to consider: Given an abbreviated legend display string,
Use setLegendItemToolTipGenerator() to display the full, unbroken string as a tool tip.
renderer.setLegendItemToolTipGenerator(
new StandardXYSeriesLabelGenerator("Legend {0}"));
Use addChartMouseListener(), shown here, and forward mouse moved events over the legend to an adjacent text component.
Alright, I made it work as my client wanted.
First, you need to make a new kind of Legend, for example named MyLegend (but please, don't name it like that in the real world).
That class needs to extend Legend and implement Serializable, the same way StandardLegend does.
To be honest, I even copied/pasted the whole StandardLegend in MyLegend.
Then, you can modify the standard legend to your custom one.
For my needs, I changed :
draw() for the height and width calculation of the whole Legend group
drawSeriesElements() to split the legend's label and draw every lines one under another.
// Multi line management for Legend
String[] multiline = item.getItem().getLabel().split(System.getProperty("line.separator"));
for(int j = 0; j<multiline.length; j++) {
RefineryUtilities.drawAlignedString(multiline[j], g2,
(float) item.getLabelPosition().getX(), (float) item
.getLabelPosition().getY() + g2.getFontMetrics().getHeight()*j, TextAnchor.CENTER_LEFT);
}
createDrawableLegendItem() to calculate each item width and height.
Since, now legends are multiline, each line of one item doesn't have the same width than others. We need to find the longest one to define the item's real width.
Same goes for height. Now it's multiline, so it needs to calculate how many lines it got to know the item's real height.
Optionally, you could change drawLegendTitle() to make it multiline too.
When that class is configured as you want to, you need to apply it on your chart.
So, you do as usual :
JFreeChart chart = new JFreeChart(...);
chart.set ... // apply your series and options
MyLegend legend = new MyLegend();
legend.set... // apply your legend options if applicable
chart.setLegend(legend);
That's it.
Result :
I am using JFreeChart to render a stacked area chart. By default, the chart legend is rendered below the plot with the elements laid out horizontally. I would like the legend to appear on the right of the plot with the elements laid out as a vertical list.
Is this possible and, if so, how do I do it?
A little more time examining the API would have given me the answer:
LegendTitle legend = chart.getLegend();
legend.setPosition(RectangleEdge.RIGHT);
Here is the equivalent for older versions:
StandardLegend legend = new StandardLegend();
legend.setPreferredWidth(100);
legend.setAnchor(Legend.EAST);
jfreechart.setLegend(legend);