You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.1 KiB

4 years ago
  1. # Copyright 2015 Bloomberg Finance L.P.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. r"""
  15. ====
  16. Axes
  17. ====
  18. .. currentmodule:: bqplot.axes
  19. .. autosummary::
  20. :toctree: _generate/
  21. Axis
  22. ColorAxis
  23. """
  24. from traitlets import Int, Unicode, Instance, Enum, Dict, Bool
  25. from traittypes import Array
  26. from ipywidgets import Widget, Color, widget_serialization
  27. from .scales import Scale, ColorScale
  28. from .traits import array_serialization, array_dimension_bounds
  29. from ._version import __frontend_version__
  30. def register_axis(key=None):
  31. """Returns a decorator registering an axis class in the axis type registry.
  32. If no key is provided, the class name is used as a key. A key is provided
  33. for each core bqplot axis so that the frontend can use this key regardless
  34. of the kernel language.
  35. """
  36. def wrap(axis):
  37. name = key if key is not None else axis.__module__ + axis.__name__
  38. BaseAxis.axis_types[name] = axis
  39. return axis
  40. return wrap
  41. class BaseAxis(Widget):
  42. axis_types = {}
  43. _view_module = Unicode('bqplot').tag(sync=True)
  44. _model_module = Unicode('bqplot').tag(sync=True)
  45. _view_module_version = Unicode(__frontend_version__).tag(sync=True)
  46. _model_module_version = Unicode(__frontend_version__).tag(sync=True)
  47. @register_axis('bqplot.Axis')
  48. class Axis(BaseAxis):
  49. """A line axis.
  50. A line axis is the visual representation of a numerical or date scale.
  51. Attributes
  52. ----------
  53. icon: string (class-level attribute)
  54. The font-awesome icon name for this object.
  55. axis_types: dict (class-level attribute)
  56. A registry of existing axis types.
  57. orientation: {'horizontal', 'vertical'}
  58. The orientation of the axis, either vertical or horizontal
  59. side: {'bottom', 'top', 'left', 'right'} or None (default: None)
  60. The side of the axis, either bottom, top, left or right.
  61. label: string (default: '')
  62. The axis label
  63. tick_format: string or None (default: '')
  64. The tick format for the axis, for dates use d3 string formatting.
  65. scale: Scale
  66. The scale represented by the axis
  67. num_ticks: int or None (default: None)
  68. If tick_values is None, number of ticks
  69. tick_values: numpy.ndarray or None (default: None)
  70. Tick values for the axis
  71. offset: dict (default: {})
  72. Contains a scale and a value {'scale': scale or None,
  73. 'value': value of the offset}
  74. If offset['scale'] is None, the corresponding figure scale is used
  75. instead.
  76. label_location: {'middle', 'start', 'end'}
  77. The location of the label along the axis, one of 'start', 'end' or
  78. 'middle'
  79. label_color: Color or None (default: None)
  80. The color of the axis label
  81. grid_lines: {'none', 'solid', 'dashed'}
  82. The display of the grid lines
  83. grid_color: Color or None (default: None)
  84. The color of the grid lines
  85. color: Color or None (default: None)
  86. The color of the line
  87. label_offset: string or None (default: None)
  88. Label displacement from the axis line. Units allowed are 'em', 'px'
  89. and 'ex'. Positive values are away from the figure and negative
  90. values are towards the figure with respect to the axis line.
  91. visible: bool (default: True)
  92. A visibility toggle for the axis
  93. tick_style: Dict (default: {})
  94. Dictionary containing the CSS-style of the text for the ticks.
  95. For example: font-size of the text can be changed by passing
  96. `{'font-size': 14}`
  97. tick_rotate: int (default: 0)
  98. Degrees to rotate tick labels by.
  99. """
  100. icon = 'fa-arrows'
  101. orientation = Enum(['horizontal', 'vertical'], default_value='horizontal')\
  102. .tag(sync=True)
  103. side = Enum(['bottom', 'top', 'left', 'right'],
  104. allow_none=True, default_value=None).tag(sync=True)
  105. label = Unicode().tag(sync=True)
  106. grid_lines = Enum(['none', 'solid', 'dashed'], default_value='solid')\
  107. .tag(sync=True)
  108. tick_format = Unicode(None, allow_none=True).tag(sync=True)
  109. scale = Instance(Scale).tag(sync=True, **widget_serialization)
  110. num_ticks = Int(default_value=None, allow_none=True).tag(sync=True)
  111. tick_values = Array(None, allow_none=True)\
  112. .tag(sync=True, **array_serialization)\
  113. .valid(array_dimension_bounds(1, 1))
  114. offset = Dict().tag(sync=True, **widget_serialization)
  115. label_location = Enum(['middle', 'start', 'end'],
  116. default_value='middle').tag(sync=True)
  117. label_color = Color(None, allow_none=True).tag(sync=True)
  118. grid_color = Color(None, allow_none=True).tag(sync=True)
  119. color = Color(None, allow_none=True).tag(sync=True)
  120. label_offset = Unicode(default_value=None, allow_none=True).tag(sync=True)
  121. visible = Bool(True).tag(sync=True)
  122. tick_style = Dict().tag(sync=True)
  123. tick_rotate = Int(0).tag(sync=True)
  124. _view_name = Unicode('Axis').tag(sync=True)
  125. _model_name = Unicode('AxisModel').tag(sync=True)
  126. _ipython_display_ = None # We cannot display an axis outside of a figure.
  127. @register_axis('bqplot.ColorAxis')
  128. class ColorAxis(Axis):
  129. """A colorbar axis.
  130. A color axis is the visual representation of a color scale.
  131. Attributes
  132. ----------
  133. scale: ColorScale
  134. The scale represented by the axis
  135. """
  136. orientation = Enum(['horizontal', 'vertical'],
  137. default_value='horizontal').tag(sync=True)
  138. side = Enum(['bottom', 'top', 'left', 'right'],
  139. default_value='bottom').tag(sync=True)
  140. label = Unicode().tag(sync=True)
  141. scale = Instance(ColorScale).tag(sync=True, **widget_serialization)
  142. _view_name = Unicode('ColorAxis').tag(sync=True)
  143. _model_name = Unicode('ColorAxisModel').tag(sync=True)