0

I am using Struts2 jquery chart plugin 3.4.0. I am getting blank chart when i use json for getting date values from the Action class.If i use simple action then same code works fine. Here is my jsp code.

   <s:url id="chartDataUrl" action="jsonChartData"/>
    <sjc:chart
        id="chartDate"
        xaxisMode="time"
        xaxisTimeformat="%m.%Y"
        xaxisMin="%{minTime}"
        xaxisMax="%{maxTime}"
        xaxisColor="#666"
        xaxisTickSize="[3, 'month']"
        xaxisTickColor="#aaa"
        xaxisPosition="top"
        yaxisPosition="right"
        yaxisTickSize="10"
        cssStyle="width: 600px; height: 400px;"
        >
        <sjc:chartData
            id="chartAjaxData1"
            label="Map -Double, Double-"
            href="%{chartDataUrl}"  // when i remove json call then it works fine
            list="dateFromMap"
            reloadTopics="reloadMap"
            lines="{show : true}"
            />
    </sjc:chart>

struts.xml code

   <action name="jsonChartData"  
           class="com.ebhasin.fitnessbliss.actions.GraphsAction">
        <result   type="json" name="success"></result>
    </action>

Action class code:

public class GraphsAction extends ActionSupport {

    private String currentDate;
    private Map<Date, Float> dateFromMap;
    HomeService homeService = new HomeService();
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

    @Override
    public String execute() {
        System.out.println("execute");
        float weight;
        Date date = new Date();
        Map session = ActionContext.getContext().getSession();
        Integer loginId = (Integer) session.get("loginId");
        if (loginId != null) {
            dateFromMap = new TreeMap<Date, Float>();
            List list = homeService.getWeightGraphData(loginId);
            if (list.size() > 0) {
                Iterator itr = list.iterator();
                while (itr.hasNext()) {
                    UserStats userStats = (UserStats) itr.next();
                    weight = userStats.getWeight();
                    date = userStats.getCreatedDate();
                    //currentDate = formatter.format(date);
                    dateFromMap.put(date, weight);
                }
            } else {
                // dateFromMap.put("my",2F );
            }
        } else {
        }
        return SUCCESS;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }


    public Map<Date, Float> getDateFromMap() {
        return dateFromMap;
    }

    public void setDateFromMap(Map<Date, Float> dateFromMap) {
        this.dateFromMap = dateFromMap;
    }
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Jeetu Verma
  • 199
  • 1
  • 5
  • 19

1 Answers1

0

EDIT: Please put a System.out.println("something"); on every if / else block to see exactly where are you passing;

As a note, the preferred way to get the session object is to implement SessionAware, instead of using ActionContext;


Does your JSON Action looks like this ? :

http://struts.jgeppert.com/struts2-jquery-showcase/index.action

(go to More Widgets -> Charts -> JSON Action, in the bottom right tab.

@ParentPackage(value = "showcase")
public class JsonChartData extends ActionSupport {

  private static final long   serialVersionUID = 6659512910595305843L;
  private List<ListValue>     objList;
  private Map<Double, Double> doubleMap;

  @Actions( {
    @Action(value = "/json-chart-data", results = {
      @Result(name = "success", type = "json")
    })
  })
  public String execute()
  {

    objList = new ArrayList<ListValue>();
    doubleMap = new TreeMap<Double, Double>();

    Random generator = new Random();
    for (int i = 1; i <= 24; i++)
    {
      doubleMap.put(Double.valueOf("" + i), generator.nextDouble() * 10.0);
    }

    for (int i = 1; i <= 24; i++)
    {
      objList.add(new ListValue("" + i, "" + generator.nextInt(30)));
    }

    return SUCCESS;
  }

  public String getJSON()
  {
    return execute();
  }

  public List<ListValue> getObjList()
  {
    return objList;
  }

  public void setObjList(List<ListValue> objList)
  {
    this.objList = objList;
  }

  public Map<Double, Double> getDoubleMap()
  {
    return doubleMap;
  }

  public void setDoubleMap(Map<Double, Double> doubleMap)
  {
    this.doubleMap = doubleMap;
  }
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you for response, I did the same , in this example of AJAx(http://struts.jgeppert.com/struts2-jquery-showcase/index.action) they are using Double values on both axes, i want date on y - axis , for this i use the same example **Chart with Date Values** from the showcase. But i am not getting graph. – Jeetu Verma Dec 04 '12 at 09:30
  • Can you post your JSON Action ? – Andrea Ligios Dec 04 '12 at 10:45
  • I posted my Struts.xml & Action class code Above.Please check it. – Jeetu Verma Dec 04 '12 at 11:57
  • Thank you for your response again, if i call simple Action then same code works fine for me.But json it is not working – Jeetu Verma Dec 04 '12 at 15:39