maximus87

Untitled

Sep 24th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. <div id="chartie-container" style="display: flex; justify-content: space-between; flex-wrap: wrap; width: 100%;">
  2. <div style="text-align: center;">
  3. <div id="chart1"></div>
  4. <div style="font-size: 18px; color: #857a59; font-family: 'Assistant', sans-serif; font-weight: bold">Accessing Charitable Food</div>
  5. </div>
  6. <div style="text-align: center;">
  7. <div id="chart2"></div>
  8. <div style="font-size: 18px; color: #857a59; font-family: 'Assistant', sans-serif; font-weight: bold">Not Accessing Charitable Food</div>
  9. </div>
  10. </div>
  11.  
  12. <script>
  13. let chart1, chart2;
  14.  
  15. chart1 = Highcharts.chart('chart1', {
  16. chart: {
  17. type: 'pie',
  18. height: 500,
  19. backgroundColor: null,
  20. events: {
  21. load: function() {
  22. chart2 = Highcharts.charts.find(c => c !== chart1); // Find the second chart
  23. }
  24. }
  25. },
  26. title: {
  27. text: '',
  28. },
  29. tooltip: {
  30. enabled: false // Disable tooltip
  31. },
  32. plotOptions: {
  33. pie: {
  34. allowPointSelect: true,
  35. cursor: 'pointer',
  36. borderWidth: 0,
  37. dataLabels: {
  38. enabled: true,
  39. distance: -50, // Adjust for better centering
  40. formatter: function() {
  41. return '<div class="pie2-text">' +
  42. Math.round(this.percentage) +
  43. '%<span style="font-size: 14px">' +
  44. this.point.name +
  45. '</span></div>';
  46. },
  47. style: {
  48. fontSize: '30px',
  49. color: '#FFFFFF',
  50. textOutline: 'none',
  51. fontWeight: 'normal',
  52. },
  53. useHTML: true,
  54. verticalAlign: 'middle',
  55. align: 'center'
  56. },
  57. point: {
  58. events: {
  59. mouseOver: function() {
  60. const otherChart = this.series.chart === chart1 ? chart2 : chart1;
  61. resetAllHighlights(chart1);
  62. resetAllHighlights(otherChart);
  63. highlightMatching(this.name, chart1);
  64. highlightMatching(this.name, otherChart);
  65. setOpacityForNonMatching(this.name, chart1);
  66. setOpacityForNonMatching(this.name, otherChart);
  67. },
  68. mouseOut: function() {
  69. resetAllHighlights(chart1);
  70. resetAllHighlights(chart2);
  71. resetOpacity(chart1);
  72. resetOpacity(chart2);
  73. }
  74. }
  75. }
  76. }
  77. },
  78. series: [{
  79. name: 'Accessing Charitable Food',
  80. data: [
  81. { name: 'White', y: 9, color: '#FBAE50' },
  82. { name: 'Black', y: 48, color: '#894000' },
  83. { name: 'Hispanic', y: 29, color: '#DC7A00' },
  84. { name: 'Other', y: 13, color: '#985500' }
  85. ],
  86. center: ['50%', '50%'],
  87. size: '60%'
  88. }],
  89. responsive: {
  90. rules: [{
  91. condition: {
  92. maxWidth: 600
  93. },
  94. chartOptions: {
  95. series: [{
  96. center: ['50%', '50%'],
  97. size: '100%'
  98. }]
  99. }
  100. }]
  101. }
  102. });
  103.  
  104. chart2 = Highcharts.chart('chart2', {
  105. chart: {
  106. type: 'pie',
  107. height: 500,
  108. backgroundColor: null
  109. },
  110. title: {
  111. text: '',
  112. },
  113. tooltip: {
  114. enabled: false // Disable tooltip
  115. },
  116. plotOptions: {
  117. pie: {
  118. allowPointSelect: true,
  119. cursor: 'pointer',
  120. borderWidth: 0,
  121. dataLabels: {
  122. enabled: true,
  123. distance: -50, // Adjust for better centering
  124. formatter: function() {
  125. return '<div class="pie2-text">' +
  126. Math.round(this.percentage) +
  127. '%<span style="font-size: 14px">' +
  128. this.point.name +
  129. '</span></div>';
  130. },
  131. style: {
  132. fontSize: '30px',
  133. color: '#FFFFFF',
  134. textOutline: 'none',
  135. fontWeight: 'normal',
  136. },
  137. useHTML: true,
  138. verticalAlign: 'middle',
  139. align: 'center'
  140. },
  141. point: {
  142. events: {
  143. mouseOver: function() {
  144. const otherChart = this.series.chart === chart1 ? chart2 : chart1;
  145. resetAllHighlights(chart1);
  146. resetAllHighlights(otherChart);
  147. highlightMatching(this.name, chart1);
  148. highlightMatching(this.name, otherChart);
  149. setOpacityForNonMatching(this.name, chart1);
  150. setOpacityForNonMatching(this.name, otherChart);
  151. },
  152. mouseOut: function() {
  153. resetAllHighlights(chart1);
  154. resetAllHighlights(chart2);
  155. resetOpacity(chart1);
  156. resetOpacity(chart2);
  157. }
  158. }
  159. }
  160. }
  161. },
  162. series: [{
  163. name: 'Not Accessing Charitable Food',
  164. data: [
  165. { name: 'White', y: 26, color: '#FBAE50' },
  166. { name: 'Black', y: 37, color: '#894000' },
  167. { name: 'Hispanic', y: 20, color: '#DC7A00' },
  168. { name: 'Other', y: 17, color: '#985500' }
  169. ],
  170. center: ['50%', '50%'],
  171. size: '60%'
  172. }],
  173. responsive: {
  174. rules: [{
  175. condition: {
  176. maxWidth: 600
  177. },
  178. chartOptions: {
  179. series: [{
  180. center: ['50%', '50%'],
  181. size: '100%'
  182. }]
  183. }
  184. }]
  185. }
  186. });
  187.  
  188. // Highlight functions here
  189. function highlightMatching(name, chart) {
  190. if (!chart) return; // Check if the chart is defined
  191. chart.series.forEach(series => {
  192. series.data.forEach(point => {
  193. if (point.name === name) {
  194. point.setState('hover');
  195. }
  196. });
  197. });
  198. }
  199.  
  200. function resetAllHighlights(chart) {
  201. if (!chart) return; // Check if the chart is defined
  202. chart.series.forEach(series => {
  203. series.data.forEach(point => point.setState(''));
  204. });
  205. }
  206.  
  207. function setOpacityForNonMatching(name, chart) {
  208. if (!chart) return; // Check if the chart is defined
  209. chart.series.forEach(series => {
  210. series.data.forEach(point => {
  211. if (point.name !== name) {
  212. // Store the original color if not already stored
  213. if (!point.originalColor) {
  214. point.originalColor = point.color;
  215. }
  216. point.update({ color: Highcharts.color(point.originalColor).setOpacity(0.4).get() });
  217. }
  218. });
  219. });
  220. }
  221.  
  222. function resetOpacity(chart) {
  223. if (!chart) return; // Check if the chart is defined
  224. chart.series.forEach(series => {
  225. series.data.forEach(point => {
  226. if (point.originalColor) {
  227. point.update({ color: point.originalColor });
  228. }
  229. });
  230. });
  231. }
  232. </script>
  233.  
  234. <style>
  235. @media (min-width: 1024px) {
  236. #chartie-container > div {
  237. width: 50%;
  238. }
  239. }
  240. @media (max-width: 1023px) {
  241. #chartie-container > div {
  242. width: 100%;
  243. }
  244. }
  245. #chart1, #chart2 {
  246. min-height: 400px; /* Ensure both charts have a minimum height */
  247. }
  248. </style>
  249.  
Advertisement
Add Comment
Please, Sign In to add comment