Цвет шкалы VEGA CHART

Я хочу сделать каждую полосу на графике - в разном цвете. пожалуйста помоги.

поэтому диаграмма по умолчанию, как описано в документации: https://vega.github.io/vega/examples/bar-chart/

например - я хочу сделать что-то вроде: синий, красный, желтый, зеленый, синий, синий.

пожалуйста помоги! Благодарность!


person Dima Pog    schedule 06.11.2017    source источник


Ответы (4)


Используя пример гистограммы Vega, с которым вы связались:

Сначала создайте шкалу для определения цветов:

{
  "name": "colors",
  "type": "ordinal",
  "domain": {"data": "table", "field": "category"},
  "range": {"scheme": "category10"}
}

Значение для «схемы» может быть встроенной схемой от Vega, или нестандартная схема.

Затем вам нужно указать цвет заливки в наборе свойств метки update, указав имя схемы:

"update": {
    "fill": {"scale": "colors", "field": "category"}
},
person M Falanga    schedule 25.02.2019

У меня была та же проблема, и с небольшой помощью https://vega.github.io/vega-lite/examples/bar_color_disabled_scale.html Я пришел к следующему - идея состоит в том, чтобы закодировать цвет в данных:

    {
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A simple bar chart with embedded data.",
  "data": {
    "values": [
      {"a": "A", "b": 28, "c": "black"},
      {"a": "B", "b": 55, "c": "black"},
      {"a": "C", "b": 43, "c": "black"},
      {"a": "D", "b": 91, "c": "red"}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "ordinal"},
    "y": {"field": "b", "type": "quantitative"},
    "color": {"field": "c", "type": "nominal", "scale": null}
  }
}

в качестве альтернативы это также работает и сохраняет легенду, если она вам нужна:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A simple bar chart with embedded data.",
  "data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": 91}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "ordinal", "sort": ["C", "A", "B", "D"]},
    "y": {"field": "b", "type": "quantitative"},
    "color": {"field": "a", "type": "nominal", "sort": ["C", "A", "B", "D"], "scale": {"range": ["red", "black", "black", "black", "black"]}}
  }
}
person alex314159    schedule 18.10.2019

Для гистограмм

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A bar chart that directly encodes color names in the data.",
  "data": {
    "values": [
      {
        "color": "yellow",
        "b": 28
      },
      {
        "color": "green",
        "b": 55
      },
      {
        "color": "blue",
        "b": 43
      }
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "color",
      "type": "nominal"
    },
    "y": {
      "field": "b",
      "type": "quantitative"
    },
    "color": {
      "field": "color",
      "type": "nominal",
      "scale": null
    }
  }
}

Для круговых диаграмм

 {
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 200,
  "height": 200,
  "autosize": "none",

  "signals": [
{
  "name": "startAngle", "value": 0,
  "bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
},
{
  "name": "endAngle", "value": 6.29,
  "bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
},
{
  "name": "padAngle", "value": 0,
  "bind": {"input": "range", "min": 0, "max": 0.1}
},
{
  "name": "innerRadius", "value": 0,
  "bind": {"input": "range", "min": 0, "max": 90, "step": 1}
},
{
  "name": "cornerRadius", "value": 0,
  "bind": {"input": "range", "min": 0, "max": 10, "step": 0.5}
},
{
  "name": "sort", "value": false,
  "bind": {"input": "checkbox"}
}
  ],

  "data": [
{
  "name": "table",
  "values": [
    {"field": 10,"color": "blue"},
    {"field": 3,"color": "green"},
    {"field": 7,"color": "red"},
    {"field": 8,"color":"orange"},
    {"field": 15,"color":"yellow"}
  ],
  "transform": [
    {
      "type": "pie",
      "field": "field",
      "startAngle": {"signal": "startAngle"},
      "endAngle": {"signal": "endAngle"},
      "sort": {"signal": "sort"}
    }
  ]
}
  ],

  "scales": [
{
  "name": "color",
  "type": "ordinal",
  "domain": {"data": "table", "field": "color"},
  "range": {"scheme": "category20"}
}
  ],

  "marks": [
{
  "type": "arc",
  "from": {"data": "table"},
  "encode": {
    "enter": {
      "fill": { "field": "color",
              "type": "nominal",
            "scale": null},
      "x": {"signal": "width / 2"},
      "y": {"signal": "height / 2"}
    },
    "update": {
      "startAngle": {"field": "startAngle"},
      "endAngle": {"field": "endAngle"},
      "padAngle": {"signal": "padAngle"},
      "innerRadius": {"signal": "innerRadius"},
      "outerRadius": {"signal": "width / 2"},
      "cornerRadius": {"signal": "cornerRadius"}
    }
  }
}
  ]
}

   

person Vineet Gandhi    schedule 20.01.2020

Вы можете определить категориальную цветовую шкалу и использовать ее для изменения цвета полосы. Ознакомьтесь с некоторыми другими примерами, в которых используются категориальные цветовые шкалы.

person dominik    schedule 30.11.2017