简体   繁体   English

如何使用 Tabletop.js 从 Google 表格中提取数据以绘制热图?

[英]How to pull data from Google sheets using Tabletop.js for a plotly heatmap chart?

I'm trying to plot a heatmap chart using plotly with data being pulled from Google sheets (as json) using Tabletop.js.我正在尝试使用使用 Tabletop.js 从 Google 表格(作为 json)提取数据的 plotly 绘制热图图表。 I have so far accomplished to pull data from Googleseet as json and display it in the console.到目前为止,我已经完成了从 Googleseet 中提取数据作为 json 并将其显示在控制台中。 But, getting it to display as a heatmap is proving cumbersome.但是,将其显示为热图证明很麻烦。 Any help would be greatly appreciated.任何帮助将不胜感激。

 var publicSpreadsheetUrl = 'https://docs.google.com/spreadsheets/d/1BFPjROVypCGNeLxhk5_PW6PoOb4FDzJsL3DEOEdW_Rc/edit?usp=sharing'; function init() { Tabletop.init({ key: publicSpreadsheetUrl, callback: showInfo, simpleSheet: true }) } function showInfo(data, tabletop) { console.log(data); } window.addEventListener('DOMContentLoaded', init) var data = [{ graphdata, type: 'heatmap' }]; Plotly.newPlot('myDiv', data1);

Plotly's heatmap needs a two dimensional array ( y being the first index, x the 2nd) for its z-values. Plotly 的热图需要一个二维数组( y是第一个索引, x是第二个索引)作为其 z 值。 tabletop returns a JSON, so it needs to be parsed into an array. tabletop返回一个 JSON,因此需要将其解析为一个数组。

 var publicSpreadsheetUrl = 'https://docs.google.com/spreadsheets/d/1BFPjROVypCGNeLxhk5_PW6PoOb4FDzJsL3DEOEdW_Rc/edit?usp=sharing'; function init() { Tabletop.init({ key: publicSpreadsheetUrl, callback: showPlot, simpleSheet: true }) } function showPlot(data, tabletop) { var xValues = []; //all the values which are shown on the x-axis var yValues = []; //all the values which are shown on the y-axis //get all possible x and y-values for (var i = 0; i < data.length; i++) { if (xValues.indexOf(data[i].x) === -1) { xValues.push(data[i].x); } if (yValues.indexOf(data[i].y) === -1) { yValues.push(data[i].y); } } //create an empty array for all possible z-values based on the dimensions of x and y var zValues = new Array(yValues.length).fill(0).map(row => new Array(xValues.length).fill(0)); var x = 0; var y = 0; for (i = 0; i < data.length; i++) { x = xValues.indexOf(data[i].x); y = yValues.indexOf(data[i].y); if (x !== -1 && y !== -1) { zValues[y][x] = parseFloat(data[i].z); } } //the data which is passed to Plotly var plotlyData = [{ x: xValues, y: yValues, z: zValues, type: 'heatmap' }]; //finally draw the plot Plotly.plot('myPlot', plotlyData); } init()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/tabletop.js/1.5.1/tabletop.min.js"></script> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <div id="myPlot"></div>

Please see my comments.请看我的评论。 I've described how you can convert the data to a needed format.我已经描述了如何将数据转换为所需的格式。 I used to used lodash in order to do that.我曾经使用 lodash 来做到这一点。

 var publicSpreadsheetUrl = 'https://docs.google.com/spreadsheets/d/1BFPjROVypCGNeLxhk5_PW6PoOb4FDzJsL3DEOEdW_Rc/edit?usp=sharing'; function init() { Tabletop.init({ key: publicSpreadsheetUrl, callback: showInfo, simpleSheet: true }) } function showInfo(data, tabletop) { if (!data || !data.length) { throw new Error('Unable to get any data from the spreadsheet'); } // First we want to parse some values var parsed = data.map(val => { return {month: val.x, year: parseInt(val.y), z: parseFloat(val.z)}; }); // I'm going to hardcode the months here as we wanna keep the order. // You might consider using something like moment.js to get month names. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var minYear = _.minBy(parsed, p => p.year); var maxYear = _.maxBy(parsed, p => p.year); // Create the range of years. This is going to be our Y axis. var years = _.range(minYear.year, maxYear.year); // Now we are going to iterate over months and years, searching for values. // By doing this we're going to end up with the data format accepted by plotly. // https://plot.ly/javascript/heatmaps/#heatmap-with-categorical-axis-labels var zValues = _.map(months, m => { return _.map(years, y => { var matchingElement = _.find(parsed, p => p.year === y && p.month === m); if (matchingElement) { return matchingElement.z; } // If we haven't found the value fill it with null. return null; }) }) var chartData = [{ z: zValues, x: months, y: years, type: 'heatmap' }]; Plotly.newPlot('myDiv', chartData); } window.addEventListener('DOMContentLoaded', init)
 <div id="myDiv"></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/tabletop.js/1.5.1/tabletop.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js'></script> <!-- Latest compiled and minified plotly.js JavaScript --> <script type="text/javascript" src="https://cdn.plot.ly/plotly-latest.min.js"></script>

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

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