Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:20:05+00:00 2026-06-16T04:20:05+00:00

I have my line graph, with 2 lines. Data to draw my line graph

  • 0

I have my line graph, with 2 lines.
Data to draw my line graph is pulled from a .csv file.

Can anyone explain how I could start off with an empty graph, and when I click a button,
my lines animate across the graph?

Thanks in advance!!

var button=d3.select(“#button”);

var margin = {top: 30, right: 20, bottom: 50, left: 60},
width = 700 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d-%b-%y").parse;


//treats value passed to it as a time/date
//OUTPUT RANGE
var x = d3.time.scale()
    .range([0, width]);

//OUTPUT RANGE  
var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis().scale(x)
    .orient("bottom")
    .ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left")
    .ticks(5);

//assigns coordinates for each piece of data    
var valueline = d3.svg.line()
    .interpolate("linear")
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });

//second line data
var valueline2 = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.open); });

//create area for 'area' below line 
var area = d3.svg.area()
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.close); });    

//creates area to draw graph    
var svg = d3.select("body")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
//groups content    
.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

function make_x_axis() {
    return d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .ticks(5)
}

function make_y_axis() {
    return d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(30)
}

// csv callback function
d3.csv("myData2.csv", function(data) {
    data.forEach(function(d) {
    d.date = parseDate(d.date);
    //+ operator sets any 'close' values to nuneric
    d.close = +d.close;
    d.open = +d.open;   

});

//INPUT DOMAINS
//.extent() returns min and max values of argument
x.domain(d3.extent(data, function(d) { return d.date; }));
//returns max of whichever set of data is bigger
y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open); })]);

//draws lines
//passes the valueline array to path object
svg.append("path") // Add the valueline path.
    .attr("class", "line")
    //adds dashed line
    .style("stroke-dasharray", ("5, 9")) // <== This line here!!
    .attr("d", valueline(data));


button.on("click", function() { 
    svg.append("path")      // Add the valueline2 path. 
        .attr("class", "line2")
        .transition()
        .attr("d", valueline2(data));
    })


svg.append("g") // Add the X Axis
    .attr("class", "x axis")
    //moves x axis to bottom of graph
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

//text label for x-axis 
svg.append("text") // text label for the x axis
    .attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom - 5 ) + ")")
    .style("text-anchor", "middle")
    .text("Date");

svg.append("g") // Add the Y Axis
    .attr("class", "y axis")
    .call(yAxis);

//text label for y-axis 
svg.append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 0 - margin.left)
    .attr("x",0 - (height / 2))
    //adds extra left padding as original y pos = 0
    .attr("dy", "1em")
    .style("text-anchor", "middle")
    .text("Value");


//adding a title to the graph
svg.append("text")
    .attr("x", (width / 2))
    .attr("y", 0 - (margin.top / 2))
    .attr("text-anchor", "middle")
    .style("font-size", "16px")
    .style("text-decoration", "underline")
    .text("Me Graph Larry");


//draw x axis grid  
svg.append("g")
    .attr("class", "grid")
    .attr("transform", "translate(0," + height + ")")
    .call(make_x_axis()
    .tickSize(-height, 0, 0)
    .tickFormat("")
)

//draw y axis grid
svg.append("g")
    .attr("class", "grid")
    .call(make_y_axis()
    .tickSize(-width, 0, 0)
    .tickFormat("")
)

});<!--d3.csv close-->

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:20:07+00:00Added an answer on June 16, 2026 at 4:20 am

    You need to setup a reasonable start value for the animation:

    var startvalueline2 = d3.svg.line()
        .x(function(d) { return x(d.date); })
        .y(function(d) { return y(0);      })
    
    button.on("click", function() { 
        svg.append("path")                     // Add the valueline2 path. 
            .attr("class", "line2")
            .attr("d", startvalueline2(data)); // set starting position
            .transition()
            .attr("d", valueline2(data));      // set end position
    });
    

    You might also have a look at Mike’s path transitions page. To see how to implement smooth (non-wobbly) animations when using svg:path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I have a line graph with categories, but plot the lines in
I want to Draw multiple line Graph with each line have different color and
I can get the line graph to be displayed using gRaphael, but I have
I have line: imagejpeg($this->image, $file, $quality); How can add on this image watermark.png?
I have data with a best fit line draw. I need to draw two
I'm trying to draw a simple line graph which would use values from the
How can I draw such a graph using Swing? I have used a JFreeChart
I have a basic line graph with two lines. When I click a button,
Ext-js Chart, I have a line graph, and I wish to add a single
I have a very simple line graph I'm trying to create in gRaphael. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.