Guest User

Untitled

a guest
Oct 17th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import React from 'react';
  2. import { sentiments } from "config";
  3. import { percentage } from 'utils/index';
  4. import './ParentChildLabelBySentimentBar.css';
  5. import { labelFilter } from './filterLabels.js';
  6.  
  7. export default (props) => {
  8. if (!props.data) {
  9. return <span>No data</span>;
  10. }
  11. const { data: { loading, error, dataMapping }, width, height, data, showLabel } = props;
  12. if (loading) {
  13. return <span>Chart Loading</span>;
  14. }
  15. if (error) {
  16. data.refetch().catch(e => null);
  17. return <span>Connecting</span>;
  18. }
  19. if (dataMapping.buckets.length === 0) {
  20. return <span>No data</span>
  21. }
  22.  
  23. const labelsChecked = {};
  24. labelFilter.forEach(label => {
  25. if (label.check) {
  26. if (label.labels.length > 0) {
  27. label.labels.forEach(item => {
  28. if (item.check) {
  29. labelsChecked[item._id] = {
  30. name: item.name,
  31. parentName: label.name,
  32. parentId: label._id,
  33. };
  34. }
  35. });
  36. } else {
  37. labelsChecked[label._id] = {
  38. name: label.name,
  39. parentName: null,
  40. parentId: null
  41. };
  42. }
  43. }
  44. });
  45. // console.log("labelsChecked: ", labelsChecked);
  46. // console.log("labelData: ", dataMapping.buckets);
  47. const labelData = dataMapping.buckets;
  48. const labelWithParent = {};
  49. labelData.forEach(label => {
  50. const currentLabel = labelsChecked[label.key];
  51. if (currentLabel && currentLabel.parentId) {
  52. console.log("currentLabel.parentId: ", currentLabel.parentId);
  53. if (labelWithParent.hasOwnProperty(currentLabel.parentId)) {
  54. label.groupBy.buckets.forEach(bucket => {
  55. if (labelWithParent[currentLabel.parentId]['groupBy']['buckets'].hasOwnProperty(bucket.key)) {
  56. labelWithParent[currentLabel.parentId]['groupBy']['buckets'][bucket.key] = labelWithParent[currentLabel.parentId]['groupBy']['buckets'][bucket.key] + bucket.doc_count;
  57. } else {
  58. labelWithParent[currentLabel.parentId]['groupBy']['buckets'][bucket.key] = bucket.doc_count;
  59. }
  60.  
  61. });
  62. labelWithParent[currentLabel.parentId]['labels'].push(label);
  63. labelWithParent[currentLabel.parentId]['doc_count'] = labelWithParent[currentLabel.parentId]['doc_count'] + label.doc_count;
  64. } else {
  65. console.log("else.parentId: ", currentLabel.parentId);
  66.  
  67. const buckets = {};
  68. label.groupBy.buckets.forEach(bucket => {
  69. buckets[bucket.key] = bucket.doc_count;
  70. });
  71. labelWithParent[currentLabel.parentId] = {
  72. name: currentLabel.parentName,
  73. labels: [label],
  74. groupBy: {
  75. buckets
  76. },
  77. doc_count: label.doc_count
  78. }
  79. }
  80. } else if (currentLabel) {
  81. labelWithParent[label.key] = {
  82. name: label.name,
  83. doc_count: label.doc_count,
  84. groupBy: label.groupBy,
  85. labels: []
  86. }
  87. }
  88. });
  89. let dataFinal = [];
  90. for (let [key, value] of Object.entries(labelWithParent)) {
  91. if (value.labels.length > 0) {
  92. const buckets = [];
  93. for (let [k, v] of Object.entries(value.groupBy.buckets)) {
  94. buckets.push({
  95. key: k,
  96. doc_count: v
  97. });
  98. }
  99. dataFinal.push({
  100. key: key,
  101. doc_count: value.doc_count,
  102. name: value.name,
  103. groupBy: {
  104. buckets: buckets
  105. },
  106. parent: true
  107. });
  108.  
  109. //push child
  110. const newData = [...dataFinal, ...value.labels];
  111. dataFinal = newData;
  112. } else {
  113. const buckets = [];
  114. for (let [k, v] of Object.entries(value.groupBy.buckets)) {
  115. buckets.push({
  116. key: v.key,
  117. doc_count: v.doc_count
  118. });
  119. }
  120. dataFinal.push({
  121. key: key,
  122. doc_count: value.doc_count,
  123. name: value.name,
  124. groupBy: {
  125. buckets: buckets
  126. },
  127. parent: true
  128. });
  129. }
  130. }
  131. //map data to chart
  132. const content = dataFinal.map((bucket, i) => {
  133. let iconParrent,
  134. classParent = null,
  135. styleLabel = {
  136. display: 'none'
  137. };
  138. if (bucket.parent) {
  139. iconParrent = (<i className="fa fa-caret-down"></i>);
  140. classParent = 'is-parent';
  141. styleLabel = null;
  142. } else if (showLabel) {
  143. styleLabel = null;
  144. }
  145. const progressSentiment = bucket.groupBy.buckets.map((item, k) => {
  146. const sentimentType = item.key == '1' ? 'positive' : item.key == '2' ? 'negative' : 'neutral';
  147. const value = percentage(item.doc_count, dataFinal[0].doc_count);
  148. const graphData = {
  149. total: item.doc_count,
  150. sentimentName: sentimentType,
  151. labelName: bucket.name,
  152. sentimentId: item.key,
  153. labelId: bucket.key,
  154. parent: bucket.parent || false
  155. };
  156. return (
  157. <div
  158. className={`progress-bar bg-${sentimentType}`}
  159. role="progressbar"
  160. style={{ width: value + '%' }}
  161. aria-valuenow={value}
  162. aria-valuemin="0"
  163. aria-valuemax="100"
  164. data-toggle="tooltip"
  165. data-placement="top"
  166. title={`${sentimentType}: ${item.doc_count} buzz`}
  167. key={k} >
  168. {item.doc_count}
  169. </div>
  170. )
  171. });
  172. return (
  173. <div className={`columns is-mobile ${classParent}`} key={i} style={styleLabel}>
  174. <div className="column">
  175. <div className="label-name">{bucket.name} {iconParrent}</div>
  176. </div>
  177. <div className="column">
  178. <div className="progress">
  179. {progressSentiment}
  180. <span className="total-buzz">{bucket.doc_count}&nbsp;buzz</span>
  181. </div>
  182. </div>
  183. </div>
  184. );
  185. });
  186.  
  187. return (
  188. <div className="parrent-child-label">
  189. {content}
  190. </div>
  191. );
  192. }
Add Comment
Please, Sign In to add comment