4 removals
18 additions
1.(function(Chart) {1.(function(Chart) {
2. var cloneArray = function(srcAry) {2. var cloneArray = function(srcAry) {
3. var dstAry = [];3. var dstAry = [];
4. var length = srcAry.length;4. var length = srcAry.length;
5.5.
6. for (var i = 0; i < length; i++) {6. for (var i = 0; i < length; i++) {
7. dstAry.push(srcAry[i]);7. dstAry.push(srcAry[i]);
8. }8. }
9. return dstAry;9. return dstAry;
10. };10. };
11.11.
12. var setOriginalData = function(data) {12. var setOriginalData = function(data) {
13. data.originalData = data.datasets.map(function(dataset) {13. data.originalData = data.datasets.map(function(dataset) {
14. return cloneArray(dataset.data);14. return cloneArray(dataset.data);
15. });15. });
16. };16. };
17.17.
18. // set calculated rate (xx%) to data.calculatedData18. // set calculated rate (xx%) to data.calculatedData
19. var calculateRate = function(data) {19. var calculateRate = function(data) {
20. var visibles = data.datasets.map(function(dataset) {20. var visibles = data.datasets.map(function(dataset) {
21. if (!dataset._meta) return true;21. if (!dataset._meta) return true;
22.22.
23. for (var i in dataset._meta) {23. for (var i in dataset._meta) {
24. return !dataset._meta[i].hidden;24. return !dataset._meta[i].hidden;
25. }25. }
26. });26. });
27.27.
28. var totals = Array.apply(null, new Array(data.datasets[0].data.length)).map(function(el, i) {28. var totals = Array.apply(null, new Array(data.datasets[0].data.length)).map(function(el, i) {
29. return data.datasets.reduce(function(sum, dataset, j) {29. return data.datasets.reduce(function(sum, dataset, j) {
30. var key = dataset.stack;30. var key = dataset.stack;
31. if (!sum[key]) sum[key] = 0;31. if (!sum[key]) sum[key] = 0;
32. sum[key] += dataset.data[i] * visibles[j];32. sum[key] += (dataset.type === "line" ? 0 : dataset.data[i]) * visibles[j];
33.33.
34. return sum;34. return sum;
35. }, {});35. }, {});
36. });36. });
37.37.
38. data.calculatedData = data.datasets.map(function(dataset, i) {38. data.calculatedData = data.datasets.map(function(dataset, i) {
39. return dataset.data.map(function(val, i) {39. return dataset.data.map(function(val, i) {
40. var total = totals[i][dataset.stack];40. if(dataset.type === "line") {
41. return val && total ? Math.round(val * 1000 / total) / 10 : 0;41. return val;
42. } else {
43. var total = totals[i][dataset.stack];
44. return val && total ? Math.round(val * 1000 / total) / 10 : 0;
45. }
42. });46. });
43. });47. });
44. };48. };
45.49.
46. var tooltipLabel = function(tooltipItem, data) {50. var tooltipLabel = function(tooltipItem, data) {
47. var datasetIndex = tooltipItem.datasetIndex;51. var datasetIndex = tooltipItem.datasetIndex;
48. var index = tooltipItem.index;52. var index = tooltipItem.index;
49. var datasetLabel = data.datasets[datasetIndex].label || "";53. var datasetLabel = data.datasets[datasetIndex].label || "";
50. var originalValue = data.originalData[datasetIndex][index];54. var originalValue = data.originalData[datasetIndex][index];
51. var rateValue = data.calculatedData[datasetIndex][index];55. var rateValue = data.calculatedData[datasetIndex][index];
52.56.
53. return "" + datasetLabel + ": " + rateValue + "% (" + originalValue + ")";57. return "" + datasetLabel + ": " + rateValue + "% (" + originalValue + ")";
54. };58. };
55.59.
56. var reflectData = function(srcData, datasets) {60. var reflectData = function(srcData, datasets) {
57. if (!srcData) return;61. if (!srcData) return;
58.62.
59. srcData.forEach(function(data, i) {63. srcData.forEach(function(data, i) {
60. datasets[i].data = data;64. datasets[i].data = data;
61. });65. });
62. };66. };
63.67.
64. var Stacked100Plugin = {68. var Stacked100Plugin = {
65. id: "stacked100",69. id: "stacked100",
66.70.
67. beforeInit: function(chartInstance, pluginOptions) {71. beforeInit: function(chartInstance, pluginOptions) {
68. if (!pluginOptions.enable) return;72. if (!pluginOptions.enable) return;
69.73.
70. var xAxes = chartInstance.options.scales.xAxes;74. var xAxes = chartInstance.options.scales.xAxes;
71. var yAxes = chartInstance.options.scales.yAxes;75. var yAxes = chartInstance.options.scales.yAxes;
72. var isVertical = chartInstance.config.type === "bar" || chartInstance.config.type === "line";76. var isVertical = chartInstance.config.type === "bar" || chartInstance.config.type === "line";
73.77.
74. [xAxes, yAxes].forEach(function(axes) {78. [xAxes, yAxes].forEach(function(axes) {
75. axes.forEach(function(hash) {79. axes.forEach(function(hash) {
76. hash.stacked = true;80. if(hash.stacked === false) return;
81. hash.stacked = true;
77. });82. });
78. });83. });
79. (isVertical ? yAxes : xAxes).forEach(function(hash) {84. (isVertical ? yAxes : xAxes).forEach(function(hash) {
85. if(hash.stacked === false) return;
80. hash.ticks.min = 0;86. hash.ticks.min = 0;
81. hash.ticks.max = 100;87. hash.ticks.max = 100;
82. });88. });
83.89.
84. chartInstance.options.tooltips.callbacks.label = tooltipLabel;90. chartInstance.options.tooltips.callbacks.label = tooltipLabel;
85. },91. },
86.92.
87. beforeDatasetsUpdate: function(chartInstance, pluginOptions) {93. beforeDatasetsUpdate: function(chartInstance, pluginOptions) {
88. if (!pluginOptions.enable) return;94. if (!pluginOptions.enable) return;
89.95.
90. setOriginalData(chartInstance.data);96. setOriginalData(chartInstance.data);
91. calculateRate(chartInstance.data);97. calculateRate(chartInstance.data);
92. reflectData(chartInstance.data.calculatedData, chartInstance.data.datasets);98. reflectData(chartInstance.data.calculatedData, chartInstance.data.datasets);
93. },99. },
94.100.
95. afterDatasetsUpdate: function(chartInstance, pluginOptions) {101. afterDatasetsUpdate: function(chartInstance, pluginOptions) {
96. if (!pluginOptions.enable) return;102. if (!pluginOptions.enable) return;
97.103.
98. reflectData(chartInstance.data.originalData, chartInstance.data.datasets);104. reflectData(chartInstance.data.originalData, chartInstance.data.datasets);
99. }105. }
100. };106. };
101.107.
102. Chart.pluginService.register(Stacked100Plugin);108. Chart.pluginService.register(Stacked100Plugin);
103.}.call(this, Chart));109.}.call(this, Chart));
original text
changed text