JFreeChart - Axis label positioning - java

I want the label of one of my axis to be two words, each aligned to the beginning and end of said axis - I've been doing this by inserting spaces in the Axis label, but it's a crappy solution.
Is there a way to align label text for a JFreeChart chart?
Thanks for any replies!

You can override drawLabel to achieve any desired effect. Invoke getFontMetrics() on the parameter g2 to position the text precisely. The JCommon class TextUtilities, used extensively in JFreeChart, may offer more convenience.

Related

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.

Drawing a range column chart using DOT language or java or similar?

I want to draw a range column chart for my data, something similar to this.
Any suggestion?
GraphViz isn't suitable for this sort of chart - it draws graphs of the "nodes and edges" type, rather than this sort of style.
It looks like the "range column" chart is essentially a stacked bar chart with four elements in each stack, with the bottom element and third element drawn without borders and with a transparent fill. So, I'd suggest trying to use a conventional graphing library that gives enough flexibility of control over bar charts to produce what you want.

Remove an axis from JavaFX Chart

I've created a XYChart with numerical values different (for example temperatue with pressure) so I want to draw my own axeS just beside my chart. To do the following I've to unshow the YAxis, how should I do that ?
By using a trick: The Chart needs the Y Axis to remain in place so it knows where to render your content. You can, however, hide it. Hide the tick labels and set the axis' opacity to 0 using this code:
chart.getYAxis().setTickLabelsVisible(false);
chart.getYAxis().setOpacity(0);
The axis will still be there, but not shown.
I found that if I hid the chart using the following code:
chart.getXAxis().setTickLabelsVisible(false);
chart.getXAxis().setTickMarkVisible(false);
((Path)chart.getXAxis().lookup(".axis-minor-tick-mark")).setVisible(false);
Then I get about ~10 pixels less blank space on the bottom. IF the space was an issue for your application then you could use css offsets to correct it. This solution may have more predictable offsets.
SOLVED: I got this to work for sharing a common x-axis for two charts stacked vertically:
Create two charts, each with their own identical copy of the x-axis object, setting identical upper and lower bounds (optionally by binding).
Then hide the x-axis in the second chart like this:
chart = new LineChart<Number,Number>(xaxis2,yaxis2) {
{// hide xAxis in constructor, since not public
getChartChildren().remove(getXAxis());
// not getPlotChildren()
}
};
You'll want to set the widths of your y-axes to be the identical, e.g.
int w = 60;
yaxis.setMaxWidth(w);
yaxis.setMinWidth(w);
yaxis.setPrefWidth(w);
yaxis2.setMaxWidth(w);
yaxis2.setMinWidth(w);
yaxis2.setPrefWidth(w);

Displaying labels vertically on bars in barCharts in JFreeChart

I was unable to find a way to display the labels (the corresponding values of y axis) vertically. By default it displays horizontally but if values are big it gets overlapped.
Please I am not looking to vertically display the labels of domain axis (x axis) but to display corresponding y values as a label on top of bar horizontally.
BarChartDemo7 among the demos is an example.
Crowding can be a problem, but changing ItemLabelPosition or the ItemLabelAnchor may not solve the problem. As an alternative, consider using a tooltip generator, discussed here. For more complex rendering needs, add a ChartMouseListener, mentioned here, and update an adjacent component with the details.

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

Categories

Resources