【问题标题】:Read google sheet json data into chartjs将google sheet json数据读入chartjs
【发布时间】:2020-05-27 17:30:25
【问题描述】:

我有一个简单的两列谷歌表,一列是标签,另一列是值。

我成功将其转换为json,并使用以下js在表中使用此类值

$.getJSON("https://spreadsheets.google.com/feeds/list/1GnakUnNQvFXjuzMSPnBpU9eufb4SooQLGL2oFc3lfAs/od6/public/values?alt=json", function (data) {

      var sheetData = data.feed.entry;

      var i;
      for (i = 0; i < sheetData.length; i++) {

        var names = data.feed.entry[i]['gsx$names']['$t'];
        var numbers = data.feed.entry[i]['gsx$numbers']['$t'];

        

      }
    });

并且想在雷达 chart.js 中使用它,我设法使它工作,但只能使用硬编码数据

var randomScalingFactor = function() {
  return Math.round(Math.random() * 100);
};
var chartColors = {
    red: 'rgba(253, 48, 76)',
    orange: 'rgb(255, 159, 64)',
    yellow: 'rgba(240,236,211)',
    green: 'rgb(75, 192, 192)',
    blue: 'rgba(42,105,163)',
    purple: 'rgb(153, 102, 255)',
    grey: 'rgba(48,48,50)'
};

var color = Chart.helpers.color;
var config = {
  type: 'radar',
  data: {
    labels: [
      "label1",
      "label2", "label3", "label3", "label4", "label5", "label6"
    ],
    datasets: [{
      label: "Current level",
      backgroundColor: color(chartColors.red).alpha(0.2).rgbString(),
      borderColor: chartColors.red,
      pointBackgroundColor: chartColors.red,
      data: [
        95,
        65,
        74,
        87,
        70,
        78,
        93
      ]
    }, {
      label: "Goal level",
      backgroundColor: color(chartColors.blue).alpha(0.2).rgbString(),
      borderColor: chartColors.blue,
      pointBackgroundColor: chartColors.blue,
      data: [
        95,
        80,
        85,
        90,
        89,
        87,
        95
      ]
    }, ]
  },
  options: {
    legend: {
      position: 'top',
      labels: {
        fontColor: 'white'
      }
    },
    title: {
      display: true,
      text: 'Curent level vs goal',
      fontColor: 'white'
    },
      maintainAspectRatio: false,
    scale: {
      ticks: {
        beginAtZero: true,
        fontColor: 'white', // labels such as 10, 20, etc
        showLabelBackdrop: false // hide square behind text
      },
      pointLabels: {
        fontColor: 'white' // labels around the edge like 'Running'
      },
      gridLines: {
        color: 'rgba(255, 255, 255, 0.2)'
      },
      angleLines: {
        color: 'white' // lines radiating from the center
      }
    }
  }
};

// A plugin to draw the background color
Chart.plugins.register({
  beforeDraw: function(chartInstance) {
    var ctx = chartInstance.chart.ctx;
    ctx.fillStyle = '#303032';
    ctx.fillRect(0, 0, chartInstance.chart.width, chartInstance.chart.height);
  }
})

window.myRadar = new Chart(document.getElementById("canvas"), config);

问题是我不能让它们一起工作,从某种意义上说,如果我添加一个新行(标签和值),它将更新图表,我试图用图表代码中的数据部分替换我创建的变量(名称和数字),但它不起作用。 另外,我打算将它与 d3 一起使用,而不仅仅是 chart.js,以及添加新列,它的工作方式是否相同?

【问题讨论】:

    标签: javascript google-sheets chart.js


    【解决方案1】:

    请注意jQuery.getJSON()是异步执行的。因此,只有在数据可用并处理为适合您图表的格式时,您才应该创建图表。如果您将负责图表创建的代码放在jQuery.getJSON() 成功处理程序(回调函数)中,则可以做到这一点,如下所示。

    $.getJSON("https://spreadsheets.google.com/feeds/list/1GnakUnNQvFXjuzMSPnBpU9eufb4SooQLGL2oFc3lfAs/od6/public/values?alt=json", data => {
      var labels = [];
      var numbers = [];
      data.feed.entry.forEach(e => {
        labels.push(e['gsx$names']['$t']);
        numbers.push(Number(e['gsx$numbers']['$t']));
      });
      new Chart(document.getElementById('myChart'), {
        type: 'radar',
        data: {
          labels: labels,
          datasets: [{
            label: 'Current level',
            data: numbers,
            backgroundColor: 'rgba(253, 48, 76, 0.2)',
            borderColor: 'rgb(253, 48, 76)',
            pointBackgroundColor: 'rgb(253, 48, 76)'
          }]
        },
        options: {
          tooltips: {
            callbacks: {
              title: (tooltipItem, data) => data.labels[tooltipItem[0].index]
            }
          }
        }
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
    <canvas id="myChart"></canvas>

    【讨论】:

    • 非常感谢,它成功了!我不知道它是异步执行的,大概是为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2022-06-13
    • 2016-12-20
    • 2019-12-21
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多