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);
Related
I want to get the bounds (i.e. Bounds) of the XYChart plot area, which I define as the area contained within the axes of the plot (i.e. the axes form two legs of a rectangle). The ultimate purpose is to constrain a rubber band rectangle on the plot area. I started with this example and added in the constraint.
Based on the comments from James_D and kleopatra, I created a MVCE. I think the MVCE works remarkably well and compiles without warnings. More importantly, kleopatra's comment led to finding the bug when using the chart-plot-background method. The MVCE implements both methods. The chart-plot-background works for both ValueAxis and CategoryAxis. The axis method currently only works for ValueAxis.
Is using the bounds of .chart-plot-background a reliable method for determining the plot area or is using the value bounds of each axis more reliable? The axis approach definitely has more steps and more code.
Other options
I looked at the JavaFX source and plotArea looked promising but it isn't accessible.
The plotContent member is accessible via lookup(".plot-content") but groups all the data points, some of which may be outside the range set in the axes. Thus, the bounds of plotContent can be larger than the plot area bounds.
Is it possible to generate a bar graph in jFreeChart where bars all start around a particular value instead of the zero axis? Our team has interest in this view.
Here is an example where the default behavior has the bars all between the value and the origin. We want the bars to be between the value and the mean (average).
I suppose a workaround is we calculate the offset from the mean and plot them in respect to the origin and then hide the axis but then we are not able to show the axis to our users.
Instead of having bar graphs stop at another value besides the origin we are just redefining the graph axis origin to be at the mean and offsetting everything. So instead of something like exposure level we now call the value axis distance from aggregate mean. Not exactly what I was originally looking in the OP but a cleaner solution overall. We'll let the user toggle between the two modes.
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.
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);
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);