pie_chart_widget.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var numberUtils = require('utils/number_utils');
  20. App.PieChartDashboardWidgetView = App.DashboardWidgetView.extend({
  21. templateName: require('templates/main/dashboard/widgets/pie_chart'),
  22. maxValue: 100,
  23. modelFieldMax: null,
  24. modelFieldUsed: null,
  25. hiddenInfo: null,
  26. isPieExist: null,
  27. dataForPieChart: null,
  28. widgetHtmlId: null,
  29. getUsed: function() {
  30. return this.get('model').get(this.get('modelFieldUsed')) || 0;
  31. },
  32. getMax: function() {
  33. return this.get('model').get(this.get('modelFieldMax')) || 0;
  34. },
  35. calcHiddenInfo: function() {
  36. var used = this.getUsed();
  37. var max = this.getMax();
  38. var percent = max > 0 ? 100 * used / max : 0;
  39. var result = [];
  40. result.pushObject(percent.toFixed(1) + '% used');
  41. result.pushObject(numberUtils.bytesToSize(used, 1, 'parseFloat', 1024 * 1024) + ' of ' + numberUtils.bytesToSize(max, 1, 'parseFloat', 1024 * 1024));
  42. return result;
  43. },
  44. calcIsPieExists: function() {
  45. return (this.get('model').get(this.get('modelFieldMax')) > 0);
  46. },
  47. calcDataForPieChart: function() {
  48. var used = this.get('model').get(this.get('modelFieldUsed'));
  49. var total = this.get('model').get(this.get('modelFieldMax'));
  50. var percent = total > 0 ? ((used)*100 / total).toFixed() : 0;
  51. var percent_precise = total > 0 ? ((used)*100 / total).toFixed(1) : 0;
  52. return [percent, percent_precise];
  53. },
  54. calc: function() {
  55. this.set('hiddenInfo', this.calcHiddenInfo());
  56. this.set('isPieExist', this.calcIsPieExists());
  57. this.set('dataForPieChart', this.calcDataForPieChart());
  58. },
  59. didInsertElement: function() {
  60. this._super();
  61. this.addObserver('model.' + this.get('modelFieldMax'), this, this.calc);
  62. this.addObserver('model.' + this.get('modelFieldUsed'), this, this.calc);
  63. },
  64. content: App.ChartPieView.extend({
  65. model: null, //data bind here
  66. id: function() {
  67. return this.get('parentView.widgetHtmlId');
  68. }.property('parentView.widgetHtmlId'), // html id
  69. stroke: '#D6DDDF', //light grey
  70. thresh1: null, //bind from parent
  71. thresh2: null,
  72. innerR: 25,
  73. existCenterText: true,
  74. centerTextColor: function () {
  75. return this.get('contentColor');
  76. }.property('contentColor'),
  77. palette: new Rickshaw.Color.Palette({
  78. scheme: [ '#FFFFFF', '#D6DDDF'].reverse()
  79. }),
  80. data: function() {
  81. var ori_data = this.get('parentView.dataForPieChart');
  82. return [ ori_data[0], 100 - ori_data[0]];
  83. }.property(),
  84. setData: function() {
  85. this.set('data', this.get('parentView.dataForPieChart'));
  86. }.observes('parentView.dataForPieChart'),
  87. contentColor: function () {
  88. var used = parseFloat(this.get('parentView.dataForPieChart')[1]);
  89. var thresh1 = parseFloat(this.get('thresh1'));
  90. var thresh2 = parseFloat(this.get('thresh2'));
  91. var color_green = '#95A800';
  92. var color_red = '#B80000';
  93. var color_orange = '#FF8E00';
  94. if (used <= thresh1) {
  95. this.set('palette', new Rickshaw.Color.Palette({
  96. scheme: [ '#FFFFFF', color_green ].reverse()
  97. }));
  98. return color_green;
  99. } else if (used <= thresh2) {
  100. this.set('palette', new Rickshaw.Color.Palette({
  101. scheme: [ '#FFFFFF', color_orange ].reverse()
  102. }));
  103. return color_orange;
  104. } else {
  105. this.set('palette', new Rickshaw.Color.Palette({
  106. scheme: [ '#FFFFFF', color_red ].reverse()
  107. }));
  108. return color_red;
  109. }
  110. }.property('data', 'thresh1', 'thresh2'),
  111. // refresh text and color when data in model changed
  112. refreshSvg: function () {
  113. // remove old svg
  114. var old_svg = $("#" + this.get('id'));
  115. if(old_svg){
  116. old_svg.remove();
  117. }
  118. // draw new svg
  119. this.appendSvg();
  120. }.observes('data', 'thresh1', 'thresh2')
  121. })
  122. });