Customise Bar Chart to pain images instead of a Bar

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
muks
Posts: 3
Joined: Mon Jun 27, 2016 5:45 pm
antibot: No, of course not.

Customise Bar Chart to pain images instead of a Bar

Post by muks » Wed Jun 29, 2016 5:56 pm

Hello,

I am trying develop a bar chart which paints images such as discs instead of a bar as shown in the following image. I am able to display the icons on the axis, its the actual bar I am struggling with.

Image

I also posted this question on stackoverflow, the url is

http://stackoverflow.com/questions/3805 ... jfreechart

I have tried applying a new painter to the renderer as show below, but it's still painting the normal bar.

Code: Select all

renderer.setBarPainter(new StandardBarPainter()
    {
    	public void paintBar(Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) 
    	{	
	      Paint itemPaint = renderer.getItemPaint(row, column);
	      GradientPaintTransformer t = renderer.getGradientPaintTransformer();
	      if (t != null && itemPaint instanceof GradientPaint) {
	          itemPaint = t.transform((GradientPaint) itemPaint, bar);
	      }
	      
	      ImageIcon img = new ImageIcon(MoodGraphUtil.class.getResource("/com/engage/utils/disc.png"));
	      g2.drawImage(img.getImage(), bar.getBounds().x, bar.getBounds().x, img.getImageObserver());
	      
    	}
    	
    	 public boolean equals(Object obj) {
             return true;
    	 }
    });
To be honest I am not sure how to achieve this, and I couldn't find resources on the net to help.

Any help will be much appreciated.

Kind regards, Mukhtar.

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

Re: Customise Bar Chart to pain images instead of a Bar

Post by paradoxoff » Thu Jun 30, 2016 11:46 am

Does this work as expected? All bars should be drawn in light green.

Code: Select all

public class BarChart1 {

    public static void main(String[] args) {
        DefaultCategoryDataset dcd = new DefaultCategoryDataset();
        dcd.addValue(5, "Row 1", "Col 1");
        dcd.addValue(10, "Row 1", "Col 2");
        dcd.addValue(2, "Row 2", "Col 1");
        dcd.addValue(7, "Row 2", "Col 2");
        BarRenderer br = new BarRenderer();
        br.setBarPainter(new StandardBarPainter() {
            public void paintBar(Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) {
                g2.setColor(Color.black);
                g2.draw(bar);
                g2.setColor(Color.green);
                g2.fill(bar);
            }

        }
        );
        CategoryPlot plot = new CategoryPlot(dcd, new CategoryAxis("Category"), new NumberAxis("Values"), br);
        JFreeChart chart = new JFreeChart(plot);
        ChartPanel cp = new ChartPanel(chart);
        JFrame f = new JFrame("Bar Chart");

        f.getContentPane().add(cp);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

muks
Posts: 3
Joined: Mon Jun 27, 2016 5:45 pm
antibot: No, of course not.

Re: Customise Bar Chart to pain images instead of a Bar

Post by muks » Thu Jun 30, 2016 12:54 pm

Hi,

Thanks for the reply, I am trying to display the discs instead of the bars as show in the attached image.

Thanks, Mukhtar.

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

Re: Customise Bar Chart to pain images instead of a Bar

Post by paradoxoff » Thu Jun 30, 2016 1:12 pm

I have understood your what you want to achieve.
There are simply too many things that could explain the behaviour you observe:
- The renderer in your snippet isn´t actually used to render your chart
- The image isn´t found, and the programm crashes.
- You are applying a Chart theme after you have created the chart.
- Something else in your development environment .
The goal of my question was to check whether at least a simply program with a custom bar painter works as expected, and then move forward.
Feel free to replace your custom bar painter with mine to check whether my custom bar painter works.

muks
Posts: 3
Joined: Mon Jun 27, 2016 5:45 pm
antibot: No, of course not.

Re: Customise Bar Chart to pain images instead of a Bar

Post by muks » Thu Jun 30, 2016 2:00 pm

Hi,

I understand, the custom painter is being called now. I was using a BarRenderer3D before, upon changing it to BarRenderer the custom bar painter in being called.

I have managed to progress with the issue, in the sense that I now have a good idea how to draw the icons instead of a bar. I need to simply paint the icons instead as shown below:

Code: Select all

renderer.setBarPainter(new StandardBarPainter()
 {
    	@Override
    	public void paintBar(Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) 
    	{	
                ImageIcon img = new ImageIcon(GraphUtil.class.getResource("Path to image");
                g2.drawImage(img.getImage(), bar.getBounds().x, bar.getBounds().y, img.getImageObserver());
        }

       public void paintBarShadow(Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base, boolean pegShadow) {
       }
});

This will now draw the icons instead. I will also need to override the paintBarShadow() and leave it empty so that a shadow is not painted.
Now its a matter of working out how many icons to display and to crop the last icon if it doesn't fit in the bar rectangle, I should be able to manage this now.

Thanks, Mukhtar.

Locked