简体   繁体   English

Google脚本中的Amcharts(通过电子表格)

[英]Amcharts in Google script (via Spreadsheet)

Background - I have a spreadsheet with some data in it, I want to prepare and present some "dynamic charts" from this data, what I have thought is create some charts in script editor - HTML and then in the .gs code call this HTML file with a doGet function (after publishing - will be used by Internal members only) 背景-我有一个包含一些数据的电子表格,我想根据这些数据准备并显示一些“动态图表”,我认为是在脚本编辑器-HTML中创建一些图表,然后在.gs代码中将其称为HTML具有doGet函数的文件(发布后-仅供内部成员使用)

A similar example of chart can be viewed here but when I add the HTML code to the HTML page and Javascript code as with script tags in the HTML page, nothing is displayed in the browser. 可以在此处查看图表的类似示例,但是当我将HTML代码添加到HTML页面和Javascript代码(与HTML页面中的脚本标签一样)时,浏览器中什么也没有显示。

How can I implment this chart (or other with similar type) within google doGet function. 我如何在Google doGet函数中实现此图表(或其他类似类型的图表)。

Code

<script src="http://www.amcharts.com/lib/amcharts.js" type="text/javascript"></script>
<div id="chartdiv" style="width: 100%; height: 362px;"></div>
var lineChartData = [{
    date: new Date(2009, 10, 2),
    value: 5},
{
    date: new Date(2009, 10, 3),
    value: 15},
{
    date: new Date(2009, 10, 4),
    value: 13},
{
    date: new Date(2009, 10, 5),
    value: 17},

{
    date: new Date(2009, 11, 4),
    value: 26}];

AmCharts.ready(function() {
    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = lineChartData;
    chart.pathToImages = "http://www.amcharts.com/lib/images/";
    chart.categoryField = "date";

// sometimes we need to set margins manually
// autoMargins should be set to false in order chart to use custom margin values
    chart.autoMargins = false;
    chart.marginRight = 0;
    chart.marginLeft = 0;
    chart.marginBottom = 22;
    chart.marginTop = 0;

// AXES
// category                
var categoryAxis = chart.categoryAxis;
categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
categoryAxis.minPeriod = "DD"; // our data is daily, so we set minPeriod to DD
categoryAxis.gridAlpha = 0;
categoryAxis.tickLength = 0;
categoryAxis.axisAlpha = 0;

// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.dashLength = 4;
valueAxis.axisAlpha = 0;
chart.addValueAxis(valueAxis);

// GRAPH
var graph = new AmCharts.AmGraph();
graph.type = "line";
graph.valueField = "value";
graph.lineColor = "#D8E63C";
graph.customBullet = "http://www.amcharts.com/lib/images/star.gif"; // bullet for all          data points
graph.bulletSize = 14; // bullet image should be a rectangle (width = height)
graph.customBulletField = "customBullet"; // this will make the graph to display custom      bullet (red star)
chart.addGraph(graph);

// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chart.addChartCursor(chartCursor);

// WRITE
chart.write("chartdiv");
});

Apologies, if I am not able to explain it properly. 抱歉,如果我无法正确解释。 I am still a newbie in this... 我仍然是这方面的新手...

(Note:- have deleted some part of actual code to make it short) (注意:-已删除部分实际代码以使其简短)

If I understood your question right, you need to use the HTML Service to display that code. 如果我正确理解您的问题,则需要使用HTML服务来显示该代码。 Basically, your Apps Script project will have two files, one Code.gs and an html file. 基本上,您的Apps Script项目将包含两个文件,一个是Code.gs,另一个是html文件。 Then, use the following in Code.gs, assuming the file with the HTML is called "index.html". 然后,在Code.gs中使用以下内容,假设带有HTML的文件称为“ index.html”。

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

Good luck using AmCharts, though - I've never tried that one. 不过,使用AmCharts祝您好运-我从未尝试过这样做。 However, every other charts library I've tried has failed in HTML Service except for Piety. 但是,除Piety之外,我尝试过的所有其他图表库都在HTML Service中失败了。 The Caja sanitizer prevents a lot of stuff from running. Caja消毒剂可防止很多东西运行。

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

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