{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.canvasxpress.org/schema/cxplot-1.0.schema.json",
  "title": "CanvasXpress cxplot grammar snapshot",
  "description": "JSON Schema for the materialized cxplot grammar-of-graphics snapshot — the plain-value form of a figure's grammar (layers, scales, coordinates, facets). The live cxplot read-model is accessor-shaped (each slot is a function) and is not itself serializable; this schema targets its materialized projection. schemaVersion and materialized are the contract fields; the grammar slots mirror the figure's grammar. Lenient (additionalProperties allowed) where runtime detail is engine-internal.",
  "type": "object",
  "required": [
    "schemaVersion",
    "materialized",
    "mapping",
    "layers"
  ],
  "properties": {
    "schemaVersion": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+$",
      "description": "Semver MAJOR.MINOR of this grammar contract. NEW field (does not exist in the engine yet — added by ). Readers ignore unknown keys within a MAJOR line; a MAJOR bump ships a migration."
    },
    "materialized": {
      "const": true,
      "description": "Marks a plain-value snapshot (as opposed to the live accessor-shaped meta.cxplot)."
    },
    "data": {
      "type": "array",
      "description": "The post-wrangle data frame as an array of row objects (cx.getWrangled()).",
      "items": {
        "type": "object"
      }
    },
    "dataPipeline": {
      "type": "array",
      "description": "The data-grammar recipe: ordered dplyr/tidyr-style op records (group/segregate/transform/cluster) from cx.expandDataPipeline().",
      "items": {
        "type": "object",
        "properties": {
          "op": {
            "type": "string",
            "description": "Verb name, e.g. group, segregate, transform, cluster."
          }
        },
        "required": [
          "op"
        ],
        "additionalProperties": true
      }
    },
    "mapping": {
      "type": "object",
      "description": "Aesthetic bindings: aesthetic -> data column name (or null when unbound).",
      "properties": {
        "x": {
          "type": [
            "string",
            "array",
            "null"
          ],
          "items": {
            "type": "string"
          }
        },
        "y": {
          "type": [
            "string",
            "array",
            "null"
          ],
          "items": {
            "type": "string"
          }
        },
        "z": {
          "type": [
            "string",
            "array",
            "null"
          ],
          "items": {
            "type": "string"
          }
        },
        "color": {
          "type": [
            "string",
            "null"
          ]
        },
        "size": {
          "type": [
            "string",
            "null"
          ]
        },
        "shape": {
          "type": [
            "string",
            "null"
          ]
        },
        "pattern": {
          "type": [
            "string",
            "null"
          ]
        },
        "line": {
          "type": [
            "string",
            "null"
          ]
        },
        "linetype": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "additionalProperties": true
    },
    "layers": {
      "type": "array",
      "description": "Ordered geom layers from the geomRegistry preset for graphType plus any userLayers (addLayer).",
      "items": {
        "type": "object",
        "properties": {
          "geom": {
            "type": "string",
            "description": "Geometry name, e.g. point, line, bar, area, boxplot, tile."
          },
          "stat": {
            "type": "string",
            "description": "Statistical transform, e.g. identity, count, bin, density, smooth."
          },
          "when": {
            "type": [
              "string",
              "null"
            ],
            "description": "Optional gating condition for the layer."
          }
        },
        "required": [
          "geom"
        ],
        "additionalProperties": true
      }
    },
    "scales": {
      "type": "object",
      "description": "Positional + aesthetic scales. Positional binders x/y/smp/var/size; aesthetic palette refs colors/shapes/sizes/patterns/linePatterns. In the LIVE object these carry methods (map/mapValue/toPixel); in the materialized form they are descriptors.",
      "properties": {
        "x": {
          "type": "object"
        },
        "y": {
          "type": "object"
        },
        "smp": {
          "type": "object"
        },
        "var": {
          "type": "object"
        },
        "size": {
          "type": "object"
        },
        "colors": {},
        "shapes": {},
        "sizes": {},
        "patterns": {},
        "linePatterns": {}
      },
      "additionalProperties": true
    },
    "coord": {
      "type": "object",
      "description": "Coordinate system descriptor.",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "cartesian",
            "flip",
            "polar"
          ]
        },
        "invert": {
          "type": "boolean"
        }
      },
      "additionalProperties": true
    },
    "facet": {
      "type": "object",
      "description": "Small-multiples read-model.",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "none",
            "grid",
            "wrap"
          ]
        },
        "active": {
          "type": "boolean"
        },
        "bySamples": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "byVariables": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "rows": {
          "type": "integer"
        },
        "cols": {
          "type": "integer"
        },
        "topology": {
          "type": "string",
          "description": "\"RxC\" grid string."
        },
        "freeScales": {
          "type": "boolean"
        }
      },
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
