简体   繁体   English

d3.v3散点图,所有圆的半径相同

[英]d3.v3 scatterplot with all circles the same radius

Every example I have found shows all of the scatter plot points to be of random radii. 我发现的每个例子都显示所有散点图都是随机半径。 Is it possible to have them all the same size? 有可能让它们大小相同吗? If I try to statically set the radius all of the circles will be very small (I'm assuming the default radius). 如果我尝试静态设置半径,则所有圆圈都将非常小(我假设默认半径)。 However, if I use Math.random() as in most examples there are circles large and small. 但是,如果我在大多数示例中使用Math.random(),则会有大小的圆圈。 I want all the circles to be large. 我希望所有的圈子都很大。 Is there a way to do that? 有没有办法做到这一点? Here's the code snippet forming the graph data using Math.random() (this works fine for some reason): 这是使用Math.random()形成图数据的代码片段(由于某种原因,这可以正常工作):

function scatterData(xData, yData) 
{
  var data = [];

  for (var i = 0; i < seismoNames.length; i++) 
  {
    data.push({
      key: seismoNames[i],
      values: []
    });

    var xVals=""+xData[i];
    xVals=xVals.split(",");
    var yVals=""+yData[i];
    yVals=yVals.split(",");

    for (var j = 0; j < xVals.length; j++) 
    {
      data[i].values.push({
        x: xVals[j], 
        y: yVals[j], 
        size: Math.random()
      });

    }
  }

  return data;
}

Math.random() spits out values between 0 and 1 such as 0.164259538891095 and 0.9842195005008699. Math.random()吐出0到1之间的值,例如0.164259538891095和0.9842195005008699。 I have tried putting these as static values in the 'size' attribute, but no matter what the circles are always really small. 我已经尝试将这些作为静态值放在'size'属性中,但不管圆圈总是很小。 Is there something I'm missing? 有什么我想念的吗?

Update: The NVD3 API has changed, and now uses pointSize , pointSizeDomain , etc. instead of just size . 更新:NVD3 API已经改变,现在使用pointSizepointSizeDomain等,而不仅仅是size The rest of the logic for exploring the current API without complete documentation still applies. 在没有完整文档的情况下探索当前API的其余逻辑仍然适用。


For NVD3 charts, the idea is that all adjustments you make can be done by calling methods on the chart function itself (or its public components) before calling that function to draw the chart in a specific container element. 对于NVD3图表,我们的想法是,在调用该函数以在特定容器元素中绘制图表之前,可以通过调用图表函数本身(或其公共组件)上的方法来完成所有调整。

For example, in the example you linked too, the chart function was initialized like this: 例如,在您链接的示例中,图表函数已初始化为:

 var chart = nv.models.scatterChart()
               .showDistX(true)
               .showDistY(true)
               .color(d3.scale.category10().range());

 chart.xAxis.tickFormat(d3.format('.02f'));
 chart.yAxis.tickFormat(d3.format('.02f'));

The .showDistX() and .showDistY() turn on the tick-mark distribution in the axes; .showDistX().showDistY()打开轴上的刻度分布; .color() sets the series of colours you want to use for the different categories. .color()设置要用于不同类别的一系列颜色。 The next too lines access the default axis objects within the chart and set the number format to be a two-digit decimal. 下一行也访问图表中的默认轴对象,并将数字格式设置为两位十进制。 You can play around with these options by clicking on the scatterplot option from the "Live Code" page . 您可以通过单击“实时代码”页面中的散点图选项来解决这些选项。

Unfortunately, the makers of the NVD3 charts don't have a complete documentation available yet describing all the other options you can set for each chart. 遗憾的是,NVD3图表的制造商没有完整的文档 ,但描述了您可以为每个图表设置的所有其他选项。 However, you can use the javascript itself to let you find out what methods are available. 但是,您可以使用javascript本身来找出可用的方法。

Inspecting a NVD3.js chart object to determine options 检查NVD3.js图表​​对象以确定选项

Open up a web page that loads the d3 and nvd3 library. 打开一个加载d3和nvd3库的网页。 The live code page on their website works fine. 他们网站上的实时代码页工作正常。 Then open up your developer's console command line (this will depend on your browser, search your help pages if you don't know how yet). 然后打开开发人员的控制台命令行(这取决于您的浏览器,如果您还不知道如何,请搜索您的帮助页面)。 Now, create a new nvd3 scatter chart function in memory: 现在,在内存中创建一个新的nvd3散点图函数:

