Charts.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @author tlwr [toby@toby.codes]
  3. * @author Matt C [me@mitt.dev]
  4. * @copyright Crown Copyright 2019
  5. * @license Apache-2.0
  6. */
  7. import OperationError from "../errors/OperationError.mjs";
  8. /**
  9. * @constant
  10. * @default
  11. */
  12. export const RECORD_DELIMITER_OPTIONS = ["Line feed", "CRLF"];
  13. /**
  14. * @constant
  15. * @default
  16. */
  17. export const FIELD_DELIMITER_OPTIONS = ["Space", "Comma", "Semi-colon", "Colon", "Tab"];
  18. /**
  19. * Default from colour
  20. *
  21. * @constant
  22. * @default
  23. */
  24. export const COLOURS = {
  25. min: "white",
  26. max: "black"
  27. };
  28. /**
  29. * Gets values from input for a plot.
  30. *
  31. * @param {string} input
  32. * @param {string} recordDelimiter
  33. * @param {string} fieldDelimiter
  34. * @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
  35. * @param {number} length
  36. * @returns {Object[]}
  37. */
  38. export function getValues(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded, length) {
  39. let headings;
  40. const values = [];
  41. input
  42. .split(recordDelimiter)
  43. .forEach((row, rowIndex) => {
  44. const split = row.split(fieldDelimiter);
  45. if (split.length !== length) throw new OperationError(`Each row must have length ${length}.`);
  46. if (columnHeadingsAreIncluded && rowIndex === 0) {
  47. headings = split;
  48. } else {
  49. values.push(split);
  50. }
  51. });
  52. return { headings, values };
  53. }
  54. /**
  55. * Gets values from input for a scatter plot.
  56. *
  57. * @param {string} input
  58. * @param {string} recordDelimiter
  59. * @param {string} fieldDelimiter
  60. * @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
  61. * @returns {Object[]}
  62. */
  63. export function getScatterValues(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded) {
  64. let { headings, values } = getValues(
  65. input,
  66. recordDelimiter,
  67. fieldDelimiter,
  68. columnHeadingsAreIncluded,
  69. 2
  70. );
  71. if (headings) {
  72. headings = {x: headings[0], y: headings[1]};
  73. }
  74. values = values.map(row => {
  75. const x = parseFloat(row[0], 10),
  76. y = parseFloat(row[1], 10);
  77. if (Number.isNaN(x)) throw new OperationError("Values must be numbers in base 10.");
  78. if (Number.isNaN(y)) throw new OperationError("Values must be numbers in base 10.");
  79. return [x, y];
  80. });
  81. return { headings, values };
  82. }
  83. /**
  84. * Gets values from input for a scatter plot with colour from the third column.
  85. *
  86. * @param {string} input
  87. * @param {string} recordDelimiter
  88. * @param {string} fieldDelimiter
  89. * @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
  90. * @returns {Object[]}
  91. */
  92. export function getScatterValuesWithColour(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded) {
  93. let { headings, values } = getValues(
  94. input,
  95. recordDelimiter, fieldDelimiter,
  96. columnHeadingsAreIncluded,
  97. 3
  98. );
  99. if (headings) {
  100. headings = {x: headings[0], y: headings[1]};
  101. }
  102. values = values.map(row => {
  103. const x = parseFloat(row[0], 10),
  104. y = parseFloat(row[1], 10),
  105. colour = row[2];
  106. if (Number.isNaN(x)) throw new OperationError("Values must be numbers in base 10.");
  107. if (Number.isNaN(y)) throw new OperationError("Values must be numbers in base 10.");
  108. return [x, y, colour];
  109. });
  110. return { headings, values };
  111. }
  112. /**
  113. * Gets values from input for a time series plot.
  114. *
  115. * @param {string} input
  116. * @param {string} recordDelimiter
  117. * @param {string} fieldDelimiter
  118. * @param {boolean} columnHeadingsAreIncluded - whether we should skip the first record
  119. * @returns {Object[]}
  120. */
  121. export function getSeriesValues(input, recordDelimiter, fieldDelimiter, columnHeadingsAreIncluded) {
  122. const { values } = getValues(
  123. input,
  124. recordDelimiter, fieldDelimiter,
  125. false,
  126. 3
  127. );
  128. let xValues = new Set();
  129. const series = {};
  130. values.forEach(row => {
  131. const serie = row[0],
  132. xVal = row[1],
  133. val = parseFloat(row[2], 10);
  134. if (Number.isNaN(val)) throw new OperationError("Values must be numbers in base 10.");
  135. xValues.add(xVal);
  136. if (typeof series[serie] === "undefined") series[serie] = {};
  137. series[serie][xVal] = val;
  138. });
  139. xValues = new Array(...xValues);
  140. const seriesList = [];
  141. for (const seriesName in series) {
  142. const serie = series[seriesName];
  143. seriesList.push({name: seriesName, data: serie});
  144. }
  145. return { xValues, series: seriesList };
  146. }