Progressive Line

Progressive Line - 图1

config animation data

  1. const config = {
  2. type: 'line',
  3. data: {
  4. datasets: [{
  5. borderColor: Utils.CHART_COLORS.red,
  6. borderWidth: 1,
  7. radius: 0,
  8. data: data,
  9. },
  10. {
  11. borderColor: Utils.CHART_COLORS.blue,
  12. borderWidth: 1,
  13. radius: 0,
  14. data: data2,
  15. }]
  16. },
  17. options: {
  18. animation,
  19. interaction: {
  20. intersect: false
  21. },
  22. plugins: {
  23. legend: false
  24. },
  25. scales: {
  26. x: {
  27. type: 'linear'
  28. }
  29. }
  30. }
  31. };
  1. const totalDuration = 10000;
  2. const delayBetweenPoints = totalDuration / data.length;
  3. const previousY = (ctx) => ctx.index === 0 ? ctx.chart.scales.y.getPixelForValue(100) : ctx.chart.getDatasetMeta(ctx.datasetIndex).data[ctx.index - 1].getProps(['y'], true).y;
  4. const animation = {
  5. x: {
  6. type: 'number',
  7. easing: 'linear',
  8. duration: delayBetweenPoints,
  9. from: NaN, // the point is initially skipped
  10. delay(ctx) {
  11. if (ctx.type !== 'data' || ctx.xStarted) {
  12. return 0;
  13. }
  14. ctx.xStarted = true;
  15. return ctx.index * delayBetweenPoints;
  16. }
  17. },
  18. y: {
  19. type: 'number',
  20. easing: 'linear',
  21. duration: delayBetweenPoints,
  22. from: previousY,
  23. delay(ctx) {
  24. if (ctx.type !== 'data' || ctx.yStarted) {
  25. return 0;
  26. }
  27. ctx.yStarted = true;
  28. return ctx.index * delayBetweenPoints;
  29. }
  30. }
  31. };
  1. const data = [];
  2. const data2 = [];
  3. let prev = 100;
  4. let prev2 = 80;
  5. for (let i = 0; i < 1000; i++) {
  6. prev += 5 - Math.random() * 10;
  7. data.push({x: i, y: prev});
  8. prev2 += 5 - Math.random() * 10;
  9. data2.push({x: i, y: prev2});
  10. }