How can I remove the y labels from a JFreeChart chart? I use a NumberAxis for my y axis.
I can't seem to find a simple method for this anywhere.
I would like something similar to the remove legend syntax:
// Remove the legend
chart.removeLegend();
Note that I do want to define the title in the NumberAxis:
NumberAxis axis1 = new NumberAxis("A random title");
I simply don't want it to show up in the final chart.
I think that you mean that you want to hide the tick labels for the Y axis, but still want to see the label for the axis itself. Am I correct?
You can do that with:
axis1.setTickLabelsVisible(false);
Okay, if you want to:
hide the label in the chart
but still have it in the NumberAxis
Then there is one solution, that isn't perfect either, that you could use. If you set the "attributed label" (a label with extra font markup attributes), it will draw the attributed label instead.
You can set it to a single space (a zero-length string doesn't work - the font rendering code doesn't allow that).
rangeAxis.setAttributedLabel(" ");
At least axis1.getLabel() will still return your old label, but that's the only benefit of this that I can see.
Otherwise, you can subclass NumberAxis and override the method drawLabel in the subclass to do nothing:
protected AxisState drawLabel(String label, Graphics2D g2,
Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge,
AxisState state) {
return state;
}
My best solution so far is:
axis1.setLabel(null);
But this is just overwriting the original label (so not really a good solution).
Related
I am trying to make an XYChart that is scrollable (actually pannable, but since ScrollPane offers a setPannable(boolean pannable) function, they are really equivalent). The type of XYChart I am using is a candle-stick chart, that is essentially identical to the code released by rterp # Github which is available here:
StockChartsFX
Which is itself seemingly derived mostly from Ensemble8, I discovered.
As it was originally setup:
CandleStickChart extends XYChart extends Chart
but then I realized that XYChart does not expose enough data to be able to really mess around with how the chart functions, so I created a ScrollableXYChart class and, since I had already gone that far, decided to create a MultiChart class as well just in case I needed to go up to that level (it is not only scrolling that I am implementing, I am also implementing toolbars and other such things, but do not need assistance with that at the moment). ScrollableXYChart and MultiChart were just copy-pasted from XYChart and Chart respectively (from Java 8u40 ea 23).
In order to make the chart content scrollable, I did the following:
private ScrollPane plotAreaScrollPane = new ScrollPane();
public ScrollableXYChart(Axis<X> xAxis, Axis<Y> yAxis)
{
...
plotAreaScrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
plotAreaScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
plotAreaScrollPane.setPannable(true);
plotAreaScrollPane.setPrefSize(800, 600);
plotAreaScrollPane.setContent(plotArea);
getChartChildren().addAll(plotBackground, plotAreaScrollPane, xAxis, yAxis);
...
}
After doing that the chart content was inside a ScrollPane and the width looked correctly scaled to the chart, the only problem is that, when scrolling to the right, candles are not rendered. I noticed that resizing the window of the application will force more candles to be drawn, so I thought that maybe if I set the width inside of layoutChartChildren(...) that would force more candles to be drawn. It does appear to do that, but doing so makes the ScrollPane unscrollable (the horizontal thumb of the scrollbar becomes the full width of the bar).
After looking into the layoutPlotChildren(...) method of the CandleStickChart I realized that it was iterating over the data like so:
Iterator<Data<String, Number>> iter = getDisplayedDataIterator(series);
while (iter.hasNext())
{
// draw candle
}
So just in case that was cutting off the number of candles drawn, I replaced it with:
int candleIndex = 0;
for (Data<String, Number> datum: getData().get(seriesIndex).getData())
{
// draw candle
}
But that did not change anything.
Here are some screenshots (and yes there is more data than is being shown.. I tried with 5000+ candles) to be sure:
Thanks very much.
Update:
After some more thought and research, I changed the CandleStickChart from an XYChart<String, Number> to an XYChart<Number, Number> Doing so allowed me to set the bounds of the x-axis, via setLowerBound(...) and setUpperBound(...) methods in ValueAxis<Number>. With access to these new methods, I atttempted to add the following change listener on the hvalueProperty of the ScrollPane:
private double lastHoffset = 0d;
...
plotAreaScrollPane.hvalueProperty().addListener((observableValue, oldValue, newValue) -> {
double hmin = plotAreaScrollPane.getHmin();
double hmax = plotAreaScrollPane.getHmax();
double hvalue = plotAreaScrollPane.getHvalue();
double contentWidth = plotAreaScrollPane.getContent().getLayoutBounds().getWidth();
double viewportWidth = plotAreaScrollPane.getViewportBounds().getWidth();
double hoffset = Math.max(0, contentWidth - viewportWidth) * (hvalue - hmin) / (hmax - hmin);
double dHoffset = hoffset - lastHoffset;
lastHoffset = hoffset;
xAxis.setAutoRanging(false);
xAxis.setAnimated(false);
getXAxis().setLowerBound(getxAxis().getLowerBound() + hoffset);
getXAxis().setUpperBound(getxAxis().getUpperBound() + hoffset);
});
Hopeful, I tried seeing what happened. However, no (visible) change.
Update:
I was able to get it working by tinkering with almost every possible setting...I still am not quite sure what combination of settings made it work, but it involved commenting out plotArea.setClip(plotAreaClip); and setting:
plotArea.setAutoSizeChildren(false);
plotContent.setAutoSizeChildren(false);
to:
plotArea.setAutoSizeChildren(true);
plotContent.setAutoSizeChildren(true);
and
plotContent.setManaged(false);
plotArea.setManaged(false);
to:
plotContent.setManaged(true);
plotArea.setManaged(true);
Somehow this makes it work. Someone much smarter than I am could probably explain why, and which settings are actually truly necessary, and what are the (potentially negative) implications of changing these settings, as I am sure the author put them there for good reasons.
This is a simple UI I made to learn the charts API in JavaFX. The AreaChart looks great however, I was wondering if it is possible to hide the tiny dots that signify the plotted values ?
The reason is that as the dots come closer, when the value of X axis increases, they become smaller and harder to comprehend. Sometimes they overlap. In such a situation, the graph would be more legible without the dots.
There is a method to hide the dots (or any other graphical representation of a x/y point).
final LineChart<Number,Number> lineChart =
new LineChart<Number,Number>(xAxis,yAxis);
//here be code...
lineChart.setCreateSymbols(false); //hide dots
Call setNode(...) on the XYChart.Data objects and pass in something invisible.
For example:
XYChart.Data data = new XYChart.Data(x,y);
Rectangle rect = new Rectangle(0, 0);
rect.setVisible(false);
data.setNode(rect);
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);
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);