How do I remove and replace the line with another line in SVG javascript? I have a regression line function that displays the regression line for a given region. However, when I select another region, I want to remove the existing line and display the new line. Here is my code.
if (_subset != "United States" ) {
let filtered = data_subset.filter(obj => obj.region ==
_subset);
let pov_array = [];
let life_array = [];
let result = [];
pov_array = filtered.map(function (d) { return d.poverty_rate;
});
life_array = filtered.map(function (d) { return d.life_expectancy;
});
for (let i = 0; i < pov_array.length; i++) {
result[i] = [pov_array[i], life_array[i]];
}
let linear =
ss.linearRegressionLine(ss.linearRegression(result));
let yValue1 = linear(0);
let yValue2 = linear(30);
let y_value1 = y(yValue1);
let y_value2 = y(yValue2);
svg.append("line")
.attr("x1", 40)
.attr("x2", 460)
.attr("y1", y_value1)
.attr("y2", y_value2)
.attr('stroke-width', 2)
.attr("stroke", "black")
.attr("stroke-dasharray", "5,5");
} else {
let pov_array = [];
let life_array = [];
let result = [];
pov_array = data_subset.map(function (d) { return d.poverty_rate;
});
life_array = data_subset.map(function (d) { return
d.life_expectancy; });
for (let i = 0; i < pov_array.length; i++) {
result[i] = [pov_array[i], life_array[i]];
}
let linear =
ss.linearRegressionLine(ss.linearRegression(result));
let yValue1 = linear(0);
let yValue2 = linear(30);
let y_value1 = y(yValue1);
let y_value2 = y(yValue2);
svg.append("line")
.attr("x1", 40)
.attr("x2", 460)
.attr("y1", y_value1)
.attr("y2", y_value2)
.attr('stroke-width', 2)
.attr("stroke", "black")
.attr("stroke-dasharray", "5,5");
}
//CODE SNIPPET
var
newstr =
""
;
for
(
var
i = 0; i
< str.length; i++ )
if
(
!(str[i] ==
'\n'
|| str[i] ==
'\r'
) )
newstr
+= str[i];
//OR
function
remove_linebreaks(
var
str ) {
return
str.replace( /[\r\n]+/gm,
""
);
}
//EXAMPLE
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
Remove
all line breaks from
a
string using JavaScript
</
title
>
<
script
>
// Method
1
// Slice and
Stitch
function
remove_linebreaks_ss( str ) {
var
newstr = "";
for(
var i = 0; i <
str.length
; i++
)
if(
!(str[i] == '\n' || str[i] == '\r') )
newstr
+= str[i];
return
newstr;
}
// Method
2
// Regular
Expression
function
remove_linebreaks( str ) {
return
str.replace( /[\r\n]+/gm, "" );
}
function
removeNewLines() {
var
sample_str
=
document
.getElementById('raw-text').value;
console.time();
//
For printing time taken on console.
document.getElementById('res-1')
.innerHTML
=
remove_linebreaks_ss
( sample_str );
console.timeEnd();
console.time();
document.getElementById('res-2')
.innerHTML
=
remove_linebreaks
( sample_str);
console.timeEnd();
}
</script>
</
head
>
<
body
>
<
center
>
<
textarea
id
=
"raw-text"
></
textarea
>
<
br
>
<
button
onclick
=
"removeNewLines()"
>
Remove
Newlines
</
button
>
<
h6
>Method
1:</
h6
>
<
p
id
=
'res-1'
></
p
>
<
h6
>Method
2:</
h6
>
<
p
id
=
'res-2'
></
p
>
</
center
>
</
body
>
</
html
>
Get Answers For Free
Most questions answered within 1 hours.