Utils

Disclaimer

The Utils file contains multiple helper functions that the chart.js sample pages use to generate charts. These functions are subject to change, including but not limited to breaking changes without prior notice.

Because of this please don’t rely on this file in production environments.

Functions

  1. import colorLib from '@kurkle/color';
  2. import {DateTime} from 'luxon';
  3. import 'chartjs-adapter-luxon';
  4. import {valueOrDefault} from '../../dist/helpers.mjs';
  5. // Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
  6. var _seed = Date.now();
  7. export function srand(seed) {
  8. _seed = seed;
  9. }
  10. export function rand(min, max) {
  11. min = valueOrDefault(min, 0);
  12. max = valueOrDefault(max, 0);
  13. _seed = (_seed * 9301 + 49297) % 233280;
  14. return min + (_seed / 233280) * (max - min);
  15. }
  16. export function numbers(config) {
  17. var cfg = config || {};
  18. var min = valueOrDefault(cfg.min, 0);
  19. var max = valueOrDefault(cfg.max, 100);
  20. var from = valueOrDefault(cfg.from, []);
  21. var count = valueOrDefault(cfg.count, 8);
  22. var decimals = valueOrDefault(cfg.decimals, 8);
  23. var continuity = valueOrDefault(cfg.continuity, 1);
  24. var dfactor = Math.pow(10, decimals) || 0;
  25. var data = [];
  26. var i, value;
  27. for (i = 0; i < count; ++i) {
  28. value = (from[i] || 0) + this.rand(min, max);
  29. if (this.rand() <= continuity) {
  30. data.push(Math.round(dfactor * value) / dfactor);
  31. } else {
  32. data.push(null);
  33. }
  34. }
  35. return data;
  36. }
  37. export function points(config) {
  38. const xs = this.numbers(config);
  39. const ys = this.numbers(config);
  40. return xs.map((x, i) => ({x, y: ys[i]}));
  41. }
  42. export function bubbles(config) {
  43. return this.points(config).map(pt => {
  44. pt.r = this.rand(config.rmin, config.rmax);
  45. return pt;
  46. });
  47. }
  48. export function labels(config) {
  49. var cfg = config || {};
  50. var min = cfg.min || 0;
  51. var max = cfg.max || 100;
  52. var count = cfg.count || 8;
  53. var step = (max - min) / count;
  54. var decimals = cfg.decimals || 8;
  55. var dfactor = Math.pow(10, decimals) || 0;
  56. var prefix = cfg.prefix || '';
  57. var values = [];
  58. var i;
  59. for (i = min; i < max; i += step) {
  60. values.push(prefix + Math.round(dfactor * i) / dfactor);
  61. }
  62. return values;
  63. }
  64. const MONTHS = [
  65. 'January',
  66. 'February',
  67. 'March',
  68. 'April',
  69. 'May',
  70. 'June',
  71. 'July',
  72. 'August',
  73. 'September',
  74. 'October',
  75. 'November',
  76. 'December'
  77. ];
  78. export function months(config) {
  79. var cfg = config || {};
  80. var count = cfg.count || 12;
  81. var section = cfg.section;
  82. var values = [];
  83. var i, value;
  84. for (i = 0; i < count; ++i) {
  85. value = MONTHS[Math.ceil(i) % 12];
  86. values.push(value.substring(0, section));
  87. }
  88. return values;
  89. }
  90. const COLORS = [
  91. '#4dc9f6',
  92. '#f67019',
  93. '#f53794',
  94. '#537bc4',
  95. '#acc236',
  96. '#166a8f',
  97. '#00a950',
  98. '#58595b',
  99. '#8549ba'
  100. ];
  101. export function color(index) {
  102. return COLORS[index % COLORS.length];
  103. }
  104. export function transparentize(value, opacity) {
  105. var alpha = opacity === undefined ? 0.5 : 1 - opacity;
  106. return colorLib(value).alpha(alpha).rgbString();
  107. }
  108. export const CHART_COLORS = {
  109. red: 'rgb(255, 99, 132)',
  110. orange: 'rgb(255, 159, 64)',
  111. yellow: 'rgb(255, 205, 86)',
  112. green: 'rgb(75, 192, 192)',
  113. blue: 'rgb(54, 162, 235)',
  114. purple: 'rgb(153, 102, 255)',
  115. grey: 'rgb(201, 203, 207)'
  116. };
  117. const NAMED_COLORS = [
  118. CHART_COLORS.red,
  119. CHART_COLORS.orange,
  120. CHART_COLORS.yellow,
  121. CHART_COLORS.green,
  122. CHART_COLORS.blue,
  123. CHART_COLORS.purple,
  124. CHART_COLORS.grey,
  125. ];
  126. export function namedColor(index) {
  127. return NAMED_COLORS[index % NAMED_COLORS.length];
  128. }
  129. export function newDate(days) {
  130. return DateTime.now().plus({days}).toJSDate();
  131. }
  132. export function newDateString(days) {
  133. return DateTime.now().plus({days}).toISO();
  134. }
  135. export function parseISODate(str) {
  136. return DateTime.fromISO(str);
  137. }

File on githubUtils - 图1 (opens new window)

Components

Some of the samples make reference to a components object. This is an artifact of using a module bundler to build the samples. The creation of that components object is shown below. If chart.js is included as a browser script, these items are accessible via the Chart object, i.e Chart.Tooltip.

  1. // Add Chart components needed in samples here.
  2. // Usable through `components[name]`.
  3. export {Tooltip} from '../../dist/chart.mjs';

File on githubUtils - 图2 (opens new window)