api.zoom.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Chart } from './core'
  2. import { isDefined } from './util'
  3. Chart.prototype.zoom = function(domain) {
  4. var $$ = this.internal
  5. if (domain) {
  6. if ($$.isTimeSeries()) {
  7. domain = domain.map(function(x) {
  8. return $$.parseDate(x)
  9. })
  10. }
  11. if ($$.config.subchart_show) {
  12. $$.brush.selectionAsValue(domain, true)
  13. } else {
  14. $$.updateXDomain(null, true, false, false, domain)
  15. $$.redraw({ withY: $$.config.zoom_rescale, withSubchart: false })
  16. }
  17. $$.config.zoom_onzoom.call(this, $$.x.orgDomain())
  18. return domain
  19. } else {
  20. return $$.x.domain()
  21. }
  22. }
  23. Chart.prototype.zoom.enable = function(enabled) {
  24. var $$ = this.internal
  25. $$.config.zoom_enabled = enabled
  26. $$.updateAndRedraw()
  27. }
  28. Chart.prototype.unzoom = function() {
  29. var $$ = this.internal
  30. if ($$.config.subchart_show) {
  31. $$.brush.clear()
  32. } else {
  33. $$.updateXDomain(null, true, false, false, $$.subX.domain())
  34. $$.redraw({ withY: $$.config.zoom_rescale, withSubchart: false })
  35. }
  36. }
  37. Chart.prototype.zoom.max = function(max) {
  38. var $$ = this.internal,
  39. config = $$.config,
  40. d3 = $$.d3
  41. if (max === 0 || max) {
  42. config.zoom_x_max = d3.max([$$.orgXDomain[1], max])
  43. } else {
  44. return config.zoom_x_max
  45. }
  46. }
  47. Chart.prototype.zoom.min = function(min) {
  48. var $$ = this.internal,
  49. config = $$.config,
  50. d3 = $$.d3
  51. if (min === 0 || min) {
  52. config.zoom_x_min = d3.min([$$.orgXDomain[0], min])
  53. } else {
  54. return config.zoom_x_min
  55. }
  56. }
  57. Chart.prototype.zoom.range = function(range) {
  58. if (arguments.length) {
  59. if (isDefined(range.max)) {
  60. this.domain.max(range.max)
  61. }
  62. if (isDefined(range.min)) {
  63. this.domain.min(range.min)
  64. }
  65. } else {
  66. return {
  67. max: this.domain.max(),
  68. min: this.domain.min()
  69. }
  70. }
  71. }