Samples

The samples in this section can be used inside jsp pages. You need to import com.gp.api.jsp package to use these examples: <%@ page import = "com.gp.api.jsp.*" %>

Generating chart's bytes

To generate the chart's bytes and/or to save the chart into a file:

  1. Obtain an instance of MxServerComponent. If you do not have access to the servlet context to use with getDefaultInstance, you will need to create a new instance of this class:
    MxServerComponent svr = new MxServerComponent(new MxServerProperties());
    Due to a number of the different checks that are performed during server component instantiation it is a relatively long operation and we recommend you to create a single instance of this class. This class supports multi-threading and can be called from different threads.
  2. Create an instance of MxChartDescription and fill it.
    MxChartDescription chart = svr.newImageSpec();
    chart.width = 400 ;
    chart.height= 300 ;
    chart.type = "swf";
    chart.style = "<pieChart type='Pie'/>" ;
    chart.model = "<XML type='default'><COL>2000</COL>\n<COL>2001</COL>\n<COL>2002</COL>\n<COL>2003</COL><ROW col0='100.0' col1='200.0' col2='100.0'/></XML>";
  3. Obtain chart's bytes by using getBytes:
    byte bytes [] = svr.getBytes(chart);
    or save the chart into a file:
    svr.saveBytesTo(chart, "C:\\test.swf");

Using direct method of generating charts

When you use direct method of generating charts, the image tag (or plugin tag) url points to some jsp page that returns chart's bytes instead of pointing to getimage.jsp that retrieves the bytes from the image cache. This method guarantees that the image will never expire, but cannot be used to produce interactive images since image maps has to be embedded into the page. This method can be used to produce interactive Flash and SVG charts.

  1. Obtain an instance of MxServerComponent. As opposed to indirect method, you do not have to use getDefaultInstance method and can create an instance directly. However, we recommend to use getDefaultInstance method to facilitate caching and take advantage of built-in resource management. (See also comments for the previous sample).
    MxServerComponent svr = MxServerComponent.getDefaultInstance(application);
  2. Create an instance of MxChartDescription and fill it:
    MxChartDescription chart = svr.newImageSpec();
    chart.width = 400 ;
    chart.height= 300 ;
    chart.type = "swf";
    chart.style = "<pieChart type='Pie'/>" ;
    chart.model = "<XML type='default'><COL>2000</COL>\n<COL>2001</COL>\n<COL>2002</COL>\n<COL>2003</COL><ROW col0='100.0' col1='200.0' col2='100.0'/></XML>";
  3. Return image bytes to the server. You can obtain image bytes and then write them to response, or call writeBytesTo function which will set content length and type and write the bytes:
    svr.writeBytesTo(chart,response);

Note that a page that returns actual image bytes cannot have any text in it including spaces and newlines. Make sure that there are no spaces after the last %> and that your file starts with:

<%@ page import = "com.gp.api.jsp.*" %><%
so that there are no spaces or newlines between %> and <%. After you test the page in the browser you can include a reference to it into your html page.

Using indirect method of generating charts

When you use indirect method of generating charts (method used by the code produced by Designer), the image tag (or plugin tag) url points to a special jsp page and contains a unique image id that is used to retrieve the image from the cache. This page can be located anywhere and named in any way, as long as the same name is provided as an argument to getImageTag function. The main advantages of using indirect method are:

To produce charts using indirect method:

  1. Obtain an instance of MxServerComponent. As opposed to direct method, you should use getDefaultInstance method unless you modify getimage.jsp file.
    MxServerComponent svr = MxServerComponent.getDefaultInstance(application);
  2. Create an instance of MxChartDescription and fill it:
    MxChartDescription chart = svr.newImageSpec();
    chart.width = 400 ;
    chart.height= 300 ;
    chart.type = "swf";
    chart.style = "<pieChart type='Pie'/>" ;
    chart.model = "<XML type='default'><COL>2000</COL>\n<COL>2001</COL>\n<COL>2002</COL>\n<COL>2003</COL><ROW col0='100.0' col1='200.0' col2='100.0'/></XML>";
  3. Produce image or plugin tag using getImageTag function and embed this tag into your page:
    out.write(svr.getImageTag(chart,"getImage.jsp?image="));
    getImageTag will generate html tags based on the type of the image and append the unique id of the image to the provided url.