chart.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from typing import Dict, List
  2. from ..dependencies import js_dependencies, register_component
  3. from ..element import Element
  4. dependencies = [
  5. 'lib/highcharts.js',
  6. 'lib/highcharts-more.js',
  7. 'lib/highcharts-3d.js',
  8. ]
  9. optional_dependencies = [
  10. 'lib/highcharts_modules/sankey.js',
  11. 'lib/highcharts_modules/accessibility.js',
  12. 'lib/highcharts_modules/exporting.js',
  13. 'lib/highcharts_modules/export-data.js',
  14. 'lib/highcharts_modules/solid-gauge.js',
  15. 'lib/highcharts_modules/annotations-advanced.js',
  16. 'lib/highcharts_modules/annotations.js',
  17. 'lib/highcharts_modules/arc-diagram.js',
  18. 'lib/highcharts_modules/arrow-symbols.js',
  19. 'lib/highcharts_modules/boost-canvas.js',
  20. 'lib/highcharts_modules/boost.js',
  21. 'lib/highcharts_modules/broken-axis.js',
  22. 'lib/highcharts_modules/bullet.js',
  23. 'lib/highcharts_modules/coloraxis.js',
  24. 'lib/highcharts_modules/current-date-indicator.js',
  25. 'lib/highcharts_modules/datagrouping.js',
  26. 'lib/highcharts_modules/data.js',
  27. 'lib/highcharts_modules/debugger.js',
  28. 'lib/highcharts_modules/dependency-wheel.js',
  29. 'lib/highcharts_modules/dotplot.js',
  30. 'lib/highcharts_modules/draggable-points.js',
  31. 'lib/highcharts_modules/drag-panes.js',
  32. 'lib/highcharts_modules/drilldown.js',
  33. 'lib/highcharts_modules/dumbbell.js',
  34. 'lib/highcharts_modules/full-screen.js',
  35. 'lib/highcharts_modules/funnel.js',
  36. 'lib/highcharts_modules/gantt.js',
  37. 'lib/highcharts_modules/grid-axis.js',
  38. 'lib/highcharts_modules/heatmap.js',
  39. 'lib/highcharts_modules/histogram-bellcurve.js',
  40. 'lib/highcharts_modules/item-series.js',
  41. 'lib/highcharts_modules/lollipop.js',
  42. 'lib/highcharts_modules/marker-clusters.js',
  43. 'lib/highcharts_modules/networkgraph.js',
  44. 'lib/highcharts_modules/no-data-to-display.js',
  45. 'lib/highcharts_modules/offline-exporting.js',
  46. 'lib/highcharts_modules/oldie.js',
  47. 'lib/highcharts_modules/oldie-polyfills.js',
  48. 'lib/highcharts_modules/organization.js',
  49. 'lib/highcharts_modules/overlapping-datalabels.js',
  50. 'lib/highcharts_modules/parallel-coordinates.js',
  51. 'lib/highcharts_modules/pareto.js',
  52. 'lib/highcharts_modules/pathfinder.js',
  53. 'lib/highcharts_modules/pattern-fill.js',
  54. 'lib/highcharts_modules/price-indicator.js',
  55. 'lib/highcharts_modules/series-label.js',
  56. 'lib/highcharts_modules/series-on-point.js',
  57. 'lib/highcharts_modules/sonification.js',
  58. 'lib/highcharts_modules/static-scale.js',
  59. 'lib/highcharts_modules/stock.js',
  60. 'lib/highcharts_modules/stock-tools.js',
  61. 'lib/highcharts_modules/streamgraph.js',
  62. 'lib/highcharts_modules/sunburst.js',
  63. 'lib/highcharts_modules/tilemap.js',
  64. 'lib/highcharts_modules/timeline.js',
  65. 'lib/highcharts_modules/treegraph.js',
  66. 'lib/highcharts_modules/treegrid.js',
  67. 'lib/highcharts_modules/variable-pie.js',
  68. 'lib/highcharts_modules/variwide.js',
  69. 'lib/highcharts_modules/vector.js',
  70. 'lib/highcharts_modules/venn.js',
  71. 'lib/highcharts_modules/windbarb.js',
  72. 'lib/highcharts_modules/wordcloud.js',
  73. 'lib/highcharts_modules/xrange.js',
  74. 'lib/highcharts_modules/funnel3d.js',
  75. 'lib/highcharts_modules/heikinashi.js',
  76. 'lib/highcharts_modules/hollowcandlestick.js',
  77. 'lib/highcharts_modules/pyramid3d.js',
  78. 'lib/highcharts_modules/cylinder.js',
  79. ]
  80. register_component('chart', __file__, 'chart.js', dependencies, optional_dependencies)
  81. class Chart(Element):
  82. def __init__(self, options: Dict, *, type: str = 'chart', extras: List[str] = []) -> None:
  83. """Chart
  84. An element to create a chart using `Highcharts <https://www.highcharts.com/>`_.
  85. Updates can be pushed to the chart by changing the `options` property.
  86. After data has changed, call the `update` method to refresh the chart.
  87. By default, a `Highcharts.chart` is created.
  88. To use, e.g., `Highcharts.stockChart` instead, set the `type` property to "stockChart".
  89. :param options: dictionary of Highcharts options
  90. :param type: chart type (e.g. "chart", "stockChart", "mapChart", ...; default: "chart")
  91. :param extras: list of extra dependencies to include (e.g. "annotations", "arc-diagram", "solid-gauge", ...)
  92. """
  93. super().__init__('chart')
  94. self._props['type'] = type
  95. self._props['options'] = options
  96. self._props['extras'] = [
  97. dependency.import_path
  98. for dependency in js_dependencies.values()
  99. if dependency.optional and dependency.path.stem in extras and 'chart' in dependency.dependents
  100. ]
  101. @property
  102. def options(self) -> Dict:
  103. return self._props['options']
  104. def update(self) -> None:
  105. super().update()
  106. self.run_method('update_chart')