|
|
|
@ -1,6 +1,7 @@
|
|
|
|
class ChartLoader {
|
|
|
|
class ChartLoader {
|
|
|
|
constructor(canvas, detectionCanvas, parent, legend, dataDiv) {
|
|
|
|
constructor(canvas, effectCanvas, detectionCanvas, parent, legend, dataDiv) {
|
|
|
|
this.canvas = canvas
|
|
|
|
this.canvas = canvas
|
|
|
|
|
|
|
|
this.effectCanvas = effectCanvas
|
|
|
|
this.detectionCanvas = detectionCanvas
|
|
|
|
this.detectionCanvas = detectionCanvas
|
|
|
|
this.parent = parent
|
|
|
|
this.parent = parent
|
|
|
|
this.legend = legend
|
|
|
|
this.legend = legend
|
|
|
|
@ -39,32 +40,79 @@ class ChartLoader {
|
|
|
|
y: e.clientY
|
|
|
|
y: e.clientY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var ctx = this.detectionCanvas.getContext('2d');
|
|
|
|
let ctx = this.detectionCanvas.getContext('2d');
|
|
|
|
var pixelData = ctx.getImageData(pos.x, pos.y, 1, 1).data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (pixelData[3] === 255) {
|
|
|
|
// Get neigboring pixels
|
|
|
|
let index = (pixelData[0] * 256 + pixelData[1]) * 256 + pixelData[2]
|
|
|
|
let imageData = Array.from(ctx.getImageData(pos.x - 2, pos.y - 2, 5, 5).data)
|
|
|
|
let obj = newChart.objects[index]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let divHitBox = this.dataDiv
|
|
|
|
//console.log(imageData)
|
|
|
|
divHitBox.style.display = "block"
|
|
|
|
|
|
|
|
|
|
|
|
// Convert into indexes
|
|
|
|
|
|
|
|
let indexes = []
|
|
|
|
|
|
|
|
while (imageData.length > 0) {
|
|
|
|
|
|
|
|
let pixel = imageData.splice(0, 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// only if alpha is 100%
|
|
|
|
|
|
|
|
if (pixel[3] === 255) {
|
|
|
|
|
|
|
|
let index = (pixel[0] * 256 + pixel[1]) * 256 + pixel[2]
|
|
|
|
|
|
|
|
indexes.push(index)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function mode(array)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(array.length == 0)
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var modeMap = {};
|
|
|
|
|
|
|
|
var maxEl = array[0], maxCount = 1;
|
|
|
|
|
|
|
|
for(var i = 0; i < array.length; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var el = array[i];
|
|
|
|
|
|
|
|
if(modeMap[el] == null)
|
|
|
|
|
|
|
|
modeMap[el] = 1;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
modeMap[el]++;
|
|
|
|
|
|
|
|
if(modeMap[el] > maxCount)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
maxEl = el;
|
|
|
|
|
|
|
|
maxCount = modeMap[el];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxEl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// To avoid edge cases because of anti aliasing
|
|
|
|
|
|
|
|
let mostCommonIndex = mode(indexes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mostCommonIndex !== null) {
|
|
|
|
|
|
|
|
let obj = newChart.objects[mostCommonIndex]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Effect
|
|
|
|
|
|
|
|
let effectCtx = this.effectCanvas.getContext("2d")
|
|
|
|
|
|
|
|
effectCtx.clearRect(0, 0, effectCanvas.width, effectCanvas.height)
|
|
|
|
|
|
|
|
this.effectCanvas.style.opacity = 1
|
|
|
|
|
|
|
|
newChart.drawEffect(effectCtx, [obj])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.dataDiv.style.display = "block"
|
|
|
|
|
|
|
|
|
|
|
|
// make the info fit into the chart div
|
|
|
|
// make the info fit into the chart div
|
|
|
|
if (pos.x + divHitBox.clientWidth <= this.parent.clientWidth - 2)
|
|
|
|
if (pos.x + this.dataDiv.clientWidth <= this.parent.clientWidth - 2)
|
|
|
|
divHitBox.style.left = pos.x + "px"
|
|
|
|
this.dataDiv.style.left = pos.x + "px"
|
|
|
|
else
|
|
|
|
else
|
|
|
|
this.dataDiv.style.left = pos.x - divHitBox.clientWidth + "px"
|
|
|
|
this.dataDiv.style.left = pos.x - this.dataDiv.clientWidth + "px"
|
|
|
|
|
|
|
|
|
|
|
|
if (pos.y + divHitBox.clientHeight <= parent.clientHeight - 2)
|
|
|
|
if (pos.y + this.dataDiv.clientHeight <= parent.clientHeight - 2)
|
|
|
|
this.dataDiv.style.top = pos.y + "px"
|
|
|
|
this.dataDiv.style.top = pos.y + "px"
|
|
|
|
else
|
|
|
|
else
|
|
|
|
this.dataDiv.style.top = pos.y - divHitBox.clientHeight + "px"
|
|
|
|
this.dataDiv.style.top = pos.y - this.dataDiv.clientHeight + "px"
|
|
|
|
|
|
|
|
|
|
|
|
this.dataDiv.style.display = "block"
|
|
|
|
this.dataDiv.style.display = "block"
|
|
|
|
this.dataDiv.innerHTML = "<b>" + obj.name + "</b><br><p>" + obj.value + "</p>"
|
|
|
|
this.dataDiv.innerHTML = "<b>" + obj.name + "</b><br><p>" + obj.value + "</p>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
this.dataDiv.style.display = "none"
|
|
|
|
this.dataDiv.style.display = "none"
|
|
|
|
|
|
|
|
this.effectCanvas.style.opacity = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ -73,22 +121,22 @@ class ChartLoader {
|
|
|
|
newChart.resizeCanvas(this.parent, this.legend.offsetHeight)
|
|
|
|
newChart.resizeCanvas(this.parent, this.legend.offsetHeight)
|
|
|
|
newChart.draw()
|
|
|
|
newChart.draw()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
|
|
this.effectCanvas.width = this.canvas.width
|
|
|
|
|
|
|
|
this.effectCanvas.height = this.canvas.height
|
|
|
|
this.detectionCanvas.width = this.canvas.width
|
|
|
|
this.detectionCanvas.width = this.canvas.width
|
|
|
|
this.detectionCanvas.height = this.canvas.height
|
|
|
|
this.detectionCanvas.height = this.canvas.height
|
|
|
|
this.chart.drawDetectionMap(this.detectionCanvas.getContext("2d"))
|
|
|
|
this.chart.drawDetectionMap(this.detectionCanvas.getContext("2d"))
|
|
|
|
|
|
|
|
}, 0)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
drawChart(graphSettings, data) {
|
|
|
|
drawChart(graphSettings, data) {
|
|
|
|
//objects = []
|
|
|
|
|
|
|
|
this.chart = new Chart(this.canvas, data, graphSettings)
|
|
|
|
this.chart = new Chart(this.canvas, data, graphSettings)
|
|
|
|
this.chart.updateLegend(graphSettings.displayLegend, this.legend)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.chart.updateLegend(graphSettings.displayLegend, this.legend)
|
|
|
|
this.chart.resizeCanvas(this.parent, this.legend.offsetHeight)
|
|
|
|
this.chart.resizeCanvas(this.parent, this.legend.offsetHeight)
|
|
|
|
|
|
|
|
|
|
|
|
this.detectionCanvas.width = this.canvas.width
|
|
|
|
|
|
|
|
this.detectionCanvas.height = this.canvas.height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Choose the correct graph
|
|
|
|
//Choose the correct graph
|
|
|
|
switch (graphSettings.type) {
|
|
|
|
switch (graphSettings.type) {
|
|
|
|
case "point":
|
|
|
|
case "point":
|
|
|
|
@ -120,8 +168,20 @@ class ChartLoader {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.time("1")
|
|
|
|
this.chart.draw()
|
|
|
|
this.chart.draw()
|
|
|
|
|
|
|
|
console.timeEnd("1")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
|
|
console.time("2")
|
|
|
|
|
|
|
|
this.effectCanvas.width = this.canvas.width
|
|
|
|
|
|
|
|
this.effectCanvas.height = this.canvas.height
|
|
|
|
|
|
|
|
this.detectionCanvas.width = this.canvas.width
|
|
|
|
|
|
|
|
this.detectionCanvas.height = this.canvas.height
|
|
|
|
this.chart.drawDetectionMap(this.detectionCanvas.getContext("2d"))
|
|
|
|
this.chart.drawDetectionMap(this.detectionCanvas.getContext("2d"))
|
|
|
|
|
|
|
|
console.timeEnd("2")
|
|
|
|
|
|
|
|
}, 0); {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.addListener(this.chart)
|
|
|
|
this.addListener(this.chart)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|