简体   繁体   English

如何使用javascript对象在d3 v4中画线?

[英]How to draw a line in d3 v4 using javascript objects?

I have the following object 我有以下对象

var data =[
  {"steps":200,calories:200,distance:200,date:new Date(2012,09,1)},
  {"steps":200,calories:200,distance:200,date:new Date(2012,09,2)},
  {"steps":200,calories:200,distance:200,date:new Date(2012,09,3)},
  {"steps":200,calories:200,distance:200,date:new Date(2012,09,4)},
  {"steps":200,calories:200,distance:200,date:new Date(2012,09,5)},
]

I'd like to draw a graph between the steps and the date object in d3 v4 我想在d3 v4中的stepsdate对象之间绘制图形

I'm doing something like this to draw a line. 我正在做这样的事情来画一条线。 Here's the full code.. 这是完整的代码。

    var dataLine = [
  {"x":new Date(2012,0,1),"y":10},
  {"x":new Date(2012,0,2),"y":9},
  {"x":new Date(2012,0,3),"y":3},
  {"x":new Date(2012,0,4),"y":2}
];

var parseTime = d3.timeParse("%d-%b-%y");





var svgContainer = d3.select(".dsh-svg-element");

var MARGIN = {left:50,right:20,top:20,bottom:30};

var WIDTH  = 960 - (MARGIN.left + MARGIN.right);
var HEIGHT = 500 - (MARGIN.top + MARGIN.bottom);

svgContainer.attr("width",WIDTH+(MARGIN.left + MARGIN.right))
  .attr("height",HEIGHT+(MARGIN.top+MARGIN.bottom))
  .attr("transform","translate(" + MARGIN.left + "," + MARGIN.top + ")");

var xMax =100;
var yMax =100;

var x = d3.scaleTime().domain([new Date(2012,0,1), new Date(2012,0,31)]).range([0, WIDTH])
var y = d3.scaleLinear().domain([0,yMax]).range([HEIGHT,0]);

var xAxis = d3.axisBottom(x);
var yAxis = d3.axisLeft(y);

svgContainer.append("g").attr("transform", "translate(50," + (HEIGHT+MARGIN.top) + ")").call(xAxis)
svgContainer.append("g").attr("transform", "translate(50,20)").call(yAxis).attr("id","yAxis")




var lineFunction = d3.line().x(function(d){return x(d.y)}).y(function(d){return y(d.x)})




svgContainer.append("path")
.attr("d",lineFunction(dataLine))
.attr("stroke","blue").attr("stroke-width", 2).attr("fill", "none");

I checked the inspector, the x() and y() functions seem to be returning the right pixels to be drawn on the graph. 我检查了检查器,x()和y()函数似乎返回了要在图形上绘制的正确像素。

But the path of the line is "M-455079.8680521219,-5964102899550L-455079.86805246526,-5964491699550L-455079.86805452546,-5964880499550L-455079.8680548688,-5965269299550" in the inspector.. It seems to be drawing the line outside the svg element. 但是,该行的路径是检查器中的"M-455079.8680521219,-5964102899550L-455079.86805246526,-5964491699550L-455079.86805452546,-5964880499550L-455079.8680548688,-5965269299550" 。这似乎是在svg元素外部绘制了该线。 Any idea how to fix this? 任何想法如何解决这个问题?

Any tips or simple code on drawing a line are appreciated. 画线的任何技巧或简单的代码都值得赞赏。

Fiddle 小提琴

You have 2 main problems: 您有两个主要问题:

First, your x domain is completely unrelated to your data array. 首先,您的x域与您的数据数组完全无关。 Just use d3.extent to get the first and last date: 只需使用d3.extent即可获取第一个和最后一个日期:

var x = d3.scaleTime()
    .domain(d3.extent(dataLine, function(d) {
        return d.x
    }))
    .range([MARGIN.left, WIDTH])

Second, your line generator is wrong, you're using dx with the y scale and dy with the x scale. 其次,您的线路生成器是错误的,您正在使用y比例尺的dxx比例尺的dy It should be: 它应该是:

var lineFunction = d3.line()
    .x(function(d) {
        return x(d.x)
    }).y(function(d) {
        return y(d.y)
    })

Here is your updated fiddle: https://jsfiddle.net/mxsLdntg/ 这是您更新的小提琴: https : //jsfiddle.net/mxsLdntg/

Have in mind that the line in the fiddle is based on dataLine , not data . 请记住,小提琴中的行是基于dataLine而不是data You have to decide which data array you want to use and set the domains accordingly. 您必须确定要使用哪个数据阵列,并相应地设置域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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