Linear Scale - Step Size

Linear Scale - Step Size - 图1

RandomizeAdd DatasetAdd DataRemove DatasetRemove Data

config setup actions

  1. const config = {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. responsive: true,
  6. plugins: {
  7. tooltip: {
  8. mode: 'index',
  9. intersect: false
  10. },
  11. title: {
  12. display: true,
  13. text: 'Chart.js Line Chart'
  14. }
  15. },
  16. hover: {
  17. mode: 'index',
  18. intersec: false
  19. },
  20. scales: {
  21. x: {
  22. title: {
  23. display: true,
  24. text: 'Month'
  25. }
  26. },
  27. y: {
  28. title: {
  29. display: true,
  30. text: 'Value'
  31. },
  32. min: 0,
  33. max: 100,
  34. ticks: {
  35. // forces step size to be 50 units
  36. stepSize: 50
  37. }
  38. }
  39. }
  40. },
  41. };
  1. const DATA_COUNT = 7;
  2. const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 100};
  3. const labels = Utils.months({count: 7});
  4. const data = {
  5. labels: labels,
  6. datasets: [
  7. {
  8. label: 'Dataset 1',
  9. data: Utils.numbers(NUMBER_CFG),
  10. borderColor: Utils.CHART_COLORS.red,
  11. backgroundColor: Utils.CHART_COLORS.red,
  12. },
  13. {
  14. label: 'Dataset 2',
  15. data: Utils.numbers(NUMBER_CFG),
  16. borderColor: Utils.CHART_COLORS.blue,
  17. backgroundColor: Utils.CHART_COLORS.blue,
  18. }
  19. ]
  20. };
  1. const actions = [
  2. {
  3. name: 'Randomize',
  4. handler(chart) {
  5. chart.data.datasets.forEach(dataset => {
  6. dataset.data = Utils.numbers({count: chart.data.labels.length, min: 0, max: 100});
  7. });
  8. chart.update();
  9. }
  10. },
  11. {
  12. name: 'Add Dataset',
  13. handler(chart) {
  14. const data = chart.data;
  15. const dsColor = Utils.namedColor(chart.data.datasets.length);
  16. const newDataset = {
  17. label: 'Dataset ' + (data.datasets.length + 1),
  18. backgroundColor: dsColor,
  19. borderColor: dsColor,
  20. data: Utils.numbers({count: data.labels.length, min: 0, max: 100}),
  21. };
  22. chart.data.datasets.push(newDataset);
  23. chart.update();
  24. }
  25. },
  26. {
  27. name: 'Add Data',
  28. handler(chart) {
  29. const data = chart.data;
  30. if (data.datasets.length > 0) {
  31. data.labels = Utils.months({count: data.labels.length + 1});
  32. for (let index = 0; index < data.datasets.length; ++index) {
  33. data.datasets[index].data.push(Utils.rand(0, 100));
  34. }
  35. chart.update();
  36. }
  37. }
  38. },
  39. {
  40. name: 'Remove Dataset',
  41. handler(chart) {
  42. chart.data.datasets.pop();
  43. chart.update();
  44. }
  45. },
  46. {
  47. name: 'Remove Data',
  48. handler(chart) {
  49. chart.data.labels.splice(-1, 1); // remove the label first
  50. chart.data.datasets.forEach(dataset => {
  51. dataset.data.pop();
  52. });
  53. chart.update();
  54. }
  55. }
  56. ];

Docs