Hi All
I am creating a chart with a call to ChartFactory.createXYBarChart(... using an IntervalXYDataset as the data. The chart displays OK except the individual bars only occupy about 20% of the available space between bars.
Can anyone advise, is there a simple method to increase the bar width (or decrease space between bars?).
I can get the XYBarRenderer with the following: XYBarRenderer r = (XYBarRenderer)plot.getRenderer();
But efforts to change the bar width via the renderer have thus far defeated me.
Thanks, db.
XYBarChart - Bar width problem
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: XYBarChart - Bar width problem
The location of the bars is defined by the return values of getStartX(int series, int item) and getEndX(int series, int item) methods. The gap betwen bars that you are seeing is the difference between getStartX(int series, int item) and getEndX(int series, int item) of neighbouring items.
In order to remove the gaps between bars, you could change the contents of your dataset. You haven´t specified what type of dataset you are using. IntervalXYDataset is only the interface.
In order to remove the gaps between bars, you could change the contents of your dataset. You haven´t specified what type of dataset you are using. IntervalXYDataset is only the interface.
Re: XYBarChart - Bar width problem
I have found a way to change the appearance of the Bars:
Code: Select all
public static JFreeChart createTicketChart(String title, String xAxis, String yAxis, final IntervalXYDataset dataset) {
final JFreeChart chart = ChartFactory.createXYBarChart(title, xAxis, true, yAxis, dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(Color.white);
XYPlot plot = chart.getXYPlot();
// customise the range axis...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
final DateAxis dateaxis = (DateAxis) plot.getDomainAxis();
DateTickUnit unit = new DateTickUnit(DateTickUnitType.MONTH, 12);
dateaxis.setTickUnit(unit);
dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
dateaxis.setTickMarksVisible(Boolean.FALSE);
dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
dateaxis.setAutoTickUnitSelection(Boolean.TRUE);
ClusteredXYBarRenderer renderer = new ClusteredXYBarRenderer(1, true);
renderer.setBaseToolTipGenerator(new XYToolTipGenerator() {
@Override
public String generateToolTip(XYDataset dataset, int series, int item) {
Date d = new Date((long) dataset.getXValue(series, item));
SimpleDateFormat sdf = new SimpleDateFormat("MMM yyyy");
String s = "At " + sdf.format(d);
s += String.format(" Items: %.0f", dataset.getY(series, item));
return s;
}
});
renderer.setSeriesPaint(0, Color.green.darker());
XYBarPainter painter = new XYBarPainter() {
@Override
public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base, boolean pegShadow) {
// TODO Auto-generated method stub
}
@Override
public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) {
// TODO Auto-generated method stub
bar.setFrame(bar.getX(), bar.getY(), bar.getWidth() + 8, bar.getHeight());
// g2.setBackground(Color.GREEN);
g2.setColor(Color.green.darker());
g2.fill(bar);
g2.draw(bar);
}
};
renderer.setBarPainter(painter);
plot.setRenderer(renderer);
return chart;
}