Example usage for org.apache.wicket.ajax.json JSONFunction JSONFunction

List of usage examples for org.apache.wicket.ajax.json JSONFunction JSONFunction

Introduction

In this page you can find the example usage for org.apache.wicket.ajax.json JSONFunction JSONFunction.

Prototype

public JSONFunction(CharSequence value) 

Source Link

Document

Function to be used to output the json value without quotes

Usage

From source file:com.vaynberg.wicket.select2.json.Json.java

License:Apache License

/**
 * Writes a key/value pair into the {@code writer} where {@code value} represents a javascript function and should
 * be written out unencoded if the value is not {@code null}
 *
 * @param writer/*from   w ww.  ja  v  a  2  s.  c o  m*/
 *            json writer
 * @param key
 *            key
 * @param value
 *            value
 * @throws JSONException
 */
public static void writeFunction(JSONStringer writer, String key, String value) throws JSONException {
    if (value != null) {
        writer.key(key).value(new JSONFunction(value));
    }
}

From source file:org.wicketstuff.gchart.DataCell.java

License:Apache License

/**
 * Convert a Calendar object to a JavaScript {@code Date()} call that
 * generates the same value. Return as a human readable JsonFunction
 * ({@code new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]])})
 * for rendering in JSONObjects.//  w  w  w .j  a  va  2s .  co m
 *
 * @param cal Calender value to render.
 * @return JavaScript Date function for the value.
 */
public static JSONFunction createExplicitJsDate(Calendar cal) {
    StringBuilder sb = new StringBuilder("new Date(");
    sb.append(cal.get(Calendar.YEAR));
    sb.append(", ").append(cal.get(Calendar.MONTH));
    sb.append(", ").append(cal.get(Calendar.DAY_OF_MONTH));
    sb.append(", ").append(cal.get(Calendar.HOUR_OF_DAY));
    sb.append(", ").append(cal.get(Calendar.MINUTE));
    if ((cal.isSet(Calendar.SECOND) && cal.get(Calendar.SECOND) != 0)
            || (cal.isSet(Calendar.MILLISECOND) && cal.get(Calendar.MILLISECOND) != 0)) {
        sb.append(", ").append(cal.get(Calendar.SECOND));
    }
    if (cal.isSet(Calendar.MILLISECOND) && cal.get(Calendar.MILLISECOND) != 0) {
        sb.append(", ").append(cal.get(Calendar.MILLISECOND));
    }
    sb.append(")");
    return new JSONFunction(sb.toString());
}

From source file:org.wicketstuff.gchart.DataCell.java

License:Apache License

/**
 * Convert a Calendar object to a JavaScript {@code Date()} call that
 * generates the same value. Return as a
 * JsonFunction({@code new Date(value);}) (milliseconds since epoch) for
 * rendering in JSONObjects .//  ww  w . j  a  va  2  s. c  o m
 *
 * @param cal Calender value to render.
 * @return JavaScript Date function for the value.
 */
public static JSONFunction createJsDate(Calendar cal) {
    StringBuilder sb = new StringBuilder("new Date(");
    sb.append(cal.getTimeInMillis());
    sb.append(")");
    return new JSONFunction(sb.toString());
}

From source file:org.wicketstuff.gchart.DataCell.java

License:Apache License

/**
 * Convert a Date object to a JavaScript {@code Date()} call that generates
 * the same value. Return as a//from w w w.ja  v  a 2s .co m
 * JsonFunction({@code new Date(value);})(milliseconds since epoch) for
 * rendering in JSONObjects.
 *
 * @param date Date value to render.
 * @return JavaScript Date function for the value.
 */
public static JSONFunction createJsDate(Date date) {
    StringBuilder sb = new StringBuilder("new Date(");
    sb.append(date.getTime());
    sb.append(")");
    return new JSONFunction(sb.toString());
}

From source file:org.wicketstuff.gchart.examples.HomePage.java

License:Apache License

/**
 * Line chart with two series and two axes according to
 * <a href="https://developers.google.com/chart/interactive/docs/gallery/linechart#dual-y-charts">Google's
 * example</a>. Make use of OptionBuiler. Shows use of {@link OptionModifier}
 * to demonstrate series configuring.//from   www  .  j a v a 2 s  .  c o m
 *
 * @return Line chart ready to use on page.
 */
