Quick start
 
 Dynamic style
 
 Dynamic model
 
 Drill-down
 
 Real time
 
 Ajax
 
 Embedded
 
 
 
 
   Overview         Example         Source       

In some cases the chart's model cannot be loaded directly from a database or a file, but rather should be constructed manually. You can produce data in XML format manually, or use MxStandardChartModel to construct the model using API and then automatically convert it into XML. This page demonstrates the second approach to display Income, Expence and optionally NET values depending upon the state of a checkbox. In addition, the example on this page explains how to use page-persistent models - i.e. models that are stored within the page and do not need to be recalculated on every page request. Optionally, you can store your model in the session object.

First of all, you need to create a function that produces your original model:


<%!
private static MxStandardChartModel getDefaultChartModel() {
  MxStandardChartModel model  = new MxStandardChartModel();
   
   // Define model's columns. You need to define rows prior to 
   // inserting columns (or vice a versa), since the number of 
   // the rows in the inserted column is clipped to the number 
   // of the rows in the model.

   model.insertRow("Income",null,-1);
   model.insertRow("Expense", null,-1);


   double v2001[] = { 1000,-800 };
   double v2002[] = { 1020,-810 };
   double v2003[] = { 1100,-880 };
   double v2004[] = { 1190,-920 };

   // insert rows into the model
   model.insertCol("2001", v2001,-1);
   model.insertCol("2002", v2002,-1);
   model.insertCol("2003", v2003,-1);
   model.insertCol("2004", v2004,-1);

   return model ;
}
%>

The chart's model will be stored inside a hidden field named modelXML. To create a model we need to check the value of this field. If it is not defined, we will use getDefaultChartModel defined above, otherwise we recreate the object from XML:

   String modelXML = request.getParameter("modelXML");
   MxStandardChartModel model = modelXML == null 
           ? getDefaultChartModel() 
           : (MxStandardChartModel) MxStandardChartModel.fromXML(modelXML, null);    

The chart should display NET values depending upon the status of 'showNET' checkbox. When this checkbox is set, we'll need to compute and insert net values into the chart if they are not present there:

     if(model.getRowCount() == 2) {
         double values [] = new double[model.getColCount()];
         for(int i=0;i

Otherwise, we need to remove this column if it is present:

     if(model.getRowCount() == 3) 
           model.deleteRow(2);

Once the model is ready, you can easily convert it to XML by using standard toXML() function and then product the chart. The final thing to do is to declare a form with the hidden field that will keep chart's model and the checkbox that will control model:

<form name=ModelForm id=ModelForm method="POST">
   <input type=hidden name="modelXML" value="<%=myChart.model.replace('"', '\'') %>">
   <input type=checkbox name=showNET 
		 <%="on".equalsIgnoreCase(showNET) ? "checked" : ""%>
		 onclick="javascript:document.ModelForm.submit()">Show NET
</form>

 

   Copyright © 1994-2010 GreenPoint, Inc. All rights reserved.