ChartJS TypeError:无法读取null的属性(阅读'length'),这是什麽意思?

rhfm7lfc  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(218)

有一个我的问题,我试图解决。Cannot read properties of null (reading 'transition'). What does it mean?。我检查了选项与集成mychart。更新()从这个问题https://github.com/chartjs/Chart.js/issues/5149。但当我试图开始出现其他错误。

  1. TypeError: Cannot read properties of null (reading 'length')
  2. at Object.acquireContext (Chart.js?473e:7756:1)
  3. at Chart.construct (Chart.js?473e:9324:1)
  4. at new Chart (Chart.js?473e:9311:1)
  5. at VueComponent.draw (Analytics-test.vue?b2a7:69:1)
  6. at VueComponent.loadTrainings (Analytics-test.vue?b2a7:444:1)
  7. at async VueComponent.mounted (Analytics-test.vue?b2a7:476:1)

在你可以看到最后的代码,我试图启动。

  1. <template>
  2. <div> Прибыль/посещаемость <div class="small">
  3. <canvas id="mychart" height="400"></canvas>
  4. </div>
  5. </div>
  6. </template>
  7. <script>
  8. import Chart from 'chart.js'
  9. export default {
  10. data: () => ({
  11. flagStartDate: false,
  12. chartData: null,
  13. labels: [],
  14. dataset: {},
  15. draftData: null,
  16. data: [],
  17. datacollection: [],
  18. clubsId: ['5c3c5e12ba86198828baa4a7', '5c3c5e20ba86198828baa4c5', '60353d6edbb58a135bf41856', '61e9995d4ec0f29dc8447f81', '61e999fc4ec0f29dc844835e'],
  19. // clubsId: ['5c3c5e12ba86198828baa4a7', '61e999fc4ec0f29dc844835e'],
  20. // clubsId: ['61e999fc4ec0f29dc844835e'],
  21. }),
  22. methods: {
  23. draw() {
  24. if (this.mychart) {
  25. this.mychart.destroy();
  26. }
  27. const ctx = document.getElementById('main-chart');
  28. this.mychart = new Chart(ctx,
  29. {
  30. type: 'line',
  31. data: {
  32. labels: this.labels,
  33. datasets: this.datacollection
  34. },
  35. options: {
  36. responsive: true,
  37. elements: {
  38. line: {
  39. tension: 0 // disables bezier curves
  40. }
  41. },
  42. scales: {
  43. xAxes: [{
  44. type: "time",
  45. display: true,
  46. scaleLabel: {
  47. display: true,
  48. labelString: 'Time'
  49. },
  50. ticks: {
  51. major: {
  52. fontStyle: "bold",
  53. fontColor: "#FF0000"
  54. }
  55. }
  56. }],
  57. yAxes: [{
  58. display: true,
  59. scaleLabel: {
  60. display: true,
  61. labelString: this.labelY
  62. },
  63. ticks: {
  64. min: 0, // it is for ignoring negative step.
  65. beginAtZero: true,
  66. stepSize: 1 // if i use this it always set it '1', which look very awkward if it have high value e.g. '100'.
  67. }
  68. }]
  69. }
  70. }
  71. });
  72. },
  73. participantsCountClub(clubId) {
  74. switch (clubId) {
  75. case '5c3c5e12ba86198828baa4a7':
  76. return { label: "Тренировок на Фрунзенской", borderColor: "#3e95cd", fill: false }
  77. case '5c3c5e20ba86198828baa4c5':
  78. return { label: "Тренировок на Чернышевской", borderColor: "#8e5ea2", fill: false };
  79. case '60353d6edbb58a135bf41856':
  80. return { label: "Тренировок на Василеостровской", borderColor: "#e8c3b9", fill: false };
  81. case '61e9995d4ec0f29dc8447f81':
  82. return { label: "Тренировок на Московской", borderColor: "#3cba9f", fill: false };
  83. case '61e999fc4ec0f29dc844835e':
  84. return { label: "Тренировок на Лесной", borderColor: "#c45850", fill: false };
  85. default:
  86. return 'Неизвестный клуб';
  87. }
  88. },
  89. async loadTrainings(clubsId) {
  90. try {
  91. for (let clubId in clubsId) {
  92. clubId = clubsId[clubId]
  93. let dateFrom = '2021-06-01T00:00:00'
  94. let dateTo = '2022-08-01T23:59:59'
  95. let groupBy = 'month'
  96. await this.$store.dispatch('loadAvgSchedule', { clubId, dateFrom, dateTo, groupBy })
  97. this.draftData = this.$store.state.avgShedule
  98. if (this.labels.length === 0) {
  99. this.getDatesAvgIncome()
  100. }
  101. this.flagStartDate = true
  102. await this.getParticipantsCount(clubId)
  103. this.flagStartDate = false
  104. }
  105. this.draw()
  106. } catch (e) {
  107. console.error(e)
  108. }
  109. },
  110. async getParticipantsCount(clubId) {
  111. for (let item in this.draftData) {
  112. let positionDate = this.labels.indexOf(this.draftData[item].date.slice(0, 7))
  113. if (this.flagStartDate && positionDate > 0) {
  114. let zerroArray = await this.bindDataDates(positionDate)
  115. this.data = this.data.concat(zerroArray)
  116. }
  117. this.data.push(this.draftData[item].participantsCount)
  118. this.flagStartDate = false
  119. }
  120. this.dataset.data = this.data
  121. Object.assign(this.dataset, this.participantsCountClub(clubId))
  122. this.datacollection.push(this.dataset)
  123. console.log('this.datacollection', this.datacollection)
  124. this.data = []
  125. this.dataset = {}
  126. },
  127. },
  128. async mounted() {
  129. await this.loadTrainings(this.clubsId)
  130. },
  131. }
  132. </script>
  133. <style>
  134. .small {
  135. max-width: 600px;
  136. margin: 150px auto;
  137. }
  138. </style>

