|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en-US"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=Edge"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 7 | + <link rel="stylesheet" type="text/css" href="../style.css"> |
| 8 | + <link rel="icon" type="image/ico" href="../favicon.ico"> |
| 9 | + <script src="https://cdn.jsdelivr.net/npm/chart.js@2.7.0/dist/Chart.min.js"></script> |
| 10 | + <script src="../../dist/chartjs-plugin-datalabels.js"></script> |
| 11 | + <script src="../utils.js"></script> |
| 12 | +</head> |
| 13 | +<body> |
| 14 | + <div id="side"> |
| 15 | + <div id="header"></div> |
| 16 | + <div id="actions"> |
| 17 | + <button onclick="randomize(this)">Randomize</button> |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + |
| 21 | + <div id="charts"> |
| 22 | + <div class="wrapper"><canvas id="chart-0"></canvas></div> |
| 23 | + </div> |
| 24 | + |
| 25 | + <script> |
| 26 | + var SAMPLE_INFO = { |
| 27 | + group: 'Advanced', |
| 28 | + name: 'Multiple Labels', |
| 29 | + desc: 'This example displays 3 labels per data, one for the ' |
| 30 | + + 'index, one for the label and one for the value. Move ' |
| 31 | + + 'the mouse over the chart to display label ids.' |
| 32 | + }; |
| 33 | + </script> |
| 34 | + |
| 35 | + <script id="script-init"> |
| 36 | + var DATA_COUNT = 4; |
| 37 | + var labels = [ |
| 38 | + 'Earth', |
| 39 | + 'Mars', |
| 40 | + 'Saturn', |
| 41 | + 'Jupiter' |
| 42 | + ]; |
| 43 | + |
| 44 | + Samples.srand(4); |
| 45 | + |
| 46 | + Chart.helpers.merge(Chart.defaults.global, { |
| 47 | + aspectRatio: 4 / 3, |
| 48 | + tooltips: false, |
| 49 | + layout: { |
| 50 | + padding: { |
| 51 | + top: 42, |
| 52 | + right: 16, |
| 53 | + bottom: 32, |
| 54 | + left: 8 |
| 55 | + } |
| 56 | + }, |
| 57 | + elements: { |
| 58 | + line: { |
| 59 | + fill: false |
| 60 | + }, |
| 61 | + point: { |
| 62 | + hoverRadius: 7, |
| 63 | + radius: 5 |
| 64 | + } |
| 65 | + }, |
| 66 | + plugins: { |
| 67 | + legend: false, |
| 68 | + title: false |
| 69 | + } |
| 70 | + }); |
| 71 | + </script> |
| 72 | + |
| 73 | + <script id="script-construct"> |
| 74 | + var chart = new Chart('chart-0', { |
| 75 | + type: 'doughnut', |
| 76 | + data: { |
| 77 | + labels: labels, |
| 78 | + datasets: [{ |
| 79 | + backgroundColor: Samples.colors({ |
| 80 | + color: Samples.color(1), |
| 81 | + mode: 'darken' |
| 82 | + }), |
| 83 | + hoverBorderColor: 'white', |
| 84 | + data: Samples.numbers({ |
| 85 | + count: DATA_COUNT, |
| 86 | + min: 0, |
| 87 | + max: 100 |
| 88 | + }), |
| 89 | + datalabels: { |
| 90 | + labels: { |
| 91 | + index: { |
| 92 | + align: 'end', |
| 93 | + anchor: 'end', |
| 94 | + color: function(ctx) { |
| 95 | + return ctx.dataset.backgroundColor; |
| 96 | + }, |
| 97 | + font: {size: 18}, |
| 98 | + formatter: function(value, ctx) { |
| 99 | + return ctx.active |
| 100 | + ? 'index' |
| 101 | + : '#' + (ctx.dataIndex + 1); |
| 102 | + }, |
| 103 | + offset: 8, |
| 104 | + opacity: function(ctx) { |
| 105 | + return ctx.active ? 1 : 0.5; |
| 106 | + } |
| 107 | + }, |
| 108 | + name: { |
| 109 | + align: 'top', |
| 110 | + font: {size: 16}, |
| 111 | + formatter: function(value, ctx) { |
| 112 | + return ctx.active |
| 113 | + ? 'name' |
| 114 | + : ctx.chart.data.labels[ctx.dataIndex]; |
| 115 | + } |
| 116 | + }, |
| 117 | + value: { |
| 118 | + align: 'bottom', |
| 119 | + backgroundColor: function(ctx) { |
| 120 | + var value = ctx.dataset.data[ctx.dataIndex]; |
| 121 | + return value > 50 ? 'white' : null; |
| 122 | + }, |
| 123 | + borderColor: 'white', |
| 124 | + borderWidth: 2, |
| 125 | + borderRadius: 4, |
| 126 | + color: function(ctx) { |
| 127 | + var value = ctx.dataset.data[ctx.dataIndex]; |
| 128 | + return value > 50 |
| 129 | + ? ctx.dataset.backgroundColor |
| 130 | + : 'white'; |
| 131 | + }, |
| 132 | + formatter: function(value, ctx) { |
| 133 | + return ctx.active |
| 134 | + ? 'value' |
| 135 | + : Math.round(value * 1000) / 1000; |
| 136 | + }, |
| 137 | + padding: 4 |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }] |
| 142 | + }, |
| 143 | + options: { |
| 144 | + cutoutPercentage: 8, |
| 145 | + padding: 64, |
| 146 | + plugins: { |
| 147 | + datalabels: { |
| 148 | + color: 'white', |
| 149 | + display: function(ctx) { |
| 150 | + return ctx.dataset.data[ctx.dataIndex] > 10; |
| 151 | + }, |
| 152 | + font: {weight: 'bold'}, |
| 153 | + offset: 0, |
| 154 | + padding: 0 |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + }); |
| 159 | + </script> |
| 160 | + |
| 161 | + <script id="script-actions"> |
| 162 | + function randomize() { |
| 163 | + chart.data.datasets.forEach(function(dataset, i) { |
| 164 | + dataset.backgroundColor = Samples.colors({ |
| 165 | + color: Samples.color(), |
| 166 | + mode: 'darken' |
| 167 | + }); |
| 168 | + dataset.data = dataset.data.map(function(value) { |
| 169 | + return Samples.rand(0, 100); |
| 170 | + }); |
| 171 | + }); |
| 172 | + chart.update(); |
| 173 | + } |
| 174 | + </script> |
| 175 | +</body> |
| 176 | +</html> |
0 commit comments