{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "Barras por subcategoría con la medida que elige un segmentador (parámetro de campo de medidas). Cada medida conserva su formato del modelo. Por CSalcedoDataBI · https://csalcedodatabi.com/deneb/segmentador-medidas-parametro-campo",
  "usermeta": {
    "deneb": {
      "build": "2.0.1.0",
      "metaVersion": 2,
      "provider": "vegaLite",
      "providerVersion": "6.4.3"
    },
    "interactivity": {
      "tooltip": true,
      "contextMenu": false,
      "selection": true,
      "selectionMode": "simple",
      "highlight": false,
      "dataPointLimit": 50
    },
    "information": {
      "name": "Segmentador de medidas con parámetro de campo",
      "description": "Barras por categoría con la medida que elige un segmentador (parámetro de campo); cada medida llega con su nombre y su formato. Autor: Cristobal Salcedo Beltrán.",
      "author": "Cristobal Salcedo Beltrán",
      "uuid": "c0d49f4c-97de-48f9-9144-1a27b9becc0f",
      "generated": "2026-12-03T00:47:39.000Z",
      "previewImageBase64PNG": ""
    },
    "config": "{\n  \"view\": {\n    \"stroke\": null\n  }\n}",
    "datasets": {
      "dataset": [
        {
          "key": "__dataset.0__",
          "name": "SubCategoryName",
          "description": "",
          "type": "text",
          "kind": "column",
          "supportFieldConfiguration": {
            "highlight": false,
            "highlightStatus": false,
            "highlightComparator": false,
            "format": false,
            "formatted": false,
            "names": false
          }
        },
        {
          "key": "__dataset.1__",
          "name": "Medida Fields",
          "description": "",
          "type": "numeric",
          "kind": "parameter",
          "supportFieldConfiguration": {
            "highlight": false,
            "highlightStatus": false,
            "highlightComparator": false,
            "format": true,
            "formatted": false,
            "names": true
          }
        }
      ]
    }
  },
  "data": {
    "name": "dataset"
  },
  "width": "container",
  "height": "container",
  "autosize": {
    "type": "fit",
    "contains": "padding"
  },
  "padding": {
    "top": 56,
    "right": 16,
    "bottom": 8,
    "left": 8
  },
  "transform": [
    {
      "calculate": "datum['__dataset.1__'][0]",
      "as": "valor"
    },
    {
      "calculate": "datum['__dataset.1____names'][0]",
      "as": "medida"
    },
    {
      "calculate": "datum['__dataset.1____format'][0]",
      "as": "formato"
    },
    {
      "calculate": "length(datum['__dataset.1__'])",
      "as": "elegidas"
    },
    {
      "filter": "isValid(datum.valor)"
    },
    {
      "calculate": "abs(datum.valor)",
      "as": "valorAbs"
    },
    {
      "joinaggregate": [
        {
          "op": "mean",
          "field": "valor",
          "as": "media"
        },
        {
          "op": "count",
          "as": "n"
        },
        {
          "op": "max",
          "field": "valorAbs",
          "as": "maxAbs"
        }
      ]
    },
    {
      "window": [
        {
          "op": "rank",
          "as": "puesto"
        }
      ],
      "sort": [
        {
          "field": "valor",
          "order": "descending"
        }
      ]
    },
    {
      "calculate": "datum.maxAbs >= 1e6 ? 1e6 : datum.maxAbs >= 1e4 ? 1e3 : 1",
      "as": "unidad"
    },
    {
      "calculate": "datum.unidad === 1 || datum.maxAbs / datum.unidad >= 100 ? 0 : 1",
      "as": "decimales"
    },
    {
      "calculate": "indexof(datum.formato, '%') >= 0 || datum.unidad === 1 ? pbiFormat(datum.valor, datum.formato) : pbiFormat(datum.valor, datum.formato, {value: datum.unidad, precision: datum.decimales})",
      "as": "etiqueta"
    },
    {
      "calculate": "'Media: ' + (indexof(datum.formato, '%') >= 0 || datum.unidad === 1 ? pbiFormat(datum.media, datum.formato) : pbiFormat(datum.media, datum.formato, {value: datum.unidad, precision: datum.decimales}))",
      "as": "mediaEtiqueta"
    },
    {
      "calculate": "(datum.valor >= datum.media ? '+' : '') + pbiFormat(datum.valor - datum.media, datum.formato)",
      "as": "vsAverage"
    },
    {
      "calculate": "datum.puesto + ' de ' + datum.n",
      "as": "puestoTexto"
    },
    {
      "calculate": "datum.medida + ' por subcategoría' + (datum.elegidas > 1 ? ' (elige una sola medida)' : '')",
      "as": "titulo"
    },
    {
      "calculate": "max(datum.valor, 0)",
      "as": "xEtiqueta"
    }
  ],
  "encoding": {
    "y": {
      "field": "__dataset.0__",
      "type": "nominal",
      "sort": {
        "field": "valor",
        "op": "max",
        "order": "descending"
      },
      "axis": {
        "title": null,
        "domain": false,
        "ticks": false,
        "labelFontSize": 12,
        "labelColor": "#3B3F45",
        "labelPadding": 8,
        "labelLimit": 240
      }
    }
  },
  "layer": [
    {
      "name": "Mark_Annotation_Media",
      "transform": [
        {
          "filter": "datum.puesto === 1"
        }
      ],
      "mark": {
        "type": "rule",
        "color": "#8A8F98",
        "strokeDash": [
          4,
          3
        ]
      },
      "encoding": {
        "x": {
          "field": "media",
          "type": "quantitative",
          "axis": null,
          "scale": {
            "zero": true,
            "nice": false
          }
        },
        "y": {
          "value": 0
        },
        "y2": {
          "value": {
            "expr": "height"
          }
        }
      }
    },
    {
      "name": "Mark_Bar",
      "params": [
        {
          "name": "__select__",
          "select": {
            "type": "point",
            "on": "click",
            "clear": false
          }
        }
      ],
      "mark": {
        "type": "bar",
        "cornerRadiusEnd": 3,
        "height": {
          "band": 0.7
        },
        "cursor": "pointer"
      },
      "encoding": {
        "x": {
          "field": "valor",
          "type": "quantitative",
          "axis": null,
          "scale": {
            "zero": true,
            "nice": false
          }
        },
        "color": {
          "condition": {
            "test": "datum.valor < 0",
            "value": "#B5483B"
          },
          "value": "#2F6B9A"
        },
        "opacity": {
          "condition": {
            "test": "datum.__selected__ === 'off'",
            "value": 0.3
          },
          "value": 1
        },
        "tooltip": [
          {
            "field": "__dataset.0__",
            "type": "nominal",
            "title": "Subcategoría"
          },
          {
            "field": "medida",
            "type": "nominal",
            "title": "Medida"
          },
          {
            "field": "etiqueta",
            "type": "nominal",
            "title": "Valor"
          },
          {
            "field": "puestoTexto",
            "type": "nominal",
            "title": "Puesto"
          },
          {
            "field": "vsAverage",
            "type": "nominal",
            "title": "Frente a la media"
          }
        ]
      }
    },
    {
      "name": "Mark_Annotation_MediaTexto_Halo",
      "transform": [
        {
          "filter": "datum.puesto === 1"
        }
      ],
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "bottom",
        "dx": 4,
        "fontSize": 11,
        "color": "#FFFFFF",
        "stroke": "#FFFFFF",
        "strokeWidth": 7,
        "strokeJoin": "round",
        "strokeOpacity": 1
      },
      "encoding": {
        "x": {
          "field": "media",
          "type": "quantitative"
        },
        "y": {
          "value": -2
        },
        "text": {
          "field": "mediaEtiqueta",
          "type": "nominal"
        }
      }
    },
    {
      "name": "Mark_Annotation_MediaTexto",
      "transform": [
        {
          "filter": "datum.puesto === 1"
        }
      ],
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "bottom",
        "dx": 4,
        "fontSize": 11,
        "color": "#5F6670"
      },
      "encoding": {
        "x": {
          "field": "media",
          "type": "quantitative"
        },
        "y": {
          "value": -2
        },
        "text": {
          "field": "mediaEtiqueta",
          "type": "nominal"
        }
      }
    },
    {
      "name": "Mark_Label_Halo",
      "mark": {
        "type": "text",
        "align": "left",
        "dx": 6,
        "fontSize": 12,
        "color": "#FFFFFF",
        "stroke": "#FFFFFF",
        "strokeWidth": 7,
        "strokeJoin": "round",
        "strokeOpacity": 1
      },
      "encoding": {
        "x": {
          "field": "xEtiqueta",
          "type": "quantitative"
        },
        "text": {
          "field": "etiqueta",
          "type": "nominal"
        }
      }
    },
    {
      "name": "Mark_Label",
      "mark": {
        "type": "text",
        "align": "left",
        "dx": 6,
        "fontSize": 12,
        "color": "#3B3F45"
      },
      "encoding": {
        "x": {
          "field": "xEtiqueta",
          "type": "quantitative"
        },
        "text": {
          "field": "etiqueta",
          "type": "nominal"
        }
      }
    },
    {
      "name": "Mark_Title",
      "transform": [
        {
          "filter": "datum.puesto === 1"
        }
      ],
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "bottom",
        "fontSize": 14,
        "fontWeight": "bold",
        "color": "#1F2328"
      },
      "encoding": {
        "x": {
          "value": 0
        },
        "y": {
          "value": -36
        },
        "text": {
          "field": "titulo",
          "type": "nominal"
        }
      }
    },
    {
      "name": "Mark_Subtitle",
      "transform": [
        {
          "filter": "datum.puesto === 1"
        }
      ],
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "bottom",
        "fontSize": 11,
        "color": "#5F6670"
      },
      "encoding": {
        "x": {
          "value": 0
        },
        "y": {
          "value": -20
        },
        "text": {
          "value": "Cambia la medida en el segmentador: cada una llega con su formato del modelo"
        }
      }
    }
  ]
}