Question

How do I remove and replace the line with another line in SVG javascript? I have...

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");
}
  

Homework Answers

Answer #1

//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>                

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
FOR MGMT 205 * I DO NOT WANT AN ANSWER THAT TALKS ABOUT TOBACCO OR BONDS**...
FOR MGMT 205 * I DO NOT WANT AN ANSWER THAT TALKS ABOUT TOBACCO OR BONDS** Your problem will have exactly two variables (an X1 and an X2) and will incorporate a maximization (either profit or revenue) objective. You will include at least four constraints (not including the X1 ? 0 and X2 ? 0 [i.e., the “Non-negativity” or “Duh!”] constraints). At least one of these four must be a “?” constraint, and at least one other must be a...
1.    In a multiple regression model, the following coefficients were obtained: b0 = -10      b1 =...
1.    In a multiple regression model, the following coefficients were obtained: b0 = -10      b1 = 4.5     b2 = -6.0 a.    Write the equation of the estimated multiple regression model. (3 pts) b     Suppose a sample of 25 observations produces this result, SSE = 480. What is the estimated standard error of the estimate? (5 pts) 2.    Consider the following estimated sample regression equation: Y = 12 + 6X1 -- 3 X2 Determine which of the following statements are true,...
Applications I Consider the following data representing the total time (in hours) a student spent on...
Applications I Consider the following data representing the total time (in hours) a student spent on reviewing for the Stat final exam and the actual score on the final. The sample of 10 students was taken from a class and the following answers were reported. time score 0 23 4 30 5 32 7 50 8 45 10 55 12 60 15 70 18 80 20 100 Part 1: Use the formulas provided on the 3rd formula sheet to compute...
Part I A firm that sells a dry skin cream exclusively through drug stores currently operates...
Part I A firm that sells a dry skin cream exclusively through drug stores currently operates in 15 marketing districts in the Midwest. As part of an expansion feasibility study, the company wants to model district sales (Y) as a function of target population (X1), per capita income (X2), and number of drug stores (X3) in the region. Data collected for each of the 15 districts were used to fit a first-order linear model. A summary of the regression results...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
Questions 1 through 6 work with the length of the sidereal year vs. distance from the...
Questions 1 through 6 work with the length of the sidereal year vs. distance from the sun. The table of data is shown below. Planet Distance from Sun (in millions of miles) Years (as a fraction of Earth years) ln(Dist) ln(Year) Mercury 36.19 0.2410 3.5889 -1.4229 Venus 67.63 0.6156 4.2140 -0.4851 Earth 93.50 1.0007 4.5380 0.0007 Mars 142.46 1.8821 4.9591 0.6324 Jupiter 486.46 11.8704 6.1871 2.4741 Saturn 893.38 29.4580 6.7950 3.3830 Uranus 1,794.37 84.0100 7.4924 4.4309 Neptune 2,815.19 164.7800 7.9428...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...