private Chart createChartDualLine() {
    IComponentAssignedModel<ChartOptions> optionsModel = new ComponentModel<ChartOptions>() {
        private static final long serialVersionUID = 1L;

        @Override
        public ChartOptions getObject(Component component) {
            OptionBuilder builder = OptionBuilder.classic((Chart) component);
            ChartOptions opts = new ChartOptions("options");

            builder.title("Average Temperatures and Daylight in Iceland Throughout the Year");
            opts.put("width", 900);
            opts.put("height", 500);

            builder.axis("TEMP", "Temps (Celsius)").axis("DAYLIGHT", "Daylight");

            // change color of series 1 to orange to demonstrate use of modifiers
            OptionModifier daylightSeriesModifier = new OptionModifier() {

                @Override
                public void modify(ChartOptions options) {
                    options.put("color", "#FF7D0E");
                }
            };
            builder.mapSeries("TEMP").mapSeries("DAYLIGHT", daylightSeriesModifier);

            ChartOptions hAxisOpts = new ChartOptions();
            JSONArray ticksJSON = new JSONArray();
            for (int month = 0; month < 12; month++) {
                JSONFunction dateFun = new JSONFunction(String.format("new Date(2014, %d)", month));
                ticksJSON.put(dateFun);
            }
            hAxisOpts.put("ticks", ticksJSON);
            opts.put("hAxis", hAxisOpts);

            ChartOptions vAxisOpts = new ChartOptions();
            ChartOptions viewWindowOpts = new ChartOptions();
            viewWindowOpts.put("max", 30);
            vAxisOpts.put("viewWindow", viewWindowOpts);
            opts.put("vAxis", vAxisOpts);

            opts.putAll(builder.build());
            return opts;
        }
    };

    IComponentAssignedModel<DataTable> dataModel = new ComponentModel<DataTable>() {
        private static final long serialVersionUID = 1L;

        @Override
        public DataTable getObject(Component component) {

            List<ColumnDeclaration> colDefs;
            List<DataRow> rows;
            colDefs = new ArrayList<>();
            colDefs.add(new ColumnDeclaration(ColumnType.DATE, "Month"));
            colDefs.add(new ColumnDeclaration(ColumnType.NUMBER, "Average Temperature"));
            colDefs.add(new ColumnDeclaration(ColumnType.NUMBER, "Average Hours of Daylight"));

            Object[][] exampleData = new Object[][] {
                    { new GregorianCalendar(2014, Calendar.JANUARY, 1, 0, 0), -0.5, 5.7 },
                    { new GregorianCalendar(2014, Calendar.FEBRUARY, 1, 8, 0), .4, 8.7 },
                    { new GregorianCalendar(2014, Calendar.MARCH, 1, 0, 0), .5, 12 },
                    { new GregorianCalendar(2014, Calendar.APRIL, 1, 0, 0), 2.9, 15.3 },
                    { new GregorianCalendar(2014, Calendar.MAY, 1, 0, 0), 6.3, 18.6 },
                    { new GregorianCalendar(2014, Calendar.JUNE, 1, 0, 0), 9, 20.9 },
                    { new GregorianCalendar(2014, Calendar.JULY, 1, 0, 0), 10.6, 19.8 },
                    { new GregorianCalendar(2014, Calendar.AUGUST, 1, 0, 0), 10.3, 16.6 },
                    { new GregorianCalendar(2014, Calendar.SEPTEMBER, 1, 0, 0), 7.4, 13.3 },
                    { new GregorianCalendar(2014, Calendar.OCTOBER, 1, 0, 0), 4.4, 9.9 },
                    { new GregorianCalendar(2014, Calendar.NOVEMBER, 1, 0, 0), 1.1, 6.6 },
                    { new GregorianCalendar(2014, Calendar.DECEMBER, 1, 0, 0), -.2, 4.5 } };
            rows = DataTable.fromArray(exampleData);

            return new DataTable("data", colDefs, rows);
        }
    };
    final Chart chart = new Chart("chartDualLine", Model.of(ChartType.LINE), optionsModel, dataModel,
            chartLibLoader);
    chart.setResponsive(false);
    return chart;
}