Extension:Graph/Interactive Graph Tutorial/pl
<translate> This page is outdated.</translate> |
In this tutorial we will create an interactive graph that will display historical fertility rates per country, with a slider to pick the year, and a map to show rate distribution around the world. If you want to investigate the source code of this graph only, it's available as a separate wiki page. Use Interactive Graph Sandbox for experimentations. You might also be interested in the complete Vega documentation.
<graph mode=interactive title="Historical Fertility Rates"> {
"$schema": "https://vega.github.io/schema/vega/v5.json", "description": "An interactive scatter plot of global health statistics by country and year.", "width": 800, "height": 600, "padding": 5, "data": [ { "name": "gapminder", "url": "https://www.mediawiki.org/wiki/Extension:Graph/data/gapminder-json?action=raw" }, { "name": "clusters", "values": [ {"id": 0, "name": "South Asia"}, {"id": 1, "name": "Europe & Central Asia"}, {"id": 2, "name": "Sub-Saharan Africa"}, {"id": 3, "name": "America"}, {"id": 4, "name": "East Asia & Pacific"}, {"id": 5, "name": "Middle East & North Africa"} ] }, { "name": "country_timeline", "source": "gapminder", "transform": [ {"type": "filter", "expr": "timeline && datum.country == timeline.country"}, {"type": "collect", "sort": {"field": "year"}} ] }, { "name": "thisYear", "source": "gapminder", "transform": [ {"type": "filter", "expr": "datum.year == currentYear"} ] }, { "name": "prevYear", "source": "gapminder", "transform": [ {"type": "filter", "expr": "datum.year == currentYear - stepYear"} ] }, { "name": "nextYear", "source": "gapminder", "transform": [ {"type": "filter", "expr": "datum.year == currentYear + stepYear"} ] }, { "name": "countries", "source": "gapminder", "transform": [ {"type": "aggregate", "groupby": ["country"]} ] }, { "name": "interpolate", "source": "countries", "transform": [ { "type": "lookup", "from": "thisYear", "key": "country", "fields": ["country"], "as": ["this"], "default": {} }, { "type": "lookup", "from": "prevYear", "key": "country", "fields": ["country"], "as": ["prev"], "default": {} }, { "type": "lookup", "from": "nextYear", "key": "country", "fields": ["country"], "as": ["next"], "default": {} }, { "type": "formula", "as": "target_fertility", "expr": "interYear > currentYear ? datum.next.fertility : (datum.prev.fertility||datum.this.fertility)" }, { "type": "formula", "as": "target_life_expect", "expr": "interYear > currentYear ? datum.next.life_expect : (datum.prev.life_expect||datum.this.life_expect)" }, { "type": "formula", "as": "inter_fertility", "expr": "interYear==2000 ? datum.this.fertility : datum.this.fertility + (datum.target_fertility-datum.this.fertility) * abs(interYear-datum.this.year)/5" }, { "type": "formula", "as": "inter_life_expect", "expr": "interYear==2000 ? datum.this.life_expect : datum.this.life_expect + (datum.target_life_expect-datum.this.life_expect) * abs(interYear-datum.this.year)/5" } ] }, { "name": "trackCountries", "on": [ {"trigger": "active", "toggle": "{country: active.country}"} ] } ], "signals": [ { "name": "minYear", "value": 1955 }, { "name": "maxYear", "value": 2005 }, { "name": "stepYear", "value": 5 }, { "name": "active", "value": {}, "on": [ {"events": "@point:mousedown, @point:touchstart", "update": "datum"}, {"events": "window:mouseup, window:touchend", "update": "{}"} ] }, { "name": "isActive", "update": "active.country" }, { "name": "timeline", "value": {}, "on": [ {"events": "@point:mouseover", "update": "isActive ? active : datum"}, {"events": "@point:mouseout", "update": "active"}, {"events": {"signal": "active"}, "update": "active"} ] }, { "name": "tX", "on": [{ "events": "mousemove!, touchmove!", "update": "isActive ? scale('x', active.this.fertility) : tX" }] }, { "name": "tY", "on": [{ "events": "mousemove, touchmove", "update": "isActive ? scale('y', active.this.life_expect) : tY" }] }, { "name": "pX", "on": [{ "events": "mousemove, touchmove", "update": "isActive ? scale('x', active.prev.fertility) : pX" }] }, { "name": "pY", "on": [{ "events": "mousemove, touchmove", "update": "isActive ? scale('y', active.prev.life_expect) : pY" }] }, { "name": "nX", "on": [{ "events": "mousemove, touchmove", "update": "isActive ? scale('x', active.next.fertility) : nX" }] }, { "name": "nY", "on": [{ "events": "mousemove, touchmove", "update": "isActive ? scale('y', active.next.life_expect) : nY" }] }, { "name": "thisDist", "value": 0, "on":[{ "events": "mousemove, touchmove", "update": "isActive ? sqrt(pow(x()-tX, 2) + pow(y()-tY, 2)) : thisDist" }] }, { "name": "prevDist", "value": 0, "on":[{ "events": "mousemove, touchmove", "update": "isActive ? sqrt(pow(x()-pX, 2) + pow(y()-pY, 2)): prevDist" }] }, { "name": "nextDist", "value": 0, "on":[{ "events": "mousemove, touchmove", "update": "isActive ? sqrt(pow(x()-nX, 2) + pow(y()-nY, 2)) : nextDist" }] }, { "name": "prevScore", "value": 0, "on": [{ "events": "mousemove, touchmove", "update": "isActive ? ((pX-tX) * (x()-tX) + (pY-tY) * (y()-tY))/prevDist || -999999 : prevScore" }] }, { "name": "nextScore", "value": 0, "on": [{ "events": "mousemove, touchmove", "update": "isActive ? ((nX-tX) * (x()-tX) + (nY-tY) * (y()-tY))/nextDist || -999999 : nextScore" }] }, { "name": "interYear", "value": 1980, "on": [{ "events": "mousemove, touchmove", "update": "isActive ? (min(maxYear, currentYear+5, max(minYear, currentYear-5, prevScore > nextScore ? (currentYear - 2.5*prevScore/sqrt(pow(pX-tX, 2) + pow(pY-tY, 2))) : (currentYear + 2.5*nextScore/sqrt(pow(nX-tX, 2) + pow(nY-tY, 2)))))) : interYear" }] }, { "name": "currentYear", "value": 1980, "on":[{ "events": "mousemove, touchmove", "update": "isActive ? (min(maxYear, max(minYear, prevScore > nextScore ? (thisDist < prevDist ? currentYear : currentYear-5) : (thisDist < nextDist ? currentYear : currentYear+5)))) : currentYear" }] } ], "scales": [ { "name": "x", "type": "linear", "nice": true, "domain": {"data": "gapminder", "field": "fertility"}, "range": "width" }, { "name": "y", "type": "linear", "nice": true, "zero": false, "domain": {"data": "gapminder", "field": "life_expect"}, "range": "height" }, { "name": "color", "type": "ordinal", "domain": {"data": "gapminder", "field": "cluster"}, "range": "category" }, { "name": "label", "type": "ordinal", "domain": {"data": "clusters", "field": "id"}, "range": {"data": "clusters", "field": "name"} } ], "axes": [ { "title": "Fertility", "orient": "bottom", "scale": "x", "grid": true, "tickCount": 5 }, { "title": "Life Expectancy", "orient": "left", "scale": "y", "grid": true, "tickCount": 5 } ], "legends": [ { "fill": "color", "title": "Region", "orient": "right", "encode": { "symbols": { "enter": { "fillOpacity": {"value": 0.5} } }, "labels": { "update": { "text": {"scale": "label", "field": "value"} } } } } ], "marks": [ { "type": "text", "encode": { "update": { "text": {"signal": "currentYear"}, "x": {"value": 300}, "y": {"value": 300}, "fill": {"value": "grey"}, "fillOpacity": {"value": 0.25}, "fontSize": {"value": 100} } } }, { "type": "text", "from": {"data": "country_timeline"}, "interactive": false, "encode": { "enter": { "x": {"scale": "x", "field": "fertility", "offset": 5}, "y": {"scale": "y", "field": "life_expect"}, "fill": {"value": "#555"}, "fillOpacity": {"value": 0.6}, "text": {"field": "year"} } } }, { "type": "line", "from": {"data": "country_timeline"}, "encode": { "update": { "x": {"scale": "x", "field": "fertility"}, "y": {"scale": "y", "field": "life_expect"}, "stroke": {"value": "#bbb"}, "strokeWidth": {"value": 5}, "strokeOpacity": {"value": 0.5} } } }, { "name": "point", "type": "symbol", "from": {"data": "interpolate"}, "encode": { "enter": { "fill": {"scale": "color", "field": "this.cluster"}, "size": {"value": 150} }, "update": { "x": {"scale": "x", "field": "inter_fertility"}, "y": {"scale": "y", "field": "inter_life_expect"}, "fillOpacity": [ { "test": "datum.country==timeline.country || indata('trackCountries', 'country', datum.country)", "value": 1 }, {"value": 0.5} ] } } }, { "type": "text", "from": {"data": "interpolate"}, "interactive": false, "encode": { "enter": { "fill": {"value": "#333"}, "fontSize": {"value": 14}, "fontWeight": {"value": "bold"}, "text": {"field": "country"}, "align": {"value": "center"}, "baseline": {"value": "bottom"} }, "update": { "x": {"scale": "x", "field": "inter_fertility"}, "y": {"scale": "y", "field": "inter_life_expect", "offset": -7}, "fillOpacity": [ { "test": "datum.country==timeline.country || indata('trackCountries', 'country', datum.country)", "value": 0.8 }, {"value": 0} ] } } } ]
} </graph>
Move the slider to change the year, and hover the mouse over countries to read the rate. Try it !
Drawing Shapes
We start with drawing a few elements (marks).
The graph code is surrounded by the <graph mode=interactive>...</graph>
tag, even though the graph is not yet interactive.
Open Interactive Graph Sandbox and copy the following code without the <graph> tag to experiment.
Hint: Use "edit source" to copy the graph spec to the Vega Editor for experimentation.
<graph mode="interactive">{
// We want to use Vega 2, and specify image size "version": 2, "width": 300, "height": 80, // Set padding to the same value on all sides "padding": 12, // By default the background is transparent "background": "#edf1f7", "marks": [ { // Draw a horizontal line "name": "scrollLine", "type": "rule", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 40}, "x2": {"value": 300}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 2} } } }, { // Draw a triangle shape with a hover effect // naming objects allows us to reference them later "name": "handle", "type": "path", "properties": { "enter": { "x": {"value": 200}, "y": {"value": 40}, // path syntax is the same as SVG's path tag "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"}, "stroke": {"value": "#880"}, "strokeWidth": {"value": 2.5} }, "update": {"fill": {"value": "#fff"} }, // Change fill color of the object on mouse hover "hover": {"fill": {"value": "#f00"} } } } ] } </graph> |
zobacz kod źródłowy |
{
// We want to use Vega 2, and specify image size
"version": 2, "width": 300, "height": 80,
// Set padding to the same value on all sides
"padding": 12,
// By default the background is transparent
"background": "#edf1f7",
"marks": [
{
// Draw a horizontal line
"name": "scrollLine",
"type": "rule",
"properties": {
"enter": {
"x": {"value": 0},
"y": {"value": 40},
"x2": {"value": 300},
"stroke": {"value": "#000"},
"strokeWidth": {"value": 2}
}
}
},
{
// Draw a triangle shape with a hover effect
// naming objects allows us to reference them later
"name": "handle",
"type": "path",
"properties": {
"enter": {
"x": {"value": 200},
"y": {"value": 40},
// path syntax is the same as SVG's path tag
"path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"},
"stroke": {"value": "#880"},
"strokeWidth": {"value": 2.5}
},
"update": {"fill": {"value": "#fff"}},
// Change fill color of the object on mouse hover
"hover": {"fill": {"value": "#f00"}}
}
}
]
}
Sygnał isDragging
To make our handle object dragable, we first need to figure out if it was clicked or not. For that, lets add a signal that becomes true when an object is clicked (isDragging), and a text mark to show (debug) the result:
<graph mode="interactive">{
// We want to use Vega 2, and specify image size "version": 2, "width": 300, "height": 80, // Set padding to the same value on all sides "padding": 12, // By default the background is transparent "background": "#edf1f7", "signals": [ { "name": "isDragging", "init": false, "streams": [ {"type": "@handle:mousedown","expr": "true"}, {"type": "mouseup","expr": "false"} ] }, ], "marks": [ { // Draw a horizontal line "name": "scrollLine", "type": "rule", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 40}, "x2": {"value": 300}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 2} } } }, { // Draw a triangle shape with a hover effect // naming objects allows us to reference them later "name": "handle", "type": "path", "properties": { "enter": { "x": {"value": 200}, "y": {"value": 40}, // path syntax is the same as SVG's path tag "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"}, "stroke": {"value": "#880"}, "strokeWidth": {"value": 2.5} }, "update": {"fill": {"value": "#fff"} }, // Change fill color of the object on mouse hover "hover": {"fill": {"value": "#f00"} } } }, { "name": "debugIsDragging", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 0}, "fill": {"value": "black"} }, "update": {"text": {"signal": "isDragging"} } } } ] } </graph> |
zobacz kod źródłowy |
"signals": [
{
"name": "isDragging",
"init": false,
"streams": [
{"type": "@handle:mousedown","expr": "true"},
{"type": "mouseup","expr": "false"}
]
},
],
// in "marks"
{
"name": "debugIsDragging",
"type": "text",
"properties": {
"enter": {
"x": {"value": 250},
"y": {"value": 0},
"fill": {"value": "black"}
},
"update": {"text": {"signal": "isDragging"}}
}
}
Sygnał handlePosition
Now that we know when the object is being dragged, we add a mousemove
signal that only changes its value when isDragging signal is true.
We also attach the new signal to the handle's "x" coordinate via "update" section:
<graph mode="interactive">{
// We want to use Vega 2, and specify image size "version": 2, "width": 300, "height": 80, // Set padding to the same value on all sides "padding": 12, // By default the background is transparent "background": "#edf1f7", "signals": [ { "name": "isDragging", "init": false, "streams": [ {"type": "@handle:mousedown","expr": "true"}, {"type": "mouseup","expr": "false"} ] }, { "name": "handlePosition", "init": 200, "streams": [ { "type": "mousemove[isDragging]", "expr": "eventX()" } ] } ], "marks": [ { // Draw a horizontal line "name": "scrollLine", "type": "rule", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 40}, "x2": {"value": 300}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 2} } } }, { // Draw a triangle shape with a hover effect // naming objects allows us to reference them later "name": "handle", "type": "path", "properties": { "enter": { "x": {"value": 200}, "y": {"value": 40}, // path syntax is the same as SVG's path tag "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"}, "stroke": {"value": "#880"}, "strokeWidth": {"value": 2.5} }, "update": { "fill": {"value": "#fff"}, "x": {"signal": "handlePosition"} }, // Change fill color of the object on mouse hover "hover": {"fill": {"value": "#f00"} } } }, { "name": "debugIsDragging", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 0}, "fill": {"value": "black"} }, "update": {"text": {"signal": "isDragging"} } } }, { "name": "debugHandlePosition", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 14}, "fill": {"value": "black"} }, "update": {"text": {"signal": "handlePosition"} } } } ] } </graph> |
zobacz kod źródłowy |
// in "signals"
{
"name": "handlePosition",
"init": 200,
"streams": [
{
"type": "mousemove[isDragging]",
"expr": "eventX()"
}
]
}
// add in "marks"
{
"name": "handle",
...
"update": {
"x": {"signal": "handlePosition"},
...
},
},
{
"name": "debugHandlePosition",
"type": "text",
"properties": {
"enter": {
"x": {"value": 250},
"y": {"value": 14},
"fill": {"value": "black"}
},
"update": {"text": {"signal": "handlePosition"}}
}
}
Scaling Handle Position signal
Having pixel position of the handler is not very good - we would much rather have a position that is meaningful to our graph, e.g. a year. Vega scales provide a useful mechanism for converting between our data (e.g. years) and the screen coordinates, and back (invert). In this step, we add "yearsScale" linear scale for values 1960..2013, mapping it to the whole width of the graph (excluding padding). We also add a new scaledHandlePosition signal that translates from the mouse X position to the meaningful value in years.
<graph mode="interactive">{
// We want to use Vega 2, and specify image size "version": 2, "width": 300, "height": 80, // Set padding to the same value on all sides "padding": 12, // By default the background is transparent "background": "#edf1f7", "signals": [ { "name": "isDragging", "init": false, "streams": [ {"type": "@handle:mousedown","expr": "true"}, {"type": "mouseup","expr": "false"} ] }, { "name": "handlePosition", "init": 200, "streams": [ { "type": "mousemove[isDragging]", "expr": "eventX()" } ] }, { "name": "scaledHandlePosition", "expr": "handlePosition", "scale": {"name": "yearsScale","invert": true} } ], "scales": [ { "name": "yearsScale", "type": "linear", "zero": false, "domain": [1960, 2013], "range": "width" } ], "marks": [ { // Draw a horizontal line "name": "scrollLine", "type": "rule", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 40}, "x2": {"value": 300}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 2} } } }, { // Draw a triangle shape with a hover effect // naming objects allows us to reference them later "name": "handle", "type": "path", "properties": { "enter": { "x": {"value": 200}, "y": {"value": 40}, // path syntax is the same as SVG's path tag "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"}, "stroke": {"value": "#880"}, "strokeWidth": {"value": 2.5} }, "update": { "fill": {"value": "#fff"}, "x": {"signal": "handlePosition"}, }, // Change fill color of the object on mouse hover "hover": {"fill": {"value": "#f00"} } } }, { "name": "debugIsDragging", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 0}, "fill": {"value": "black"} }, "update": {"text": {"signal": "isDragging"} } } }, { "name": "debugHandlePosition", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 14}, "fill": {"value": "black"} }, "update": {"text": {"signal": "handlePosition"} } } }, { "name": "debugScaledHandlePosition", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 28}, "fill": {"value": "black"} }, "update": {"text": {"signal": "scaledHandlePosition"} } } } ] } </graph> |
zobacz kod źródłowy |
// add "scales" root value:
"scales": [
{
"name": "yearsScale",
"type": "linear",
"zero": false,
"domain": [1960, 2013],
"range": "width"
}
],
// in "signals", add:
{
"name": "scaledHandlePosition",
"expr": "handlePosition",
"scale": {"name": "yearsScale","invert": true}
}
// add in "marks"
{
"name": "debugScaledHandlePosition",
"type": "text",
"properties": {
"enter": {
"x": {"value": 250},
"y": {"value": 28},
"fill": {"value": "black"}
},
"update": {"text": {"signal": "scaledHandlePosition"}}
}
}
Year value cleanup
As you might have noticed, the handle could be moved beyond the length of our graph, producing incorrect results. Also, the scaled value is not an integer that we expect of the year value. To fix this, we can introduce one more post-processing signal called "currentYear" to convert it to an integer and limit it to the needed range. We also initialize it to a reasonable value, and we tie both the "yearLabel" and the position of the handle bar back to this value. Note that the handle's position needs to be in screen coordinates, so we have to use the "yearsScale" to convert the value back:
<graph mode="interactive">{
// We want to use Vega 2, and specify image size "version": 2, "width": 300, "height": 80, // Set padding to the same value on all sides "padding": 12, // By default the background is transparent "background": "#edf1f7", "signals": [ { "name": "isDragging", "init": false, "streams": [ {"type": "@handle:mousedown","expr": "true"}, {"type": "mouseup","expr": "false"} ] }, { "name": "handlePosition", "init": 200, "streams": [ { "type": "mousemove[isDragging]", "expr": "eventX()" } ] }, { "name": "scaledHandlePosition", "expr": "handlePosition", "scale": {"name": "yearsScale","invert": true} }, { "name": "currentYear", "init": 2000, "expr": "clamp(parseInt(scaledHandlePosition),1960,2013)" } ], "scales": [ { "name": "yearsScale", "type": "linear", "zero": false, "domain": [1960, 2013], "range": "width" } ], "marks": [ { // draw the year label in the upper left corner "name": "yearLabel", "type": "text", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 25}, "fontSize": {"value": 32}, "fontWeight": {"value": "bold"}, "fill": {"value": "steelblue"} }, "update": {"text": {"signal": "currentYear"} } } }, { // Draw a horizontal line "name": "scrollLine", "type": "rule", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 40}, "x2": {"value": 300}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 2} } } }, { // Draw a triangle shape with a hover effect // naming objects allows us to reference them later "name": "handle", "type": "path", "properties": { "enter": { "y": {"value": 40}, // path syntax is the same as SVG's path tag "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"}, "stroke": {"value": "#880"}, "strokeWidth": {"value": 2.5} }, "update": { "x": {"scale": "yearsScale","signal": "currentYear"}, "fill": {"value": "#fff"} }, // Change fill color of the object on mouse hover "hover": {"fill": {"value": "#f00"} } } }, { "name": "debugIsDragging", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 0}, "fill": {"value": "black"} }, "update": {"text": {"signal": "isDragging"} } } }, { "name": "debugHandlePosition", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 14}, "fill": {"value": "black"} }, "update": {"text": {"signal": "handlePosition"} } } }, { "name": "debugScaledHandlePosition", "type": "text", "properties": { "enter": { "x": {"value": 250}, "y": {"value": 28}, "fill": {"value": "black"} }, "update": {"text": {"signal": "scaledHandlePosition"} } } } ] } </graph> |
see source code |
// New signal:
{
"name": "currentYear",
"init": 2000,
"expr": "clamp(parseInt(scaledHandlePosition),1960,2013)"
}
// Update yearLabel mark:
// New mark to draw the year
{
"name": "yearLabel",
"type": "text",
"properties": {
"enter": {
"x": {"value": 0},
"y": {"value": 25},
"fontSize": {"value": 32},
"fontWeight": {"value": "bold"},
"fill": {"value": "steelblue"}
},
"update": {"text": {"signal": "currentYear"}}
}
},
// Update handle mark:
{
"name": "handle",
"properties": {
"update": {
"x": {"scale": "yearsScale","signal": "currentYear"}
},
}
}
Final cleanup
Now we can remove all the debugging marks. We also don't need separate handlePosition and scaledHandlePosition signals because scaling can happen in the same step:
<graph mode="interactive">{
// We want to use Vega 2, and specify image size "version": 2, "width": 300, "height": 80, // Set padding to the same value on all sides "padding": 12, // By default the background is transparent "background": "#edf1f7", "signals": [ { "name": "isDragging", "init": false, "streams": [ {"type": "@handle:mousedown","expr": "true"}, {"type": "mouseup","expr": "false"} ] }, { "name": "scaledHandlePosition", "streams": [ { "type": "mousemove[isDragging]", "expr": "eventX()", "scale": {"name": "yearsScale","invert": true} } ] }, { "name": "currentYear", "init": 2000, "expr": "clamp(parseInt(scaledHandlePosition),1960,2013)" } ], "scales": [ { "name": "yearsScale", "type": "linear", "zero": false, "domain": [1960, 2013], "range": "width" } ], "marks": [ { // draw the year label in the upper left corner "name": "yearLabel", "type": "text", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 25}, "fontSize": {"value": 32}, "fontWeight": {"value": "bold"}, "fill": {"value": "steelblue"} }, "update": {"text": {"signal": "currentYear"} } } }, { // Draw a horizontal line "name": "scrollLine", "type": "rule", "properties": { "enter": { "x": {"value": 0}, "y": {"value": 40}, "x2": {"value": 300}, "stroke": {"value": "#000"}, "strokeWidth": {"value": 2} } } }, { // Draw a triangle shape with a hover effect // naming objects allows us to reference them later "name": "handle", "type": "path", "properties": { "enter": { "y": {"value": 40}, // path syntax is the same as SVG's path tag "path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"}, "stroke": {"value": "#880"}, "strokeWidth": {"value": 2.5} }, "update": { "x": {"scale": "yearsScale","signal": "currentYear"}, "fill": {"value": "#fff"} }, // Change fill color of the object on mouse hover "hover": {"fill": {"value": "#f00"} } } } ] }</graph> |
zobacz kod źródłowy |
{
// We want to use Vega 2, and specify image size
"version": 2, "width": 300, "height": 80,
// Set padding to the same value on all sides
"padding": 12,
// By default the background is transparent
"background": "#edf1f7",
"signals": [
{
"name": "isDragging",
"init": false,
"streams": [
{"type": "@handle:mousedown","expr": "true"},
{"type": "mouseup","expr": "false"}
]
},
{
"name": "scaledHandlePosition",
"streams": [
{
"type": "mousemove[isDragging]",
"expr": "eventX()",
"scale": {"name": "yearsScale","invert": true}
}
]
},
{
"name": "currentYear",
"init": 2000,
"expr": "clamp(parseInt(scaledHandlePosition),1960,2013)"
}
],
"scales": [
{
"name": "yearsScale",
"type": "linear",
"zero": false,
"domain": [1960, 2013],
"range": "width"
}
],
"marks": [
{
// draw the year label in the upper left corner
"name": "yearLabel",
"type": "text",
"properties": {
"enter": {
"x": {"value": 0},
"y": {"value": 25},
"fontSize": {"value": 32},
"fontWeight": {"value": "bold"},
"fill": {"value": "steelblue"}
},
"update": {"text": {"signal": "currentYear"} }
}
},
{
// Draw a horizontal line
"name": "scrollLine",
"type": "rule",
"properties": {
"enter": {
"x": {"value": 0},
"y": {"value": 40},
"x2": {"value": 300},
"stroke": {"value": "#000"},
"strokeWidth": {"value": 2}
}
}
},
{
// Draw a triangle shape with a hover effect
// naming objects allows us to reference them later
"name": "handle",
"type": "path",
"properties": {
"enter": {
"y": {"value": 40},
// path syntax is the same as SVG's path tag
"path": {"value": "m-5.5,-10l0,20l11.5,-10l-11.5,-10z"},
"stroke": {"value": "#880"},
"strokeWidth": {"value": 2.5}
},
"update": {
"x": {"scale": "yearsScale","signal": "currentYear"},
"fill": {"value": "#fff"}
},
// Change fill color of the object on mouse hover
"hover": {"fill": {"value": "#f00"} }
}
}
]
}
Next Step
Please continue to part 2