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 7966775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:31:16+00:00 2026-06-04T06:31:16+00:00

I have been playing with D3.js and I am trying to make a Streaming

  • 0

I have been playing with D3.js and I am trying to make a Streaming Graph visualization using real-time data.

I did study very much this article
http://bost.ocks.org/mike/path/

I also read this question and answers
Svg clip-path within rectangle does not work
but this is for a line. I do have a d3.svg.area object

Then I read this article
D3 Real-Time streamgraph (Graph Data Visualization)
but still couldn’t find any answers

My two questions:

  • when drawing a streaming graph, i noticed graph is always set at the bottom. Each refresh a calculating function found the maximum of data and set the bottom of the graph at the bottom of the element where you draw your graph.

    var area = d3.svg.area()
       .x(function(data) { return data.x * width / Xmaximum; })
       .y0(function(data) { return height - data.y0 * height / Ymaximum; })
       .y1(function(data) { return height - (data.y + data.y0) * height / Ymaximum; });
    

How can i do to let the graph in the middle of the axis?
i guess i have to play around with the x, y0 and y1 of area but how exactly?

  • if you try my code a part of the graph is shown at the left.
    i know there is something wrong with:

    vis.append("defs").append("clipPath")
       .attr("id", "clip")
      .append("rect")
       .attr("width", width)
       .attr("height", height);
    
     vis.append("g")
       .attr("clip-path", "url(#clip)");
    

i search for any response in others topics and all around the web but I am keep stuck here!

Here is my code:

<!DOCTYPE html>
<html>
<head>
    <title>Test visu</title>
    <style>
        .line {
            fill: none;
            stroke: #000;
            stroke-width: 1.5px;
        }
        .axis path, .axis line {
            margin-left: 30px;
            fill: none;
            stroke: #000;
            shape-rendering: crispEdges;
        }
        svg {
            font: 10px sans-serif;
        }

        #restart{
            visibility: hidden;
        }

        #visu{
            margin-left: 70px;
            //border: 5px solid black;
            width: 900px;
            height: 500px;
        }

    </style>
</head>

<body>
    <div id="visu">
        <script src="http://mbostock.github.com/d3/d3.js?2.7.2"></script>
            var CONS = 100;

            var layers_number = 4;
            var samples_number = 50;

            var margin = {top: 6, right: 0, bottom: 6, left: 40};
            var width = 900 - margin.right;
            var height = 500 - margin.top - margin.bottom;

            var Xmaximum = samples_number - 1;
            var Ymaximum = CONS * layers_number;

            var initial_table = new Array();

            for (var i = 0 ; i < layers_number ; ++i){
                initial_table[i] = new Array();
                for (var j = 0 ; j < samples_number ; ++j){
                    initial_table[i][j] = randomNber();
                }
            }

            var values = new Array();
            for (var i = 0 ; i < layers_number ; ++i)
                values[i] = new Array();

            var data0 = d3.layout.stack().offset("wiggle")(init_layers(layers_number,samples_number));

            function color (indice){
                if (indice == 0)
                    return "green";
                else if (indice == 1)
                    return "blue";
                else if (indice == 2)
                    return "red";
                else if (indice == 3)
                    return "black";
                return "black";
            }

            var y = d3.scale.linear()
                              .domain([0, Ymaximum])
                              .range([height, 0]);

            var area = d3.svg.area()
                .x(function(data) { return data.x * width / Xmaximum; })
                .y0(function(data) { return height - data.y0 * height / Ymaximum; })
                .y1(function(data) { return height - (data.y + data.y0) * height / Ymaximum; });

            var vis = d3.select("div")
                .append("svg")
                  .style("margin-left", -margin.left + "px")
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
                .append("g")
                  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

            vis.append("defs").append("clipPath")
                  .attr("id", "clip")
                .append("rect")
                  .attr("width", width)
                  .attr("height", height);

            vis.append("g")
                .attr("clip-path", "url(#clip)");

            vis.selectAll("path")
                .data(data0)
                .enter()
                .append("path")
                .style("fill", function(data, indice) { return color(indice); })
                .attr("d", area);

            vis.append("g")
                .attr("class", "y axis")
                  .call(d3.svg.axis().scale(y).ticks(5).orient("left"));

            transition();

            function transition() {
                data0 = d3.layout.stack().offset("wiggle")(update_layers(layers_number,samples_number));

                vis.selectAll("path")
                    .data(data0)
                    .attr("d", area)
                    .attr("transform", null)
                    .transition()
                    .duration(860)
                    .ease("linear")
                    .attr("transform", "translate(" + -(width / samples_number) + ")")
                    .each("end", function (data,indice) {
                        if (indice==0) transition();
                    });

                for (var i = 0 ; i < values.length ; ++i)
                    values[i].shift();
            }

            function init_layers(layers_number, samples_number) {
                return d3.range(layers_number).map(function(data,indice) {
                    for (var i = 0 ; i < samples_number ; i++)
                        values[indice][i] = initial_table[indice][i];
                    return values[indice].map(stream_index);
                });
            }

            function update_layers(layers_number, samples_number) {
                return d3.range(layers_number).map(function(data,indice) {
                    values[indice][samples_number] = randomNber();
                    return values[indice].map(stream_index);
                });
            }

            function stream_index(data, indice) {
                return {x: indice, y: Math.max(0, data)};
            }

            function randomNber(){
                return parseInt(Math.random()*100)+1;
            }   

    </div>
</body>
</html>
  • 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-04T06:31:17+00:00Added an answer on June 4, 2026 at 6:31 am

    To the question of centering the stream – use the offset feature of the stack layout:

    https://github.com/mbostock/d3/wiki/Stack-Layout#wiki-offset

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

Sidebar

Related Questions

I have been playing around with NSURLConnection. Now I'm trying to grab some data
I have been playing around with using graphs to analyze big data. Its been
I have been playing at work with some very very large sets of data,
I have been playing around with the MVP pattern using winforms for the last
I've been playing about with isotope a bit http://isotope.metafizzy.co/demos/relayout.html and have been trying to
I've been playing around with boost::spirit::qi lately and have been trying to write my
I have been playing about with a GUI today and trying add different elements
I have been playing around a bit with a fairly simple, home-made search engine,
I have been playing around with the Rhino ETL library and really like the
I have been playing around with GC.GetTotalMemory(). When I create a local variable of

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.