change the width of the bar in barchart rendered as a .png

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
raghuseeta
Posts: 7
Joined: Sat Aug 04, 2007 1:29 pm

change the width of the bar in barchart rendered as a .png

Post by raghuseeta » Thu Aug 09, 2007 12:31 pm

hi all


I am new to jfree charts . I am using a simple barchart for displaying the values . but iam not able to change the width of the bar . I am using setMaxBarWidth method but it is not causing any change .. can anybody please help me in this regard

greydad
Posts: 3
Joined: Wed Aug 08, 2007 11:52 pm

Post by greydad » Fri Aug 10, 2007 5:44 pm

If you are using the XYBarRenderer for the bar chart, the bar width can be set in the constructor:

XYBarRenderer r = new XYBarRenderer(0.4)

will create bars with 40% width. Check the API for the XYBarRenderer

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: change the width of the bar in barchart rendered as a .p

Post by RichardWest » Fri Aug 10, 2007 6:02 pm

raghuseeta wrote:I am using setMaxBarWidth method but it is not causing any change .. can anybody please help me in this regard
setMaximumBarWidth() sets the maximum bar width to be a fixed percentage of the available plot area--not a physical size for the bars. A common error is to put a value greater than 1.0 (100%) which causes no visible effect on the size of the bars because the default is 1.0. Try setting the maximum width to 0.01 (1%), and you should notice the difference. In contrast, setMinimumBarWidth() takes an absolute pixel value.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

raghuseeta
Posts: 7
Joined: Sat Aug 04, 2007 1:29 pm

Width of bar in a .png

Post by raghuseeta » Mon Aug 13, 2007 11:54 am

Hi,

Thanks for the response.... I am plotting 4 different series with number of bars in each series and I am rendering the chart as a .png of fixed size.
How do I set the width of the individual bars in each series?


[/img][/code]

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Aug 13, 2007 1:31 pm

For XYBarRenderer, the bar width comes from the starting and ending x-values specified in the dataset (IntervalXYDataset). Which implementation of IntervalXYDataset are you using?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

raghuseeta
Posts: 7
Joined: Sat Aug 04, 2007 1:29 pm

Post by raghuseeta » Mon Aug 13, 2007 2:20 pm

Thank you gilbert sir for taking notice of my query.

I am not using XYBarRenderer . I am using BarRenderer.The code snippet what i am using to genereate BarChart is as follows

Code: Select all


                         String category1=  "category1";
			 String category2=  "category2";
			 String category3=  "category3";
			 String category4=  "category4";  
	   DefaultCategoryDataset dataset1 = new   DefaultCategoryDataset();
	
	       for(int k=0; k<list1.size(); k++) {

                           // populate the dataset
        	       
        	              dataset1.addValue( ..seriesvalues.. , category1);
        	      }
	            
	       for(int k=0; k<list2.size(); k++) {

                           // populate the dataset
        	       
        	              dataset1.addValue(..seriesvalues.. ,category2);
        	      }
	            
	           
	           for(int k=0; k<list3.size(); k++) {

                           // populate the dataset
        	       
        	              dataset1.addValue(..seriesvalues.. , category4);
        	      }
	            
	           for(int k=0; k<list4.size(); k++) {

                           // populate the dataset
        	       
        	              dataset1.addValue(..seriesvalues.. , category5);
        	      }
	            

            // each list contains the datas for each series

	        BarRenderer renderer = new BarRenderer();
	        renderer.setSeriesFillPaint(1, Color.black);
	        renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

              //[b] changing the length of the bar [/b]

	        renderer.setMaxBarWidth(0.01);


	        final CategoryPlot plot = new CategoryPlot();
	        plot.setDataset(0,dataset1);
	        plot.setRenderer(0,renderer);
	        NumberAxis numberAxis = new NumberAxis("Scores");
	        numberAxis.setLabelFont(new Font("Arial",Font.PLAIN,13));
	        numberAxis.setTickLabelFont(new Font("Arial",Font.PLAIN,15));
	        numberAxis.setTickUnit(new NumberTickUnit(10.0));
	        numberAxis.setRange(0.0, 100.0);
	        renderer.setItemURLGenerator(new StandardCategoryURLGenerator("","series",""));
	        renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());

	     
	   	       
	        CategoryAxis categoryAxis =  new CategoryAxis("");
	        categoryAxis.setTickLabelFont(new Font("Arial",Font.PLAIN,15));
	        categoryAxis.setTickMarkInsideLength(10.0f);
	        plot.setDomainAxis(categoryAxis);
	        plot.setRangeAxis(numberAxis);

	        plot.setOrientation(PlotOrientation.VERTICAL);
	        plot.setRangeGridlinesVisible(true);
	        plot.setDomainGridlinesVisible(true);
	     
      
	        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
	        plot.setBackgroundPaint(Color.lightGray);
	        plot.setRangeGridlinePaint(Color.white);
	        
	         JFreeChart chart = new JFreeChart(plot);
	        chart.setTitle("ABC Chart");
	        
	
	       LegendItemCollection result = new LegendItemCollection();
	       LegendItem LegendItem1 =   new LegendItem(new AttributedString("Legend1"), "description 0", "tooltip 0", "url 0", new Rectangle2D.Double(-4.0,-4.0,8.0,8.0),Color.red);
	     
	       LegendItem LegendItem2 = new LegendItem(new AttributedString("Legend2"), "description 1", "tooltip 1", "url 1", new Rectangle2D.Double(-4.0,-4.0,8.0,8.0),Color.yellow);
	       LegendItem LegendItem3 = new LegendItem(new AttributedString("Legend3"), "description 2", "tooltip 1", "url 1", new Rectangle2D.Double(-4.0,-4.0,8.0,8.0),Color.blue);
	       LegendItem LegendItem4 = new LegendItem(new AttributedString("Legend4"), "description 3", "tooltip 1", "url 1", new Rectangle2D.Double(-4.0,-4.0,8.0,8.0),Color.green);
	     
           
	        result.add(LegendItem1);
	        result.add(LegendItem2);
	        result.add(LegendItem3);
	        result.add(LegendItem4);
		     plot.setFixedLegendItems(result);
	        chart.setBackgroundPaint(Color.white);
	      
	          
	   
	    ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
	    filename = ServletUtilities.saveChartAsPNG(chart, 800, 286, info, session);

I am using the above code in my bean class which generates the png image and getting displayed thru a servlet ... even though i am trying to change the width of each individual bar giving several combinations of values from 0.01 to 1.0 to renderer.setMaxBarWidth(widthvalue) it is not taking effect in the .png image getting displayed in the jsp.. please help me out with this..

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Aug 13, 2007 2:24 pm

With the regular BarRenderer, you don't get to specify the bar width. JFreeChart calculates the bar width according to the number of bars to be displayed, the plot width, the lower and upper margins on the axis, the margin between categories and the margin between items within each category.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

frankmt
Posts: 2
Joined: Tue Aug 21, 2007 7:39 pm

Post by frankmt » Tue Aug 21, 2007 7:58 pm

I am having a similar problem. I have too many columns in a bar chart, and they get to small in the final chart.

If the column´s width is calculated as you say, how could I change the size of the plot, then?

Thanks
Francisco

Locked