DougBurke / hvega

Create Vega-Lite visualizations from Haskell.
BSD 3-Clause "New" or "Revised" License
51 stars 11 forks source link

Get haskell code for schema from vega editor #182

Open flip111 opened 3 years ago

flip111 commented 3 years ago

I made a nice graph in the https://vega.github.io/editor/ is there any way to convert it to the haskell code needed for it?

DougBurke commented 3 years ago

Unfortunately the hvega code doesn't map cleanly to the vega-lite schema, which makes converting from schema to code potentially hard. There be ways to map certain constructs easily, but my off-the-cuff bet (with no research) is that there's no easy generic solution. If you have the schema I can try to come up with something manually (and this may helo identify whether it could be automated).

flip111 commented 3 years ago

Hi @DougBurke that's very kind :) I made the conversion now manually without running the code yet. Didn't know the haskell code was not an exact 1-on-1 mapping to vega-lite v4.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "blabla",
  "data": {
    "values": [
      {"name": "Ticket 1", "id": 1222, "storypoints": 6, "timelogged": 20},
      {"name": "Ticket 2", "id": 1223, "storypoints": 2, "timelogged": 30},
      {"name": "Ticket 3", "id": 1222, "storypoints": 5, "timelogged": 20}
    ]
  },
  "transform": [
    {"calculate": "datum.timelogged / datum.storypoints", "as": "velocity"}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "velocity", "type": "quantitative", "title": null},
    "y": {
      "field": "name",
      "type": "nominal",
      "title": null,
      "axis": {
        "offset": 5,
        "ticks": false,
        "domain": false,
        "orient": "right"
      },
      "sort": {"field": "velocity"}
    },
    "x2": {"datum": 7}
  },
  "config": {}
}
let data_rows = dataFromRows [ Parse [ ( "Year", FoDate "%Y" ) ] ]
              . dataRow [ ( "name", Str "Ticket 1" ), ( "storypoints", Number 6 ), ( "timelogged", Number 20 )]
              . dataRow [ ( "name", Str "Ticket 2" ), ( "storypoints", Number 2 ), ( "timelogged", Number 20 )]
              . dataRow [ ( "name", Str "Ticket 3" ), ( "storypoints", Number 5 ), ( "timelogged", Number 20 )]

    enc = encoding
            . position X (PName "velocity", PmType Quantitative, PNoTitle)
            . position Y (PName "name", PmType Nominal, PNoTitle, PAxis [ AxOffset 5, AxTicks False, AxDomain False, AxOrient SRight], PSort [ByChannel ChX])
            . position X (PDatum $ Number 7)

    cfg = configure
            . configuration (Axis [ GridWidth 8 ])
            . configuration (AxisX [ GridColor "red" ])
            . configuration (AxisY [ GridColor "blue" ])

    tsfr = transform
            . calculateAs "datum.timelogged / datum.storypoints" "velocity"

in toVegaLite [ data_rows []
              , enc []
              , tsfr
              , mark Bar []
              ]