简体   繁体   中英

Real time charts with D3.js

I am totally new to D3.js. I am trying to create a simple sparkline with time on the X axis and numbers upto 1000 on the Y axis. I have a web socket server which pushes random numbers unto 1000 to clients. I would like to plot a graph with these values on Y axis and time (in 24 hrs format) on the X axis. I tried a few things, but none of them work properly. My graph turns out vertical and is displayed only sometimes.

Appreciate any help in getting this working.

Below is what I have in my code, which is a tweaked version of an example I found online.

data[] is populated from the web socket call.

var margin = {top: 20, right: 20, bottom: 20, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .domain([0, data[data.length -1]])
    .range([0, width]);

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

var line = d3.svg.line()
    .x(function(d, i) { 
        var dt = new Date();
        var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
        return dt.getSeconds();
        })
    .y(function(d, i) { console.log("D is " + d+ "Y is " + y(d));return y(d); });

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

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

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + y(0) + ")")
    .call(d3.svg.axis().scale(x).orient("bottom"));

svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(0," + x(0)+")")
    .call(d3.svg.axis().scale(y).orient("left"));

var path = svg.append("g")
    .attr("clip-path", "url(#clip)")
  .append("path")
    .datum(data)
    .attr("class", "line")
    .attr("d", line);

redraw();

function redraw() {


  path
      .attr("d", line)
      .attr("transform", null)
    .transition()
      .duration(500)
      .ease("linear")
      .attr("transform", "translate(" + x(-1) + ",0)")
      .each("end", tick);

  data.shift();

}

Could someone point me to a beginner's tutorial to D3 which will help me get started with creating different types of graphs?

It may help to have the structure of data you are using.

If you want to go for Angular+D3js there are a few libraries out ther that will help you get the job done quickly.

An simple library is: http://angularjs-nvd3-directives.github.io/angularjs-nvd3-directives/

Another good set of directives which use D3.js is http://krispo.github.io/angular-nvd3/#/ The Home page has a lot of examples you can check. Although it lacks of documentation, every example has a plunker and you can check how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM