Git Product home page Git Product logo

Comments (5)

PBI-David avatar PBI-David commented on May 25, 2024

This might be a Vega issue. Here is a corrected Vega spec changing the type to scale type to linear and the legend formatType to time.

image



{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "Stock prices of 5 Tech Companies over Time.",
  "background": "white",
  "padding": 5,
  "width": 200,
  "height": 200,
  "style": "cell",
  "data": [
    {
      "name": "source_0",
      "url": "data/stocks.csv",
      "format": {"type": "csv", "parse": {"date": "date"}, "delimiter": ","},
      "transform": [
        {
          "field": "date",
          "type": "timeunit",
          "units": ["year"],
          "as": ["year_date", "year_date_end"]
        },
        {
          "type": "aggregate",
          "groupby": ["year_date", "date"],
          "ops": ["mean"],
          "fields": ["price"],
          "as": ["mean_price"]
        },
        {
          "type": "filter",
          "expr": "(isDate(datum[\"date\"]) || (isValid(datum[\"date\"]) && isFinite(+datum[\"date\"]))) && isValid(datum[\"mean_price\"]) && isFinite(+datum[\"mean_price\"])"
        }
      ]
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "symbol",
      "style": ["point"],
      "from": {"data": "source_0"},
      "encode": {
        "update": {
          "fill": {"value": "transparent"},
          "stroke": {"scale": "color", "field": "date"},
          "ariaRoleDescription": {"value": "point"},
          "description": {
            "signal": "\"date (year): \" + (timeFormat(datum[\"year_date\"], timeUnitSpecifier([\"year\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"}))) + \"; Mean of price: \" + (format(datum[\"mean_price\"], \"\")) + \"; date: \" + (timeFormat(datum[\"date\"], '%b %d, %Y'))"
          },
          "x": {"scale": "x", "field": "year_date"},
          "y": {"scale": "y", "field": "mean_price"}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "time",
      "domain": {"data": "source_0", "field": "year_date"},
      "range": [0, {"signal": "width"}]
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {"data": "source_0", "field": "mean_price"},
      "range": [{"signal": "height"}, 0],
      "nice": true,
      "zero": true
    },
    {
      "name": "color",
      "type": "linear",
      "domain": {"data": "source_0", "field": "date" },
      "range": {"scheme": "plasma"}
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "gridScale": "y",
      "grid": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "tickMinStep": {
        "signal": "datetime(2002, 0, 1, 0, 0, 0, 0) - datetime(2001, 0, 1, 0, 0, 0, 0)"
      },
      "domain": false,
      "labels": false,
      "aria": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "gridScale": "x",
      "grid": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "domain": false,
      "labels": false,
      "aria": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "date (year)",
      "format": {
        "signal": "timeUnitSpecifier([\"year\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})"
      },
      "labelFlush": true,
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "tickMinStep": {
        "signal": "datetime(2002, 0, 1, 0, 0, 0, 0) - datetime(2001, 0, 1, 0, 0, 0, 0)"
      },
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "Mean of price",
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "zindex": 0
    }
  ],
  "legends": [
    {
      "stroke": "color",
      "format": "%b %d, %Y",  "formatType":"time",
      
      "title": "date"
    }
  ]
}

from vega-lite.

nicolaskruchten avatar nicolaskruchten commented on May 25, 2024

Thanks! But if Vega is able to do this, it seems like Vega-Lite could be modified/fixed to output this kind of thing, right?

OTOH, looking at the compiled Vega for my example, it seems like it's just getting the range wrong.

from vega-lite.

PBI-David avatar PBI-David commented on May 25, 2024

Yes, here is the VL. The reason I said I thought it was a Vega issue is the range is being computed incorrectly by Vega when the type is time.

image

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Stock prices of 5 Tech Companies over Time.",
  "data": {"url": "data/stocks.csv"},
  "mark": {"type": "point"},
  "encoding": {
    "x": {"timeUnit": "year", "field": "date"},
    "y": {"aggregate": "mean", "field": "price", "type": "quantitative"},
    "color": {
      "field": "date",
      "type": "quantitative",
      "scale": {"scheme": "plasma"}, "legend":{"formatType": "time"}
    }
  }
}

from vega-lite.

nicolaskruchten avatar nicolaskruchten commented on May 25, 2024

Ah I see, thanks for that. Should I file a bug in the Vega repo?

from vega-lite.

PBI-David avatar PBI-David commented on May 25, 2024

I would do as they might know why the range is being calculated incorrectly.

from vega-lite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.