JFreechart - vertical X-axis labels on an XYChart - java

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.

Related

JFreeChart: how to move Y-axis from left to right hand side?

I use JFreeChart to create a candlestick chart. The horizontal axis is a DateAxis to indicate the time, the vertical axis is a NumberAxis, to indicate the price. As the screenshot shows, is the price axis shown on the left hand side of the chart.
I would like the vertical axis to be displayed on the right hand side of the chart, instead of the left hand side. I have been looking in the class overview for NumberAxis, ValueAxis and Axis classes in JFreeChart but could not find a method which can make this modification.
Can somebody please show me how to make this change?
As shown here, you can use the XYPlot method setRangeAxisLocation() to set the location of the primary range axis. The image below illustrates the effect of the following addition to this example:
chart.getXYPlot().setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);

How to set max label lines for Date Ticks in JFreeChart Timeseries plots

I know how to create a CategoryPlot and then set the DomainAxis labels to use two lines. This is the idea:
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
categoryAxis.setMaximumCategoryLabelLines(2); // Mmmm... nice labels
But I'm having trouble doing "the same thing" for a Timeseries chart. The problem is that the DateAxis is a ValueAxis rather than a CategoryAxis. That makes sense because the dates are values. But I don't like the look of the chart when it uses only a single line for the date. You can see a sample chart in my answer in this thread. I want to format my dates to use 2 lines. But I cannot do it like this:
DateAxis dateAxis = (DateAxis)xyPlot.getDomainAxis();
dateAxis.setMaximumCategoryLabelLines(2); // method does not exist
How can I get those Date labels onto 2 lines?
Using setVerticalTickLabels(), shown here, may alleviate the horizontal crowding; using setDateFormatOverride() may mitigate the resulting vertical space cost.

setting up the same length of ValueAxis with jfreechart

I am plotting several jfree charts in one window and want to align all chart.
All graph have the same range for x-axis and y-axis is displaying in the left.
However, when I am trying to set up fixed length for valuesAxis ,each graph still having different length of valueAxis.
There is extract of my code;
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setFixedDimension(40);
rangeAxis.setFixedAutoRange(40);
For time being I solved it by not displaying it at all
rangeAxis.setVisible(false);
Is these same way to set up the same length for all charts?
Consider using either a CombinedDomainXYPlot or a CombinedRangeXYPlot to display your data; both will ensure the data area is the same size for all their subplots (AFAIK).

JFreeChart: bar overlaps y-axis

I am using JFreeChart to generate bar charts. Everything is working find, but I have one very minor issue: the left side of the bar is overlapping the y-axis. I would prefer to have the bars sticking (but not overlapping) to the axis. If my description is unclear: everything would be perfect if the bars move 1 pixel to the right.
At first I thought the bar's stroke was the problem, but disabling the stroke gives the same problem.
Edit: The image is zoomed in at the problem area. The blue areas are the bars, and I would like the axis (the gray line) to be drawn on top, and not below.
It looks like a CategoryPlot, so you should have a CategoryDomain as the x-axis (Numeric as y) as the plot area so you'll need to set the margins (as a percent of axis) for the CategoryDomain that you've defined as the x-axis. I used this feature in a CombinedRangeCategoryPlot something like this:
CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot(numberAxis);
CategoryAxis domain = new CategoryAxis();
domain.setLowerMargin(0.2);
domain.setUpperMargin(0.2);
plot.add(new CategoryPlot(data, domain, null, renderer));
see documentation: http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/axis/CategoryAxis.html#setUpperMargin(double)
It looks like you may want to invoke setBase() on your BarRenderer. You'll probably have to adjust the value empirically.
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setBase(0.01);

JFreeChart XYPlot with labels

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);

Categories

Resources