Help needed getting setItemMargin(0.0) to work ........

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.

Help needed getting setItemMargin(0.0) to work ........

Post by rssx » Thu Dec 06, 2012 2:43 am

Ok so please tell me why this does not work??? I used the setItemMargin(0.0); // to eliminate the gap between the barcharts. Here is my code - Why does this stop working because of the lines in red:

And while we are discussing it what exactly does IntervalMarker do. Note I can not find it in the Jfree Chart guide. I have tried and I can not seem to find any intervalmarker in the guide at all.
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();


final IntervalMarker target = new IntervalMarker(400.0, 700.0);
//plot.getDomainAxis().setLowerMargin(2.0); //
//plot.getDomainAxis().setUpperMargin(2.0); / / higher the value Shrinks distance between bars
BarRenderer xyrend =(BarRenderer) plot.getRenderer();
xyrend.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);
return chart;
}

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

Re: Help needed getting setItemMargin(0.0) to work ........

Post by rssx » Thu Dec 06, 2012 2:50 am

Please help me out here?? The Chart guide has let me down!!!

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Help needed getting setItemMargin(0.0) to work ........

Post by matinh » Thu Dec 06, 2012 4:54 pm

What exactly is your problem? "Stopped working" is not very helpful.
Regarding the IntervalMarker: run the demo and have a look at the examples in Miscellaneous/Markers.

- martin

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

Re: Help needed getting setItemMargin(0.0) to work ........

Post by rssx » Fri Dec 07, 2012 4:24 pm

Okay fair enough. I was tired in my defense!
BarRenderer xyrend =(BarRenderer) plot.getRenderer();
xyrend.setItemMargin(0.0);

It appears that the BarRenderer object is not what is returned rather it is XYItemRenderer that should be returned without casting. So the wrong object was returned causing a null execption
Okay here is what I got:
Caused by: java.lang.ClassCastException: org.jfree.chart.renderer.xy.XYBarRenderer cannot be cast to org.jfree.chart.renderer.category.BarRenderer
at com.mycompany.portfolio.server.InstrumentChartUtils.createBarChart(InstrumentChartUtils.java:442)
at com.mycompany.portfolio.server.InstrumentChartUtils.prepareChartPath(InstrumentChartUtils.java:267)
at com.mycompany.portfolio.server.StockCatalogServiceImpl.getChart(StockCatalogServiceImpl.java:586)
at com.mycompany.portfolio.server.StockCatalogServiceImpl.getCharts(StockCatalogServiceImpl.java:557)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)

But it seems the only way for me to accomplish my goal of decreasing the gaps in between bars is to set this value and it can only be set it seems by this object
Further research shows the following
I start with a working XYPlot object which supports ImageMaps I am still a bit unsure what the IntervalMarker statement is for and why I can not find it listed in the JFreeChart guide I purchased most recently.
So the hierachy of objects appears to be :
Plot -
XYPlot
-CombinedDomainXYPlot
-CombinedRangeXYPlot

I am not sure if zoom might not be a useful function to lessen the gaps between the bars but it is a method available in XYPLot
The properties contained in XYPlot include:
XYItemRenderer - This is the natural return object of the getRenderer() method
- Again no setItemMargin method there is however I now see setMargin which I will now try but this was not discussed in the forum as a substitute and I take these facts very specifically given my experiences with the
JFreeChart package.
Still how does one get from Interface returned as XYItemRender to BarRenderer ??? It would appear that BarRenderer is the child of an abstract class : AbstractCategoryItemRenderer which is the child of AbstractRenderer
So now I need to starting with XYPlot go from the natural Interface object returned which presumably becomes XYBarRenderer object due to the fact createXYBarChart(..) method was called.

So now starting with XYBarRenderer how do I get a BarRenderer object or perhaps there is a way to call another method within XYBarRenderer or within one of its properties contained to set this ItemMargin value to reduce gap space between bars.

Note:
XYBarRenderer xyrend = (XYBarRenderer) plot.getRenderer();
xyrend.setMargin(0.0);

So as I see it either there is another way to go from XYBarRenderer object to a BarRenderer object so I can call the .setItemMargin(0.0); or there is another way. Any help would be appreciated here???
Last edited by rssx on Mon Dec 24, 2012 5:50 pm, edited 1 time in total.

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: Help needed getting setItemMargin(0.0) to work ........

Post by remiohead » Fri Dec 07, 2012 7:18 pm

You're confusing classes for drawing Category charts (BarRenderer) with classes for drawing XY charts (XYBarRenderer). They're in different packages for a reason. Since you appear to be using XYBarRenderer you can ignore BarRenderer since it is not relevant.

I've never used XYBarRenderer but it has a margin argument that probably does what you want.

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

Re: Help needed getting setItemMargin(0.0) to work ........

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

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

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Help needed getting setItemMargin(0.0) to work ........

Post by paradoxoff » Sat Dec 08, 2012 2:12 pm

