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);
Related
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 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).
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 created a line chart:
But I want it to make it looks like this. I also don't know what kind of chart is this.
I want to make also have a shadow and circle nodes in it. Just like this:
How can I do this? By the way I'm displaying the chart in the webpage as PNG image format if it is relevant to my question. Thanks in advance.
For info, the sample chart you are trying to replicate is included in the JFreeChart demo collection. The complete source code for the demos is included with the JFreeChart Developer Guide. You could save yourself some time and your company some money by asking them to buy the JFreeChart Developer Guide, it's not so expensive. On to the answer...
The shadow effect you are looking for can be added to any CategoryPlot or XYPlot by setting the shadow generator:
plot.setShadowGenerator(new DefaultShadowGenerator());
It looks nice, but be aware that it requires rendering the chart as a bitmap so it won't play so nicely if you are exporting your charts to SVG or PDF or other vector formats.
The shapes on the lines can be added by changing attributes on the renderer you are using (LineAndShapeRenderer in this case).
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
The setBaseShapesVisible() method sets the default (or 'base') flag value for all series. You can override that default on a per-series basis if you want to. You may also want to tweak the colors being used...by default all the shapes are drawn and filled using the series color, but there are flags that can be set to make the renderer use the series fill and series outline colors (which is done in the example to get the white fill in the shapes).
The JFreeChart renderers are very configurable, so I suggest you spend some time looking through the API documentation to see what is possible.
Here's your solution:
LineAndShapeRenderer renderer
= (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
renderer.setSeriesStroke(0, new BasicStroke(3.0f));
renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
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);