var testChart = nv.models.scatterChart();

On my (Chrome) console, the console will then print out the entire contents of the function you just created. 在我的(Chrome)控制台上,控制台将打印出您刚刚创建的功能的全部内容。 It is interesting, but very long and difficult to interpret at a glance. 这很有趣,但很长很难以一目了然地解释。 And most of the code is encapsulated so you can't change it easily. 并且大多数代码都是封装的,因此您无法轻松更改它。 You want to know which properties you can change. 您想知道可以更改哪些属性。 So run this code in the next line of your console: 因此,在控制台的下一行中运行此代码:

for (keyname in testChart){console.log(keyname + " (" + typeof(testChart[keyname]) + ")");}

The console should now print out neatly the names of all the methods and objects that you can access from that chart function. 控制台现在应该整齐地打印出您可以从该图表功能访问的所有方法和对象的名称。 Some of these will have their own methods and objects you can access; 其中一些将有自己的方法和对象,您可以访问; discover what they are by running the same routine, but replacing the testChart with testChart.propertyName , like this: 通过运行相同的例程来发现它们是什么,但用testChart替换testChart.propertyName ,如下所示:

for (keyname in testChart.xAxis){console.log(keyname + " (" + typeof(testChart.xAxis[keyname]) + ")");}

Back to your problem. 回到你的问题。 The little routine I suggested above doesn't sort the property names in any order, but skimming through the list you should see three options that relate to size (which was the data variable that the examples were using to set radius) 我上面建议的小程序没有按任何顺序对属性名称进行排序,但是浏览列表时你应该看到三个与大小相关的选项(这是示例用来设置半径的数据变量)

  • size (function) 尺寸(功能)
  • sizeDomain (function) sizeDomain(function)
  • sizeRange (function) sizeRange(函数)

Domain and range are terms used by D3 scales , so that gives me a hint about what they do. 域和范围是D3标度使用的术语,因此这给了我一个关于它们的作用的提示。 Since you don't want to scale the dots, let's start by looking at just the size property. 既然你不想缩放点,那么让我们先看看size属性。 If you type the following in the console: 如果在控制台中键入以下内容:

testChart.size

It should print back the code for that function. 它应该打印该函数的代码。 It's not terribly informative for what we're interested in, but it does show me that NVD3 follows D3's getter/setter format: if you call .property(value) you set the property to that value, but if you call .property() without any parameters, it will return back the current value of that property. 它对于我们感兴趣的内容并不是非常有用,但它确实告诉我NVD3遵循D3的getter / setter格式:如果你调用.property(value)你将属性设置为该值,但是如果你调用.property()如果没有任何参数,它将返回该属性的当前值。

So to find out what the size property is by default, call the size() method with no parameters: 因此,要找出默认情况下size属性是什么,请调用size()方法,不带参数:

testChart.size()

It should print out function (d) { return d.size || 1} 它应该打印出function (d) { return d.size || 1} function (d) { return d.size || 1} , which tells us that the default value is a function that looks for a size property in the data, and if it doesn't exist returns the constant 1. More generally, it tells us that the value set by the size method determines how the chart gets the size value from the data. function (d) { return d.size || 1} ,它告诉我们默认值是一个在数据中查找size属性的函数,如果它不存在则返回常量1.更一般地说,它告诉我们size方法设置的值确定图表如何从数据中获取大小值。 The default should give a constant size if your data has no d.size property, but for good measure you should call chart.size(1); 如果您的数据没有d.size属性,则默认值应为常量,但为了更好地衡量,您应该调用chart.size(1); in your initialization code to tell the chart function not to bother trying to determine size from the data and just use a constant value. 在你的初始化代码中告诉图表函数不要费心去尝试从数据中确定大小并只使用一个常量值。

