javascript - d3js 图形在不正确的坐标处绘制

标签 javascript html svg d3.js

我试图绘制简单的 d3js 图表。我绘制了轴,甚至绘制了数据,但数据没有出现在预期的位置。

根据下面我的 json 数据

var d1 = [{
        value1: "30",
        value2: "10"
    }];

我正在尝试在 coordinates x axis 30 and y axis 10 处绘制一个圆但图表上的圆圈出现在其他地方。

这是 jsfiddle demo

这是我的代码

    var d1 = [{
            value1: "30",
            value2: "10"
        }];



function Update(){
   var circles = vis.selectAll("circle").data(d1)
    circles
        .enter()
            .insert("svg:circle")
                .attr("cx", function (d) { return d.value1; })
                .attr("cy", function (d) { return d.value2; })
                .style("fill", "red")
    circles
        .transition().duration(1000)
            .attr("cx", function (d) { return d.value1; })
            .attr("cy", function (d) { return d.value2; })
            .attr("r", function (d) { return 5; })

    circles.exit ()
        .transition().duration(1000)
            .attr("r", 0)
                .remove ();


}



/*************************************************/


/*******************Real Stuff starts here*******************/

var vis = d3.select("#visualisation"),
  WIDTH = 600,
  HEIGHT = 400,
  MARGINS = {
    top: 20,
    right: 20,
    bottom: 20,
    left: 50
  },
  xRange = d3.scale.linear().range([MARGINS.left, WIDTH - MARGINS.right]).domain([0,100]),
  yRange = d3.scale.linear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([0,300]),


  xAxis = d3.svg.axis() // generate an axis
       .scale(xRange) // set the range of the axis
       .tickSize(5) // height of the ticks
       .tickSubdivide(true), // display ticks between text labels
  yAxis = d3.svg.axis() // generate an axis
       .scale(yRange) // set the range of the axis
       .tickSize(5) // width of the ticks
       .orient("left") // have the text labels on the left hand side
       .tickSubdivide(true); // display ticks between text labels 

function init() {
  vis.append("svg:g") // add a container for the axis
  .attr("class", "x axis") // add some classes so we can style it
  .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")") // move it into position
  .call(xAxis); // finally, add the axis to the visualisation

  vis.append("svg:g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + (MARGINS.left) + ",0)")
    .call(yAxis);
}
init();





$('#btn').click(function(){
    Update();
});

最佳答案

如果你这样做,它就会起作用

  • 将数字定义为数字而不是字符串(即 value1: 30 而不是 value1: "30")并且
  • 使用您定义的比例(即 return xRange(d.value1) 而不是 return d.value1)。

工作 jsfiddle here .

关于javascript - d3js 图形在不正确的坐标处绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19187833/

相关文章:

javascript - if 语句中表达式的顺序

javascript - 单击 php 或 Javascript 时随机 URL 重定向

css - SVG 数据图像不能用作伪元素中的背景图像

svg - 如何制作带有边框的SVG“线”?

javascript - 打开页面时显示 jQuery Lightbox

javascript - 如何将 Prop 传递给循环内的样式化组件

javascript - 如何对并排放置的两个表进行排序,以便一个中的更改反射(reflect)在另一个中

PHP MySQL 按日期查询,递增/递减日期

javascript - 为什么 HTML DOM 属性也应该反射(reflect)到 HTML DOM 属性中?

java - 如何显示 SVG 图形的子部分或 "slice"?