2

I tried to make two area charts using the Deneb visual of power bi and vega lite language.

enter image description here

Two things I struggled to do :

  • The values under 1000 (700 for example) I want to display them as 0.7K (The current format doesn't seem to convert them , it only formats the values above 1000)
  • I want to highlight the maximum value in GREEN and minimum value in RED for both charts

Here's the full code:

{
  "data": {"name": "dataset"},
  "layer": [
    {
      "mark": {
        "type": "area",
        "line": {"color": "#063970"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {"offset": 0, "color": "white"},
            {"offset": 1, "color": "#063970"}
          ]
        }
      }
    },
    {
      "mark": {
        "type": "area",
        "line": {"color": "#2596be"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {"offset": 0, "color": "white"},
            {"offset": 1, "color": "#2596be"}
          ]
        }
      },
      "encoding": {
        "y": {"field": "Variable 1", "type": "quantitative"}
      }
    },
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "Variable 2", "format": ".2s"},
        "opacity": {
          "condition": {"test": {"field": "MONTH", "equal": "off"}, "value": 0.1},
          "value": 1
        },
        "y": {"field": "Variable 2", "type": "quantitative", "axis": null}
      }
    },
    
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "Variable 1", "format": ".2s"
},
        "opacity": {
          "condition": {"test": {"field": "MONTH", "equal": "off"}, "value": 0.1},
          "value": 1
        },
        "y": {"field": "Variable 1", "type": "quantitative", "axis": null}
      }
    }
  ],
  "encoding": {
    "x": {
      "field": "MONTH",
      "type": "ordinal",
      "axis": {"labelPadding": 0},
      "title": null
    },
    "y": {
      "field": "Variable 2",
      "type": "quantitative",
      "axis": null
    }
  }
}
userrr
  • 197
  • 7
  • Can you supply a .pbix with sample data? – Davide Bacci Jun 24 '23 at 17:23
  • @DavideBacci hey davide , Here's the drive link to my file. I'd really appreciate your help a lot , I've been stuck for more than 24 hours. by the way I'm also open to any recommendations design wise. My code changed a bit compared to yesterday , I added circle markers to my graph. I want to display maximum values for both charts in red (label & circle color) – userrr Jun 25 '23 at 11:44
  • @DavideBacci dashboard link : https://drive.google.com/file/d/1JhytknLBTKhYcQyctj-XqNbclR2A7DK7/view?usp=sharing – userrr Jun 25 '23 at 11:48
  • @DavideBacci hi , do you know how can I add a forecast line for the year 2023 (for the future months only) like starting June etc.. – userrr Jul 01 '23 at 19:11
  • Hi @Mariam - please ask a brand new question with sample data and expected results and either myself or someone else will try to help. – Davide Bacci Jul 02 '23 at 08:16

1 Answers1

2

Here you go. d3format won't do what you want so just roll your own format using transforms and expressions.

enter image description here

{
  "data": {"name": "dataset"},
  "transform": [
    {
      "calculate": "format(datum['Leaves Count 2022']/1000,'0.1f')+'k'",
      "as": "l1"
    },
    {
      "calculate": "format(datum['Leaves Count 2023']/1000,'0.1f')+'k'",
      "as": "l2"
    },
    {
      "joinaggregate": [
        {
          "op": "max",
          "field": "Leaves Count 2022",
          "as": "l1max"
        },
        {
          "op": "max",
          "field": "Leaves Count 2023",
          "as": "l2max"
        },
        {
          "op": "min",
          "field": "Leaves Count 2022",
          "as": "l1min"
        },
        {
          "op": "min",
          "field": "Leaves Count 2023",
          "as": "l2min"
        }
      ]
    }
  ],
  "layer": [
    {
      "mark": {
        "type": "area",
        "line": {"color": "#063970"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {
              "offset": 0,
              "color": "white"
            },
            {
              "offset": 1,
              "color": "#063970"
            }
          ]
        }
      }
    },
    {
      "mark": {
        "type": "area",
        "line": {"color": "#2596be"},
        "color": {
          "x1": 1,
          "y1": 1,
          "gradient": "linear",
          "stops": [
            {
              "offset": 0,
              "color": "white"
            },
            {
              "offset": 1,
              "color": "#2596be"
            }
          ]
        }
      },
      "encoding": {
        "y": {
          "field": "Leaves Count 2022",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "circle",
        "size": 50,
        "fill": {
          "expr": "datum['Leaves Count 2023']==datum.l2max? 'green':datum['Leaves Count 2023']==datum.l2min?'red':'#063970'"
        },
        "stroke": "white",
        "strokeWidth": 1
      },
      "encoding": {
        "x": {
          "field": "MONTH",
          "type": "ordinal"
        },
        "y": {
          "field": "Leaves Count 2023",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "circle",
        "size": 50,
        "fill": {
          "expr": "datum['Leaves Count 2022']==datum.l1max? 'green':datum['Leaves Count 2022']==datum.l1min?'red':'#2596be'"
        },
        "stroke": "white",
        "strokeWidth": 1
      },
      "encoding": {
        "x": {
          "field": "MONTH",
          "type": "ordinal"
        },
        "y": {
          "field": "Leaves Count 2022",
          "type": "quantitative"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "l2"},
        "opacity": {
          "condition": {
            "test": {
              "field": "MONTH",
              "equal": "off"
            },
            "value": 0.1
          },
          "value": 1
        },
        "y": {
          "field": "Leaves Count 2023",
          "type": "quantitative",
          "axis": null
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "yOffset": -10,
        "size": 10,
        "color": "#000000"
      },
      "encoding": {
        "text": {"field": "l1"},
        "opacity": {
          "condition": {
            "test": {
              "field": "MONTH",
              "equal": "off"
            },
            "value": 0.1
          },
          "value": 1
        },
        "y": {
          "field": "Leaves Count 2022",
          "type": "quantitative",
          "axis": null
        }
      }
    }
  ],
  "encoding": {
    "x": {
      "field": "MONTH",
      "type": "ordinal",
      "axis": {"labelPadding": 0},
      "title": null
    },
    "y": {
      "field": "Leaves Count 2023",
      "type": "quantitative",
      "axis": null
    }
  }
}
Davide Bacci
  • 16,647
  • 3
  • 10
  • 36