English 中文(简体)
tapestry chenillekit chart : problem to defined trackFormatter function
原标题:

I need help to display chart in tapestry pages. I m using tapestry 5.2.4 and chenillekit 1.3.0. My problem is that I want to defined a custom javascript function to display date in the mouse track. The option for Flotr is trackFormatter : function(obj){...}. In my case, I send option for the graph with JSON. When the function is called, i have error like this:

"n.mouse.trackFormatter is not a function" for firefox,

"Uncaught TypeError: Property trackFormatter of object # is not a function" for chrome.

Does someone have the same problem and/or have find a solution to delcare custom javascript function like trackFormatter? thanks!

My code:

...
import org.apache.tapestry5.json.JSONLiteral;
import org.apache.tapestry5.json.JSONObject;
...

@Import(library = { "mymouseeventhandler.js" })
public class LineChart extends Chart {

private static final Logger logger = LoggerFactory.getLogger(LineChart.class);


/**
 * Map <Label , List of data> .
 */
@Parameter(required = true, defaultPrefix = BindingConstants.PROP)
private Map<String, List<XYDataItem>> counterDataWithLabel;

/**
 * Map <Label , List of data> .
 */
@Parameter(required = false, defaultPrefix = BindingConstants.PROP)
private Map<String, List<XYDataItem>> thresholdDataWithLabel;

@Parameter
private Date startTime;

@Parameter
private String graphCaption;

/**
 * Configure Flotr Graph
 * 
 * <p>
 * <a href="http
 * ://www.chenillekit.org/chenillekit-tapestry/ref/org/chenillekit/tapestry/core/components
 * /Chart.html">ChenilleKit Reference </a>
 * </p>
 * <p>
 * <a href="http://solutoire.com/flotr/docs/options/">Flotr Options </a>
 * </p>
 * 
 */
protected void configure(JSONObject config) {
    // TODO add attributes to LineChart, for being able to parameterize the graph.
    // counterDataWithLabel;
    StringBuilder dataString = new StringBuilder("[");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(getStartTime());
    int startMillisecond = calendar.get(Calendar.MILLISECOND);

    int i = 0;
    for (Map.Entry<String, List<XYDataItem>> entry : counterDataWithLabel.entrySet()) {
        String label = entry.getKey();
        List<XYDataItem> dataItems = entry.getValue();
        dataString.append(String.format("{data: %s, label:  %s  , lines:{lineWidth: 1}}",
                Arrays.toString(dataItems.toArray()), label));
        dataString.append(",");
        i++;
    }
    if (!org.apache.commons.collections.MapUtils.isEmpty(thresholdDataWithLabel)) {
        int j = 0;
        for (Map.Entry<String, List<XYDataItem>> entry : thresholdDataWithLabel.entrySet()) {
            String label = entry.getKey();
            List<XYDataItem> dataItems = entry.getValue();
            dataString.append(String.format(
                    "{data: %s, label:  %s  , points: {show: false} , lines:{lineWidth: 3 }}",
                    Arrays.toString(dataItems.toArray()), label));
            if (j < thresholdDataWithLabel.size() - 1) {
                dataString.append(",");
            }
            j++;
        }
    }

    dataString.append("]");
    config.put("data", new JSONArray(dataString.toString()));

    JSONObject options = new JSONObject();

    options = flotrChart.getOptions();
    options.put("HtmlText", false);
    JSONObject mouse = new JSONObject();
    mouse.put("track", true);
    mouse.put("trackFormatter", new JSONLiteral("dateFormatter(obj)").toString());
    options.put("mouse", mouse);

    config.put("options", options);

    logger.debug("## config : " + config.toString());

}

/**
 * @return the startTime
 */
public Date getStartTime() {
    return startTime;
}

/**
 * @param startTime
 *            the startTime to set
 */
public void setStartTime(Date startTime) {
    this.startTime = startTime;
}
}

mymouseeventhandler.js:

function dateTracker(obj){
    var dateToDisplay = new Date(parseInt(obj.x)); 
    var fullYear = dateToDisplay.getFullYear();
    var month = dateToDisplay.getMonth();
    var date = dateToDisplay.getDate();
    return  ( +fullYear+ - +month+ - +date+  ,  +obj.y+ ) ; 
}
问题回答

The function is not well recognized because of double quote "" added when creating JSONObject. Here is a solution to avoid double quote :

...
mouse.put("trackFormatter", new JSONLiteral(
(""function(obj){ return dateTracker(obj) }"").replace(""", "")));
...

I hope it will help someone else :)





相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签