createView.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var View = require("../../coord/View");
  20. var _layout = require("../../util/layout");
  21. var getLayoutRect = _layout.getLayoutRect;
  22. var bbox = require("zrender/lib/core/bbox");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. // FIXME Where to create the simple view coordinate system
  42. function getViewRect(seriesModel, api, aspect) {
  43. var option = seriesModel.getBoxLayoutParams();
  44. option.aspect = aspect;
  45. return getLayoutRect(option, {
  46. width: api.getWidth(),
  47. height: api.getHeight()
  48. });
  49. }
  50. function _default(ecModel, api) {
  51. var viewList = [];
  52. ecModel.eachSeriesByType('graph', function (seriesModel) {
  53. var coordSysType = seriesModel.get('coordinateSystem');
  54. if (!coordSysType || coordSysType === 'view') {
  55. var data = seriesModel.getData();
  56. var positions = data.mapArray(function (idx) {
  57. var itemModel = data.getItemModel(idx);
  58. return [+itemModel.get('x'), +itemModel.get('y')];
  59. });
  60. var min = [];
  61. var max = [];
  62. bbox.fromPoints(positions, min, max); // If width or height is 0
  63. if (max[0] - min[0] === 0) {
  64. max[0] += 1;
  65. min[0] -= 1;
  66. }
  67. if (max[1] - min[1] === 0) {
  68. max[1] += 1;
  69. min[1] -= 1;
  70. }
  71. var aspect = (max[0] - min[0]) / (max[1] - min[1]); // FIXME If get view rect after data processed?
  72. var viewRect = getViewRect(seriesModel, api, aspect); // Position may be NaN, use view rect instead
  73. if (isNaN(aspect)) {
  74. min = [viewRect.x, viewRect.y];
  75. max = [viewRect.x + viewRect.width, viewRect.y + viewRect.height];
  76. }
  77. var bbWidth = max[0] - min[0];
  78. var bbHeight = max[1] - min[1];
  79. var viewWidth = viewRect.width;
  80. var viewHeight = viewRect.height;
  81. var viewCoordSys = seriesModel.coordinateSystem = new View();
  82. viewCoordSys.zoomLimit = seriesModel.get('scaleLimit');
  83. viewCoordSys.setBoundingRect(min[0], min[1], bbWidth, bbHeight);
  84. viewCoordSys.setViewRect(viewRect.x, viewRect.y, viewWidth, viewHeight); // Update roam info
  85. viewCoordSys.setCenter(seriesModel.get('center'));
  86. viewCoordSys.setZoom(seriesModel.get('zoom'));
  87. viewList.push(viewCoordSys);
  88. }
  89. });
  90. return viewList;
  91. }
  92. module.exports = _default;