You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

124 lines
2.4 KiB

  1. import { floatTwo, fillArray } from '../utils/helpers';
  2. import { DEFAULT_AXIS_CHART_TYPE, AXIS_DATASET_CHART_TYPES, DEFAULT_CHAR_WIDTH } from '../utils/constants';
  3. export function dataPrep(data, type) {
  4. data.labels = data.labels || [];
  5. let datasetLength = data.labels.length;
  6. // Datasets
  7. let datasets = data.datasets;
  8. let zeroArray = new Array(datasetLength).fill(0);
  9. if(!datasets) {
  10. // default
  11. datasets = [{
  12. values: zeroArray
  13. }];
  14. }
  15. datasets.map((d, i)=> {
  16. // Set values
  17. if(!d.values) {
  18. d.values = zeroArray;
  19. } else {
  20. // Check for non values
  21. let vals = d.values;
  22. vals = vals.map(val => (!isNaN(val) ? val : 0));
  23. // Trim or extend
  24. if(vals.length > datasetLength) {
  25. vals = vals.slice(0, datasetLength);
  26. } else {
  27. vals = fillArray(vals, datasetLength - vals.length, 0);
  28. }
  29. }
  30. // Set labels
  31. //
  32. // Set type
  33. if(!d.chartType ) {
  34. if(!AXIS_DATASET_CHART_TYPES.includes(type)) type === DEFAULT_AXIS_CHART_TYPE;
  35. d.chartType = type;
  36. }
  37. });
  38. // Markers
  39. // Regions
  40. // data.yRegions = data.yRegions || [];
  41. if(data.yRegions) {
  42. data.yRegions.map(d => {
  43. if(d.end < d.start) {
  44. [d.start, d.end] = [d.end, d.start];
  45. }
  46. });
  47. }
  48. return data;
  49. }
  50. export function zeroDataPrep(realData) {
  51. let datasetLength = realData.labels.length;
  52. let zeroArray = new Array(datasetLength).fill(0);
  53. let zeroData = {
  54. labels: realData.labels.slice(0, -1),
  55. datasets: realData.datasets.map(d => {
  56. return {
  57. name: '',
  58. values: zeroArray.slice(0, -1),
  59. chartType: d.chartType
  60. }
  61. }),
  62. };
  63. if(realData.yMarkers) {
  64. zeroData.yMarkers = [
  65. {
  66. value: 0,
  67. label: ''
  68. }
  69. ];
  70. }
  71. if(realData.yRegions) {
  72. zeroData.yRegions = [
  73. {
  74. start: 0,
  75. end: 0,
  76. label: ''
  77. }
  78. ];
  79. }
  80. return zeroData;
  81. }
  82. export function getShortenedLabels(chartWidth, labels=[], isSeries=true) {
  83. let allowedSpace = chartWidth / labels.length;
  84. let allowedLetters = allowedSpace / DEFAULT_CHAR_WIDTH;
  85. let calcLabels = labels.map((label, i) => {
  86. label += "";
  87. if(label.length > allowedLetters) {
  88. if(!isSeries) {
  89. if(allowedLetters-3 > 0) {
  90. label = label.slice(0, allowedLetters-3) + " ...";
  91. } else {
  92. label = label.slice(0, allowedLetters) + '..';
  93. }
  94. } else {
  95. let multiple = Math.ceil(label.length/allowedLetters);
  96. if(i % multiple !== 0) {
  97. label = "";
  98. }
  99. }
  100. }
  101. return label;
  102. });
  103. return calcLabels;
  104. }