Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
AdamMG
Helper I
Helper I

Vega Lite - scatter plot

Hi Everyone.
Simple question 😉
How in Vega Lite draw line/rule (x=y) on scatterplot chart (basically line in 45 degrees)


8 REPLIES 8
AdamMG
Helper I
Helper I

haha.
Thanks @lbendlin 
I have it already. The biggest problem for me is to find max and put it as field.

Hi @AdamMG,


If you just want to draw a line across the width/height of the visible view, you can do something like this, where you create a layer with a simple, empty dataset (like the fixed line example) :

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/cars.json"},
  "layer": [
    {
      "data": {"values": [{}]},
      "mark": "rule",
      "encoding": {
        "x": {"value": 0},
        "x2": {"value": "width"},
        "y": {"value": "height"},
        "y2": {"value": 0}
      }
    },
    {
      "mark": "point",
      "encoding": {
        "x": {"field": "Horsepower"},
        "y": {"field": "Miles_per_Gallon"}
      }
    }
  ],
  "encoding": {"x": {"type": "quantitative"}, "y": {"type": "quantitative"}}
}

dmp_0-1711340374266.png

Link to sample specification

 

You could also use the expression language to interrogate the assigned scale domains, e.g.:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/cars.json"},
  "layer": [
    {
      "data": {"values": [{}]},
      "mark": "rule",
      "encoding": {
        "x": {"datum": {"expr": "domain('x')[0]"}},
        "x2": {"datum": {"expr": "domain('x')[1]"}},
        "y": {"datum": {"expr": "domain('y')[0]"}},
        "y2": {"datum": {"expr": "domain('y')[1]"}}
      }
    },
    {
      "mark": "point",
      "encoding": {
        "x": {"field": "Horsepower"},
        "y": {"field": "Miles_per_Gallon"}
      }
    }
  ],
  "encoding": {"x": {"type": "quantitative"}, "y": {"type": "quantitative"}}
}

 

Link to sample specification


If you want to do something fancier with the range of data for each encoding channel, this may be possible but would require a sample of your dataset to ensure that I could come up with an optimal approach. If you're not able to share, you might be able to consider an aggregate transform, to find the min/max of each series and plot based on those, or possibly a regression transform may be an option.

 

Hopefully, some of this may help you come up with more ideas. And thanks very much for trying Deneb!

 

Regards,

 

Daniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Hi @dm-p 
Thank you very much.
Your solution is very good but not good for my chart.
I need to pan and zoom the scatterplot and your solution does not reflect this.


You can do that in DAX, and add it as a column in the Deneb visual.

I can but I have like 150k points on the chart. Any additional data will have a huge impact on the performance

There's no meaningful way to show more than 5000 data points on a scatter plot.

You can pan and zoom...
I need to put all data on the chart.
I have also regression line so I need all data.

lbendlin
Super User
Super User

Find the max x and y values in your data, take the largest, and then add a layer that draws from 0 to that point.

Here is a simplified version:

 

lbendlin_0-1711212843488.png

 

{
  "layer": [
    {
      "data": {"name": "dataset"},
      "mark": "point",
      "encoding": {
        "y": {
          "field": "y",
          "type": "quantitative"
        },
        "x": {
          "field": "x",
          "type": "quantitative"
        }
      }
    },
    {
      "data": {
        "values": [
          {"x": 0, "y": 0},
          {"x": 4, "y": 4}
        ]
      },
      "mark": {
        "type": "line",
        "from": {"data": "data2"}
      },
      "encoding": {
        "y": {
          "field": "y",
          "type": "quantitative"
        },
        "x": {
          "field": "x",
          "type": "quantitative"
        }
      }
    }
  ]
}

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.