Is it possible to do this

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
San

Is it possible to do this

Post by San » Mon Jan 17, 2005 4:31 am

Hi,

I am new to jFreechart. I want to display some information in form of Piechart. I want that the information should be collected from a file and then displayed.

Please help me to do this.
[ public Piechart()
{
// Initialize the dataset
dataset.setValue("Free", new Double(10.0) );
dataset.setValue( "Remaining", new Double( 40.0 ) );
dataset.setValue( "Current", new Double( 15.0 ) );
dataset.setValue( "User", new Double( 25.0 ) );
dataset.setValue( "Administrator", new Double( 10.0 ) );]

I want that the values in "" and for the double be collected from a file and then displayed.
I have tried to do this by array but it doesnt seem to work.

Any help will be highly appreciated.
Thanks and Regrads
Sanyukta[/code]

oacis
Posts: 101
Joined: Fri Jan 07, 2005 5:57 am
Location: Australia, Sydney

Post by oacis » Mon Jan 17, 2005 5:17 am

Yes, it is certainly possible:


1. decide upon the file structure you wish to use e.g. csv, xml or other
2. load up the file
3. convert the values to the correct type
4. Add to dataset

You may wish to have a file such as
Free,10.0
Remaining,40.0
Current,15.0
User,25.0
Administrator,10.0

Then you can read it in:
(If you use the above file as an example and have it saved as filename.csv), then:

Code: Select all

import java.io.*;
import java.util.*;

public class PieChartMuncher {
	public static void main(String args[]) {
		String category = null;
		Double value = null;
		try {
			String currentLine = null;

			FileReader fileReader = new FileReader("filename.csv");
			BufferedReader bufferedReader = new BufferedReader(fileReader);

			while((currentLine = bufferedReader.readLine()) != null) {
				StringTokenizer stringTokenizer = new StringTokenizer(currentLine, ",");
				category = stringTokenizer.nextToken();
				value = Double.valueOf(stringTokenizer.nextToken());

				// add to dataset here!
				// dataset.setValue(category, value);
			}


		} catch(java.io.IOException jiioex) {
			jiioex.printStackTrace();
		}
	}
}
Last edited by oacis on Mon Jan 17, 2005 6:20 am, edited 2 times in total.

sanyukta

Thank you!!!! It works :)

Post by sanyukta » Mon Jan 17, 2005 6:12 am

Hi,

Thanks for your reply...I tried it and everything works fine... Thanks again for the help.

Regards
Sanyukta

Sanyukta

Can i collect data from a database???

Post by Sanyukta » Mon Jan 17, 2005 7:08 am

Hi ,

I am trying to collect data from the database an make a pie chart. But i dont know how to do this....

Also i want that the program can automatically update if the data is changed in the database.
Is this possible?????

Please Help.... Any help is appreciated...

Thanks and Regards
Sanyukta

oacis
Posts: 101
Joined: Fri Jan 07, 2005 5:57 am
Location: Australia, Sydney

Post by oacis » Mon Jan 17, 2005 7:53 am

Once again, this is more than possible...

Have a good look at the javadocs for

Code: Select all

org.jfree.data.jdbc.JDBCPieDataset
You will still need knowledge of connections and setting up a jdbc driver which depends on the database that you are using.

for example:

Code: Select all

public class ConnectionHelper {
  private String jdbcUrl = null;
  public ConnectionHelper(String jdbcUrl, String jdbcDriver) {
    this.jdbcUrl = jdbcUrl;

    try {
      Class.forName(jdbcDriver).newInstance();
    } catch(java.lang.ClassNotFoundException jlcnfex) {

      jlcnfex.printStackTrace();
    } catch(java.lang.InstantiationException jliex) {
      jliex.printStackTrace();
    } catch(java.lang.IllegalAccessException jliaex) {
      jliaex.printStackTrace();
    }
  }

  public Connection getConnection() throws SQLException {
    return(DriverManager.getConnection(jdbcUrl));
  }
}
where the jdbcUrl and jdbcDriver (respectively) will be something like

Code: Select all

jdbc:mysql://<hostname>:<port>/<database>?<param>=<value>&<param>=<value>
com.mysql.jdbc.Driver
Of course this is only good for mysql - so YMMV.

As for automatically updating from the database, you will probably have to poll the database at regular intervals or send out a trigger from the database. Not sure of the latter option but I believe that Oracle does have some sort of java connectivity going for it.

To have it dynamic you will need to look at DynamicDataDemo(s) in the demo jar. Haven't had too much involvement with dynamic charts so... perhaps someone else can give their input.

Locked