Articles Archive
Articles Search
Director Wiki
 

Generator?

August 31, 1998
by Pat McClellan

Dear Multimedia Handyman,

I've been reading about Macromedia's new product called Generator. Apparently, it can combine a Flash template file with external data and return a dynamically updated chart or graph. Is there a way to use Generator with Director?

Darcy McAfee

Darcy,

The simple answer is yes and no. Not so simple, huh?

Generator is very cool, giving the author the ability to create template files with external datalinks. Generator consists of two parts: the authoring extensions for Flash which allow you to create and output the template file ($199), and the server side application which does the actual combining of template and data and output the result ($3000). Currently, the authoring extensions are only for Flash, so in this sense, you can't use Director with Generator. However, you can easily integrate the output of Generator with your Director movies.

Generator can output files in Flash, gif, jpeg and png formats. Since all of these formats can be linked as external cast members in a Director movie, I can't see why you couldn't link to a template file instead. When the template file is called, Generator would do its thing and return the output to your Director movie. In my mind, the question isn't "can you do it?" but rather, "why would you need to?". I've been trying to think of a situation in which Generator and a Flash template file can create a result which Director can't duplicate on its own.

Granted, Flash can do some awesome antialiasing and scaling. That right there might be enough to justify the expense of running Generator. But if you simply want to link to an external data file and create a graph, save your money and let's get crackin' writing some Lingo.

I'll use a simple example which is shown in the Generator demo movie: a bar chart. We'll put the data is in an external txt file called bar.txt. The Shockwave file will use getNetText to read in that data and adjust its chart display accordingly. Here's the entire contents of our external data file:


J, 27
F, 13
M, 7
A, 12
M, 11
J, 14
J, 19
A, 21
S, 20
O, 24
N, 25
D, 21

This will allow us to have the name of each bar (month) and its value inserted into our chart template.

There are 2 basic operations involved in making this all happen. First, we've got to import the text information. We'll use the getNetText behavior that Chris Walcott wrote and which comes in the Behavior Library provided with Director (under the Xtras menu). This behavior is written to put the result of a getNetText operation into a field in the score. Since this isn't necessarily what you want to display, you might want to alter the behavior to put the result into a variable or a field member (not onstage). For the demo though, it will be helpful to show that the 2 operations are occurring as we plan.

I've dropped the getNetText behavior on the "Get Data" button and I've designated the field which appears to the right to receive the text. Press the Get Data button now.

Our data should have been read in from the external file and placed in the field. Now we need to tackle our second task: charting that data. This will involve putting the first item of each line into their respective fields across the bottom of the chart, and then using the value of the second item to scale the height of each bar.

I'll set up the properties with the getPropertyDescriptionList so that the author can specify the data source (field) and the other necessary items: the first and last spriteNums for the bars, the chart height in pixels and the maximum value for the chart to include. These last two items will allow us to scale the data so that the chart best reflects the range of values. Since our data goes up to 27, I'll make the maximum value 30 in the demo movie.


-- BarCharter Behavior
-- copyright © 1998 ZZP Online, LLC
property pDataField 
-- the source of the data (a field cast member)
property pData 
-- property to hold the data for parsing
property pFirstBar 
-- the spriteNum of the first bar
property pLastBar 
-- the spriteNum of the last bar
property pChartHeight 
-- the height in pixels of the chart
property pChartMax 
-- the maximum value of any bar (not in pixels)
property pScale 
-- pChartHeight/pChartMax * 1.00
property pBaseline 
-- the vertical origin of the bars

on getPropertyDescriptionList me 
  set pdlist to [:]
  addprop pdlist, #pDataField, [#comment:"Data ¬
    Source", #format:#field, #default:0]
  addprop pdlist, #pFirstBar, [#comment:"First Bar ¬
    spriteNum", #format:#integer, #default:0]
  addprop pdlist, #pLastBar, [#comment:"Last Bar ¬
    spriteNum", #format:#integer, #default:0]
  addprop pdlist, #pFirstLabel, [#comment:"First ¬
    Label spriteNum", #format:#integer, #default:0]
  addprop pdlist, #pChartHeight, [#comment:"Pixel ¬
    height of chart", #format:#integer, #default:0]
  addprop pdlist, #pChartMax, [#comment:"Max ¬
    Value", #format: #integer, #default:0]
  return pdlist
end getPropertyDescriptionList
on beginSprite me
  set pScale = pChartHeight/pChartMax * 1.00
  set pBaseline = the bottom of sprite pFirstBar
end
on mouseDown me  
  set pData = the text of field pDataField  
  insertLabels me
  scaleBars me
end
on insertLabels me
  set howManyLabels = pLastBar - pFirstBar + 1
  repeat with i = 1 to howManyLabels
    set labelMem = "bar" & i
    set labelName = item 1 of line i of pData
    put labelName into field labelMem
  end repeat
end
on scaleBars me
  set whichLine = 1
  repeat with whichSprite = pFirstBar to pLastBar
    set value = value(item 2 of line whichline ¬
      of pData)
    set indexHeight = value * pScale
    set the locV of sprite whichSprite = ¬
    pBaseline - indexHeight
    set the height of sprite whichSprite to indexHeight
    set whichLine = whichLine + 1
  end repeat
end

Now let's look at the handlers. The beginSprite handler simply sets a couple of other properties which the author need not specify. The property pScale is a way of relating the actual pixel height of the chart to the range of values to be displayed. pBaseline is needed because the locV of our bar sprites is locked to the *top* of the bar, not the bottom. Therefore, if I change the height of the bars, all of the tops will remain alligned while the bottoms reflect the range of values. I use pBaseline as a way of setting the locV of each bar to accomodate the change in its height (in the scaleBars handler).

The mouseDown handler is used to execute the 2 operations I described earlier: inserting the labels into the label field castMambers, and scaling (and resetting the locV) of the bar sprites. Note that the insertLabel handler requires that your labels be named "bar1", "bar2", etc.

In the demo movie, I made the data field editable so that you can change the values and the labels and then rechart the new data.

Generator may well make Flash a dominant technology, rather than simply a nice little graphic tool. But let's not get run over by Generator simply because we're not using Director to do everything in its capabilities. Let's show 'em what Director can do!

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.