Extension:Graph/Interactive Graph Tutorial/es

En este tutorial crearemos un gráfico interactivo que desplegará índices de fertilidad histórica por país, con una flecha deslizante para seleccionar el año y un mapa para mostrar la distribución de los índices al rededor del mundo. Si quieres investigar solo el código de fuente de este gráfico, está [$url disponible como una wikipágina separada]. Puede que te interese la documenación Vega completa. 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.

Move the slider to change the year, and hover the mouse over countries to read the rate. Try it !



Dibujando formas
Empezamos dibujando unos pocos elementos (marcas). El código del gráfico está rodeado por las etiquetas, aunque el gráfico todavía no es todavía interactivo. The graph code is surrounded by the  tag, even though the graph is not yet interactive. Open Interactive Graph Sandbox and copy the following code without the &lt;graph> tag to experiment.

Pista: Usa "Editar código" para copiar la especificación del gráfico al Editor Vega para experimentar.

isDragging signal
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:

handlePosition signal
Now that we know when the object is being dragged, we add a  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:

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.

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:

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:

Next Step
Please continue to part 2