ChartJS 无法隐藏线图中标签旁边的(指示器)框

iyr7buue  于 2023-10-18  发布在  Chart.js
关注(0)|答案(3)|浏览(180)

如果图形API响应中没有值,则不应绘制图形,并且应隐藏所有图例/标签
(多个y轴)
1.当y轴上没有值时,我可以隐藏图形。
1.但是,无法隐藏标签旁边的指示器(框)。
我尝试使用一些条件来隐藏标签但不隐藏指示符
请参考上面附上的图片。
救命啊!
App.js

  1. import React from "react";
  2. import {
  3. Chart as ChartJS,
  4. CategoryScale,
  5. LinearScale,
  6. PointElement,
  7. LineElement,
  8. Title,
  9. Tooltip,
  10. Legend,
  11. } from "chart.js";
  12. import { Line } from "react-chartjs-2";
  13. ChartJS.register(
  14. CategoryScale,
  15. LinearScale,
  16. PointElement,
  17. LineElement,
  18. Title,
  19. Tooltip,
  20. Legend
  21. );
  22. export const options = {
  23. responsive: true,
  24. interaction: {
  25. mode: "index",
  26. intersect: false,
  27. },
  28. stacked: false,
  29. plugins: {
  30. // legend: {
  31. // display:false
  32. // },
  33. title: {
  34. display: true,
  35. text: "Chart.js Line Chart - Multi Axis",
  36. },
  37. },
  38. scales: {
  39. y: {
  40. // position: "center",
  41. type: "linear",
  42. display: true,
  43. position: "left",
  44. min: 0,
  45. max: 400,
  46. grid: {
  47. drawOnChartArea: false,
  48. },
  49. ticks: { count: 5 },
  50. offset: true,
  51. },
  52. x: {
  53. offset: true,
  54. grid: {
  55. drawOnChartArea: false,
  56. },
  57. },
  58. y1: {
  59. type: "linear",
  60. display: true,
  61. position: "left",
  62. grid: {
  63. drawOnChartArea: false,
  64. },
  65. ticks: {
  66. count: 5,
  67. },
  68. min: 0,
  69. max: 100,
  70. },
  71. },
  72. };
  73. const labels = [
  74. "11:40",
  75. "11:50",
  76. "00:00",
  77. "00:10",
  78. "00:20",
  79. "00:30",
  80. "00:40",
  81. "00:50",
  82. ];
  83. const LineChartData = {
  84. TransactionId: 749508,
  85. ChargingRate: "45.3014",
  86. SoC: [
  87. // {
  88. // ValueTimeStamp: "2023-07-27T09:46:50.000Z",
  89. // MeterValue: "97",
  90. // Unit: "Percent",
  91. // },
  92. // {
  93. // ValueTimeStamp: "2023-07-27T09:46:46.000Z",
  94. // MeterValue: "97",
  95. // Unit: "Percent",
  96. // },
  97. // {
  98. // ValueTimeStamp: "2023-07-27T09:45:46.000Z",
  99. // MeterValue: "96",
  100. // Unit: "Percent",
  101. // },
  102. // {
  103. // ValueTimeStamp: "2023-07-27T09:44:46.000Z",
  104. // MeterValue: "95",
  105. // Unit: "Percent",
  106. // },
  107. // {
  108. // ValueTimeStamp: "2023-07-27T09:43:46.000Z",
  109. // MeterValue: "93",
  110. // Unit: "Percent",
  111. // },
  112. // {
  113. // ValueTimeStamp: "2023-07-27T09:42:46.000Z",
  114. // MeterValue: "92",
  115. // Unit: "Percent",
  116. // },
  117. // {
  118. // ValueTimeStamp: "2023-07-27T09:41:46.000Z",
  119. // MeterValue: "90",
  120. // Unit: "Percent",
  121. // },
  122. // {
  123. // ValueTimeStamp: "2023-07-27T09:40:47.000Z",
  124. // MeterValue: "88",
  125. // Unit: "Percent",
  126. // },
  127. ],
  128. Power: [
  129. {
  130. ValueTimeStamp: "2023-07-27T09:46:50.000Z",
  131. MeterValue: "0.0",
  132. Unit: "W",
  133. },
  134. {
  135. ValueTimeStamp: "2023-07-27T09:46:46.000Z",
  136. MeterValue: "31593.0",
  137. Unit: "W",
  138. },
  139. {
  140. ValueTimeStamp: "2023-07-27T09:45:46.000Z",
  141. MeterValue: "37282.0",
  142. Unit: "W",
  143. },
  144. {
  145. ValueTimeStamp: "2023-07-27T09:44:46.000Z",
  146. MeterValue: "44406.0",
  147. Unit: "W",
  148. },
  149. {
  150. ValueTimeStamp: "2023-07-27T09:43:46.000Z",
  151. MeterValue: "50854.0",
  152. Unit: "W",
  153. },
  154. {
  155. ValueTimeStamp: "2023-07-27T09:42:46.000Z",
  156. MeterValue: "58069.0",
  157. Unit: "W",
  158. },
  159. {
  160. ValueTimeStamp: "2023-07-27T09:41:46.000Z",
  161. MeterValue: "63113.0",
  162. Unit: "W",
  163. },
  164. {
  165. ValueTimeStamp: "2023-07-27T09:40:47.000Z",
  166. MeterValue: "59418.0",
  167. Unit: "W",
  168. },
  169. ],
  170. };
  171. export const data = {
  172. labels,
  173. datasets: [
  174. {
  175. data: LineChartData.SoC.map((meterValue) => {
  176. return meterValue.MeterValue;
  177. }),
  178. label: LineChartData.SoC.length === 0 ? "" : "Consumption",
  179. borderColor: "rgb(255, 99, 132)",
  180. backgroundColor: "rgba(255, 99, 132, 0.5)",
  181. yAxisID: "y",
  182. },
  183. {
  184. label: "Power",
  185. data: LineChartData.Power.map((meterValue) => {
  186. return meterValue.MeterValue;
  187. }),
  188. borderColor: "rgb(53, 162, 235)",
  189. backgroundColor: "rgba(53, 162, 235, 0.5)",
  190. yAxisID: "y1",
  191. },
  192. ],
  193. };
  194. console.log(data);
  195. export default function App() {
  196. return (
  197. <div>
  198. {LineChartData.Power.length === 0 ? (
  199. ""
  200. ) : (
  201. <Line options={options} data={data} />
  202. )}
  203. </div>
  204. );
  205. }

