showing results for - "doughnut chart js chart go away when no values"
Tessa
26 Jun 2019
1<html>
2   <head> 
3      <title>Hide a data in Doughnut/Pie Datasets</title> 
4      <meta name="viewport" content="width=device-width, initial-scale=1"> 
5      <script type="text/javascript">
6    window.onload=function(){/*from   w w w  .  j  a va2 s .c om*/
7/** Watch carefully,
8If you set one 'undefined' values on any of the dataset, it will result in the labels being crossed (or strikethrough). This means that if you only use one dataset, and you don't want the labels being shown in the chart and getting the labels crossed you want to set the values to 'undefined'.
9Null values will get you same result *without* the labels being crossed.
10So you want to use 'undefined' values if you would like to cross the label and hide the data, and you want to use 'null' values if you would like to just hide the data without getting the labels crossed.
11Try to play around with it and use it as you like.
12*/
13var ctx = document.getElementById("myDoughnut").getContext("2d");
14var myChart = new Chart(ctx, {
15  type: 'doughnut',
16  data: {
17    labels: ["Red", "Green", "Blue"],
18    datasets: [{
19      label: '# of Votes',
20      data: [0, 3, undefined],
21      // Play around with null and undefined values and see the difference on how the Chart reacts.
22      backgroundColor: [
23        '#f87979',
24        '#79f879',
25        '#7979f8'
26      ],
27      borderWidth: 5
28    }, {
29      label: '# of Votes',
30      data: [null, 19, undefined],
31      // Play around with null and undefined values and see the difference on how the Chart reacts.
32      backgroundColor: [
33        '#f87979',
34        '#79f879',
35        '#7979f8'
36      ]
37    }]
38  },
39  options: {
40    tooltips: {
41      mode: null
42    },
43    plugins: {
44      datalabels: {
45        borderWidth: 5,
46        borderColor: "white",
47        borderRadius: 8,
48        // color: 0,
49        font: {
50          weight: "bold"
51        },
52        backgroundColor: "lightgray"
53      }
54    }
55  }
56});
57    }
58
59      </script> 
60   </head> 
61   <body> 
62      <div id="app"> 
63         <div width="400"> 
64            <canvas id="myDoughnut"></canvas> 
65         </div> 
66      </div> 
67      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script> 
68      <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.4.0/dist/chartjs-plugin-datalabels.js"></script> 
69      <!-- Made in 21 Nov 2018 --> 
70      <!-- with ChartJS 2.7.3 -->  
71   </body>
72</html>
73
Claudio
20 May 2020
1<html>
2   <head> 
3      <title>Hide a data in Doughnut/Pie Datasets</title> 
4      <meta name="viewport" content="width=device-width, initial-scale=1"> 
5      <script type="text/javascript">
6    window.onload=function(){/*from   w w w  .  j  a va2 s .c om*/
7/** Watch carefully,
8If you set one 'undefined' values on any of the dataset, it will result in the labels being crossed (or strikethrough). This means that if you only use one dataset, and you don't want the labels being shown in the chart and getting the labels crossed you want to set the values to 'undefined'.
9Null values will get you same result *without* the labels being crossed.
10So you want to use 'undefined' values if you would like to cross the label and hide the data, and you want to use 'null' values if you would like to just hide the data without getting the labels crossed.
11Try to play around with it and use it as you like.
12*/
13var ctx = document.getElementById("myDoughnut").getContext("2d");
14var myChart = new Chart(ctx, {
15  type: 'doughnut',
16  data: {
17    labels: ["Red", "Green", "Blue"],
18    datasets: [{
19      label: '# of Votes',
20      data: [12, 3, undefined],
21      // Play around with null and undefined values and see the difference on how the Chart reacts.
22      backgroundColor: [
23        '#f87979',
24        '#79f879',
25        '#7979f8'
26      ],
27      borderWidth: 5
28    }, {
29      label: '# of Votes',
30      data: [0, 19, undefined],
31      // Play around with null and undefined values and see the difference on how the Chart reacts.
32      backgroundColor: [
33        '#f87979',
34        '#79f879',
35        '#7979f8'
36      ]
37    }]
38  },
39  options: {
40    tooltips: {
41      mode: null
42    },
43    plugins: {
44      datalabels: {
45        borderWidth: 5,
46        borderColor: "white",
47        borderRadius: 8,
48        // color: 0,
49        font: {
50          weight: "bold"
51        },
52        backgroundColor: "lightgray"
53      }
54    }
55  }
56});
57    }
58
59      </script> 
60   </head> 
61   <body> 
62      <div id="app"> 
63         <div width="400"> 
64            <canvas id="myDoughnut"></canvas> 
65         </div> 
66      </div> 
67      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script> 
68      <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.4.0/dist/chartjs-plugin-datalabels.js"></script> 
69      <!-- Made in 21 Nov 2018 --> 
70      <!-- with ChartJS 2.7.3 -->  
71   </body>
72</html>
73