简体   繁体   English

增加d3散点图的大小

[英]Increasing size of d3 scatterplot

I have a scatter plot made in d3.v3 and no matter how large i increase the width and height variables it does not take up more screen space. 我在d3.v3中有一个散点图,无论我增加多大的宽度和高度变量,它都不会占用更多的屏幕空间。

var w = 700;
        var h = 700;

        var dataset = [
                        "Over 50 pairs of coordinates that look like [0,1][0,43],
                      ];

        //Create SVG element
        var svg = d3.select("body")
                    .append("svg")
                    .attr("width", w)
                    .attr("height", h);

        svg.selectAll("rect")
           .data(dataset)
           .enter()
           .append("rect")
           .attr("x", function(d) {
                return d[0];
           })
           .attr("y", function(d) {
                return d[1];
           })
           .attr("width",5)
           .attr("height",5);

There are more than 50 coordinates in my dataset and i want to be able to display them well so that is why i want this to take up more screen space. 我的数据集中有50多个坐标,我希望能够很好地显示它们,所以这就是为什么我要占用更多的屏幕空间。 Currently there is nothing in my html, and no css for this. 目前,我的html中没有任何内容,为此也没有CSS。 How can i adjust this so that the scatter plot takes more screen space? 我该如何调整以使散点图占用更多的屏幕空间?

The code you show doesn't place data points with any consideration of width or height, it places data points based on the values in the data: 您显示的代码不会放置任何考虑宽度或高度的数据点,而是根据数据中的值放置数据点:

 .attr("x", function(d) {
     return d[0];             
 })
 .attr("y", function(d) {
     return d[1];
 })

The x and y attributes, without any SVG transformation, expect pixel values. 在不进行任何SVG转换的情况下, xy属性需要像素值。 If a point has the datum [25,25] it will be placed 25 pixels from the top and left. 如果一个点的原点[25,25] ,则将其从顶部和左侧放置25个像素。 Height and width of the svg do not matter - you are stating the pixel position based on the data, not based on the svg dimensions in combination with the data. svg的高度和宽度无关紧要-您是基于数据而不是基于svg尺寸以及数据来说明像素位置。

Scales are a fundamental part of d3 - you can scale the x and y values of your data points across a range of pixel values. 缩放比例是d3的基本组成部分-您可以在一系列像素值范围内缩放数据点的x和y值。 To do this we need to know the domain of the input data - the extent of input values - and the range of the output values - the extent of output values. 为此,我们需要知道输入数据的范围-输入值的范围-和输出值的范围-输出值的范围。

There are a number of scales built into D3, including power, logarithmic and linear. D3中内置了许多标度,包括幂,对数和线性。 I'll demonstrate a linear scale below, but the form is very similar for other continuous scales, such as those noted above. 我将在下面演示线性比例尺,但是其他连续比例尺的形式非常相似,例如上面提到的那些。

var xScale = d3.scaleLinear()  // create a new linear scale
  .domain([0,50])              // map values from 0 to 50 to:
  .range([0,width])            // to pixel values of 0 through width

var yScale = d3.scaleLinear()  // create a new linear scale
  .domain([0,50])              // map values from 0 to 50 to:
  .range([height,0])           // to pixel values of height through 0

Since in SVG coordinate space y=0 is the top of the SVG, and normally we want data with y=0 to be displayed at the bottom of the graph, we use a range of [height,0] rather than [0,height] 因为在SVG坐标空间中y = 0是SVG的顶部,并且通常我们希望y = 0的数据显示在图形的底部,所以我们使用[height,0]而不是[0,height]

With the scales set up, to return the pixel value for a given data value we use: 设置好比例尺后,为了返回给定数据值的像素值,我们使用:

xScale(d[0]); // assuming d[0] holds the x value

or 要么

yScale(d[1]); // assuming d[1] holds the y value

Together this gives us: 这些共同给我们:

 var w = 700; var h = 700; var dataset = d3.range(50).map(function(d) { return [Math.random()*50,Math.random()*50]; }) var x = d3.scaleLinear() .domain([0,50]) // input extent .range([0,w]) // output extent var y = d3.scaleLinear() .domain([0,50]) .range([h,0]) //Create SVG element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); svg.selectAll("rect") .data(dataset) .enter() .append("rect") .attr("x", function(d) { return x(d[0]); }) .attr("y", function(d) { return y(d[1]); }) .attr("width",5) .attr("height",5); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script> 


Of course we might not know the domain of the data if its dynamic, so d3 has a few built in helpers including d3.min, d3.max, d3.extent. 当然,如果它是动态的,我们可能不知道数据的域,因此d3内置了一些帮助器,包括d3.min,d3.max,d3.extent。

d3.min and d3.max iterate through an array to find the minimum and maximum values of some property of each item in the data array: d3.min和d3.max遍历数组以查找数据数组中每个项目的某些属性的最小值和最大值:

 d3.min(dataArray, function(d) {
     return d.property;
 })

 d3.max(dataArray, function(d) {
     return d.property;
 })

D3.extent does both at the same time returning an array containing min and max: D3.extent同时执行两个操作,返回一个包含min和max的数组:

 d3.extent(dataArray, function(d) {
     return d.property;
 })

We can plug those into the scales too: 我们也可以将它们插入秤中:

 var w = 700; var h = 700; var dataset = d3.range(50).map(function(d) { return [Math.random()*50,Math.random()*50]; }) var x = d3.scaleLinear() .domain([0,d3.max(dataset, function(d) { return d[0]; }) ]) // input extent .range([0,w]) // output extent var y = d3.scaleLinear() .domain( d3.extent(dataset, function(d) { return d[1]; }) ) .range([h,0]) //Create SVG element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); svg.selectAll("rect") .data(dataset) .enter() .append("rect") .attr("x", function(d) { return x(d[0]); }) .attr("y", function(d) { return y(d[1]); }) .attr("width",5) .attr("height",5); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script> 

The below works perfectly fine for me. 以下对我来说很好用。 The things you might need to check is : 您可能需要检查的内容是:

  1. The coordinates range is more than 5. (or you might need to use scale and axis) 坐标范围大于5。(或者您可能需要使用比例尺和轴)
  2. Is there a overriding styles to your g 您的g是否有最主要的样式

 var w = 700; var h = 700; var dataset = [[10,10],[10,30],[30,50],[30,70]]; //Create SVG element var svg = d3.select("#scatterplot") .append("svg") .attr("width", w) .attr("height", h); svg.selectAll("rect") .data(dataset) .enter() .append("rect") .attr("x", function(d) { return d[0]; }) .attr("y", function(d) { return d[1]; }) .attr("width",5) .attr("height",5); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <div id='scatterplot'></div> 

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

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