Skip to content

Commit dfb11f5

Browse files
binstejoelostblom
andauthored
Improve encoding section (#2735)
* Remove key channel as not relevant for Altair and not documented anyway * Add explanation and description to detail channel * Add explanation on difference between datum and value * Remove key and description also from channel option sections * Update doc/user_guide/encoding.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encoding.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encoding.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encoding.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Fix usage of alt.value in rule example * Move section on themes from configuration to customization page * Collapse object tables by default * Move encodings page into subfolder * Move content on channels and channel options into separate subpages * Introduce both concepts of a channel and channel options in the beginning. Other minor improvements * Format code * Minor language improvements * Update doc/user_guide/encodings/index.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encodings/index.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encodings/index.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encodings/index.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encodings/index.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encodings/index.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Update doc/user_guide/encodings/channels.rst Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com> * Add reference to faceted charts section * Add introductory sentences before tables Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com>
1 parent f9dfc57 commit dfb11f5

File tree

7 files changed

+613
-459
lines changed

7 files changed

+613
-459
lines changed

altair/sphinxext/schematable.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from docutils import nodes, utils
66
from docutils.parsers.rst import Directive
7+
from docutils.parsers.rst.directives import flag
78
from recommonmark.parser import CommonMarkParser
89
from sphinx import addnodes
910

@@ -181,7 +182,7 @@ class AltairObjectTableDirective(Directive):
181182
has_content = False
182183
required_arguments = 1
183184

184-
option_spec = {"properties": validate_properties}
185+
option_spec = {"properties": validate_properties, "dont-collapse-table": flag}
185186

186187
def run(self):
187188
objectname = self.arguments[0]
@@ -191,10 +192,22 @@ def run(self):
191192
schema = cls.resolve_references(cls._schema)
192193

193194
properties = self.options.get("properties", None)
195+
dont_collapse_table = "dont-collapse-table" in self.options
194196

197+
result = []
198+
if not dont_collapse_table:
199+
html = "<details><summary><a>Click to show table</a></summary>"
200+
raw_html = nodes.raw("", html, format="html")
201+
result += [raw_html]
195202
# create the table from the object
196-
table = prepare_schema_table(schema, props=properties)
197-
return [table]
203+
result.append(prepare_schema_table(schema, props=properties))
204+
205+
if not dont_collapse_table:
206+
html = "</details>"
207+
raw_html = nodes.raw("", html, format="html")
208+
result += [raw_html]
209+
210+
return result
198211

199212

200213
def setup(app):

doc/user_guide/configuration.rst

Lines changed: 6 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Here is an example:
104104
labelFontSize=14
105105
)
106106

107+
Additional properties are summarized in the following table:
108+
107109
.. altair-object-table:: altair.HeaderConfig
108110

109111

@@ -205,7 +207,8 @@ the following properties:
205207

206208
Projection Configuration
207209
------------------------
208-
:meth:`Chart.configure_projection`
210+
Projections can be configured using :meth:`Chart.configure_projection`,
211+
which has the following properties:
209212

210213
.. altair-object-table:: altair.ProjectionConfig
211214

@@ -214,7 +217,8 @@ Projection Configuration
214217

215218
Selection Configuration
216219
-----------------------
217-
:meth:`Chart.configure_selection`
220+
Selections can be configured using :meth:`Chart.configure_selection`,
221+
which has the following properties:
218222

219223
.. altair-object-table:: altair.SelectionConfig
220224

@@ -286,144 +290,3 @@ Additional properties are summarized in the following table:
286290

287291
.. altair-object-table:: altair.ViewConfig
288292

