|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | + <title>Hierarchical Bar Chart</title> |
| 6 | + <script src="https://unpkg.com/chart.js/dist/Chart.bundle.js"></script> |
| 7 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js"></script> |
| 8 | + <script src="../build/Chart.Hierarchical.js" type="text/javascript"></script> |
| 9 | + <style> |
| 10 | + canvas { |
| 11 | + -moz-user-select: none; |
| 12 | + -webkit-user-select: none; |
| 13 | + -ms-user-select: none; |
| 14 | + } |
| 15 | + |
| 16 | + </style> |
| 17 | +</head> |
| 18 | + |
| 19 | +<body> |
| 20 | + <div id="container" style="width: 75%;"> |
| 21 | + <canvas id="canvas"></canvas> |
| 22 | + </div> |
| 23 | + <script> |
| 24 | + const data = { |
| 25 | + // define label tree |
| 26 | + labels: [ |
| 27 | + 'A', |
| 28 | + { |
| 29 | + label: 'B1', |
| 30 | + expand: false, // 'focus', // expand level |
| 31 | + children: [ |
| 32 | + 'B1.1', |
| 33 | + { |
| 34 | + label: 'B1.2', |
| 35 | + children: ['B1.2.1', 'B1.2.2'] |
| 36 | + }, |
| 37 | + 'B1.3' |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + label: 'C1', |
| 42 | + children: ['C1.1', 'C1.2', 'C1.3', 'C1.4'] |
| 43 | + }, |
| 44 | + 'D' |
| 45 | + ], |
| 46 | + datasets: [{ |
| 47 | + label: 'Test', |
| 48 | + // store as the tree attribute for reference, the data attribute will be automatically managed |
| 49 | + tree: [ |
| 50 | + 1, |
| 51 | + { |
| 52 | + value: 2, |
| 53 | + children: [ |
| 54 | + 3, |
| 55 | + { |
| 56 | + value: 4, |
| 57 | + children: [4.1, 4.2] |
| 58 | + }, |
| 59 | + 5 |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + value: 6, |
| 64 | + children: [7, 8, 9, 10] |
| 65 | + }, |
| 66 | + 11 |
| 67 | + ] |
| 68 | + }] |
| 69 | + }; |
| 70 | + |
| 71 | + const scale = d3.scaleSequential(d3.interpolateBlues).domain([0, 11]); |
| 72 | + |
| 73 | + window.onload = () => { |
| 74 | + const ctx = document.getElementById("canvas").getContext("2d"); |
| 75 | + window.myBar = new Chart(ctx, { |
| 76 | + type: 'bar', |
| 77 | + data: data, |
| 78 | + options: { |
| 79 | + responsive: true, |
| 80 | + title: { |
| 81 | + display: true, |
| 82 | + text: 'Chart.js Hierarchical Bar Chart' |
| 83 | + }, |
| 84 | + layout: { |
| 85 | + padding: { |
| 86 | + // add more space at the bottom for the hierarchy |
| 87 | + bottom: 60 |
| 88 | + } |
| 89 | + }, |
| 90 | + scales: { |
| 91 | + xAxes: [{ |
| 92 | + type: 'hierarchical', |
| 93 | + // offset setings, for centering the categorical axis in the bar chart case |
| 94 | + offset: true, |
| 95 | + |
| 96 | + // grid line settings |
| 97 | + gridLines: { |
| 98 | + offsetGridLines: true |
| 99 | + }, |
| 100 | + |
| 101 | + attributes: { |
| 102 | + backgroundColor: (ctx) => { |
| 103 | + const v = ctx.dataset.data[ctx.dataIndex]; |
| 104 | + return scale(v); |
| 105 | + }, |
| 106 | + } |
| 107 | + }], |
| 108 | + yAxes: [{ |
| 109 | + ticks: { |
| 110 | + beginAtZero: true |
| 111 | + } |
| 112 | + }] |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + }; |
| 118 | + |
| 119 | + </script> |
| 120 | +</body> |
| 121 | + |
| 122 | +</html> |
0 commit comments