diff --git a/public/scripts/chart_loader.js b/public/scripts/chart_loader.js
index 363709d..0f748c2 100644
--- a/public/scripts/chart_loader.js
+++ b/public/scripts/chart_loader.js
@@ -23,11 +23,11 @@ class ChartLoader {
}
/** Function for loading chart data from the server
- * @param {String} code - The code used to identify the chart data to load
+ * @param {String} id - The id used to identify the chart
**/
- loadData(code) {
+ loadData(id) {
// Fetch chart data from the server
- fetch("/api/charts/" + code, {
+ fetch("/api/charts/" + id, {
method: 'GET',
headers: {
'Content-Type': 'json'
@@ -198,7 +198,7 @@ class ChartLoader {
// Update chart and redraw
chart.updateBounds()
- chart.draw()
+ chart.draw(false)
this.addInteractivity()
}
})
@@ -241,7 +241,7 @@ class ChartLoader {
window.addEventListener("resize", e => {
// Resize the chart canvas and redraw the chart
chart.resizeCanvas(this.parent, this.legend.offsetHeight)
- chart.draw()
+ chart.draw(false)
// Reapply interactivity
this.addInteractivity()
@@ -259,7 +259,7 @@ class ChartLoader {
// Update chart and redraw
chart.updateBounds()
- chart.draw()
+ chart.draw(false)
this.addInteractivity()
this.mouseMoveFunc(e, chart)
@@ -272,7 +272,7 @@ class ChartLoader {
*/
async addInteractivity() {
setTimeout(() => {
- //console.time("2")
+ console.time("2")
// Set dimensions of effect canvas
this.effectCanvas.width = this.canvas.width
this.effectCanvas.height = this.canvas.height
@@ -281,7 +281,7 @@ class ChartLoader {
this.detectionCanvas.height = this.canvas.height
// Draw detection map on the detection canvas
this.chart.drawDetectionMap(this.detectionCanvas.getContext("2d"))
- //console.timeEnd("2")
+ console.timeEnd("2")
}, 0)
}
@@ -291,6 +291,7 @@ class ChartLoader {
* @param {Array} data - The data to visualize on the chart.
*/
drawChart(chartSettings, data) {
+ console.time("1")
let zoomManager = new ZoomManager(chartSettings.horizontalZoom, chartSettings.verticalZoom)
// Choose the correct type of chart
@@ -332,6 +333,8 @@ class ChartLoader {
this.chart.draw()
+ console.timeEnd("1")
+
this.addInteractivity()
this.addListeners(this.chart)
}
diff --git a/public/scripts/charts/bar_chart.js b/public/scripts/charts/bar_chart.js
index 91d1652..1c1fec6 100644
--- a/public/scripts/charts/bar_chart.js
+++ b/public/scripts/charts/bar_chart.js
@@ -17,83 +17,90 @@ class BarChart extends Chart {
/**
* Draws the bar chart on the canvas.
+ * @param {Boolean} async - Says if the chart should be drawn synchronously or asynchronously
*/
- draw() {
+ draw(async = true) {
// Calculate the number of bars/categories in the chart
this.clear()
let barCount = this.data.length
// Draw the axis without displaying axis values
this.drawAxis(false)
+ // Define function for async
+ let fn = () => {
+ // Divide the space into equal section
+ // Calculate section size
+ let size = this.zoomBounds.width / this.dataLen
+ // Callculate inner size with margin
+ let innerSize = size * 0.8
+ // Width of each chart
+ let bar_width = innerSize * 0.7 / barCount // this.zoom.scaleX
- // Divide the space into equal section
- // Calculate section size
- let size = this.zoomBounds.width / this.dataLen
- // Callculate inner size with margin
- let innerSize = size * 0.8
- // Width of each chart
- let bar_width = innerSize * 0.7 / barCount
+ // Iterate over each data point to draw the bars
+ for (let i = 0; i < this.dataLen; i++) {
+ // Counter to determine the position of each bar within a data point
+ let num = 0
- // Iterate over each data point to draw the bars
- for (let i = 0; i < this.dataLen; i++) {
- // Counter to determine the position of each bar within a data point
- let num = 0
+ this.data.forEach((categ, colId) => {
+ // Value of the bar
+ let value = categ.values[i]
+ // The left position of the bar in section
+ let left = this.zoomBounds.left + (size * (i + 0.15) + (innerSize * num / barCount))
+ // The height of the bar relative to the chart scale
+ let bar_height = value / this.extreme * this.scale * this.zoom.scaleY
+ // The top position of the bar
+ let top = this.zoomBounds.xAxis - categ.values[i] / this.extreme * this.scale * this.zoom.scaleY
- this.data.forEach((categ, colId) => {
- // Value of the bar
- let value = categ.values[i]
- // The left position of the bar in section
- let left = this.zoomBounds.left + (size * (i + 0.15) + (innerSize * num / barCount))
- // The height of the bar relative to the chart scale
- let bar_height = value / this.extreme * this.scale * this.zoom.scaleY
- // The top position of the bar
- let top = this.zoomBounds.xAxis - categ.values[i] / this.extreme * this.scale * this.zoom.scaleY
+ // Increment the bar count
+ num++
- // Increment the bar count
- num++
+ // Create a new Rectangle object representing the current bar
+ let newObject = new Rectangle(this.ctx, value, colId, left, top, bar_width, bar_height)
+ // Add the new Rectangle object to the list of objects
+ this.objects.push(newObject)
- // Create a new Rectangle object representing the current bar
- let newObject = new Rectangle(this.ctx, value, colId, left, top, bar_width, bar_height)
- // Add the new Rectangle object to the list of objects
- this.objects.push(newObject)
+ // Check if the center of the new bar is within the bounds
+ if (this.isInBounds(newObject.getCenter())) {
+ // Set the fill color to the category color
+ this.ctx.fillStyle = categ.color
+ // Set the line width to 0 to prevent drawing the border
+ this.ctx.lineWidth = 0
+ // Draw the filled rectangle representing the current bar
+ newObject.draw()
+ }
+ })
+ }
- // Check if the center of the new bar is within the bounds
- if (this.isInBounds(newObject.getCenter())) {
- // Set the fill color to the category color
- this.ctx.fillStyle = categ.color
- // Set the line width to 0 to prevent drawing the border
- this.ctx.lineWidth = 0
- // Draw the filled rectangle representing the current bar
- newObject.draw()
- }
- })
- }
+ // Draw x-axis labels if enabled
+ if (this.settings.displayAxisValues) {
+ // Restore canvas state to undo region clipping
+ this.ctx.restore()
+ // Loop through each data point to draw the labels
+ for (let i = 0; i < this.dataLen; i++) {
+ let text = (i + 1).toString()
- // Draw x-axis labels if enabled
- if (this.settings.displayAxisValues) {
- // Restore canvas state to undo region clipping
- this.ctx.restore()
- // Loop through each data point to draw the labels
- for (let i = 0; i < this.dataLen; i++) {
- let text = (i + 1).toString()
+ // Begin drawing the text
+ this.ctx.beginPath()
+ this.ctx.font = "16px Arial"
+ this.ctx.fillStyle = "black"
+ this.ctx.textAlign = "center"
- // Begin drawing the text
- this.ctx.beginPath()
- this.ctx.font = "16px Arial"
- this.ctx.fillStyle = "black"
- this.ctx.textAlign = "center"
+ // Calculate the position of the label
+ let x = this.zoomBounds.left + this.zoomBounds.width / this.dataLen * i + size / 2
+ let y = this.bounds.bottom + 15
- // Calculate the position of the label
- let x = this.zoomBounds.left + this.zoomBounds.width / this.dataLen * i + size / 2
- let y = this.bounds.bottom + 15
-
- // Draw the label text
- this.ctx.fillText(text, x, y)
- // Stroke the text
- this.ctx.stroke()
- // Close the path
- this.ctx.closePath()
+ // Draw the label text
+ this.ctx.fillText(text, x, y)
+ // Stroke the text
+ this.ctx.stroke()
+ // Close the path
+ this.ctx.closePath()
+ }
}
- }
+ }
+
+ // Draw the chart
+ if (this.settings.displayPoints)
+ async ? setTimeout(() => fn(), 0) : fn()
}
}
diff --git a/public/scripts/charts/chart.js b/public/scripts/charts/chart.js
index 9953955..9f42999 100644
--- a/public/scripts/charts/chart.js
+++ b/public/scripts/charts/chart.js
@@ -249,6 +249,9 @@ class Chart {
// Filter objects based on the column ID
let objects = chartLoader.chart.objects.filter(object => object.colId == index)
+ // Add selected shape
+ if (chartLoader.selectedShapeIndex !== null)
+ objects.push(this.objects[chartLoader.selectedShapeIndex])
// Set effect canvas opacity and draw effect
chartLoader.effectCanvas.style.opacity = 1
diff --git a/public/scripts/charts/point_chart.js b/public/scripts/charts/point_chart.js
index 8d947cc..1bd8160 100644
--- a/public/scripts/charts/point_chart.js
+++ b/public/scripts/charts/point_chart.js
@@ -44,9 +44,6 @@ class PointChart extends Chart {
this.ctx.strokeStyle = this.settings.pointBorderColor
}
- /*console.log(values)
- console.log(this.dataLen)*/
-
// Iterate over the values to plot each data point
for (let i = 0; i < this.dataLen; i++) {
@@ -76,19 +73,16 @@ class PointChart extends Chart {
/**
* Draws the entire PointChart
+ * @param {Boolean} async - Says if the chart should be drawn synchronously or asynchronously
*/
- draw() {
+ draw(async = true) {
// Clear the canvas and draw the axis
this.clear()
this.drawAxis()
// Draw the data points
+ let fn = () => this.data.forEach((categ, colId) => { this.drawPoints(categ.values, colId, categ.color) })
if (this.settings.displayPoints)
- setTimeout(() => {
- console.time("1")
- this.data.forEach((categ, colId) => { this.drawPoints(categ.values, colId, categ.color) })
- console.timeEnd("1")
- }, 0)
-
+ async ? setTimeout(() => fn(), 0) : fn()
}
}
diff --git a/public/scripts/charts/primitives.js b/public/scripts/charts/primitives.js
index c543e20..21430ac 100644
--- a/public/scripts/charts/primitives.js
+++ b/public/scripts/charts/primitives.js
@@ -74,7 +74,7 @@ class Rectangle extends Shape {
getCenter() {
return {
x: this.x + this.w / 2,
- y: 50
+ y: this.ctx.canvas.clientHeight / 2
}
}
diff --git a/public/scripts/charts/smooth_line_chart.js b/public/scripts/charts/smooth_line_chart.js
index 2d4456c..811e3d9 100644
--- a/public/scripts/charts/smooth_line_chart.js
+++ b/public/scripts/charts/smooth_line_chart.js
@@ -17,47 +17,54 @@ class SmoothLineChart extends PointChart {
/**
* Draws the smooth line chart on the canvas.
+ * @param {Boolean} async - Says if the chart should be drawn synchronously or asynchronously
*/
- draw() {
+ draw(async = true) {
// Clear the canvas and draw the axis
this.clear()
this.drawAxis()
- // Iterate over each category in the data
- this.data.forEach(categ => {
- // Begin a new path for drawing lines
- this.ctx.beginPath()
- this.ctx.lineJoin = "round"
- this.ctx.strokeStyle = categ.color
-
- // Calculate the starting point for the line
- let pos1 = this.getPointPos(0, categ.values[0])
- this.ctx.moveTo(pos1.x, pos1.y)
-
- // Draw the Bezier curve for the smooth line
- for (let i = 0; i < this.dataLen - 1; i++) {
- // Calculate left point coordinates
- let leftPos = this.getPointPos(i, categ.values[i])
-
- // Calculate right point coordinates
- let rightPos = this.getPointPos(i + 1, categ.values[i + 1])
-
- // Find middle point
- let xm = (leftPos.x + rightPos.x) / 2
- // Find quarter points
- let xl = (leftPos.x + xm) / 2
- let xr = (rightPos.x + xm) / 2
-
- // Draw a curve that smoothly connects the points
- this.ctx.bezierCurveTo(xl, leftPos.y, xr, rightPos.y, rightPos.x, rightPos.y);
- }
- this.ctx.stroke()
- this.ctx.closePath()
- })
-
- // Draw points on the chart if required
- if (this.settings.displayPoints)
- this.data.forEach((categ, colId) => {this.drawPoints(categ.values, colId, categ.color)})
+ // Define function for async
+ let fn = () => {
+ // Iterate over each category in the data
+ this.data.forEach(categ => {
+ // Begin a new path for drawing lines
+ this.ctx.beginPath()
+ this.ctx.lineJoin = "round"
+ this.ctx.strokeStyle = categ.color
+
+ // Calculate the starting point for the line
+ let pos1 = this.getPointPos(0, categ.values[0])
+ this.ctx.moveTo(pos1.x, pos1.y)
+
+ // Draw the Bezier curve for the smooth line
+ for (let i = 0; i < this.dataLen - 1; i++) {
+ // Calculate left point coordinates
+ let leftPos = this.getPointPos(i, categ.values[i])
+
+ // Calculate right point coordinates
+ let rightPos = this.getPointPos(i + 1, categ.values[i + 1])
+ // Find middle point
+ let xm = (leftPos.x + rightPos.x) / 2
+ // Find quarter points
+ let xl = (leftPos.x + xm) / 2
+ let xr = (rightPos.x + xm) / 2
+
+ // Draw a curve that smoothly connects the points
+ this.ctx.bezierCurveTo(xl, leftPos.y, xr, rightPos.y, rightPos.x, rightPos.y);
+ }
+ this.ctx.stroke()
+ this.ctx.closePath()
+ })
+
+ // Draw points on the chart if required
+ if (this.settings.displayPoints)
+ this.data.forEach((categ, colId) => { this.drawPoints(categ.values, colId, categ.color) })
+ }
+
+ // Draw the chart
+ if (this.settings.displayPoints)
+ async ? setTimeout(() => fn(), 0) : fn()
}
}
diff --git a/public/scripts/charts/stacked_chart.js b/public/scripts/charts/stacked_chart.js
index 8363b0f..d13d3c6 100644
--- a/public/scripts/charts/stacked_chart.js
+++ b/public/scripts/charts/stacked_chart.js
@@ -24,7 +24,7 @@ class StackedChart extends Chart {
for (let i = 0; i < this.dataLen; i++) {
let sum = 0
this.data.forEach(categ => {
- categ.values[i] = Math.abs(categ.values[i])
+ categ.values[i] = Math.abs(categ.values[i]) || 0
sum += categ.values[i]
})
largest = sum > largest ? sum : largest
@@ -47,7 +47,7 @@ class StackedChart extends Chart {
this.data.forEach((categ, colId) => {
// Value of the bar segment
- let value = categ.values[i]
+ let value = categ.values[i] || 0
// The height of the bar segment relative to the largest total value
let bar_height = value / largest * this.zoomBounds.height
// The left position of the bar segment
diff --git a/public/scripts/edit_chart.js b/public/scripts/edit_chart.js
old mode 100755
new mode 100644
index e5c58ef..23164e5
--- a/public/scripts/edit_chart.js
+++ b/public/scripts/edit_chart.js
@@ -1,153 +1,21 @@
-/**
- * Initializes variables and event listeners after the DOM content is loaded.
- */
-let canvas, parent, legend, dataDiv, table
-
-let rcTarget = {}
-
-$(document).ready( function () {
- canvas = document.getElementById("graphCanvas")
- parent = document.getElementById("graphDiv")
- legend = document.getElementById("graphLegend")
- dataDiv = document.getElementById("dataDiv")
- table = new Table(document.getElementById("dataTable"))
-
- document.getElementById('upload').addEventListener('change', handleFileSelect, false)
-
- load_data()
- table.reloadEvLi()
- reloadShares()
-
- //Click
- document.addEventListener('mousemove', (e) => {
- const pos = {
- x: e.clientX - canvas.offsetLeft,
- y: e.clientY - canvas.offsetTop
- }
- let obj = checkHit(pos)
-
- //show point value
- if (obj !== null) {
- dataDiv.style.left = pos.x + canvas.offsetLeft + "px"
- dataDiv.style.top = pos.y + canvas.offsetTop + "px"
- dataDiv.style.display = "block"
- dataDiv.innerHTML = "" + obj.name + "