| 1 |
/*! |
| 2 |
* D3Evolution 2.0.4 (https://github.com/moisseev/D3Evolution) |
| 3 |
* Copyright (c) 2016-2017, Alexander Moisseev, BSD 2-Clause |
| 4 |
*/ |
| 5 |
|
| 6 |
/* exported D3Evolution */ |
| 7 |
/* eslint-disable-next-line max-statements */ |
| 8 |
function D3Evolution (id, options) { |
| 9 |
"use strict"; |
| 10 |
|
| 11 |
var opts = $.extend(true, { |
| 12 |
title: "", |
| 13 |
width: 800, |
| 14 |
height: 400, |
| 15 |
margin: {top: 80, right: 60, bottom: 40, left: 60}, |
| 16 |
yAxisLabel: "", |
| 17 |
|
| 18 |
type: "line", // area|line |
| 19 |
yScale: "lin", // lin|log |
| 20 |
|
| 21 |
duration: 1250, |
| 22 |
interpolate: "curveLinear", |
| 23 |
|
| 24 |
// convert: "percentage", |
| 25 |
|
| 26 |
legend: { |
| 27 |
buttonRadius: 7, |
| 28 |
space: 130, |
| 29 |
|
| 30 |
entries: [ |
| 31 |
// , |
| 32 |
// , |
| 33 |
// {label: "Greylisted", color: "#436EEE"}, |
| 34 |
// {label: "Clean", color: "#66cc00"}, |
| 35 |
] |
| 36 |
} |
| 37 |
}, options); |
| 38 |
|
| 39 |
this.destroy = function () { |
| 40 |
// Remove all event listeners before removing elements |
| 41 |
d3.select("#" + id).selectAll("*") |
| 42 |
.on("click", null) |
| 43 |
.on("mousemove", null) |
| 44 |
.on("mouseover", null) |
| 45 |
.on("mouseout", null); |
| 46 |
|
| 47 |
// Remove SVG and clean up |
| 48 |
d3.selectAll("#" + id + " svg").remove(); |
| 49 |
}; |
| 50 |
this.destroy(); |
| 51 |
|
| 52 |
const curves = { |
| 53 |
curveLinear: d3.curveLinear, |
| 54 |
curveStep: d3.curveStep, |
| 55 |
curveStepBefore: d3.curveStepBefore, |
| 56 |
curveStepAfter: d3.curveStepAfter, |
| 57 |
curveMonotoneX: d3.curveMonotoneX, |
| 58 |
curveBasis: d3.curveBasis, |
| 59 |
curveBasisOpen: d3.curveBasisOpen, |
| 60 |
curveBundle: d3.curveBundle, |
| 61 |
curveCardinal: d3.curveCardinal, |
| 62 |
curveCardinalOpen: d3.curveCardinalOpen, |
| 63 |
curveCatmullRom: d3.curveCatmullRom, |
| 64 |
curveCatmullRomOpen: d3.curveCatmullRomOpen, |
| 65 |
curveNatural: d3.curveNatural |
| 66 |
}; |
| 67 |
|
| 68 |
var data = null; |
| 69 |
var srcData = null; |
| 70 |
var legendX = null; |
| 71 |
|
| 72 |
var width = opts.width - opts.margin.left - opts.margin.right; |
| 73 |
var height = opts.height - opts.margin.top - opts.margin.bottom; |
| 74 |
|
| 75 |
var xScale = d3.scaleTime().range([0, width]); |
| 76 |
|
| 77 |
var yScale = null; |
| 78 |
var yAxisScale = null; |
| 79 |
|
| 80 |
const setYScale = function () { |
| 81 |
if (opts.yScale === "log") { |
| 82 |
yScale = d3.scaleLog().clamp(true).range([height, 0]); |
| 83 |
yAxisScale = d3.scaleLog().range([height - 30, 0]); |
| 84 |
} else { |
| 85 |
yScale = d3.scaleLinear().range([height, 0]); |
| 86 |
yAxisScale = yScale.copy(); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
setYScale(); |
| 91 |
|
| 92 |
var xAxis = d3.axisBottom().scale(xScale); |
| 93 |
var yAxis = d3.axisLeft().scale(yAxisScale).ticks(5); |
| 94 |
|
| 95 |
var xAxisGrid = d3.axisBottom().tickFormat("").scale(xScale) |
| 96 |
.tickSize(-height, 0); |
| 97 |
var yAxisGrid = d3.axisLeft().tickFormat("").scale(yAxisScale) |
| 98 |
.tickSize(-width, 0); |
| 99 |
|
| 100 |
var yScaleBoolean = d3.scaleQuantize().range([height, 0]); |
| 101 |
var areaNull = d3.area() |
| 102 |
.x(function (d) { return xScale(d.x); }) |
| 103 |
.y0(function () { return height; }) |
| 104 |
.y1(function (d) { return yScaleBoolean(d.y === null); }) |
| 105 |
.curve(d3.curveStep); |
| 106 |
|
| 107 |
var line = d3.line() |
| 108 |
.defined(function (d) { return d.y !== null; }) |
| 109 |
.x(function (d) { return xScale(d.x); }) |
| 110 |
.y(function (d) { return yScale(d.y); }) |
| 111 |
.curve(curves[opts.interpolate]); |
| 112 |
|
| 113 |
var area = d3.area() |
| 114 |
.defined(function (d) { return d.y !== null; }) |
| 115 |
.x(function (d) { return xScale(d.x); }) |
| 116 |
.y0(function (d) { return yScale(d.y0); }) |
| 117 |
.y1(function (d) { return yScale(d.y0 + d.y); }) |
| 118 |
.curve(curves[opts.interpolate]); |
| 119 |
|
| 120 |
var d3v3LayoutStack = function (arr) { |
| 121 |
arr.reduce(function (res, curr) { |
| 122 |
curr.forEach(function (d, i) { |
| 123 |
d.y0 = (res.length ? res[i].y + res[i].y0 : 0); |
| 124 |
}); |
| 125 |
return curr; |
| 126 |
}, []); |
| 127 |
}; |
| 128 |
|
| 129 |
var colorScale = d3.scaleOrdinal(d3.schemeCategory10); |
| 130 |
|
| 131 |
var pathColor = function (i) { |
| 132 |
return (typeof opts.legend.entries[i] !== "undefined" && |
| 133 |
typeof opts.legend.entries[i].color !== "undefined") |
| 134 |
? opts.legend.entries[i].color |
| 135 |
: colorScale(i); |
| 136 |
}; |
| 137 |
|
| 138 |
var pathLabel = function (i) { |
| 139 |
return (typeof opts.legend.entries[i] !== "undefined" && |
| 140 |
typeof opts.legend.entries[i].label !== "undefined") |
| 141 |
? opts.legend.entries[i].label |
| 142 |
: "path_" + i; |
| 143 |
}; |
| 144 |
|
| 145 |
var convert2Percentage = function (a) { |
| 146 |
var total = a.reduce(function (res, curr) { |
| 147 |
return curr.map(function (d, i) { return d.y + (res[i] ? res[i] : 0); }); |
| 148 |
}, []); |
| 149 |
|
| 150 |
var dataPercentage = $.extend(true, [], a); |
| 151 |
|
| 152 |
dataPercentage.forEach(function (s) { |
| 153 |
s.forEach(function (d, i) { if (total[i]) { d.y /= total[i]; } }); |
| 154 |
}); |
| 155 |
|
| 156 |
return dataPercentage; |
| 157 |
}; |
| 158 |
|
| 159 |
/** |
| 160 |
* Substitute real zeroes with values mapped to zero position on the graph. |
| 161 |
*/ |
| 162 |
const substY0 = function () { |
| 163 |
if (opts.yScale === "log") { |
| 164 |
const y0 = yScale.invert(height); |
| 165 |
data.forEach(function (s) { |
| 166 |
s.forEach(function (d) { return d.y === 0 ? d.y : y0; }); |
| 167 |
}); |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
var svg = d3.select("#" + id).append("svg") |
| 172 |
.classed("d3evolution", true) |
| 173 |
.attr("width", opts.width) |
| 174 |
.attr("height", opts.height); |
| 175 |
|
| 176 |
var legend = svg.append("g").attr("class", "legend"); |
| 177 |
|
| 178 |
var g = svg.append("g") |
| 179 |
.attr("width", width) |
| 180 |
.attr("height", height) |
| 181 |
.attr("transform", "translate(" + opts.margin.left + ", " + opts.margin.top + ")"); |
| 182 |
|
| 183 |
g.append("g") |
| 184 |
.attr("class", "x grid") |
| 185 |
.attr("transform", "translate(0," + height + ")") |
| 186 |
.call(xAxisGrid); |
| 187 |
|
| 188 |
g.append("g") |
| 189 |
.attr("class", "y grid") |
| 190 |
.attr("transform", "translate(0,0)") |
| 191 |
.call(yAxisGrid); |
| 192 |
|
| 193 |
g.append("g") |
| 194 |
.attr("class", "x axis") |
| 195 |
.attr("transform", "translate(0," + height + ")") |
| 196 |
.call(xAxis); |
| 197 |
|
| 198 |
g.append("g") |
| 199 |
.attr("class", "y axis") |
| 200 |
.attr("transform", "translate(0,0)") |
| 201 |
.call(yAxis); |
| 202 |
|
| 203 |
// Zero tick mark for log scale |
| 204 |
var y0Scale = d3.scaleOrdinal().domain([0]).range([height]); |
| 205 |
var y0Axis = d3.axisLeft().scale(y0Scale); |
| 206 |
g.append("g") |
| 207 |
.attr("class", "y-zero axis") |
| 208 |
.call(y0Axis); |
| 209 |
|
| 210 |
var yAxisLabel = g.append("text") |
| 211 |
.attr("class", "y label") |
| 212 |
.attr("x", 20 - opts.margin.left) |
| 213 |
.attr("y", -20) |
| 214 |
.style("opacity", (opts.convert === "percentage") ? 0 : 1) |
| 215 |
.text(opts.yAxisLabel); |
| 216 |
|
| 217 |
var title = svg.append("svg:text") |
| 218 |
.attr("x", (opts.width / 2)) |
| 219 |
.attr("y", (opts.margin.top / 3)) |
| 220 |
.attr("text-anchor", "middle"); |
| 221 |
|
| 222 |
title.append("tspan") |
| 223 |
.attr("class", "chart-title") |
| 224 |
.text(opts.title + " "); |
| 225 |
|
| 226 |
title.timeRange = title.append("tspan"); |
| 227 |
|
| 228 |
var cursorTime = svg.append("svg:text") |
| 229 |
.attr("x", (opts.width - 20)) |
| 230 |
.attr("y", (opts.margin.top / 3)) |
| 231 |
.attr("text-anchor", "end"); |
| 232 |
|
| 233 |
var cursorTimeText = cursorTime.append("tspan") |
| 234 |
.attr("class", "cursor-time"); |
| 235 |
|
| 236 |
cursorTime.append("svg:title") |
| 237 |
.text("Current cursor position"); |
| 238 |
|
| 239 |
const iso = function (t) { |
| 240 |
return d3.timeFormat("%Y-%m-%d %H:%M:%S")(new Date(t)); |
| 241 |
}; |
| 242 |
|
| 243 |
/** |
| 244 |
* Get column of the 'data' array and update cursor position and legend values. |
| 245 |
* @param {number} idx - Column index. |
| 246 |
* @returns {Object[]} Column of the 'data' array. |
| 247 |
*/ |
| 248 |
function getColumnByIndex (idx) { |
| 249 |
var col = data.map(function (d) { return d[idx]; }); |
| 250 |
var t = col[0].x; |
| 251 |
cursorTimeText.text(iso(t)); |
| 252 |
legend.selectAll("text.value") |
| 253 |
.text(function (_, j) { |
| 254 |
if (col[j].y === null) return null; |
| 255 |
return d3.format((opts.convert === "percentage") ? ".2~%" : ".6~")(col[j].y); |
| 256 |
}); |
| 257 |
return col; |
| 258 |
} |
| 259 |
|
| 260 |
var cursor = null; |
| 261 |
var latestIdx = null; |
| 262 |
|
| 263 |
function mousemove (event) { |
| 264 |
// Returns the closest index that corresponds to the horizontal position of the mouse pointer |
| 265 |
var getClosestXIndex = d3.bisector(function (d) { return d.x; }).left; |
| 266 |
|
| 267 |
// recover coordinate we need |
| 268 |
var x = xScale.invert(d3.pointer(event)[0]); |
| 269 |
// Guard: if there is no data (e.g. all y are null) avoid bisector call |
| 270 |
if (!data || !data[0] || !data[0].length) return; |
| 271 |
var idx = Math.max(0, getClosestXIndex(data[0], x) - 1); |
| 272 |
var col = getColumnByIndex(idx); |
| 273 |
|
| 274 |
cursor.selectAll(".x,.cursor circle") |
| 275 |
.attr("transform", "translate(" + xScale(col[0].x) + ",0)"); |
| 276 |
|
| 277 |
cursor.selectAll(".y") |
| 278 |
.attr("transform", function (_, i) { |
| 279 |
var d = col[i]; |
| 280 |
var pointerY = (opts.type === "area") ? yScale(d.y0 + d.y) : yScale(d.y); |
| 281 |
return "translate(0," + (pointerY || 0) + ")"; |
| 282 |
}) |
| 283 |
.style("display", function (_, i) { |
| 284 |
return col[i].y ? null : "none"; |
| 285 |
}); |
| 286 |
} |
| 287 |
|
| 288 |
function mouseout () { |
| 289 |
cursor.style("display", "none"); |
| 290 |
getColumnByIndex(latestIdx); |
| 291 |
} |
| 292 |
|
| 293 |
function mouseover () { |
| 294 |
cursor.style("display", null); |
| 295 |
} |
| 296 |
|
| 297 |
// Create a rectangle on top of the g area: this rectangle recovers mouse position |
| 298 |
g.append("rect") |
| 299 |
.style("fill", "none") |
| 300 |
.style("pointer-events", "all") |
| 301 |
.attr("width", width) |
| 302 |
.attr("height", height) |
| 303 |
.on("mousemove", mousemove) |
| 304 |
.on("mouseout", mouseout) |
| 305 |
.on("mouseover", mouseover); |
| 306 |
|
| 307 |
// Create paths groups as we need to dynamically add paths below cursor group |
| 308 |
var pathNullG = g.append("g"); |
| 309 |
var pathG = g.append("g"); |
| 310 |
|
| 311 |
cursor = g.append("g") |
| 312 |
.attr("class", "cursor") |
| 313 |
.style("pointer-events", "none") |
| 314 |
.style("display", "none"); |
| 315 |
|
| 316 |
// Append vertical line of the cursor |
| 317 |
cursor.append("line") |
| 318 |
.attr("class", "x background") |
| 319 |
.attr("y1", 0) |
| 320 |
.attr("y2", height); |
| 321 |
cursor.append("line") |
| 322 |
.attr("class", "x foreground") |
| 323 |
.attr("y1", 0) |
| 324 |
.attr("y2", height); |
| 325 |
|
| 326 |
var stack = function () { |
| 327 |
var yExtents = (() => { |
| 328 |
if (opts.type === "area") { |
| 329 |
d3v3LayoutStack(data); |
| 330 |
return (opts.yScale === "log") |
| 331 |
? d3.extent(d3.merge(data), function (d) { return ((d.y0 + d.y) === 0) ? null : d.y0 + d.y; }) |
| 332 |
: d3.extent(d3.merge(data), function (d) { return d.y0 + d.y; }); |
| 333 |
} |
| 334 |
return (opts.yScale === "log") |
| 335 |
? d3.extent(d3.merge(data), function (d) { return (d.y === 0) ? null : d.y; }) |
| 336 |
: d3.extent(d3.merge(data), function (d) { return d.y; }); |
| 337 |
})(); |
| 338 |
|
| 339 |
if (opts.yScale === "log") { |
| 340 |
if (typeof yExtents[0] === "undefined") { |
| 341 |
yExtents = [0.0095, 0.0105]; |
| 342 |
} else if (yExtents[0] === yExtents[1]) { |
| 343 |
yExtents[0] *= 0.9; |
| 344 |
} |
| 345 |
yAxisScale.domain([yExtents[0], yExtents[1]]); |
| 346 |
var y0 = yAxisScale.invert(height); |
| 347 |
yScale.domain([y0, yExtents[1]]); |
| 348 |
} else { |
| 349 |
yScale.domain([(yExtents[0] > 0) ? 0 : yExtents[0], yExtents[1]]); |
| 350 |
yAxisScale.domain(yScale.domain()); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Hide overlapping tick labels on logarithmic Y-axis. |
| 355 |
* @param {number} d - Tick value. |
| 356 |
* @param {object} p - Previous unhidden label. |
| 357 |
* @param {number} p.y - Y-position of the tick. |
| 358 |
* @param {string} f - Tick label format. |
| 359 |
* @returns {string} Tick label format or empty string. |
| 360 |
*/ |
| 361 |
function logFormat (d, p, f) { |
| 362 |
// Minimal interval of labeled ticks |
| 363 |
const minInterval = 15; |
| 364 |
// The nearest power of 10. |
| 365 |
const pow10 = Math.pow(10, Math.round(Math.log(d) / Math.LN10)); |
| 366 |
|
| 367 |
if ( |
| 368 |
// Never hide labels of power of 10 tick marks |
| 369 |
!(Math.abs(pow10 - d) < 1e-6) && ( |
| 370 |
// Hide if the next power of 10 tick mark is too close |
| 371 |
(Math.abs(yScale(pow10) - yScale(d)) < minInterval) || |
| 372 |
// Hide if previous label is too close |
| 373 |
((p.y - yScale(d)) < minInterval) |
| 374 |
) |
| 375 |
) { |
| 376 |
return ""; |
| 377 |
} |
| 378 |
|
| 379 |
p.y = yScale(d); |
| 380 |
return f(d); |
| 381 |
} |
| 382 |
|
| 383 |
if (opts.convert === "percentage") { |
| 384 |
var prevUnhidLabel = {y: height}; // Previous unhidden tick label |
| 385 |
const percentFormat = d3.format(".0%"); |
| 386 |
y0Axis.tickFormat(percentFormat); |
| 387 |
yAxis.tickFormat((opts.yScale === "log") |
| 388 |
? function (d) { return logFormat(d, prevUnhidLabel, percentFormat); } |
| 389 |
: percentFormat); |
| 390 |
} else { |
| 391 |
y0Axis.tickFormat(null); |
| 392 |
yAxis.tickFormat(null); |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* In some cases when extent values are to close (e.g. 0.00011 and 0.00019 |
| 397 |
* on log scale), there are no ticks generated. Possible D3 bug. |
| 398 |
* We should set at least labels for extents if there are no any ticks. |
| 399 |
*/ |
| 400 |
yAxis.tickValues(yAxisScale.ticks().length ? null : [yExtents[0], yExtents[1]]); |
| 401 |
|
| 402 |
const t = d3.transition() |
| 403 |
.duration(opts.duration); |
| 404 |
|
| 405 |
g.select(".y.grid").transition(t).call(yAxisGrid.scale(yAxisScale)); |
| 406 |
g.select(".y.axis").transition(t).call(yAxis.scale(yAxisScale)); |
| 407 |
g.select(".y-zero.axis").call(y0Axis); |
| 408 |
}; |
| 409 |
|
| 410 |
var yPreprocess = function () { |
| 411 |
if (opts.convert === "percentage") { |
| 412 |
yAxisLabel.transition().duration(opts.duration).style("opacity", 0); |
| 413 |
data = convert2Percentage(srcData); |
| 414 |
} else { |
| 415 |
yAxisLabel.transition().duration(opts.duration).style("opacity", 1); |
| 416 |
data = srcData; |
| 417 |
} |
| 418 |
latestIdx = srcData[0].length - 1; |
| 419 |
stack(); |
| 420 |
}; |
| 421 |
|
| 422 |
function getLabelTransform (i) { |
| 423 |
return "translate(" + (legendX + (opts.legend.space * i) + (2 * opts.legend.buttonRadius)) + |
| 424 |
"," + (opts.margin.top * 2 / 3) + ")"; |
| 425 |
} |
| 426 |
|
| 427 |
function positionLabels (selection) { |
| 428 |
(selection || legend.selectAll("g")) |
| 429 |
.transition().duration(opts.duration) |
| 430 |
.attr("transform", function (_, i) { return getLabelTransform(i); }); |
| 431 |
} |
| 432 |
|
| 433 |
var opacity = []; |
| 434 |
this.data = function (a) { |
| 435 |
var attachClickListener = (selection) => { |
| 436 |
selection.on("click", (event, d) => { |
| 437 |
const i = data.indexOf(d); |
| 438 |
opacity[i] = (opacity[i] === 0) ? 1 : 0; |
| 439 |
|
| 440 |
d3.select("#circle_" + i) |
| 441 |
.transition().duration(opts.duration) |
| 442 |
.style("fill-opacity", (opacity[i] === 0) ? 0.2 : 1); |
| 443 |
|
| 444 |
d3.select("#path_" + i) |
| 445 |
.transition().duration(opts.duration) |
| 446 |
.style("opacity", opacity[i]); |
| 447 |
}); |
| 448 |
}; |
| 449 |
|
| 450 |
/** |
| 451 |
* Highlight selected path and legend circle. |
| 452 |
* @param {number} s - Selected path index. |
| 453 |
* @param {boolean} [h] - If false, restore previous state. |
| 454 |
*/ |
| 455 |
const highlight = function (s, h) { |
| 456 |
d3.select("#circle_" + s) |
| 457 |
.attr("r", opts.legend.buttonRadius * (h === false ? 1 : 1.3)); |
| 458 |
|
| 459 |
const op = function (i) { |
| 460 |
if (h === false) return opacity[i]; |
| 461 |
if (i === s) return 1; |
| 462 |
return (opacity[i] === 0) ? 0 : 0.4; |
| 463 |
}; |
| 464 |
|
| 465 |
pathG.selectAll("path.path") |
| 466 |
.style("opacity", function (d, i) { return op(i); }) |
| 467 |
.style("fill-opacity", function (d, i) { return op(i); }); |
| 468 |
}; |
| 469 |
|
| 470 |
srcData = $.extend(true, [], a); |
| 471 |
legendX = opts.width - opts.margin.right - (opts.legend.space * srcData.length); |
| 472 |
|
| 473 |
// Initialize opacity array: preserve existing values, add new entries as visible (1) |
| 474 |
while (opacity.length < srcData.length) { |
| 475 |
opacity.push(1); |
| 476 |
} |
| 477 |
// Truncate if dataset has fewer series |
| 478 |
opacity.length = srcData.length; |
| 479 |
|
| 480 |
// Convert time in seconds to milliseconds |
| 481 |
srcData.forEach(function (s) { |
| 482 |
s.forEach(function (d) { d.x *= 1000; }); |
| 483 |
}); |
| 484 |
|
| 485 |
var xExtents = d3.extent(d3.merge(srcData), function (d) { return d.x; }); |
| 486 |
xScale.domain([xExtents[0], xExtents[1]]); |
| 487 |
|
| 488 |
title.timeRange |
| 489 |
.text("[ " + iso(xExtents[0]) + " / " + iso(xExtents[1]) + " ]"); |
| 490 |
|
| 491 |
var pathNull = pathNullG.selectAll("path.path-null").data(srcData); |
| 492 |
|
| 493 |
pathNull.enter() |
| 494 |
.append("path") |
| 495 |
.attr("class", "path-null"); |
| 496 |
|
| 497 |
pathNullG.selectAll("path.path-null") |
| 498 |
.transition().duration(opts.duration / 2) |
| 499 |
.style("opacity", 0) |
| 500 |
.on("end", function () { |
| 501 |
pathNullG.selectAll("path.path-null") |
| 502 |
.attr("d", areaNull) |
| 503 |
.transition().duration(opts.duration / 2) |
| 504 |
.style("opacity", 1); |
| 505 |
}); |
| 506 |
|
| 507 |
pathNull.exit() |
| 508 |
.remove(); |
| 509 |
|
| 510 |
yPreprocess(); |
| 511 |
substY0(); |
| 512 |
|
| 513 |
var path = pathG.selectAll("path.path").data(data); |
| 514 |
|
| 515 |
const pathEnter = path.enter() |
| 516 |
.append("path") |
| 517 |
.merge(path) |
| 518 |
.attr("class", "path") |
| 519 |
.attr("id", function (d, i) { return "path_" + i; }) |
| 520 |
.on("mousemove", mousemove) |
| 521 |
.on("mouseover", (event, d) => { |
| 522 |
highlight(data.indexOf(d)); |
| 523 |
mouseover(); |
| 524 |
}) |
| 525 |
.on("mouseout", (event, d) => { |
| 526 |
highlight(data.indexOf(d), false); |
| 527 |
mouseout(); |
| 528 |
}); |
| 529 |
attachClickListener(pathEnter); |
| 530 |
|
| 531 |
path.exit() |
| 532 |
.remove(); |
| 533 |
|
| 534 |
path = pathG.selectAll("path.path"); |
| 535 |
|
| 536 |
if (opts.type === "area") { |
| 537 |
path |
| 538 |
.style("fill", function (d, i) { return pathColor(i); }) |
| 539 |
.style("stroke", "none") |
| 540 |
.style("fill-opacity", function (d, i) { return opacity[i]; }); |
| 541 |
} else { |
| 542 |
path |
| 543 |
.style("fill", "none") |
| 544 |
.style("stroke", function (d, i) { return pathColor(i); }) |
| 545 |
.style("opacity", function (d, i) { return opacity[i]; }); |
| 546 |
} |
| 547 |
|
| 548 |
path |
| 549 |
.transition().duration(opts.duration) |
| 550 |
.attr("d", (opts.type === "area") ? area : line); |
| 551 |
|
| 552 |
const t = d3.transition() |
| 553 |
.duration(opts.duration); |
| 554 |
|
| 555 |
g.select(".x.grid").transition(t).call(xAxisGrid.scale(xScale)); |
| 556 |
g.select(".x.axis").transition(t).call(xAxis.scale(xScale)); |
| 557 |
|
| 558 |
// Group cursor elements per path |
| 559 |
var cursorYG = cursor.selectAll(".y").data(data); |
| 560 |
|
| 561 |
var cursorYGEnter = cursorYG.enter() |
| 562 |
.append("g") |
| 563 |
.attr("class", "y") |
| 564 |
.style("stroke", function (d, i) { return pathColor(i); }); |
| 565 |
|
| 566 |
// Append circles that travel along the curves of the chart |
| 567 |
cursorYGEnter |
| 568 |
.append("circle") |
| 569 |
.attr("class", "background"); |
| 570 |
cursorYGEnter |
| 571 |
.append("circle") |
| 572 |
.attr("class", "foreground"); |
| 573 |
cursorYGEnter.selectAll("circle") |
| 574 |
.attr("r", 7) |
| 575 |
.style("fill", "none"); |
| 576 |
|
| 577 |
// Append horizontal lines of the cursor |
| 578 |
cursorYGEnter |
| 579 |
.append("line") |
| 580 |
.attr("class", "background"); |
| 581 |
cursorYGEnter |
| 582 |
.append("line") |
| 583 |
.attr("class", "foreground"); |
| 584 |
cursorYGEnter.selectAll("line") |
| 585 |
.attr("x1", 0) |
| 586 |
.attr("x2", width); |
| 587 |
|
| 588 |
cursorYG.exit() |
| 589 |
.remove(); |
| 590 |
|
| 591 |
var buttons = legend.selectAll("circle").data(data); |
| 592 |
|
| 593 |
const buttonsEnter = buttons.enter().append("circle") |
| 594 |
.attr("id", function (d, i) { return "circle_" + i; }) |
| 595 |
.attr("cy", opts.margin.top * 2 / 3) |
| 596 |
.attr("r", opts.legend.buttonRadius) |
| 597 |
.attr("cx", function (d, i) { return legendX + (opts.legend.space * i); }) |
| 598 |
.style("fill", function (d, i) { return pathColor(i); }) |
| 599 |
.style("stroke", function (d, i) { return pathColor(i); }) |
| 600 |
.style("fill-opacity", function (d, i) { return (opacity[i] === 0) ? 0.2 : 1; }) |
| 601 |
.on("mouseover", (event, d) => highlight(data.indexOf(d))) |
| 602 |
.on("mouseout", (event, d) => highlight(data.indexOf(d), false)); |
| 603 |
attachClickListener(buttonsEnter); |
| 604 |
|
| 605 |
buttons.exit() |
| 606 |
.remove(); |
| 607 |
|
| 608 |
buttons |
| 609 |
.transition().duration(opts.duration) |
| 610 |
.attr("cx", function (d, i) { return legendX + (opts.legend.space * i); }) |
| 611 |
.style("fill-opacity", function (d, i) { return (opacity[i] === 0) ? 0.2 : 1; }); |
| 612 |
|
| 613 |
var labels = legend.selectAll("g").data(data); |
| 614 |
var labelsEnter = labels.enter().append("g") |
| 615 |
.attr("transform", function (_, i) { return getLabelTransform(i); }); |
| 616 |
|
| 617 |
const labelsName = labelsEnter |
| 618 |
.append("text") |
| 619 |
.attr("class", "name") |
| 620 |
.attr("dy", "0.3em") |
| 621 |
.text(function (d, i) { return pathLabel(i); }) |
| 622 |
.on("mouseover", (event, d) => highlight(data.indexOf(d))) |
| 623 |
.on("mouseout", (event, d) => highlight(data.indexOf(d), false)); |
| 624 |
attachClickListener(labelsName); |
| 625 |
|
| 626 |
labelsEnter |
| 627 |
.append("text") |
| 628 |
.attr("class", "value") |
| 629 |
.attr("dy", "20"); |
| 630 |
|
| 631 |
labels.exit() |
| 632 |
.remove(); |
| 633 |
|
| 634 |
positionLabels(labels); |
| 635 |
|
| 636 |
var values = legend.selectAll("text.value"); |
| 637 |
values |
| 638 |
.transition("opacity").duration(opts.duration / 2) |
| 639 |
.style("opacity", 0) |
| 640 |
.on("end", function () { |
| 641 |
getColumnByIndex(latestIdx); |
| 642 |
values |
| 643 |
.transition("opacity").duration(opts.duration / 2) |
| 644 |
.style("opacity", 1); |
| 645 |
}); |
| 646 |
|
| 647 |
return this; |
| 648 |
}; |
| 649 |
|
| 650 |
this.legend = function (a) { |
| 651 |
$.extend(true, opts.legend, a); |
| 652 |
|
| 653 |
legend.selectAll("circle") |
| 654 |
.transition().duration(opts.duration) |
| 655 |
.attr("cx", function (d, i) { return legendX + (opts.legend.space * i); }) |
| 656 |
.attr("r", opts.legend.buttonRadius) |
| 657 |
.style("fill", function (d, i) { return pathColor(i); }) |
| 658 |
.style("stroke", function (d, i) { return pathColor(i); }); |
| 659 |
|
| 660 |
legend.selectAll("text.name") |
| 661 |
.text(function (d, i) { return pathLabel(i); }); |
| 662 |
|
| 663 |
positionLabels(); |
| 664 |
|
| 665 |
pathG.selectAll("path.path") |
| 666 |
.transition().duration(opts.duration) |
| 667 |
.style("fill", (opts.type === "area") ? function (d, i) { return pathColor(i); } : "none") |
| 668 |
.style("stroke", (opts.type !== "area") ? function (d, i) { return pathColor(i); } : "none"); |
| 669 |
|
| 670 |
cursor.selectAll(".y") |
| 671 |
.style("stroke", function (d, i) { return pathColor(i); }); |
| 672 |
|
| 673 |
return this; |
| 674 |
}; |
| 675 |
|
| 676 |
this.convert = function (a) { |
| 677 |
opts.convert = a; |
| 678 |
|
| 679 |
yPreprocess(); |
| 680 |
getColumnByIndex(latestIdx); |
| 681 |
|
| 682 |
pathG.selectAll("path.path") |
| 683 |
.data(data) |
| 684 |
.transition().duration(opts.duration) |
| 685 |
.attr("d", (opts.type === "area") ? area : line); |
| 686 |
|
| 687 |
return this; |
| 688 |
}; |
| 689 |
|
| 690 |
this.interpolate = function (a) { |
| 691 |
opts.interpolate = a; |
| 692 |
|
| 693 |
area.curve(curves[opts.interpolate]); |
| 694 |
line.curve(curves[opts.interpolate]); |
| 695 |
|
| 696 |
pathG.selectAll("path.path") |
| 697 |
.attr("d", (opts.type === "area") ? area : line); |
| 698 |
|
| 699 |
return this; |
| 700 |
}; |
| 701 |
|
| 702 |
this.type = function (a) { |
| 703 |
opts.type = a; |
| 704 |
|
| 705 |
stack(); |
| 706 |
|
| 707 |
pathG.selectAll("path.path") |
| 708 |
.style("stroke", (opts.type !== "area") ? function (d, i) { return pathColor(i); } : "none") |
| 709 |
.style("fill", (opts.type === "area") ? function (d, i) { return pathColor(i); } : "none") |
| 710 |
.transition().duration(opts.duration) |
| 711 |
.attr("d", (opts.type === "area") ? area : line); |
| 712 |
|
| 713 |
return this; |
| 714 |
}; |
| 715 |
|
| 716 |
this.yAxisLabel = function (a) { |
| 717 |
opts.yAxisLabel = a; |
| 718 |
yAxisLabel |
| 719 |
.transition().duration(opts.duration / 2) |
| 720 |
.style("opacity", 0) |
| 721 |
.on("end", function () { |
| 722 |
yAxisLabel |
| 723 |
.text(opts.yAxisLabel) |
| 724 |
.transition().duration(opts.duration / 2) |
| 725 |
.style("opacity", 1); |
| 726 |
}); |
| 727 |
|
| 728 |
return this; |
| 729 |
}; |
| 730 |
|
| 731 |
this.yScale = function (a) { |
| 732 |
opts.yScale = a; |
| 733 |
|
| 734 |
setYScale(); |
| 735 |
substY0(); |
| 736 |
stack(); |
| 737 |
|
| 738 |
pathG.selectAll("path.path") |
| 739 |
.transition().duration(opts.duration) |
| 740 |
.attr("d", (opts.type === "area") ? area : line); |
| 741 |
|
| 742 |
return this; |
| 743 |
}; |
| 744 |
} |