PCXML Transformations

After you've loaded your appearance file and data, you may want to make additional modifications to your PopChart image. For example, these modifications could include adding or hiding a new text box, changing the colors or symbols for a certain data series, or adding a logo to the PopChart image. We refer to the strategy of making on-the-fly appearance file modifications as PCXML Transformations.

You will probably want to generate these PCXML transformations in much the same manner as you generate your dynamic data. You even send these transformations using the exact same commands ( loadPCXML(String), addPCXML(String), and @_LOADPCXML).

Customizing Objects from your Appearance Files

One type of PCXML transformation is to customize or override settings for objects already in your appearance file. You can do this by creating a PCXML element that is named exactly the same as the object you wish to customize. Then, you can specify the attributes or subelements that you wish to modify or override. You can even add subelements and attributes.

For example, suppose that you have set your appearance file to examples/apfiles/bar.pcxml and that you want to dynamically change the type of your graph to Line. You could do this by sending another Graph element named graph to PopChart Server, containing only a Type attribute, which is set to Line.

<Graph Name="graph" Type="Line"></Graph>

Note: When changing graph types, be sure to change to a graph type that is in the same data class (see Chapter 12, "Data Organization," in the PopChart Server Reference manual).

Another common transformation is to change the formatting for a data series. To do this, you simply override the SeriesDefinition tag for the data series we want to modify. For example, having changed the graph in the examples/apfiles/bar.pcxml file to a Line graph, suppose you also want to add symbols to and change the color of the second data series. You could do this by changing the Color and SymbolType attributes of its SeriesDefinition tag.

Example 10.2 shows the resulting PCXML.

Example 10.2 PCXML Template to Change a Graph Type, Color, and Symbol

<Chart> 

   <Graph Name='graph' Type='Line'> 

      <SeriesDefinition Number='2' Color='#FF0000' SymbolType='Square'/> 

   </Graph> 

</Chart>

This PCXML template is saved as examples/pcxml/linetransform.pcxml. You could have PopChart Server load this transformation with the loadPCXML(String) PopChart Embedder method, as shown below.

myPopChart.loadPCXML("examples/pcxml/linetransform.pcxml");

If applied to the example code from Example 4.7, this command would render the following image:


Adding Objects to Your Appearance File

Another type of PCXML transformation is to add a new object to your appearance file.

Note: When adding objects to your appearance file, be sure to give them distinct names (names that are not already being used).

For example, suppose that you have a logo that always we want to include at the top left corner of your appearance files. You've saved the PCXML for this logo to examples/images/companylogo.pcxml in your PopChart Server document root. To have PopChart Server add this logo to any PopChart image you create, simply add the following line to your PopChart Embedder code:

myPopChart.loadPCXML("examples/images/logo.pcxml");

Note: The logo.pcxml file actually exists, so you can copy this line into your example code and have the logo appear.


As another example, suppose you want to add a timestamp to all the PopChart images you display. If you've put the timestamp into a String called timestamp, you could create a new textbox with the timestamp with the following PopChart Embedder command:

myPopChart.addPCXML("<Chart><Textbox Name='timestamp' Top='20' Left='420' Height='40' Width='100'><Text>"+timestamp+"</Text></Textbox>");

Note: You can also dynamically add new graphs—in effect it is possible to create your appearance file entirely on the fly. As you do this, though, remember to send the data for a graph AFTER you have created it. In other words, you need to swap the order of your PCXML transformations and your loading of PCXML data. Fake data will be inserted into any graph for which PopChart Server does not have a <GraphData> element.

For more ideas on PCXML transformations for your graphs, read Chapter 7, "Interactive and Special Effects."