Example: Setting Data from a File on a Simple Graph

This example shows how to set data from a tab-delimited file on a bar, line, area, or pie graph. This kind of code also works with three-dimensional bar, cube, ribbon, and area graphs. This example uses the setTabularData method of the graph. The file that supplies data for the graph can have two different forms. The file might have a a single fact column of data values, with key columns that identify the measures to which the fact values belong. You will find the example of this kind of data and the code to set it on the graph in the topic on using the setTabularData method.

This topic describes how to set data on a graph from a file that has multiple fact columns.

Source data

The following table shows the data that is in a source file. In the file, this data would be separated by tabs or commas. Also, it would not have headers. Headers are included here for clarity.

The first column of numbers is Sales data. The second column of numbers is Cost data, and the last column is Profit data.

Year Sales Costs Profit
1999 4000000 3000000 1000000
2000 6500000 4500000 2000000
2001 4500000 3000000 1500000

Graph

The following figure shows the graph that this example creates. This example also works for line graphs, area graphs, and pie graphs, as well as three-dimensional bar, floating cube, ribbon, and area graphs. Note that this example does not use a time axis, because the year values are series labels. If you arranged the years as group labels, you could pass java.sql.Date objects to use a time axis.

sample graph from previous data

Grid of data for the graph

To get the desired graph, this example takes data from a file of tab-separated data and calls the setTabularData method to set the data on the graph. The setTabularData method constructs a grid of data for the graph. The following table shows the grid of data for the graph.

empty cell; date column SALES COSTS PROFIT
1999 4000000 3000000 1000000
2000 6500000 4500000 2000000
2001 4500000 3000000 1500000

Note: This grid is appropriate if the getDataRowShownAsASeries method of the graph returns true. If you map columns as series, then the years should be column labels, and the measures should be row labels.

Sample code

This example retrieves the data from the source file, which is set as a command-line argument. It uses a StringTokenizer to take the data from the file and put in an ArrayList of object arrays, which it then passes to setTabularData. For each line of data in the file, the code iterates over a Year value three times, in order to make three separate columns in the grid of data for the graph.

This code assumes that you have the full file name, including the path, and that you have a graph.


// declared earlier // you will pass this ArrayList to the graph ArrayList data = new ArrayList(); // code omitted here to get full file name try{ BufferedReader reader = new BufferedReader (new FileReader(fullFilename)); String line = ""; // put column labels in an array String[] colLabels = new String[] {"SALES", "COST", "PROFIT"}; // read rows from a tab-delimited file // while there are rows of data to read while ((line = reader.readLine()) != null && !line.equals("")){ // get each token in the line // tokens are separated by tabs StringTokenizer tokens = new StringTokenizer(line, "\t"); // not a date because it is the row label, not the column label String yearLabel = tokens.nextToken(); // first token Double salesData = new Double(tokens.nextToken()); // second token Double costData = new Double(tokens.nextToken()); // third token Double profitData = new Double(tokens.nextToken()); // fourth token // array of data values to match colLabels array Double[] dataValues = new Double[]{salesData, costData, profitData}; // for each fact column, create an array of data for (int i = 0; i < colLabels.length; i++){ Object[] rowItems = new Object[3]; // each array has three members rowItems[0] = colLabels[i]; // column label in grid rowItems[1] = yearLabel; // row label; same for each column rowItems[2] = dataValues[i]; // data value for appropriate fact column // add this array to the ArrayList data.add(rowItems); } // for each column of data } // while still reading } catch (IOException e){ System.out.println("IO problem reading " + fullFilename); e.printStackTrace(); } // catch IO exception reading file // pass the ArrayList to setTabularData graph.setTabularData(data);

Specifying Graph Data Through the setTabularData Method
Data Requirements for Different Kinds of Graphs
Handling Problems in Graph Data
Bar Graphs
Line Graphs
Area Graphs
Pie Graphs