Chartjs -在给定标签之间填充日期

pbgvytdp  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(262)

我已经用Chartjs做了几个小时的实验,但我不确定这是否可行。我有一些按日期排序的数据,但并不是所有日期都包括在内。例如:

  1. const salesChart = new Chart(document.getElementById('graph-sales'),
  2. {
  3. type: 'line',
  4. data: {
  5. labels: ['Aug 23','Aug 26','Aug 31','Sep 02'],
  6. datasets: [{
  7. label: 'Sales',
  8. borderColor: '#ccc',
  9. data: [2750.00,1100.00,3080.00,4320.00],
  10. }]
  11. }
  12. }
  13. );

Chartjs将此数据绘制为4个数据点,并使用

这样的线将它们连接起来
这很好,但我希望在图表上添加中间日期,数据点值为0。因此,本质上就像传递以下数据:

  1. labels: ['Aug 23','Aug 24','Aug 25','Aug 26','Aug 27','Aug 28','Aug 29','Aug 30','Aug 31','Sep 01','Sep 02'],
  2. datasets: [{
  3. label: 'Sales',
  4. borderColor: '#ccc',
  5. data: [2750.00,0,0,1100.00,0,0,0,0,3080.00,0,4320.00],
  6. }]

我看过时间刻度,但不能让它们工作。文档说我需要一个时间适配器,但没有使用它的例子,所以我不知道这是什么意思。

bbuxkriu

bbuxkriu1#

在提供数据的后端执行此操作可能会更容易;但是如果您想在前端用JavaScript编写,这段代码就可以工作。
我的方法是使用一个定义的自执行函数,该函数返回一个包含两个数组的数组。
第一个数组名为finalLabels,包含原始标签中提供的日期之间(含)的所有日期字符串。
也就是['Aug 23', 'Aug 24', 'Aug 25', 'Aug 26', 'Aug 27', 'Aug 28', 'Aug 29', 'Aug 30', 'Aug 31', 'Sep 01', 'Sep 02']
返回的第二个数组称为finalDatas,它包含原始标签的同一索引处的所有原始数据值;而在先前未定义该值的情况下则为零。
这就给了我们:[2750, 0, 0, 1100, 0, 0, 0, 0, 3080, 0, 4320]

工作代码代号:https://codepen.io/vpolston/pen/NWMgwOw

我的帐户没有嵌入图片的功能,但这是你最终得到的:是的。
JS系统

  1. const labels = ['Aug 23','Aug 26','Aug 31','Sep 02'];
  2. const data = [2750.00,1100.00,3080.00,4320.00];
  3. const datasets = (function () {
  4. const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
  5. /* define start and end date from labels and create date objects */
  6. const startDateString = labels[0];
  7. const endDateString = labels[labels.length - 1];
  8. const startDateObj = new Date(startDateString);
  9. const endDateObj = new Date(endDateString);
  10. /* create empty dates array to hold dates within range */
  11. let dates = [];
  12. /*
  13. create currentDateObj var to increment in while loop
  14. loop through, add currentDateObj to array, increment until false
  15. */
  16. let currentDateObj = new Date(startDateString);
  17. while( currentDateObj <= endDateObj ){
  18. /* format date to match the provided label and push to dates array */
  19. let dateString = currentDateObj.toDateString();
  20. let month = months[currentDateObj.getMonth()];
  21. let day = currentDateObj.getDate();
  22. if( day < 10 ){
  23. day = '0' + day
  24. };
  25. let date = month + ' ' + day;
  26. dates.push(date);
  27. /* increment CurrentDateObj */
  28. currentDateObj.setDate(currentDateObj.getDate() + 1);
  29. };
  30. /*
  31. use counter to loop through original datas
  32. */
  33. let valueExistsCounter = 0;
  34. let finalLabels = [];
  35. let finalDatas = [];
  36. for( const [index, date] of dates.entries() ){
  37. if( labels.includes(date) ){
  38. /* if date was provided in labels get the data value */
  39. finalLabels.push(date);
  40. finalDatas.push(data[valueExistsCounter]);
  41. valueExistsCounter += 1
  42. } else {
  43. /* set date value to 0 */
  44. finalLabels.push(date);
  45. finalDatas.push(0);
  46. }
  47. };
  48. return [finalLabels, finalDatas]
  49. }());
  50. const finalLabels = datasets[0];
  51. const finalDatas = datasets[1];
  52. /* now we can build the chart */
  53. const ctx = document.getElementById('myChart').getContext('2d');
  54. const myChart = new Chart(ctx, {
  55. type: 'line',
  56. data: {
  57. labels: finalLabels,
  58. datasets: [{
  59. label: 'Sales',
  60. 'fill': true,
  61. borderColor: '#ccc',
  62. backgroundColor: 'rgba(204,204,204,0.5)',
  63. tension: 0.2,
  64. data: finalDatas
  65. }]
  66. },
  67. options: {
  68. scales: {
  69. x: {
  70. grid: {
  71. color: 'rgba(204,204,204,0.1)'
  72. }
  73. },
  74. y: {
  75. grid: {
  76. color: 'rgba(204,204,204,0.1)'
  77. }
  78. }
  79. }
  80. }
  81. });

HTML语言

  1. <!-- Canvas -->
  2. <div class="chartContainer">
  3. <canvas id="myChart"></canvas>
  4. </div>
  5. <!-- include Chart.js 3.9.1 -->
  6. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.js" integrity="sha512-d6nObkPJgV791iTGuBoVC9Aa2iecqzJRE0Jiqvk85BhLHAPhWqkuBiQb1xz2jvuHNqHLYoN3ymPfpiB1o+Zgpw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

CSS系统

  1. body {
  2. background-color: #0d1117;
  3. }
  4. .chartContainer {
  5. width: 800px;
  6. height: 400px;
  7. }

我想这就够了吧?如果这有帮助的话,请马克回答。

展开查看全部

相关问题