interpolation-modes.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Line Chart - Cubic interpolation mode</title>
  5. <script src="../../../dist/Chart.bundle.js"></script>
  6. <script src="../../utils.js"></script>
  7. <style>
  8. canvas{
  9. -moz-user-select: none;
  10. -webkit-user-select: none;
  11. -ms-user-select: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="width:75%;">
  17. <canvas id="canvas"></canvas>
  18. </div>
  19. <br>
  20. <br>
  21. <button id="randomizeData">Randomize Data</button>
  22. <script>
  23. var randomScalingFactor = function() {
  24. return Math.round(Math.random() * 100);
  25. };
  26. var datapoints = [0, 20, 20, 60, 60, 120, NaN, 180, 120, 125, 105, 110, 170];
  27. var config = {
  28. type: 'line',
  29. data: {
  30. labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  31. datasets: [{
  32. label: 'Cubic interpolation (monotone)',
  33. data: datapoints,
  34. borderColor: window.chartColors.red,
  35. backgroundColor: 'rgba(0, 0, 0, 0)',
  36. fill: false,
  37. cubicInterpolationMode: 'monotone'
  38. }, {
  39. label: 'Cubic interpolation (default)',
  40. data: datapoints,
  41. borderColor: window.chartColors.blue,
  42. backgroundColor: 'rgba(0, 0, 0, 0)',
  43. fill: false,
  44. }, {
  45. label: 'Linear interpolation',
  46. data: datapoints,
  47. borderColor: window.chartColors.green,
  48. backgroundColor: 'rgba(0, 0, 0, 0)',
  49. fill: false,
  50. lineTension: 0
  51. }]
  52. },
  53. options: {
  54. responsive: true,
  55. title: {
  56. display: true,
  57. text: 'Chart.js Line Chart - Cubic interpolation mode'
  58. },
  59. tooltips: {
  60. mode: 'index'
  61. },
  62. scales: {
  63. xAxes: [{
  64. display: true,
  65. scaleLabel: {
  66. display: true
  67. }
  68. }],
  69. yAxes: [{
  70. display: true,
  71. scaleLabel: {
  72. display: true,
  73. labelString: 'Value'
  74. },
  75. ticks: {
  76. suggestedMin: -10,
  77. suggestedMax: 200,
  78. }
  79. }]
  80. }
  81. }
  82. };
  83. window.onload = function() {
  84. var ctx = document.getElementById('canvas').getContext('2d');
  85. window.myLine = new Chart(ctx, config);
  86. };
  87. document.getElementById('randomizeData').addEventListener('click', function() {
  88. for (var i = 0, len = datapoints.length; i < len; ++i) {
  89. datapoints[i] = Math.random() < 0.05 ? NaN : randomScalingFactor();
  90. }
  91. window.myLine.update();
  92. });
  93. </script>
  94. </body>
  95. </html>