Example: Setting Data from a File on a Bubble Graph

This example shows how to set data from a tab-delimited file on a bubble graph. This example uses the setTabularData method of the graph. The file that supplies data for the graph can have two different forms:

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.

sample bubble graph, from previous data

The following table shows the grid of data for the graph.

empty cell; product column East_US SALES East_US PROMO East_US USABILITY Central_US SALES Central_US PROMO Central_US USABILITY West_US SALES West_US PROMO West_US USABILITY
Cell Phones 2000000 40000 14000 1500000 25000 8000 4000000 50000 20000
PDAs 800000 15000 5000 200000 5000 1000 1000000 25000 10000

Notice that this grid has three columns for East_US, three for Central_US, and three 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" and "East_US USABILITY' are different labels, ensuring the appropriate three-column group of data for East_US for a bubble 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 three 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 bubble 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, Promo, and Usability 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, and the third column is used to calculate the size of the bubble in the graph.

Source data for a single fact column

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. The headers are included here for clarity.

Product Geography Measure Data Value
Cell Phones East_US SALES 2000000
Cell Phones East_US PROMO 40000
Cell Phones East_US USABILITY 15000
Cell Phones Central_US SALES 1500000
Cell Phones Central_US USABILITY 8000
Cell Phones Central_US PROMO 25000
Cell Phones West_US SALES 4000000
Cell Phones West_US PROMO 50000
Cell Phones West_US USABILITY 20000
PDAs East_US SALES 800000
PDAs East_US PROMO 15000
PDAs East_US USABILITY 5000
PDAs Central_US SALES 200000
PDAs Central_US PROMO 5000
PDAs Central_US USABILITY 1000
PDAs West_US SALES 1000000
PDAs West_US PROMO 25000
PDAs West_US USABILITY 10000

Sample code for a file that has a single fact column

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()); data.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.BUBBLE); // identify the axes in the graph graph.getX1Title().setText("Sales"); graph.getY1Title().setText("Promotional Expense"); graph.getFootnote().setText("Bubble size = Usability Expense");

Source data for a multiple fact column

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. The headers are included here for clarity.

Product Geography Sales Promotional Expense Usability Expense
Cell Phones East_US 2000000 40000 14000
Cell Phones Central_US 1500000 25000 8000
Cell Phones West_US 4000000 50000 20000
PDAs East_US 800000 15000 5000
PDAs Central_US 200000 5000 1000
PDAs West_US 1000000 25000 10000

Sample code for a file that has multiple fact columns

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[] measures = new String[] {"SALES", "PROMO", "USABILITY"}; // 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 Double usabilityData = new Double(tokens.nextToken()); // fifth token
// create an array of measure data values Double[] dataValues = new Double[]{salesData, promoData, usabilityData}; // for each column of data, create an array for (int i = 0; i < measures.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 + " " + measures[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 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); // set the graph type graph.setGraphType(Graph.BUBBLE); // identify the axes in the graph graph.getX1Title().setText("Sales"); graph.getY1Title().setText("Promotional Expense"); graph.getFootnote().setText("Bubble size = Usability Expense");

Specifying Graph Data Through the setTabularData Method
Data Requirements for Different Kinds of Graphs
Handling Problems in Graph Data
Bubble Graphs