JFreeChart difficulties

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lion_wine
Posts: 3
Joined: Tue Sep 16, 2014 9:47 pm
antibot: No, of course not.

JFreeChart difficulties

Post by lion_wine » Tue Sep 16, 2014 9:53 pm

Hello,

I'm a student and in order to finish a JAVA project, I need to use in JFreeChart with arrays.

Code: Select all

    private  PieDataset createDataset() {
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        String one = "one";
        for (int i =0;i<n;i++){
        pieDataset.setValue(input[i][0], input[i][1]);
        }
        return pieDataset;
}
But Java says "The method setValue(Comparable, Number) is ambiguous for the type DefaultPieDataset".

How to avoid this? May someone help me?

Best regards,

Vadim.

lion_wine
Posts: 3
Joined: Tue Sep 16, 2014 9:47 pm
antibot: No, of course not.

Re: JFreeChart difficulties

Post by lion_wine » Wed Sep 17, 2014 11:08 am

HELP PLEASE!

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

Re: JFreeChart difficulties

Post by david.gilbert » Wed Sep 17, 2014 8:07 pm

You should post a complete example...in the code you've posted, we can't see the declaration for 'input' or 'n', and 'one' seems unused.

Anyway, I suspect the issue is that there are methods setValue(Comparable, Number) and setValue(Comparable, double) in the DefaultPieDataset class and the compiler is having trouble determining which one to use. I forget the exact circumstances where that happens, which is why a complete example would be helpful.
David Gilbert
JFreeChart Project Leader

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

lion_wine
Posts: 3
Joined: Tue Sep 16, 2014 9:47 pm
antibot: No, of course not.

Re: JFreeChart difficulties

Post by lion_wine » Wed Sep 17, 2014 10:41 pm

Code is

Code: Select all


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import com.orsoncharts.plot.PiePlot3D;

public class Graphs2 extends JFrame {
    
    private static final int SIZE = 2;
    
    private final SpringLayout l = new SpringLayout();
    
    JPanel newGF2Panel, newGF3Panel;
    ChartPanel newKDPanel;
	
	Scanner sc;
	public static int n, sum, temp;
    public static int input[][];
    public static int norm_input[][];
    public static int max = Integer.MIN_VALUE;


