Class ChartOptions

  • All Implemented Interfaces:
    Serializable, Cloneable, Map<String,​Object>, IClusterable, JavaScriptable, Jsonable

    public class ChartOptions
    extends HashMap<String,​Object>
    implements IClusterable, JavaScriptable, Jsonable
    Specialization of HashMap<String, Object> to hold chart options and generate JSON to represent them. ChartOptions can be nested, so more complex settings are realized in JSON output. For instance
    
     ChartOptions opts = new ChartOptions("options");
     opts.put("title", "Metar Temperatur");
     //        opts.put("width", 400);
     //        opts.put("height", 300);
    
     ChartOptions hAxisOpts = new ChartOptions();
    
     ChartOptions vAxisOpts = new ChartOptions();
     ChartOptions vAxisGridline = new ChartOptions();
     vAxisGridline.put("count", 8);
     vAxisOpts.put("gridlines", vAxisGridline);
     opts.put("vAxis", vAxisOpts);
      
    results in var options = {"vAxis":{"gridlines":{"count":8}},"title":"Metar Temperatur"};

    The root instance must have a name so an identifier for the variable ist given. Values can be models (instanceof IModel). If the ChartOptions are rendered to JavaScript by toJavaScript(), getObject() will be applied automatically to the models to retreive the value that goes to JavaScript. This is even possible if values are JSONObjects with Models as values, since a subclass of JSONStringer is used, that dereferences the models before rendering the value.

    Author:
    Dieter Tremel
    See Also:
    ModelAwareJSONStringer, Serialized Form
    • Constructor Detail

      • ChartOptions

        public ChartOptions()
        Default constructor.
      • ChartOptions

        public ChartOptions​(String name)
        Constructor defining name.
        Parameters:
        name - The name is used to render a variable name in javascript.
      • ChartOptions

        public ChartOptions​(String name,
                            Map<? extends String,​? extends Object> m)
        Constructor from another map.
        Parameters:
        name - The name is used to render a variable name in javascript.
        m - Map to initialize from.
    • Method Detail

      • fromJson

        public static ChartOptions fromJson​(String json)
        Build an instance from a JSON string for instance as seen in tutorials. For users, that find coding in JSON easier than building nested ChartOptions, this my be an additional offer. Implemented as fromJson(new JSONObject(json));, see fromJson(com.github.openjson.JSONObject).
        Parameters:
        json - JSON string like {'title':'How Much Pizza I Ate Last Night','width':400,'height':300}
        Returns:
        New instance with data from the JSON String.
      • fromJson

        public static ChartOptions fromJson​(com.github.openjson.JSONObject json)
        Build instance from a JSONobject.
        Parameters:
        json - JSON object to build map from. If this contains other JSONObjects nested, a corresponding nested structure of ChartOptions will be built.
        Returns:
        New instance with data from the JSONObject.
      • getName

        public String getName()
      • setName

        public void setName​(String name)
      • getModelObjects

        public ChartOptions getModelObjects()
        Create a deep copy of the instance, where all models (instanceof IModel) are replaced with there value by getObject().
        Returns:
        Deep copy with all models replaced.
      • toJavaScript

        public String toJavaScript()
        Description copied from interface: JavaScriptable
        Return the JavaScript part.
        Specified by:
        toJavaScript in interface JavaScriptable
        Returns:
        JavaScript String for use as part of chart declaration.
      • toJavaScript

        public String toJavaScript​(String varname)
        Generate declaration js for options variable.
        Parameters:
        varname - Name of options variable identifier.
        Returns:
        Declaration js
      • toJSON

        public com.github.openjson.JSONObject toJSON()
        Description copied from interface: Jsonable
        Create the JSON serialization of the instance.
        Specified by:
        toJSON in interface Jsonable
        Returns:
        Json object for the instance.