In all of your posts you have never mentioned what data you have in your dataset.
The reason why there is no simple way to set the width of the bars in an XYBarChart is that the width of the bars is determined by the data in the dataset. Each bar will by default start at intervalDataset.getStartXValue(series, item) and end at intervalDataset.getEndXValue(series, item). This space can be shrinked by calling setMargin() on the renderer, where the margin is actually a value between 0 and 1 corresponding to 0 to 100 % shrinkage.
All the other things that you have shown in your other threads just appear to be workarounds. Note that I haven´t read them very carefully, so I might be wrong.
One last thing: instead of manually coloring your posts, you should use

Code: Select all

code tags

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

Re: Help needed getting setItemMargin(0.0) to work ........

Post by rssx » Tue Dec 11, 2012 1:14 am

Yes but I needed to adjust them to a wider value per plotting. What I have now which uses the XYBarPainter or GradientXYBarPainter over ridden methods.
I currently have what I want basically except for the fact that 3D effect I had before does not show up now which is my next problem to solve. Too bad there are no good tutorials with GradientXYBarPainter
and How to render 3D JFree Barcharts with detailed control.

Here is what I have done in 2D:


GradientXYBarPainter painterG =new GradientXYBarPainter() { //0.40, 0.40, 0.20) {
@Override
public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base, boolean pegShadow) {
System.out.println("wSHADDOW="+ bar.getWidth()+",H="+bar.getHeight());

}

@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());

renderer.setShadowVisible(false);
// renderer.setSeriesPaint(0, gp0);
// renderer.setSeriesPaint(1, gp1);
// renderer.setSeriesPaint(2, gp2);

//renderer.setShadowXOffset(0);
// renderer.setShadowYOffset(0);
//renderer.setDrawBarOutline(true);
//renderer.setD
//renderer.setBaseOutlinePaint(Color.black);
//g2.setBackground(Color.GREEN);
g2.setColor(Color.red);
//g2.setColor(GradientPaintTransformType.CENTER);
//g2.fill(bar);
g2.fill3DRect((int) bar.getX(), (int) bar.getY(),(int) (bar.getWidth()+barWidthAdjust),(int) bar.getHeight(), true);
g2.draw3DRect((int) bar.getX(), (int) bar.getY(),(int) (bar.getWidth()+barWidthAdjust),(int) bar.getHeight(),true);
//g2.draw(bar);

}

};

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("wSHADDOW="+ bar.getWidth()+",H="+bar.getHeight());

}

@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.red.darker());
g2.fill(bar);
g2.draw(bar);

}
xyrend.setBarPainter(painterG); or xyrend.setBarPainter(painter);
plot.setRenderer(xyrend);

Now that expands each bar additional barWidthAdjust distance to the right, however, the 3D rendering is no longer working. Any ideas how I can fix this??

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

Re: Help needed getting setItemMargin(0.0) to work ........

Post by rssx » Tue Dec 11, 2012 1:15 am

Yes but I needed to adjust them to a wider value per plotting. What I have now which uses the XYBarPainter or GradientXYBarPainter over ridden methods.
I currently have what I want basically except for the fact that 3D effect I had before does not show up now which is my next problem to solve. Too bad there are no good tutorials with GradientXYBarPainter
and How to render 3D JFree Barcharts with detailed control.

Here is what I have done in 2D:


GradientXYBarPainter painterG =new GradientXYBarPainter() { //0.40, 0.40, 0.20) {
@Override
public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base, boolean pegShadow) {
System.out.println("wSHADDOW="+ bar.getWidth()+",H="+bar.getHeight());

}

@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());

renderer.setShadowVisible(false);
// renderer.setSeriesPaint(0, gp0);
// renderer.setSeriesPaint(1, gp1);
// renderer.setSeriesPaint(2, gp2);

//renderer.setShadowXOffset(0);
// renderer.setShadowYOffset(0);
//renderer.setDrawBarOutline(true);
//renderer.setD
//renderer.setBaseOutlinePaint(Color.black);
//g2.setBackground(Color.GREEN);
g2.setColor(Color.red);
//g2.setColor(GradientPaintTransformType.CENTER);
//g2.fill(bar);
g2.fill3DRect((int) bar.getX(), (int) bar.getY(),(int) (bar.getWidth()+barWidthAdjust),(int) bar.getHeight(), true);
g2.draw3DRect((int) bar.getX(), (int) bar.getY(),(int) (bar.getWidth()+barWidthAdjust),(int) bar.getHeight(),true);
//g2.draw(bar);

}

};

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("wSHADDOW="+ bar.getWidth()+",H="+bar.getHeight());

}

@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.red.darker());
g2.fill(bar);
g2.draw(bar);

}
xyrend.setBarPainter(painterG); or xyrend.setBarPainter(painter);
plot.setRenderer(xyrend);

Now that expands each bar additional barWidthAdjust distance to the right, however, the 3D rendering is no longer working. Any ideas how I can fix this??

Locked