    public Graphs2() {
        super("GRAPHS");
        setLayout(l);
        setSize(1050, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
/////////////////////////////////////////////////////////////////////////////////////////////////////////
        JMenuBar menuBar = new JMenuBar();
        JMenu mainMenu = new JMenu("Menu");
        JMenu newMenu = new JMenu("Set");
        mainMenu.add(newMenu);
 
        JMenuItem GF2Item = new JMenuItem("GF2");
        newMenu.add(GF2Item);
        GF2Item.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent evt) {
            	addGF2();
            }
        });
        
        JMenuItem GF3Item = new JMenuItem("GF3");
        newMenu.add(GF3Item);
        GF3Item.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent evt) {
            	addGF3();
            }
        });
        
        JMenuItem KDItem = new JMenuItem("KD");
        newMenu.add(KDItem);
        KDItem.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent evt) {
            	addKD();
            }
        });

        mainMenu.addSeparator();
         
        JMenuItem exitItem = new JMenuItem("Exit");
        mainMenu.add(exitItem);
        exitItem.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent e) {
                System.exit(0);             
            }           
        });
        
        menuBar.add(mainMenu);                 
        setJMenuBar(menuBar);
    }
   
    public final void addGF2() {
		try
		{	
			sc = new Scanner(new File("input.txt"));  
			n = sc.nextInt();
			input= new int[n][2];
			for (int i = 0; i < n; i++)
				{
					for (int j = 0; j < 2; j++)
					{   
						if(sc.hasNextInt())
							input[i][j] = sc.nextInt();
					}
				}
			sc.close();
		}
		catch (FileNotFoundException e)
			{
				System.out.println("Sorry, File not found!");
			}
		catch (InputMismatchException e)
			{
				System.out.println("Sorry, InputMismatchException");
			}
		
        newGF2Panel = new GF2();    			
        newGF2Panel.setPreferredSize(new Dimension(300,300)); 
        add(newGF2Panel);
        l.putConstraint(SpringLayout.WEST, newGF2Panel, 20, SpringLayout.WEST, this);
        l.putConstraint(SpringLayout.NORTH, newGF2Panel, 20, SpringLayout.NORTH, this);
        newGF2Panel.updateUI();
    }
    public final void addGF3() {
 		try
 		{	
 			sc = new Scanner(new File("input.txt"));   
 			n = sc.nextInt();
 			input= new int[n][2];
 			for (int i = 0; i < n; i++)
 				{
 					for (int j = 0; j < 2; j++)
 					{   
 						if(sc.hasNextInt())
 							input[i][j] = sc.nextInt();
 					}
 				}
 			sc.close();
 		}
 		catch (FileNotFoundException e)
 			{
 				System.out.println("Sorry, File not found!");
 			}
 		catch (InputMismatchException e)
 			{
 				System.out.println("Sorry, InputMismatchException");
 			}
 		
        for (int i = 0; i < n; i++) {     //поиск максимума
        		if(max<input[i][1])
        			max = input[i][1];	
        }
        norm_input= new int[n][2];
        for (int i = 0; i < n; i++){
				for (int j = 0; j < 2; j++)
				{   
					norm_input[i][j]=input[i][j];
				}
		}
        for (int i = 0; i < n; i++){  
					norm_input[i][1]=input[i][1]*100/max;
		}
 		
         newGF3Panel = new GF3();    			
         newGF3Panel.setPreferredSize(new Dimension(300,300)); 
         add(newGF3Panel);
         l.putConstraint(SpringLayout.WEST, newGF3Panel, 340, SpringLayout.WEST, this);
         l.putConstraint(SpringLayout.NORTH, newGF3Panel, 20, SpringLayout.NORTH, this);
         newGF3Panel.updateUI();
     }
    public final void addKD() {
 		try
 		{	
 			sc = new Scanner(new File("input.txt"));   
 			n = sc.nextInt();
 			input= new int[n][2];
 			for (int i = 0; i < n; i++)
 				{
 					for (int j = 0; j < 2; j++)
 					{   
 						if(sc.hasNextInt())
 							input[i][j] = sc.nextInt();
 					}
 				}
 			sc.close();
 		}
 		catch (FileNotFoundException e)
 			{
 				System.out.println("Sorry, File not found!");
 			}
 		catch (InputMismatchException e)
 			{
 				System.out.println("Sorry, InputMismatchException");
 			}
 		
 		PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, "KD");
 
        newKDPanel = new ChartPanel(chart);   			
        newKDPanel.setPreferredSize(new Dimension(400,300)); 
        add(newKDPanel);
         
        String filename = "chart.jpg";  
        try {
			ChartUtilities.saveChartAsJPEG(new File(filename), chart, 500, 300);
		} catch (IOException e) {
			e.printStackTrace();
		}  
         
         
        l.putConstraint(SpringLayout.WEST, newKDPanel, 660, SpringLayout.WEST, this);
        l.putConstraint(SpringLayout.NORTH, newKDPanel, 20, SpringLayout.NORTH, this);
        newKDPanel.updateUI();
     }
    
    private class GF2 extends JPanel{
    	@Override 
    		public void paintComponent(Graphics g) {
            super.paintComponent(g);             
            Graphics2D gr2d = (Graphics2D) g;
            gr2d.setColor(Color.RED);
            gr2d.setStroke(new BasicStroke(SIZE));
            gr2d.drawLine(0,0, 0, getHeight());
            gr2d.drawLine(0,getHeight(), getWidth(), getHeight());
            gr2d.drawString("X", getWidth()-8, getHeight()-8);
            gr2d.drawString("Y", 8, 10);
            String s;
            for (int i =0; i<getWidth();i+=50)
            {
            	s = String.valueOf(i);
            	gr2d.drawLine(i,getHeight()-5, i, getHeight());
            	gr2d.drawString(s, i+5, getHeight()-8);
            }
            for (int i =0; i<getHeight();i+=50)
            {
            	s = String.valueOf(i);
            	gr2d.drawLine(0,getHeight()-i, 5, getHeight()-i);
            	gr2d.drawString(s, 5, getHeight()-i-8);
            }
            
            gr2d.setColor(Color.BLUE);
            for (int i=0;i<n-1;i++){
            		gr2d.drawLine(input[i][0],getHeight()-input[i][1],input[i+1][0],getHeight()-input[i+1][1]);
            	} 
            }
    }
    private class GF3 extends JPanel{
    	@Override 
    		public void paintComponent(Graphics g) {
            super.paintComponent(g);
        
            Graphics2D gr2d = (Graphics2D) g;
            gr2d.setColor(Color.RED);
            gr2d.setStroke(new BasicStroke(SIZE));
            gr2d.drawLine(0,0, 0, getHeight());
            gr2d.drawLine(0,getHeight(), getWidth(), getHeight());
            gr2d.drawString("X", getWidth()-8, getHeight()-8);
            gr2d.drawString("Y", 8, 10);
            String s;
            for (int i =0; i<getWidth();i+=50)
            {
            	s = String.valueOf(i);
            	gr2d.drawLine(i,getHeight()-5, i, getHeight());
            	gr2d.drawString(s, i+5, getHeight()-8);
            }
            for (int i =0; i<10;i++)
            {
            	s = String.valueOf(i*10);
            	gr2d.drawLine(0,getHeight()/10*i, 5, getHeight()/10*i);
            	gr2d.drawString(s+"%", 5, getHeight()- getHeight()/10*i-8);
            }
            
            gr2d.setColor(Color.BLUE);
            for (int i=0;i<n-1;i++){
            		gr2d.drawLine(norm_input[i][0],getHeight()-getHeight()/100*norm_input[i][1],norm_input[i+1][0],getHeight()-getHeight()/100*norm_input[i+1][1]);
            	} 
            }
    }

    private  PieDataset createDataset() {
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        for (int i =0;i<n;i++){
        pieDataset.setValue(input[i][0], input[i][1]);
        }
        return pieDataset; 
    }
    
    private JFreeChart createChart(PieDataset dataset, String title) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false);

        return chart;
        
    }
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
            	new Graphs2().setVisible(true);
            }
        });    
    }
}
and
6
0 50
50 100
100 120
150 170
200 100
220 200

hfontanez
Posts: 5
Joined: Mon Oct 06, 2014 5:09 pm
antibot: No, of course not.

Re: JFreeChart difficulties

Post by hfontanez » Fri Oct 10, 2014 9:28 pm

Your problem is because the first argument being passed to the setValue method is ambiguous. You have to pass something that it would know how to compare. For example, to compare a String is different that to compare a Number. You can easily fix this by doing something like this:

Code: Select all

   private  PieDataset createDataset() {
       DefaultPieDataset pieDataset = new DefaultPieDataset();
       for (int i =0;i<n;i++){
       pieDataset.setValue(String.valueOf(input[i][0]), input[i][1]);
       }
       return pieDataset;
   }
or:

Code: Select all

   private  PieDataset createDataset() {
       DefaultPieDataset pieDataset = new DefaultPieDataset();
       for (int i =0;i<n;i++){
       pieDataset.setValue(Integer.valueOf((input[i][0])), input[i][1]);
       }
       return pieDataset;
   }
The key is that you have to be very specific what kind of Comparable object you are passing. I am sure this is a problem because of Autoboxing. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. The compiler cannot guess what Comparable object you need to use.

Locked