widget2chart.all.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview Replaces widgets with charts using Chart.js.
  7. *
  8. * This file should be included on websites as CKEditor returns just a div element with data attributes that needs to be replaced with a proper chart.
  9. * The "Preview" plugin is using this file automatically.
  10. */
  11. /* global chartjs_colors:false, chartjs_colors_json:false, chartjs_config:false, chartjs_config_json:false, console:false, Chart:false */
  12. // For IE8 and below the code will not be executed.
  13. if ( typeof document.addEventListener !== 'undefined' )
  14. document.addEventListener( 'DOMContentLoaded', function() {
  15. // Make sure Chart.js is enabled on a page.
  16. if ( typeof Chart === 'undefined' ) {
  17. if ( typeof console !== 'undefined' ) {
  18. console.log( 'ERROR: You must include chart.min.js on this page in order to use Chart.js' );
  19. }
  20. return;
  21. }
  22. // Loop over all found elements.
  23. [].forEach.call( document.querySelectorAll( 'div.chartjs' ), function( el ) {
  24. var colors, config;
  25. // Color sets defined on a website.
  26. if ( typeof chartjs_colors !== 'undefined' ) {
  27. colors = chartjs_colors;
  28. }
  29. // Color sets provided by contentPreview event handler.
  30. else if ( typeof chartjs_colors_json !== 'undefined' ) {
  31. colors = JSON.parse( chartjs_colors_json );
  32. }
  33. // Default hardcoded values used if file is included on a website that did not set "chartjs_colors" variable.
  34. else {
  35. colors = {
  36. // Colors for Bar/Line chart: http://www.chartjs.org/docs/#bar-chart-data-structure
  37. fillColor: 'rgba(151,187,205,0.5)',
  38. strokeColor: 'rgba(151,187,205,0.8)',
  39. highlightFill: 'rgba(151,187,205,0.75)',
  40. highlightStroke: 'rgba(151,187,205,1)',
  41. // Colors for Doughnut/Pie/PolarArea charts: http://www.chartjs.org/docs/#doughnut-pie-chart-data-structure
  42. data: [ '#B33131', '#B66F2D', '#B6B330', '#71B232', '#33B22D', '#31B272', '#2DB5B5', '#3172B6', '#3232B6', '#6E31B2', '#B434AF', '#B53071' ]
  43. };
  44. }
  45. // Chart.js config defined on a website.
  46. if ( typeof chartjs_config !== 'undefined' ) {
  47. config = chartjs_config;
  48. }
  49. // Chart.js config provided by contentPreview event handler.
  50. else if ( typeof chartjs_config_json !== 'undefined' ) {
  51. config = JSON.parse( chartjs_config_json );
  52. }
  53. else {
  54. config = {
  55. Bar: { animation: false },
  56. Doughnut: { animateRotate: false },
  57. Line: { animation: false },
  58. Pie: { animateRotate: false },
  59. PolarArea: { animateRotate: false }
  60. };
  61. }
  62. // Get chart information from data attributes.
  63. var chartType = el.getAttribute( 'data-chart' ),
  64. values = JSON.parse( el.getAttribute( 'data-chart-value' ) );
  65. // Malformed element, exit.
  66. if ( !values || !values.length || !chartType )
  67. return;
  68. // <div> may contain some text like "chart" or &nbsp which is there just to prevent <div>s from being deleted.
  69. el.innerHTML = '';
  70. // Prepare some DOM elements for Chart.js.
  71. var canvas = document.createElement( 'canvas' );
  72. canvas.height = el.getAttribute( 'data-chart-height' );
  73. el.appendChild( canvas );
  74. var legend = document.createElement( 'div' );
  75. legend.setAttribute( 'class', 'chartjs-legend' );
  76. el.appendChild( legend );
  77. // The code below is the same as in plugin.js.
  78. // ########## RENDER CHART START ##########
  79. // Prepare canvas and chart instance.
  80. var i, ctx = canvas.getContext( '2d' ),
  81. chart = new Chart( ctx );
  82. // Set some extra required colors by Pie/Doughnut charts.
  83. // Ugly charts will be drawn if colors are not provided for each data.
  84. // http://www.chartjs.org/docs/#doughnut-pie-chart-data-structure
  85. if ( chartType != 'bar' ) {
  86. for ( i = 0; i < values.length; i++ ) {
  87. values[i].color = colors.data[i];
  88. values[i].highlight = colors.data[i];
  89. }
  90. }
  91. // Prepare data for bar/line charts.
  92. if ( chartType == 'bar' || chartType == 'line' ) {
  93. var data = {
  94. // Chart.js supports multiple datasets.
  95. // http://www.chartjs.org/docs/#bar-chart-data-structure
  96. // This plugin is simple, so it supports just one.
  97. // Need more features? Create a Pull Request :-)
  98. datasets: [
  99. {
  100. label: '',
  101. fillColor: colors.fillColor,
  102. strokeColor: colors.strokeColor,
  103. highlightFill: colors.highlightFill,
  104. highlightStroke: colors.highlightStroke,
  105. data: []
  106. } ],
  107. labels: []
  108. };
  109. // Bar charts accept different data format than Pie/Doughnut.
  110. // We need to pass values inside datasets[0].data.
  111. for ( i = 0; i < values.length; i++ ) {
  112. if ( values[i].value ) {
  113. data.labels.push( values[i].label );
  114. data.datasets[0].data.push( values[i].value );
  115. }
  116. }
  117. // Legend makes sense only with more than one dataset.
  118. legend.innerHTML = '';
  119. }
  120. // Render Bar chart.
  121. if ( chartType == 'bar' ) {
  122. chart.Bar( data, config.Bar );
  123. }
  124. // Render Line chart.
  125. else if ( chartType == 'line' ) {
  126. chart.Line( data, config.Line );
  127. }
  128. // Render Line chart.
  129. else if ( chartType == 'polar' ) {
  130. //chart.PolarArea( values );
  131. legend.innerHTML = chart.PolarArea( values, config.PolarArea ).generateLegend();
  132. }
  133. // Render Pie chart and legend.
  134. else if ( chartType == 'pie' ) {
  135. legend.innerHTML = chart.Pie( values, config.Pie ).generateLegend();
  136. }
  137. // Render Doughnut chart and legend.
  138. else {
  139. legend.innerHTML = chart.Doughnut( values, config.Doughnut ).generateLegend();
  140. }
  141. // ########## RENDER CHART END ##########
  142. }
  143. );
  144. } );