1

我正在使用Google Apps腳本創建帶有趨勢線的散點圖。 I have taken an idea from Google Charts API on how to create the trendline,這已成功。但是,格式化趨勢線的方法不適用於圖表API描述。我已經使用setOption(「trendlines」,「0:{}」)來創建我的趨勢線,但無論我在大括號之間放置什麼,我添加的格式似乎都不起作用。我希望趨勢線的點數小於散點圖的實際繪製點數,我希望趨勢線系列爲黑色。如果我能成功地設定趨勢線的格式,那麼這將解決很多高中科學教師嘗試將Google Drive作爲Excel類別的真正替代品的問題。在谷歌應用程序腳本中使用setoption()格式化趨勢線

這是我的代碼:

function scatterMe(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[0]; 

    var chart = sheet.newChart().asScatterChart() 
    .addRange(sheet.getRange("A1:B500")) 
    .setPosition(5, 5, 0, 0) 

    //this is the code that builds the trendline... the example CSS code at 
    //https://developers.google.com/chart/interactive/docs/gallery/trendlines 
    //does not seem to have any effect when I add it in between the curly braces. 
    .setOption('trendlines', '0: {}') 

    .build(); 

    sheet.insertChart(chart); 
} 

回答

2

用下面的代碼(樣本數據)我可以進行一些變化,如顏色,線條寬度,等等。

function scatterMe() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[2]; 

    var trendlinesopt = { 
    0: { 
     color: 'black', 
     lineWidth: 6, 
     opacity: 0.2, 
     labelInLegend: 'Test data', 
     visibleInLegend: true 
    } 
    }; 
    var chart = sheet.newChart().asScatterChart() 
    .addRange(sheet.getRange("A1:B12")) 
    .setPosition(3, 4, 0, 0) 

    //this is the code that builds the trendline... the example CSS code at 
    //https://developers.google.com/chart/interactive/docs/gallery/trendlines 
    //does not seem to have any effect when I add it in between the curly braces. 
    .setOption('trendlines', trendlinesopt) 

    .build(); 

    sheet.insertChart(chart); 
} 

enter image description here

+1

@Jenn,在此澄清,不同的是wchiquito被添加選項作爲對象而不是字符串。添加像這樣的選項'.setOption('trendlines',{0:{}})'使其工作。 – asgallant

+0

我想標記這個答案正是我所需要的,但我需要15個聲望。所以,從長遠來看:這是完全正確的,我很欣賞你花時間回答這個問題的方式!會有很多感恩的科學教師。 – Jenn

+0

@Jenn,很高興幫助。 – wchiquito