289-
290-
.. _chart-themes:
291-
292-
Altair Themes
293-
-------------
294-
Altair makes available a theme registry that lets users apply chart configurations
295-
globally within any Python session. This is done via the ``alt.themes`` object.
296-
297-
The themes registry consists of functions which define a specification dictionary
298-
that will be added to every created chart.
299-
For example, the default theme configures the default size of a single chart:
300-
301-
>>> import altair as alt
302-
>>> default = alt.themes.get()
303-
>>> default()
304-
{'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}}}
305-
306-
You can see that any chart you create will have this theme applied, and these configurations
307-
added to its specification:
308-
309-
.. altair-plot::
310-
:output: repr
311-
312-
import altair as alt
313-
from vega_datasets import data
314-
315-
chart = alt.Chart(data.cars.url).mark_point().encode(
316-
x='Horsepower:Q',
317-
y='Miles_per_Gallon:Q'
318-
)
319-
320-
chart.to_dict()
321-
322-
The rendered chart will then reflect these configurations:
323-
324-
.. altair-plot::
325-
326-
chart
327-
328-
Changing the Theme
329-
~~~~~~~~~~~~~~~~~~
330-
If you would like to enable any other theme for the length of your Python session,
331-
you can call ``alt.themes.enable(theme_name)``.
332-
For example, Altair includes a theme in which the chart background is opaque
333-
rather than transparent:
334-
335-
.. altair-plot::
336-
:output: repr
337-
338-
alt.themes.enable('opaque')
339-
chart.to_dict()
340-
341-
.. altair-plot::
342-
343-
chart
344-
345-
Notice that the background color of the chart is now set to white.
346-
If you would like no theme applied to your chart, you can use the
347-
theme named ``'none'``:
348-
349-
.. altair-plot::
350-
:output: repr
351-
352-
alt.themes.enable('none')
353-
chart.to_dict()
354-
355-
.. altair-plot::
356-
357-
chart
358-
359-
Because the view configuration is not set, the chart is smaller
360-
than the default rendering.
361-
362-
If you would like to use any theme just for a single chart, you can use the
363-
``with`` statement to enable a temporary theme:
364-
365-
.. altair-plot::
366-
:output: none
367-
368-
with alt.themes.enable('default'):
369-
spec = chart.to_json()
370-
371-
Currently Altair does not offer many built-in themes, but we plan to add
372-
more options in the future.
373-
374-
Defining a Custom Theme
375-
~~~~~~~~~~~~~~~~~~~~~~~
376-
The theme registry also allows defining and registering custom themes.
377-
A theme is simply a function that returns a dictionary of default values
378-
to be added to the chart specification at rendering time, which is then
379-
registered and activated.
380-
381-
For example, here we define a theme in which all marks are drawn with black
382-
fill unless otherwise specified:
383-
384-
.. altair-plot::
385-
386-
import altair as alt
387-
from vega_datasets import data
388-
389-
# define the theme by returning the dictionary of configurations
390-
def black_marks():
391-
return {
392-
'config': {
393-
'view': {
394-
'height': 300,
395-
'width': 400,
396-
},
397-
'mark': {
398-
'color': 'black',
399-
'fill': 'black'
400-
}
401-
}
402-
}
403-
404-
# register the custom theme under a chosen name
405-
alt.themes.register('black_marks', black_marks)
406-
407-
# enable the newly registered theme
408-
alt.themes.enable('black_marks')
409-
410-
# draw the chart
411-
cars = data.cars.url
412-
alt.Chart(cars).mark_point().encode(
413-
x='Horsepower:Q',
414-
y='Miles_per_Gallon:Q'
415-
)
416-
417-
418-
If you want to restore the default theme, use:
419-
420-
.. altair-plot::
421-
:output: none
422-
423-
alt.themes.enable('default')
424-
425-
426-
For more ideas on themes, see the `Vega Themes`_ repository.
427-
428-
429-
.. _Vega Themes: https://github.com/vega/vega-themes/

doc/user_guide/customization.rst

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,145 @@ it is rendererd, you can set ``width`` or ``height`` to the string ``"container"
566566
Note that this will only scale with the container if its parent element has a size determined
567567
outside the chart itself; For example, the container may be a ``<div>`` element that has style
568568
``width: 100%; height: 300px``.
569+
570+
571+
.. _chart-themes:
572+
573+
Chart Themes
574+
------------
575+
Altair makes available a theme registry that lets users apply chart configurations
576+
globally within any Python session. This is done via the ``alt.themes`` object.
577+
578+
The themes registry consists of functions which define a specification dictionary
579+
that will be added to every created chart.
580+
For example, the default theme configures the default size of a single chart:
581+
582+
>>> import altair as alt
583+
>>> default = alt.themes.get()
584+
>>> default()
585+
{'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}}}
586+
587+
You can see that any chart you create will have this theme applied, and these configurations
588+
added to its specification:
589+
590+
.. altair-plot::
591+
:output: repr
592+
593+
import altair as alt
594+
from vega_datasets import data
595+
596+
chart = alt.Chart(data.cars.url).mark_point().encode(
597+
x='Horsepower:Q',
598+
y='Miles_per_Gallon:Q'
599+
)
600+
601+
chart.to_dict()
602+
603+
The rendered chart will then reflect these configurations:
604+
605+
.. altair-plot::
606+
607+
chart
608+
609+
Changing the Theme
610+
~~~~~~~~~~~~~~~~~~
611+
If you would like to enable any other theme for the length of your Python session,
612+
you can call ``alt.themes.enable(theme_name)``.
613+
For example, Altair includes a theme in which the chart background is opaque
614+
rather than transparent:
615+
616+
.. altair-plot::
617+
:output: repr
618+
619+
alt.themes.enable('opaque')
620+
chart.to_dict()
621+
622+
.. altair-plot::
623+
624+
chart
625+
626+
Notice that the background color of the chart is now set to white.
627+
If you would like no theme applied to your chart, you can use the
628+
theme named ``'none'``:
629+
630+
.. altair-plot::
631+
:output: repr
632+
633+
alt.themes.enable('none')
634+
chart.to_dict()
635+
636+
.. altair-plot::
637+
638+
chart
639+
640+
Because the view configuration is not set, the chart is smaller
641+
than the default rendering.
642+
643+
If you would like to use any theme just for a single chart, you can use the
644+
``with`` statement to enable a temporary theme:
645+
646+
.. altair-plot::
647+
:output: none
648+
649+
with alt.themes.enable('default'):
650+
spec = chart.to_json()
651+
652+
Currently Altair does not offer many built-in themes, but we plan to add
653+
more options in the future.
654+
655+
Defining a Custom Theme
656+
~~~~~~~~~~~~~~~~~~~~~~~
657+
The theme registry also allows defining and registering custom themes.
658+
A theme is simply a function that returns a dictionary of default values
659+
to be added to the chart specification at rendering time, which is then
660+
registered and activated.
661+
662+
For example, here we define a theme in which all marks are drawn with black
663+
fill unless otherwise specified:
664+
665+
.. altair-plot::
666+
667+
import altair as alt
668+
from vega_datasets import data
669+
670+
# define the theme by returning the dictionary of configurations
671+
def black_marks():
672+
return {
673+
'config': {
674+
'view': {
675+
'height': 300,
676+
'width': 400,
677+
},
678+
'mark': {
679+
'color': 'black',
680+
'fill': 'black'
681+
}
682+
}
683+
}
684+
685+
# register the custom theme under a chosen name
686+
alt.themes.register('black_marks', black_marks)
687+
688+
# enable the newly registered theme
689+
alt.themes.enable('black_marks')
690+
691+
# draw the chart
692+
cars = data.cars.url
693+
alt.Chart(cars).mark_point().encode(
694+
x='Horsepower:Q',
695+
y='Miles_per_Gallon:Q'
696+
)
697+
698+
699+
If you want to restore the default theme, use:
700+
701+
.. altair-plot::
702+
:output: none
703+
704+
alt.themes.enable('default')
705+
706+
707+
For more ideas on themes, see the `Vega Themes`_ repository.
708+
709+
710+
.. _Vega Themes: https://github.com/vega/vega-themes/

doc/user_guide/data.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ data before usage in Altair using GeoPandas for example as such:
441441
:hidden:
442442

443443
self
444-
encoding
444+
encodings/index
445445
marks/index
446446
transform/index
447447
interactions

0 commit comments

Comments
 (0)