0
votes

Here's my data for charts. 1 row = 1 separate chart.I have a script that creates a line chart. See the screen of data (each row = 1 separate chart with horizontal axis containing months (header row))

I cannot add a horizontal axis with months so I could get nice lines on a graph.

Right now statistics points are shown as dots instead of lines.

I looked through many topics and Google documentation on it but it's confusing to me.

Here's how I create a chart:

 var chart = sheet.newChart()
        .setChartType(Charts.ChartType.LINE)
        .addRange(range) //pass a range with data from a sheet
        .setPosition(3, 3, 0, 0)
        .setOption("title", name)        
        .build();

 sheet.insertChart(chart);

So it ads data as .addRange but how can I add months as horizontal axes

I get ranges dinamically when looping throught sheet cells (i from 1 to last row). That is an example where I send first 2 rows as a range (addRange()) and get first row to use as horizontal axis (headers).

        var range = sheet.getRange(i, 3, 2, 12);          

        var months = sheet.getRange(1, 3, 1, 12)  

This method doesn't add horizontal axis:

.setOption("hAxis", months)
1
Try addRange(months).addRange(range) or addRange(months).addRange(range).setTransposeRowsAndColumns(true) - TheMaster
I tried this, and in the 1st case it added months to the chart as values, in the 2nd case it added properly but duplicated months so I got them 2 times. How can I share a screenshot with datasheet? in chat? - kiki
Why not add it to your question? Or in comments? If you're worried about privacy, Chat comments/links are also recorded in SO forever. Anyway, try var range = sheet.getRange(i, 3, 1, 12);(only one row) and/or addRange(months).addRange(range).setTransposeRowsAndColumns(true).setOption("useFirstColumnAsDomain", true) - TheMaster
this line works same (duplicates). Also I have vertical data. I know that manually I can create a chart from vertical data, I need to figure out how to fo that with code. Updated the post. - kiki
Is table set by default to have 10 horizontal labels at the most? I got maximum 10 records there with 25 months duplicated two times. I need 12 months there or less if I have less months. - kiki

1 Answers

0
votes

This line created correct horizontal data:

.addRange(range).setTransposeRowsAndColumns(true).setOption("useFirstColumnAsDomain", true)