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);
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 created a line chart:
But I want it to make it looks like this. I also don't know what kind of chart is this.
I want to make also have a shadow and circle nodes in it. Just like this:
How can I do this? By the way I'm displaying the chart in the webpage as PNG image format if it is relevant to my question. Thanks in advance.
For info, the sample chart you are trying to replicate is included in the JFreeChart demo collection. The complete source code for the demos is included with the JFreeChart Developer Guide. You could save yourself some time and your company some money by asking them to buy the JFreeChart Developer Guide, it's not so expensive. On to the answer...
The shadow effect you are looking for can be added to any CategoryPlot or XYPlot by setting the shadow generator:
plot.setShadowGenerator(new DefaultShadowGenerator());
It looks nice, but be aware that it requires rendering the chart as a bitmap so it won't play so nicely if you are exporting your charts to SVG or PDF or other vector formats.
The shapes on the lines can be added by changing attributes on the renderer you are using (LineAndShapeRenderer in this case).
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
The setBaseShapesVisible() method sets the default (or 'base') flag value for all series. You can override that default on a per-series basis if you want to. You may also want to tweak the colors being used...by default all the shapes are drawn and filled using the series color, but there are flags that can be set to make the renderer use the series fill and series outline colors (which is done in the example to get the white fill in the shapes).
The JFreeChart renderers are very configurable, so I suggest you spend some time looking through the API documentation to see what is possible.
Here's your solution:
LineAndShapeRenderer renderer
= (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
renderer.setSeriesStroke(0, new BasicStroke(3.0f));
renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
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.
I have an XYLineChart, where the labels on the X axis are written horizontally. I would like to be able to write them vertically (descending).
I can already do this for BarCharts:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
but an XYChart returns an XYPlot, rather than a CategoryPlot, and the getDomainAxis() of an XYPlot returns a ValueAxis, not a CategoryAxis. ValueAxis lets me call
setVerticalTickLabels(true);
which is almost there! But it draws them ascending, rather than descending. Any way around this?
Thanks,
Edit: I need the domain axis to stay at the bottom of the chart. Hadn't considered it being any other way when making the original post.
ValueAxis does this automatically in drawTickMarksAndLabels() for an axis on the RectangleEdge.TOP edge:
xyPlot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
Example based on a variation of ScatterAdd.
Answering my own question, this didn't seem to be possible, so I had to add the functionality to the jfreechart source myself.
I'm using an XYPlot in JFreeChart. All the lines on it are XYSeries objects. Both axes are NumberAxis objects. The Y-Axis range is from 0-1, with ticks every .1. Along with displaying the numbers though, I'd like to display text on the Y-Axis, like High/Medium/Low. High would cover .7-1, etc. What is the best way to go about doing this?
I have some JFreeChart experience, and after a little research I don't have an answer for adding the three labels to the axis.
However, as an alternative approach, you should be able to delineate these three areas on the plot with colors by setting a MarkerAxisBand for the NumberAxis (using this method).
You could then add interval markers to the MarkerAxisBand to highlight the three areas.
try this ... it can give similare result
JFreeChart Text Annotations not Working?
XYTextAnnotation textAnnotaion = new XYTextAnnotation(description, xMid, yMid);
plot.addAnnotation(textAnnotaion);
textAnnotaion.setRotationAngle(90.0);