-
Notifications
You must be signed in to change notification settings - Fork 56
/
bar-floating-horizontal.html
244 lines (220 loc) · 6.13 KB
/
bar-floating-horizontal.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>
Horizontal Floating Bar demo | Chart.js Drag Data Points Plugin
</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no"
/>
<script src="assets/chart.min.js"></script>
<script src="assets/lodash.min.js"></script>
<script src="assets/chartjs-plugin-datalabels.min.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
}
canvas {
background-color: #eee;
position: absolute;
max-width: 100vw;
max-height: 100vh;
}
</style>
</head>
<body>
<canvas id="chartJSContainer"></canvas>
<script>
// demo environment build
const isFirefox = navigator.userAgent.match(/firefox|fxios/i),
chartConfiguration = {
type: "bar",
options: {
plugins: { datalabels: { display: false } },
indexAxis: "y",
scales: { x: { min: 1, max: 28.5 } },
},
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [
[6, 18],
[9.5, 28.5],
[1.5, 4.5],
[2.5, 7.5],
[1, 3],
[1.5, 4.5],
],
fill: true,
tension: 0.4,
borderWidth: 1,
pointHitRadius: 25,
},
{
label: "# of Points",
data: [
[3.5, 10.5],
[5.5, 16.5],
[2.5, 7.5],
[4, 12],
[1.5, 4.5],
[3.5, 10.5],
],
fill: true,
tension: 0.4,
borderWidth: 1,
pointHitRadius: 25,
},
],
},
};
window.isPluginLoaded = false;
window.setupChart = function setupChart(options) {
// demo config loading
const {
disablePlugin,
draggableAxis,
roundingPrecision,
renderOnHoverCirclesTrail = false,
} = options;
const isTest = false;
let onDrag = undefined;
function onPluginScriptLoaded() {
if (!window.isPluginLoaded) console.log("Plugin script loaded");
window.isPluginLoaded = true;
const bothAxesDraggable = draggableAxis === "both",
xAxisDraggable =
!draggableAxis || bothAxesDraggable || draggableAxis === "x",
yAxisDraggable = bothAxesDraggable || draggableAxis === "y";
function drawDragMarker(e) {
// demo environment stub
}
const configuration = _.merge(
{
options: {
animation: !isTest,
plugins: {
dragData: disablePlugin
? false
: {
dragX: xAxisDraggable,
dragY: yAxisDraggable,
round: roundingPrecision,
showTooltip: true,
onDragStart: function (e) {
if (renderOnHoverCirclesTrail) drawDragMarker(e);
},
onDrag: function (...args) {
const [e] = args;
if (e.target?.style)
e.target.style.cursor = "grabbing";
onDrag?.(...args);
if (renderOnHoverCirclesTrail) drawDragMarker(e);
},
onDragEnd: function (e) {
if (e.target?.style)
e.target.style.cursor = "default";
if (renderOnHoverCirclesTrail) drawDragMarker(e);
},
},
},
onHover: function (e) {
const point = e.chart.getElementsAtEventForMode(
e,
e.chart.options.interaction.mode,
{ intersect: true },
false,
);
if (point.length) e.native.target.style.cursor = "grab";
else e.native.target.style.cursor = "default";
},
},
},
{
...chartConfiguration,
options: {
...chartConfiguration.options,
plugins: {
...(chartConfiguration.options.plugins ?? {}),
datalabels: {
...(chartConfiguration.options.plugins?.datalabels ?? {}),
...(chartConfiguration.options.plugins?.datalabels?.display
? {
formatter: function (value, context) {
return (
dateFns.differenceInDays(value[1], value[0]) + "d"
);
},
}
: {}),
},
},
},
data: {
...chartConfiguration.data,
...(chartConfiguration.scales?.x?.type === "time"
? {
datasets: chartConfiguration.data.datasets.map((d) => ({
...d,
data: d.data.map((darr) =>
darr.map((date) => new Date(date)),
),
})),
}
: {}),
},
},
);
var ctx = document
.getElementById("chartJSContainer")
.getContext("2d");
Chart.register(ChartDataLabels);
window.testedChart = new Chart(ctx, configuration);
let originalTestedChartData = _.cloneDeep(window.testedChart.data);
window.resetData = function resetData() {
console.log(
"[resetData] Resetting data to original data passed to setupChart()",
originalTestedChartData,
);
window.testedChart.data = _.cloneDeep(originalTestedChartData);
window.testedChart.update("none");
};
window.isTestReady = true;
}
if (window.isPluginLoaded) {
onPluginScriptLoaded();
} else {
// load the plugin script dynamically - either with IstanbulJS's coverage statements or not, depending on the value of isTest
let scriptElement = document.createElement("script");
scriptElement.src = isTest
? "assets/chartjs-plugin-dragdata-test-browser.js"
: "assets/chartjs-plugin-dragdata.min.js";
console.log("Loading plugin script from:", scriptElement.src);
scriptElement.onload = onPluginScriptLoaded;
document.head.appendChild(scriptElement);
}
};
document.body.onload = function onBodyLoaded() {
let urlSearchParams = new URLSearchParams(window.location.search);
if (
!urlSearchParams.has("isTest") ||
urlSearchParams.get("isTest") !== "true"
) {
console.log("Demo build running");
setupChart({
isTest: false,
disablePlugin: false,
draggableAxis: "both",
roundingPrecision: 2,
});
}
};
</script>
</body>
</html>