Repository navigation
Replies: 2 comments
|
You might look at adjusting the |
|
So I repeatedly played with width, height, number of bars, ... It's possible to get the perfect combination but that only works for that combination. As soon as you change something, like the width of the chart or number of bars, you have to start all over again. It's basically not doable if you have to make your chart fit somewhere and you want to change things later. So I cheated and created two new classes to deal with this problem, a new type of category axis and a new type of stacked bar renderer: public class PixelCategoryAxis extends CategoryAxis
{
public PixelCategoryAxis(final String name)
{
super(name);
}
@Override
public double getCategoryStart(int category, int categoryCount, Rectangle2D area, RectangleEdge edge)
{
return Math.floor(
super.getCategoryStart(category, categoryCount, area, edge)
);
}
@Override
protected double calculateCategoryGapSize(int categoryCount, Rectangle2D area, RectangleEdge edge)
{
return Math.ceil(
super.calculateCategoryGapSize(categoryCount, area, edge)
);
}
@Override
protected double calculateCategorySize(int categoryCount, Rectangle2D area, RectangleEdge edge)
{
return Math.floor(
super.calculateCategorySize(categoryCount, area, edge)
);
}
}
public class PixelBarRenderer extends StackedBarRenderer
{
@Override
protected void calculateBarWidth(CategoryPlot plot, Rectangle2D dataArea, int rendererIndex, CategoryItemRendererState state)
{
super.calculateBarWidth(plot, dataArea, rendererIndex, state);
state.setBarWidth(Math.floor(state.getBarWidth()));
}
}Then tied them in as follows: plot.setRenderer(new PixelBarRenderer());
plot.setDomainAxis(new PixelCategoryAxis(""));Now the bars are the same width all the time and the separation is always the same. It's not perfect - for example the right margin of the X axis is not exact anymore (might be off a pixel or two), but that's less of a problem, hardly noticeable compared to differing bar widths/bar gaps. |
Uh oh!
There was an error while loading. Please reload this page.
Hello
I'm trying to achieve a pixel perfect bar chart (no anti-aliasing effects, etc...). However with my current code, at some chart widths, the bars are not always equal size, for example:
Notice how bars D and E are 22 pixels wide, whilst the rest are 21 pixels wide. The spacing between the bars is also inconsistent. How can I prevent this from happening?
All reactions