This example shows how to set data from a tab-delimited file on a scatter graph.
This example uses
the setTabularData
method of the graph. The file that supplies
data for the graph can have two different forms.
Single fact column -- 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.
Multiple fact column -- The file might have a fact column for each measure that you want to display in the graph.
The setTabularData
method creates a simple DataSource
from the data that you pass. This grid is the same for both forms of source
data, and the graph is the same. The following figure shows the graph that this
example creates.
The following table shows the grid of data for this graph.
![]() |
East_US SALES | East_US PROMO | Central_US SALES | Central_US PROMO | West_US SALES | West_US PROMO |
---|---|---|---|---|---|---|
Cell Phones | 2000000 | 40000 | 1500000 | 25000 | 4000000 | 50000 |
PDAs | 800000 | 15000 | 200000 | 5000 | 1000000 | 25000 |
Notice that this grid has two columns for East_US, two for Central_US, and two for West_US. The "East_US SALES" label identifies the unique column for all of the Sales values for the Eastern United States. The "East_US PROMO" is a different label, ensuring the appropriate two-column group of data for East_US for a scatter graph.
Note: This grid is appropriate if the getDataRowShownAsASeries
method of the graph returns true
. If you map columns as series,
then the grid should have two distinct rows for each Geography value.
In an OLAP data source, you could simply place the Geography and Measure dimensions
on the column edge, and the data source would create a grid that has two layers
of metadata on the column edge. The simple data source that setTabularData
creates supports only one layer in the column edge, so you must provide distinct
labels for each column. In a scatter graph, these labels appear in the tooltips
for markers, but they do not appear in any other labels in the graph.
Notice, also, that the Sales and Profit columns always occur in the same order. The first measure column for each Geography is plotted along the X-axis in the scatter graph. The second column in the group is plotted along the Y-axis.
The following table shows the data that is in the source file that has a single fact column. In the file, this data would be separated by tabs or commas. Also, it would not have headers. Headers are included here for clarity.
Product | Geography | Measure | Data |
---|---|---|---|
Cell Phones | East_US | SALES | 2000000 |
Cell Phones | East_US | PROMO | 40000 |
Cell Phones | Central_US | SALES | 1500000 |
Cell Phones | Central_US | PROMO | 25000 |
Cell Phones | West_US | SALES | 4000000 |
Cell Phones | West_US | PROMO | 50000 |
PDAs | East_US | SALES | 800000 |
PDAs | East_US | PROMO | 15000 |
PDAs | Central_US | SALES | 200000 |
PDAs | Central_US | PROMO | 5000 |
PDAs | West_US | SALES | 1000000 |
PDAs | West_US | PROMO | 25000 |
This example retrieves the data from the source file that has a single fact
column. The file is set as a command-line argument. This example uses a StringTokenizer
to take the data from the file and put in an ArrayList
of object
arrays, which it then passes to setTabularData
.
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 = ""; // read rows from a tab-delimited file // while there are rows of data to read while ((line = reader.readLine()) != null && !line.equals("")){ StringTokenizer tokens = new StringTokenizer(line, "\t"); // create a 3-member Object array Object[] rowItems = new Object[3]; // second element in the array is the row label in the grid rowItems[1] = tokens.nextToken(); // first element is the column label in the grid // concatenate next two tokens to get unique label // the first measure value in the file // will appear on the X-axis of the graph rowItems[0] = tokens.nextToken() + " " + tokens.nextToken(); // third item in the array is the data value rowItems[2] = new Double(tokens.nextToken()); dataRows.add(rowItems); } // 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); // set the graph type graph.setGraphType(Graph.SCATTER); // identify the axes in the graph graph.getX1Title().setText("Sales"); graph.getY1Title().setText("Promotional Expense");
The following table shows the data that is in the source file that has more than one fact column. In the file, this data would be separated by tabs or commas. Also, it would not have headers. Headers are included here for clarity.
Product | Geography | Sales | Promotional Expense |
---|---|---|---|
Cell Phones | East_US | 2000000 | 40000 |
Cell Phones | Central_US | 1500000 | 25000 |
Cell Phones | West_US | 4000000 | 50000 |
PDAs | East_US | 800000 | 15000 |
PDAs | Central_US | 200000 | 5000 |
PDAs | West_US | 1000000 | 25000 |
This example retrieves the data from the source file that has more than one
fact column. The file is set as a command-line argument. This example uses a
StringTokenizer
to take the data from the file and put in an ArrayList
of object arrays, which it then passes to setTabularData
. As is
the case when you pass a file with multiple fact columns to a simple graph,
this code iterates over each row value twice, in order to make two 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 = ""; // create an array of measure labels String[] colLabels = new String[] {"SALES", "PROMO"}; // read rows from a tab-delimited file // while there are still rows to read while ((line = reader.readLine()) != null && !line.equals("")){ StringTokenizer tokens = new StringTokenizer(line, "\t"); String productLabel = tokens.nextToken(); // first token String geogLabel = tokens.nextToken(); // second token Double salesData = new Double(tokens.nextToken()); // third token Double promoData = new Double(tokens.nextToken()); // fourth token // create an array of measure data values Double[] dataValues = new Double[]{salesData, promoData}; // for each column of data, create an array for (int i = 0; i < colLabels.length; i++){ // each array has three members Object[] rowItems = new Object[3]; // the first member is the column label in the grid // concatenate the geography and measure label // to ensure unique columns rowItems[0] = geogLabel + " " + colLabels[i]; // the second element is the row label in the grid // same for each grid column in this loop rowItems[1] = productLabel; // the third element is the data value // get the data value that corresponds // to the measure rowItems[2] = dataValues[i]; // add this array to the ArrayList dataRows.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); // set the graph type graph.setGraphType(Graph.SCATTER); // identify the axes in the graph graph.getX1Title().setText("Sales"); graph.getY1Title().setText("Promotional Expense");
Specifying
Graph Data Through the setTabularData Method
Data Requirements for
Different Kinds of Graphs
Handling Problems
in Graph Data
Scatter Graphs