Is there a simple way to set the width of XYBarChart bars?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rssx
Posts: 17
Joined: Wed Dec 05, 2012 6:49 am
antibot: No, of course not.

Is there a simple way to set the width of XYBarChart bars?

Post by rssx » Fri Dec 07, 2012 7:07 pm

Hello
I found what I believe to be a possible solution except my domain scale is a percent and the code involved setting up the following:

Note the main points of interest:
setBaseToolTipGenerator - sets up a call back method to handle a toolTipGenerator which is probably irrelevant to the question at hand I guess this allows interactive displays based on rollover effects etc..?? Nothing to do with bar width???

Then there is a XYBarPainter object set up and the assigned to the XYBarRenderer object via the method setBarPainter( and specifes the XYBarPainter defined)

I was of the impression I could just set the bar size somewhere Is that not possible. Must I resort to a custom Painter to change the size of the bars displayed???
Is there a better solution to this??



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;
}

rssx
Posts: 17
Joined: Wed Dec 05, 2012 6:49 am
antibot: No, of course not.

Re: Is there a simple way to set the width of XYBarChart bar

Post by rssx » Fri Dec 07, 2012 10:13 pm

I tried the following to expand the bar widths but lost my gradient 3D effect. Any way to implement the default 3D behavior in red??

Here is the code used thus far:
Yes I see that. Actually all I wanted to do really as it turned out was expand the width of each bar on the Domain access

I tried the following but lost the 3d effect I had prior to doing this:


private static JFreeChart createBarChart(String title, String legendX, String legendY, IntervalXYDataset dataset){

final JFreeChart chart = ChartFactory.createXYBarChart(
title,
legendX,
false,
legendY,
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
XYPlot plot = (XYPlot) chart.getPlot();

//XYItemRenderer tmpuu;
final IntervalMarker target = new IntervalMarker(400.0, 700.0);
//final IntervalMarker target = new IntervalMarker(400.0, 1000.0);
// IntervalMarker tx=new IntervalMarker
//plot.getDomainAxis().setLowerMargin(2.0); //
//plot.getDomainAxis().setUpperMargin(2.0); / / higher the value Shrinks distance between bars
System.out.println(plot.getRendererCount());
//plot.getRen

XYBarRenderer xyrend = (XYBarRenderer) plot.getRenderer();
//xyrend.setMargin(0.10);
plot.setOutlineVisible(true);
plot.setOutlinePaint(Color.black);

XYBarPainter painter = new XYBarPainter() {

@Override
public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base, boolean pegShadow) {
System.out.println("w="+ bar.getWidth()+",H="+bar.getHeight());
bar.setFrame(bar.getX(), bar.getY(), bar.getWidth() + barWidthAdjust+4, bar.getHeight());

g2.setBackground(Color.black);
//g2.setColor(Color.green.darker());
g2.fill(bar);
g2.draw(bar);

}

@Override
public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) {
System.out.println("w="+ bar.getWidth()+",H="+bar.getHeight());
bar.setFrame(bar.getX(), bar.getY(), bar.getWidth() + barWidthAdjust, bar.getHeight());

g2.setBackground(Color.GREEN);
g2.setColor(Color.green.darker());
g2.fill(bar);
g2.draw(bar);

}
};



//plot.getDomainAxis().setTickMarkInsideLength(400.0f); -- Grid line length not what we want
//plot.getDomainAxis().setLowerMargin(0.0001);
//plot.getDomainAxis().setUpperMargin(4.0);
//xyrend.setBarAlignmentFactor(DEFAULT_CHART_WIDTH*40);
//xyrend.set
//XYItemRenderer xyir;
//BarRenderer xyrend2 =(BarRenderer) plot.getRenderer();
//xyrend2.setMinimumBarLength(100);
//xyir.setI
//xyrend2.setItemMargin(0.0);
//Renderer rnn =plot.getRenderer();


target.setLabel("Target Range");
target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11));
target.setLabelAnchor(RectangleAnchor.LEFT);
target.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
target.setPaint(new Color(222, 222, 255, 128));
plot.addRangeMarker(target, Layer.BACKGROUND);
xyrend.setBarPainter(painter);
plot.setRenderer(xyrend);
return chart;
}

rssx
Posts: 17
Joined: Wed Dec 05, 2012 6:49 am
antibot: No, of course not.

Re: Is there a simple way to set the width of XYBarChart bar

Post by rssx » Sat Dec 08, 2012 1:56 am

I made some more progress. It seems I can change the width of my bars through the creation of XYBarPainter although it does not seem to be the most straightforward method and it stops the 3d look in the bar rendering.
Now I tried to use the XYGradientXYBarPainter which seems much harder to use although it has similar methods e.g paintBar.

Thus far I have not found a lot of documentation on this . Even the JFreeChart guide has sketchy at best documentation on using this Object.

Does anyone have any sample code on this???

Locked