api.axis.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Chart } from './core'
  2. import { isValue, isDefined } from './util'
  3. Chart.prototype.axis = function() {}
  4. Chart.prototype.axis.labels = function(labels) {
  5. var $$ = this.internal
  6. if (arguments.length) {
  7. Object.keys(labels).forEach(function(axisId) {
  8. $$.axis.setLabelText(axisId, labels[axisId])
  9. })
  10. $$.axis.updateLabels()
  11. }
  12. // TODO: return some values?
  13. }
  14. Chart.prototype.axis.max = function(max) {
  15. var $$ = this.internal,
  16. config = $$.config
  17. if (arguments.length) {
  18. if (typeof max === 'object') {
  19. if (isValue(max.x)) {
  20. config.axis_x_max = max.x
  21. }
  22. if (isValue(max.y)) {
  23. config.axis_y_max = max.y
  24. }
  25. if (isValue(max.y2)) {
  26. config.axis_y2_max = max.y2
  27. }
  28. } else {
  29. config.axis_y_max = config.axis_y2_max = max
  30. }
  31. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true })
  32. } else {
  33. return {
  34. x: config.axis_x_max,
  35. y: config.axis_y_max,
  36. y2: config.axis_y2_max
  37. }
  38. }
  39. }
  40. Chart.prototype.axis.min = function(min) {
  41. var $$ = this.internal,
  42. config = $$.config
  43. if (arguments.length) {
  44. if (typeof min === 'object') {
  45. if (isValue(min.x)) {
  46. config.axis_x_min = min.x
  47. }
  48. if (isValue(min.y)) {
  49. config.axis_y_min = min.y
  50. }
  51. if (isValue(min.y2)) {
  52. config.axis_y2_min = min.y2
  53. }
  54. } else {
  55. config.axis_y_min = config.axis_y2_min = min
  56. }
  57. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true })
  58. } else {
  59. return {
  60. x: config.axis_x_min,
  61. y: config.axis_y_min,
  62. y2: config.axis_y2_min
  63. }
  64. }
  65. }
  66. Chart.prototype.axis.range = function(range) {
  67. if (arguments.length) {
  68. if (isDefined(range.max)) {
  69. this.axis.max(range.max)
  70. }
  71. if (isDefined(range.min)) {
  72. this.axis.min(range.min)
  73. }
  74. } else {
  75. return {
  76. max: this.axis.max(),
  77. min: this.axis.min()
  78. }
  79. }
  80. }
  81. Chart.prototype.axis.types = function(types) {
  82. const $$ = this.internal
  83. if (types === undefined) {
  84. return {
  85. y: $$.config.axis_y_type,
  86. y2: $$.config.axis_y2_type
  87. }
  88. } else {
  89. if (isDefined(types.y)) {
  90. $$.config.axis_y_type = types.y
  91. }
  92. if (isDefined(types.y2)) {
  93. $$.config.axis_y2_type = types.y2
  94. }
  95. $$.updateScales()
  96. $$.redraw()
  97. }
  98. }