Going back to the live code scatterplot can test that out. 回到实时代码散点图可以测试出来。 Edit the code to add in the size call, like this: 编辑代码以添加大小调用,如下所示:

 var chart = nv.models.scatterChart()
               .showDistX(true)
               .showDistY(true)
               .color(d3.scale.category10().range())
               .size(1);

 chart.xAxis.tickFormat(d3.format('.02f'));
 chart.yAxis.tickFormat(d3.format('.02f'));

Adding that extra call successfully sets all the dots to the same size -- but that size is definitely not 1 pixel, so clearly there is some scaling going on. 添加额外的调用成功地将所有点设置为相同的大小 - 但该大小绝对不是1像素,所以显然有一些缩放。

First guess for getting bigger dots would be to change chart.size(1) to chart.size(100) . 获得更大点的第一个猜测是将chart.size(1)更改为chart.size(100) Nothing changes, however. 然而,没有任何改变。 The default scale is clearly calculating it's domain based on the data and then outputting to a standard range of sizes. 默认比例显然是根据数据计算其域,然后输出到标准范围的大小。 This is why you couldn't get big circles by setting the size value of every data element to 0.99, even if that would create a big circle when some of the data was 0.01 and some was 0.99. 这就是为什么你不能通过将每个数据元素的大小值设置为0.99来获得大圆圈,即使在某些数据为0.01且某些数据为0.99时会产生大圆圈。 Clearly, if you want to change the output size, you're going to have to set the .sizeRange() property on the chart, too. 显然,如果要更改输出大小,则还必须在图表上设置.sizeRange()属性。

Calling testChart.sizeRange() in the console to find out the default isn't very informative: the default value is null (nonexistent). 在控制台中调用testChart.sizeRange()来查找默认值并不是很有用:默认值为null(不存在)。 So I just made a guess that, same as the D3 linear scale .range() function, the expected input is a two-element array consisting of the max and min values. 所以我只是猜测,与D3线性比例.range()函数相同,预期输入是由max和min值组成的双元素数组。 Since we want a constant, the max and min will be the same. 因为我们想要一个常数,所以max和min将是相同的。 So in the live code I change: 所以在实时代码中我改变了:

.size(1);

to

.size(1).sizeRange([50,50]);

Now something's happening! 现在发生了一些事情! But the dots are still pretty small: definitely not 50 pixels in radius, it looks closer to 50 square pixels in area. 但是这些点仍然很小:绝对不是半径50像素,它看起来更接近50平方像素的面积。 Having size computed based on the area makes sense when sizing from the data, but that means that to set a constant size you'll need to figure out the approximate area you want: values up to 200 look alright on the example, but the value you choose will depend on the size of your graph and how close your data points are to each other. 在根据数据进行大小调整时,根据区域计算大小是有意义的,但这意味着要设置一个恒定大小,您需要找出所需的大致区域:最多200个值在示例中看起来没问题,但值您选择的内容取决于图表的大小以及数据点彼此之间的距离。

--ABR --ABR

PS I added the NVD3.js tag to your question; PS我在你的问题中添加了NVD3.js标签; be sure to use it as your main tag in the future when asking questions about the NVD3 chart functions. 在询问有关NVD3图表功能的问题时,请务必将其用作主标签。

The radius is measured in pixels. 半径以像素为单位测量。 If you set it to a value less than one, yes, you will have a very small circle. 如果将其设置为小于1的值,是的,您将拥有一个非常小的圆圈。 Most of the examples that use random numbers also use a scaling factor. 大多数使用随机数的示例也使用缩放因子。

If you want all the circles to have a constant radius you don't need to set the value in the data, just set it when you add the radius attribute. 如果希望所有圆都具有恒定的半径,则无需在数据中设置该值,只需在添加radius属性时进行设置即可。

Not sure which tutorials you were looking at, but start here: https://github.com/mbostock/d3/wiki/Tutorials 不确定你在看哪些教程,但从这里开始: https//github.com/mbostock/d3/wiki/Tutorials

The example "Three little circles" does a good step-by-step of the different things you can do with circles: http://mbostock.github.io/d3/tutorial/circle.html “三个小圈子”的例子可以很好地逐步完成你可以用圈子做的不同事情: http//mbostock.github.io/d3/tutorial/circle.html

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

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