简体   繁体   English

Plotly.js 多个子图未按预期工作

[英]Plotly.js multiple subplots not working as expected

Edit to add: Looking for the "plotly.js" way to do this.编辑添加:寻找“plotly.js”的方式来做到这一点。 This "small multiple" visualization should have some "plotly.js" solution out there but haven't found it yet.这个“小倍数”可视化应该有一些“plotly.js”解决方案,但还没有找到。

I am using an array (example element below) to populate traces for plotly.js multiple subplots per their multiple-subplots example我正在使用一个数组(下面的示例元素)来填充traces每个子图示例的多个子图的plotly.js

[{
    "key": "Ontario|Toronto",
    "values": [{
        "key": "2020-01-25",
        "value": 1
    }, {
        "key": "2020-01-27",
        "value": 1
    }, {
        "key": "2020-05-12",
        "value": 218
    }, {
        "key": "2020-05-13",
        "value": 169
    }]
}, { 
    etc
}]

The array has 94 elements which contain info needed to create each trace.该数组有 94 个元素,其中包含创建每个跟踪所需的信息。 This would result in 94 subplots, one per trace.这将产生 94 个子图,每条迹线一个。 This plotly.js visualization could also be called "small multiples".这个plotly.js可视化也可以称为“小倍数”。

I am creating the traces dynamically and populating subplot definitions in a loop using code below:我正在使用以下代码动态创建traces并在循环中填充子图定义:

// create chart data
var traces = [];
var rowCount = (caseRegionByDate.length / 2).toFixed()

for (var i=0; i<caseRegionByDate.length; i++) {
    //console.log(caseRegionByDate[i]['key']);
    var trace = {};
    var x = [];
    var y = [];
    for (var j=0; j<caseRegionByDate[i]['values'].length; j++) {
        //console.log(caseRegionByDate[i]['values'][j]['key']);
        x.push(caseRegionByDate[i]['values'][j]['key']);
        y.push(caseRegionByDate[i]['values'][j]['value']);
    }
    // create trace i
    trace = {
    "x":x,
    "y":y,
    "xaxis":"x"+i,
    "yaxis":"y"+i,
    "type":"scatter"
    }
    // push trace to traces
    traces.push(trace);
}
console.log(JSON.stringify(traces));

var layout = {
    grid: {rows: rowCount, columns: 2, pattern: 'independent'},
    };

Plotly.newPlot('multiple_charts', traces, layout);

This creates the traces variable populated by each trace that looks like example below.这将创建由每个跟踪填充的traces变量,如下面的示例所示。 It looks correct:它看起来正确:

[{
    "x": ["2020-03-16", "2020-03-23", "2020-03-24", "2020-03-25", "2020-03-31", "2020-04-01", "2020-04-02", "2020-04-03", "2020-04-06", "2020-04-07", "2020-04-08", "2020-04-09", "2020-04-10", "2020-04-11", "2020-04-13", "2020-04-14", "2020-04-15", "2020-04-16", "2020-04-17", "2020-04-18", "2020-04-21", "2020-04-22", "2020-04-23", "2020-04-24", "2020-04-25", "2020-04-26", "2020-04-27", "2020-04-28", "2020-04-29", "2020-04-30", "2020-05-01", "2020-05-02", "2020-05-03", "2020-05-04", "2020-05-05", "2020-05-06", "2020-05-07", "2020-05-08", "2020-05-09", "2020-05-10", "2020-05-11", "2020-05-12", "2020-05-13"],
    "y": [1, 1, 1, 1, 9, 35, 3, 16, 33, 13, 9, 5, 5, 1, 22, 3, 4, 7, 19, 4, 7, 2, 18, 11, 9, 9, 9, 13, 1, 3, 7, 18, 5, 4, 4, 5, 3, 1, 2, 2, 3, 1, 2],
    "xaxis": "x0",
    "yaxis": "y0",
    "type": "scatter"
}, {
    "x": ["2020-03-14", "2020-03-26", "2020-03-27", "2020-04-02", "2020-04-06", "2020-04-09", "2020-04-14", "2020-04-17", "2020-04-18", "2020-04-20", "2020-04-22"],
    "y": [1, 1, 1, 2, 2, 3, 1, 1, 1, 2, 1],
    "xaxis": "x1",
    "yaxis": "y1",
    "type": "scatter"
},
    etc
]

However, the result appears to be one row with two columns that have all of the traces (there are 94 traces) squashed into them.但是,结果似乎是一行有两列,其中所有迹线(有 94 条迹线)都被压缩到其中。 Here is screenshot.这是屏幕截图。

在此处输入图像描述

Any ideas what is happening?任何想法发生了什么? I expect to have 48 rows with 2 columns, one subplot per trace.我希望有 48 行 2 列,每个跟踪一个子图。

The only difference from the multiple subplots example is that my xaxis have date strings instead of numbers.与多个子图示例的唯一区别是我的 xaxis 有日期字符串而不是数字。 Everything else is same.其他一切都是一样的。

The subplots are actually being created in 3 x rowCount grid.子图实际上是在 3 x rowCount 网格中创建的。 However they are all squashed vertically as in screenshot.然而,它们都被垂直压扁,如屏幕截图所示。

It appears that a chart's default height and width dimensions, where they are not explicitly defined using layout.height , are what is shown in my screenshot eg too small for 94 subplots.似乎图表的默认高度和宽度维度(未使用layout.height明确定义)是我的屏幕截图中显示的内容,例如对于 94 个子图来说太小了。

The quick fix is to simply increase the chart's layout.height size.快速解决方法是简单地增加图表的layout.height大小。 Then all subplots are visible.然后所有子图都是可见的。 Dynamically calculating layout.height , in spirit of Juan's suggestion, relative to number of rows works well.按照 Juan 的建议,相对于行数动态计算layout.height效果很好。

Apparently it is also possible to set each subplot's x and y domain attributes to resize subplots which will also give desired results.显然,也可以设置每个子图的 x 和 y 域属性来调整子图的大小,这也将给出所需的结果。

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

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