我还尝试了这个选项(使用update()):

  1. <template>
  2. <div> Прибыль/посещаемость <div class="small">
  3. <canvas id="mychart" height="400"></canvas>
  4. </div>
  5. </div>
  6. </template>
  7. <script>
  8. import Chart from 'chart.js'
  9. export default {
  10. data: () => ({
  11. flagStartDate: false,
  12. chartData: null,
  13. labels: [],
  14. dataset: {},
  15. draftData: null,
  16. data: [],
  17. datacollection: [],
  18. clubsId: ['5c3c5e12ba86198828baa4a7', '5c3c5e20ba86198828baa4c5', '60353d6edbb58a135bf41856', '61e9995d4ec0f29dc8447f81', '61e999fc4ec0f29dc844835e'],
  19. // clubsId: ['5c3c5e12ba86198828baa4a7', '61e999fc4ec0f29dc844835e'],
  20. // clubsId: ['61e999fc4ec0f29dc844835e'],
  21. }),
  22. methods: {
  23. draw() {
  24. if (this.mychart) {
  25. this.mychart.destroy();
  26. }
  27. const ctx = document.getElementById('main-chart');
  28. this.mychart = new Chart(ctx,
  29. {
  30. type: 'line',
  31. data: {
  32. labels: this.labels,
  33. datasets: this.datacollection
  34. },
  35. options: {
  36. responsive: true,
  37. elements: {
  38. line: {
  39. tension: 0 // disables bezier curves
  40. }
  41. },
  42. scales: {
  43. xAxes: [{
  44. type: "time",
  45. display: true,
  46. scaleLabel: {
  47. display: true,
  48. labelString: 'Time'
  49. },
  50. ticks: {
  51. major: {
  52. fontStyle: "bold",
  53. fontColor: "#FF0000"
  54. }
  55. }
  56. }],
  57. yAxes: [{
  58. display: true,
  59. scaleLabel: {
  60. display: true,
  61. labelString: this.labelY
  62. },
  63. ticks: {
  64. min: 0, // it is for ignoring negative step.
  65. beginAtZero: true,
  66. stepSize: 1 // if i use this it always set it '1', which look very awkward if it have high value e.g. '100'.
  67. }
  68. }]
  69. }
  70. }
  71. });
  72. },
  73. participantsCountClub(clubId) {
  74. switch (clubId) {
  75. case '5c3c5e12ba86198828baa4a7':
  76. return { label: "Тренировок на Фрунзенской", borderColor: "#3e95cd", fill: false }
  77. case '5c3c5e20ba86198828baa4c5':
  78. return { label: "Тренировок на Чернышевской", borderColor: "#8e5ea2", fill: false };
  79. case '60353d6edbb58a135bf41856':
  80. return { label: "Тренировок на Василеостровской", borderColor: "#e8c3b9", fill: false };
  81. case '61e9995d4ec0f29dc8447f81':
  82. return { label: "Тренировок на Московской", borderColor: "#3cba9f", fill: false };
  83. case '61e999fc4ec0f29dc844835e':
  84. return { label: "Тренировок на Лесной", borderColor: "#c45850", fill: false };
  85. default:
  86. return 'Неизвестный клуб';
  87. }
  88. },
  89. async loadTrainings(clubsId) {
  90. try {
  91. for (let clubId in clubsId) {
  92. clubId = clubsId[clubId]
  93. let dateFrom = '2021-06-01T00:00:00'
  94. let dateTo = '2022-08-01T23:59:59'
  95. let groupBy = 'month'
  96. await this.$store.dispatch('loadAvgSchedule', { clubId, dateFrom, dateTo, groupBy })
  97. this.draftData = this.$store.state.avgShedule
  98. if (this.labels.length === 0) {
  99. this.getDatesAvgIncome()
  100. }
  101. this.flagStartDate = true
  102. await this.getParticipantsCount(clubId)
  103. this.flagStartDate = false
  104. }
  105. // Below I tried refresh charts after updates data
  106. this.mychart.update()
  107. } catch (e) {
  108. console.error(e)
  109. }
  110. },
  111. async getParticipantsCount(clubId) {
  112. for (let item in this.draftData) {
  113. let positionDate = this.labels.indexOf(this.draftData[item].date.slice(0, 7))
  114. if (this.flagStartDate && positionDate > 0) {
  115. let zerroArray = await this.bindDataDates(positionDate)
  116. this.data = this.data.concat(zerroArray)
  117. }
  118. this.data.push(this.draftData[item].participantsCount)
  119. this.flagStartDate = false
  120. }
  121. this.dataset.data = this.data
  122. Object.assign(this.dataset, this.participantsCountClub(clubId))
  123. this.datacollection.push(this.dataset)
  124. console.log('this.datacollection', this.datacollection)
  125. this.data = []
  126. this.dataset = {}
  127. },
  128. },
  129. async mounted() {
  130. await this.loadTrainings(this.clubsId)
  131. this.draw()
  132. },
  133. }
  134. </script>
  135. <style>
  136. .small {
  137. max-width: 600px;
  138. margin: 150px auto;
  139. }
  140. </style>

当我在调试工具中开启字串时:

您可以看到引用这一行:

如果检查最后一个参考:

您可以看到这一行:

ybzsozfc

ybzsozfc1#

属性length用于Array,错误说明无法读取legth的null(重要的是单词“null”),因此问题是该属性的const或var由于任何原因变为null。

3ks5zfa0

3ks5zfa02#

问题出在模板上。我忘记为画布写正确的id了。一定是这样的:

  1. <canvas id="main-chart" height="400"></canvas>

相关问题