How to set a margin on a ValueAxis with auto-calculate disabled? - java

I'm using JFreeChart for displaying charts in PDF with Apache PdfBox.
My problem is this: I have a Scatter Plot Chart (image attached) that has fixed lower and upper bound, so auto-calculate is not an option. The chart displays a blue dot with the result. However, if the value of dot is 0 or 2 (edge values), the dot is cut out, so I need to set up a margin in this case. I tried with xAxis.setUpperMargin, but with no luck.
This is part of the code:
NumberAxis xAxis = (NumberAxis) xyPlot.getDomainAxis();
double tickSize = maxValue > 10 ? 1 : 0.5;
xAxis.setTickUnit(new NumberTickUnit(tickSize));
xAxis.setRange(1, maxValue);

As you have observed, a "margin is added only when the axis range is auto-calculated—if you set the axis range manually, the margin is ignored." Alternatively, you can add a suitable margin when manually setting the range. Starting from this example, the following change to adjustAxis() adds a 10% margin to each end of each axis, producing the result shown.
axis.setRange(-1.1, 1.1);

Related

How to set paddings from left and right y-axis in a LineChart

How do I set padding from the left y-axis and right y-axis in line chart as shown in the picture? The data has a range from 1 to n points. The visible x-axis range is a maximum 5 points.
The following should work for you:
mChart.getXAxis().setSpaceMin(5f);
mChart.getAxis().setSpaceMax(5f);
Here's the javadoc for those two methods:
Sets extra spacing for axisMinimum to be added to automatically calculated axisMinimum
and:
Sets extra spacing for axisMaximum to be added to automatically calculated axisMaximum
As you can see, they basically automatically add space/padding on the xAxis to the min and max, which is what we wanted.

Set number of angular grid lines to a fixed value for a PolarPlot with jfreechart

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.

How to add range axis label in a Stacked barchart

I want to know how to add range axis label in a Stacked Bar chart.The below image shows a stacked bar chart.I do not want the range axis line to be displayed.I have been able to remove the range axis line ,but along with it the range axis label i.e Revenue($M) is also removed.
Can anyone please help me with this.
http://i.stack.imgur.com/GyuMZ.png
I want Revenue($M) to be displayed.
Normally you would hide the axis line using the setAxisLineVisible() method:
ValueAxis yAxis = plot.getRangeAxis();
yAxis.setAxisLineVisible(false);
I'm guessing that you did something else if the axis label disappeared also.

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

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

Categories

Resources