Home > Software design >  How to add a HistogramChart with the Google API Spread Sheet
How to add a HistogramChart with the Google API Spread Sheet

Time:06-28

I'm trying to create a Histogram Chart by following the enter image description here

And here the request I've started to realize:

request_chart = {
    'requests' : [
        {
            'addChart' : {
                'chart' : {
                    'spec' : {
                        'title' : 'En nombre',
                        'histogramChart' : {
                            "showItemDividers": False,
                            'legendPosition' : 'RIGHT_LEGEND',
                            'series' : [
                                {
                                    'data': {
                                        'sourceRange':{
                                            'sources': [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 1,
                                                    "endRowIndex": 7,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8,
                                                }
                                            ]
                                        }
                                    }
                                }
                            ]
                        }
                    },
                    'position' : {
                    'newSheet' : True
                    }
                }
            }
        }
    ]
}

service_sheets.spreadsheets().batchUpdate(spreadsheetId = id_fichier_historique, body = request_chart).execute()
return

Do you have any idea ?

CodePudding user response:

If you are looking to create the exact same chart from the picture, then the request should go like this:

'requests' : [
        {
            'addChart' : {
                'chart' : {
                    'spec' : {
                        'title' : 'Number',
                        'basicChart' : {
                            "chartType": "COLUMN",
                            'legendPosition' : 'RIGHT_LEGEND',
                            'domains' : [
                                {
                                    'domain' : {
                                        'sourceRange':{
                                            'sources': [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 0,
                                                    "endRowIndex": 1,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    }
                                }
                            ],
                            'series' : [
                                {
                                    "series": {
                                        "sourceRange": {
                                            "sources": [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 1,
                                                    "endRowIndex": 2,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    },
                                },
                                {
                                    "series": {
                                        "sourceRange": {
                                            "sources": [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 2,
                                                    "endRowIndex": 3,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    },
                                },
                                {
                                    "series": {
                                        "sourceRange": {
                                            "sources": [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 3,
                                                    "endRowIndex": 4,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    },
                                },
                                {
                                    "series": {
                                        "sourceRange": {
                                            "sources": [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 4,
                                                    "endRowIndex": 5,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    },
                                },
                                {
                                    "series": {
                                        "sourceRange": {
                                            "sources": [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 5,
                                                    "endRowIndex": 6,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    },
                                },
                                {
                                    "series": {
                                        "sourceRange": {
                                            "sources": [
                                                {
                                                    "sheetId": 0,
                                                    "startRowIndex": 6,
                                                    "endRowIndex": 7,
                                                    "startColumnIndex": 0,
                                                    "endColumnIndex": 8
                                                }
                                            ]
                                        }
                                    },
                                },
                            ],
                            "headerCount": 1,
                            "stackedType": "STACKED"
                        }
                    },
                    'position' : {
                    'newSheet' : True
                    }
                }
            }
        }
    ]

The error you were getting was because the length of the sourceRange sources should be 1 as stated in the enter image description here

References:

  • Related