注:Soc数组下的注解行表示Soc没有数据。因此,预期结果应为:1.将不会绘制该SoC的图形2.如果没有数据,将不会显示SoC的标签。3.标签旁边的指示器(框)也不会显示。

rjee0c15

rjee0c151#

您需要检查从API获得的响应是否为空。如果为空,则不创建图形。简单.
int count = {count};//您的API响应
let isEmpty = Object.values(data.datasets).every(dataset => dataset.data.length = 0);
现在检查这里是否为isEmpty值,然后如果返回true值则创建图

dba5bblo

dba5bblo2#

将图例属性添加为“无”可以删除指示器框。

  1. import React from 'react';
  2. import {
  3. Chart as ChartJS,
  4. CategoryScale,
  5. LinearScale,
  6. BarElement,
  7. Title,
  8. Tooltip,
  9. Legend,
  10. } from 'chart.js';
  11. import { Bar } from 'react-chartjs-2';
  12. ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend);
  13. export const options = {
  14. maintainAspectRatio: false,
  15. responsive: true,
  16. plugins: {
  17. legend: {
  18. position: 'none', // set this none to remove indicator box
  19. },
  20. title: {
  21. display: true,
  22. text: '',
  23. },
  24. },
  25. scales: {
  26. x: {
  27. grid: {
  28. display: false,
  29. },
  30. },
  31. },
  32. };
  33. const labels = ['January', 'Feb', 'Mar', 'April', 'May', 'June', 'July'];
  34. export const data = {
  35. labels,
  36. datasets: [
  37. {
  38. label: 'Revenue',
  39. barThickness: 24,
  40. borderRadius: 4,
  41. hoverBackgroundColor: '#62D974',
  42. backgroundColor: 'red',
  43. data: [200, 400, 200, 100, 120, 600, 500, 600],
  44. backgroundColor: '#62D974',
  45. },
  46. ],
  47. };
  48. const BarChart = () => {
  49. return <Bar options={options} data={data} />;
  50. };
  51. export default BarChart;
展开查看全部
yqkkidmi

yqkkidmi3#

你必须在代码中添加一个useEffect函数,并为dataSets维护一个单独的状态。将该状态变量用于数据集。如果没有数据,则更新状态并在数据集中使用它。

  1. const [stateVaraible, useStateVariable] = useState([])
  2. stateVaraible = [{}] // if there is only one object
  3. export const data = {
  4. labels,
  5. datasets: [stateVaraible]. // update this variable if not data is there
  6. };

相关问题