{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "description": "Ventas por subcategoría que resaltan los demás visuales con un clic y muestran la parte que resalta otro visual (resaltado cruzado, cross-highlight). Por CSalcedoDataBI · https://csalcedodatabi.com/deneb/resaltado-cruzado-deneb",
  "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": true,
      "dataPointLimit": 50
    },
    "information": {
      "name": "Resaltado cruzado (cross-highlight) en Deneb 2",
      "description": "Ventas por subcategoría que resaltan los demás visuales con un clic y dibujan la parte que resalta otro visual. Autor: Cristobal Salcedo Beltrán.",
      "author": "Cristobal Salcedo Beltrán",
      "uuid": "bff83c10-8c7b-4b62-a455-9a0a417a70a4",
      "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": "Total Sales",
          "description": "",
          "type": "numeric",
          "kind": "measure",
          "supportFieldConfiguration": {
            "highlight": true,
            "highlightStatus": false,
            "highlightComparator": false,
            "format": true,
            "formatted": false,
            "names": false
          }
        }
      ]
    }
  },
  "data": {
    "name": "dataset"
  },
  "width": "container",
  "height": "container",
  "autosize": {
    "type": "fit",
    "contains": "padding"
  },
  "padding": {
    "top": 8,
    "right": 16,
    "bottom": 8,
    "left": 8
  },
  "transform": [
    {
      "filter": "isValid(datum['__dataset.1__'])"
    },
    {
      "calculate": "datum['__dataset.1__']",
      "as": "ventas"
    },
    {
      "calculate": "!isDefined(datum['__dataset.1____highlight']) ? datum.ventas : isValid(datum['__dataset.1____highlight']) ? datum['__dataset.1____highlight'] : 0",
      "as": "resaltado"
    },
    {
      "calculate": "datum.resaltado !== datum.ventas ? 1 : 0",
      "as": "difiere"
    },
    {
      "calculate": "abs(datum.ventas)",
      "as": "valorAbs"
    },
    {
      "joinaggregate": [
        {
          "op": "max",
          "field": "difiere",
          "as": "hayResaltado"
        },
        {
          "op": "sum",
          "field": "ventas",
          "as": "totalVentas"
        },
        {
          "op": "mean",
          "field": "ventas",
          "as": "media"
        },
        {
          "op": "count",
          "as": "n"
        },
        {
          "op": "max",
          "field": "valorAbs",
          "as": "maxAbs"
        }
      ]
    },
    {
      "window": [
        {
          "op": "rank",
          "as": "puesto"
        }
      ],
      "sort": [
        {
          "field": "ventas",
          "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": "datum.unidad === 1 ? pbiFormat(datum.ventas, datum['__dataset.1____format']) : pbiFormat(datum.ventas, datum['__dataset.1____format'], {value: datum.unidad, precision: datum.decimales})",
      "as": "total"
    },
    {
      "calculate": "datum.unidad === 1 ? pbiFormat(datum.resaltado, datum['__dataset.1____format']) : pbiFormat(datum.resaltado, datum['__dataset.1____format'], {value: datum.unidad, precision: datum.decimales})",
      "as": "parte"
    },
    {
      "calculate": "datum.hayResaltado === 1 ? datum.parte + ' de ' + datum.total : datum.total",
      "as": "etiqueta"
    },
    {
      "calculate": "pbiFormat(datum.ventas / datum.totalVentas, '0.0%')",
      "as": "shareOfTotal"
    },
    {
      "calculate": "datum.hayResaltado === 1 ? pbiFormat(datum.resaltado / datum.ventas, '0.0%') : 'sin resaltado'",
      "as": "highlightShare"
    },
    {
      "calculate": "datum.puesto + ' de ' + datum.n",
      "as": "puestoTexto"
    },
    {
      "calculate": "'Media: ' + (datum.unidad === 1 ? pbiFormat(datum.media, datum['__dataset.1____format']) : pbiFormat(datum.media, datum['__dataset.1____format'], {value: datum.unidad, precision: datum.decimales}))",
      "as": "mediaEtiqueta"
    }
  ],
  "title": {
    "text": "Ventas por subcategoría",
    "subtitle": "Clic en un país: resalta su parte en cada barra · clic en una barra: resalta los países",
    "anchor": "start",
    "fontSize": 14,
    "subtitleFontSize": 11,
    "color": "#1F2328",
    "subtitleColor": "#5F6670",
    "offset": 22
  },
  "encoding": {
    "y": {
      "field": "__dataset.0__",
      "type": "nominal",
      "sort": {
        "field": "ventas",
        "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_Total",
      "params": [
        {
          "name": "__select__",
          "select": {
            "type": "point",
            "on": "click",
            "clear": false
          }
        }
      ],
      "mark": {
        "type": "bar",
        "color": "#2F6B9A",
        "cornerRadiusEnd": 3,
        "height": {
          "band": 0.7
        },
        "cursor": "pointer"
      },
      "encoding": {
        "x": {
          "field": "ventas",
          "type": "quantitative",
          "axis": null,
          "scale": {
            "zero": true,
            "nice": false
          }
        },
        "opacity": {
          "condition": [
            {
              "test": "datum.hayResaltado === 1",
              "value": 0.25
            },
            {
              "test": "datum.__selected__ === 'off'",
              "value": 0.3
            }
          ],
          "value": 1
        },
        "tooltip": [
          {
            "field": "__dataset.0__",
            "type": "nominal",
            "title": "Subcategoría"
          },
          {
            "field": "total",
            "type": "nominal",
            "title": "Ventas"
          },
          {
            "field": "shareOfTotal",
            "type": "nominal",
            "title": "Peso en el total"
          },
          {
            "field": "puestoTexto",
            "type": "nominal",
            "title": "Puesto"
          },
          {
            "field": "highlightShare",
            "type": "nominal",
            "title": "Parte resaltada"
          }
        ]
      }
    },
    {
      "name": "Mark_Highlight",
      "transform": [
        {
          "filter": "datum.hayResaltado === 1"
        }
      ],
      "mark": {
        "type": "bar",
        "color": "#2F6B9A",
        "cornerRadiusEnd": 3,
        "height": {
          "band": 0.7
        }
      },
      "encoding": {
        "x": {
          "field": "resaltado",
          "type": "quantitative"
        }
      }
    },
    {
      "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": "ventas",
          "type": "quantitative"
        },
        "text": {
          "field": "etiqueta",
          "type": "nominal"
        }
      }
    },
    {
      "name": "Mark_Label",
      "mark": {
        "type": "text",
        "align": "left",
        "dx": 6,
        "fontSize": 12,
        "color": "#3B3F45"
      },
      "encoding": {
        "x": {
          "field": "ventas",
          "type": "quantitative"
        },
        "text": {
          "field": "etiqueta",
          "type": "nominal"
        }
      }
    }
  ]
}