How to draw diagonal rule in Vega Lite? - Stack Overflow

admin2025-04-29  1

This code should produce diagonal line from [x=0, y=0] to [x2=1, y2=1], but produces empty chart playground:

{
  "encoding": {
    "x": {
      "value": 0,
      "type": "quantitative"
    },
    "x2": {
      "value": 1,
      "type": "quantitative"
    },
    "y": {
      "value": 0,
      "type": "quantitative"
    },
    "y2": {
      "value": 1,
      "type": "quantitative"
    }
  },
  "mark": {
    "type": "rule"
  }
}

I want to set data points explicitly with the value, not reference it from the data, as the actual plot is more complicated and rule needs to be added separately from the data.

This code should produce diagonal line from [x=0, y=0] to [x2=1, y2=1], but produces empty chart playground:

{
  "encoding": {
    "x": {
      "value": 0,
      "type": "quantitative"
    },
    "x2": {
      "value": 1,
      "type": "quantitative"
    },
    "y": {
      "value": 0,
      "type": "quantitative"
    },
    "y2": {
      "value": 1,
      "type": "quantitative"
    }
  },
  "mark": {
    "type": "rule"
  }
}

I want to set data points explicitly with the value, not reference it from the data, as the actual plot is more complicated and rule needs to be added separately from the data.

Share Improve this question asked Jan 7 at 3:29 Alex CraftAlex Craft 15.5k14 gold badges90 silver badges156 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Two changes to make:

  1. You need data, even if empty.
  2. The property for the literal values here is "datum", not "value". I don't fully understand the difference between the two, but I believe datum is used when there is an existing scale/domain in play (the x- and y-axes), whereas value is used in the absence of a preexisting scale/domain (e.g. color and shape of points in a plot that doesn't assign them to a channel).
{
  "encoding": {
    "x": {"datum": 0, "type": "quantitative"},
    "x2": {"datum": 1, "type": "quantitative"},
    "y": {"datum": 0, "type": "quantitative"},
    "y2": {"datum": 1, "type": "quantitative"}
  },
  "mark": {"type": "rule"},
  "data": {"values": [{}]}
}
转载请注明原文地址:http://anycun.com/QandA/1745933822a91322.html