-2
I’m trying to create a line chart based on the values entered by a user in the HTML input field, but I’m having trouble converting the SVG element commands to Javascript.
How can I convert? I need that when the user enters the value, it will appear in the graph dynamically.
function line() {
var svgElement = document.getElementById('svgCanvas');
let data = new Array();
data[0] = document.getElementById('data1').value;
data[1] = document.getElementById('data2').value;
data[2] = document.getElementById('data3').value;
data[3] = document.getElementById('data4').value;
data[4] = document.getElementById('data5').value;
data[5] = document.getElementById('data6').value;
let index=0;
let dx;
dx += 20;
var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d','M 100,0' +
'L' + dx + data[0] +
'L' + dx + data[1] +
'L' + dx + data[2] +
'L' + dx + data[3] +
'L' + dx + data[4] +
'L' + dx + data[5] +
'L' + dx + data[6]
);
path.setAttribute('stroke','red');
path.setAttribute('fill','none');
path.setAttribute('stroke-width', 5);
svgElement.appendChild(path);
}
If anyone can help me, I’d appreciate it enormously.