问题
I am using Google Apps Script to create a scatterplot with a trendline. I have taken an idea from Google Charts API on how to create the trendline, and this has been successful. But, the approach to formatting the trendline does not work as the Charts API describes. I have used setOption("trendlines", "0: {}") to create my trendline, but it does not matter what I put in between the curly brackets, the formatting that I add there doesn't seem to work. I would like the trendline to have smaller points than the actual, plotted points of the scatterplot, and I would like the trendline series to be black. If I can successfully format the trendline, then this would solve the problem of many high school science teachers who are trying to use Google Drive as a real alternative to Excel in their classes.
This is my code:
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);
}
回答1:
With the following code (sample data) I can make some changes, such as color, line width, among others.
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);
}
来源:https://stackoverflow.com/questions/18954127/formatting-trendline-using-setoption-in-google-apps-script