javascriptgoogle-visualization

Trendline On Google Line Chart


I am trying to figure out how to add a trendline to my google line chart but am failing miserably. Is anyone able to point me in the right direction? I have seen that google do not provide a way to do this on a line chart but have also seen that it is possible to add a new series into the chart to achieve something similar. Here is what I have so far which works great besides not having a trendline. I have tried a few suggestions I found online but none of them made the maths clear to get the trendline to be a straight line:

function drawChartCustomerRevenue_93() {
    var revenueChart_93;
    var data = new google.visualization.DataTable();
    data.addColumn('string', Date);
    data.addColumn('number', 'Sales Value');
    data.addRow(['Apr 2013', 271176.940000000]);
    data.addRow(['May 2013', 419031.540000000]);
    data.addRow(['Jun 2013', 429155.540000000]);
    data.addRow(['Jul 2013', 443780.400000000]);
    data.addRow(['Aug 2013', 320353.540000000]);
    data.addRow(['Sep 2013', 310640.910000000]);
    data.addRow(['Oct 2013', 252187.700000000]);
    data.addRow(['Nov 2013', 301414.560000000]);
    data.addRow(['Dec 2013', 224296.850000000]);
    data.addRow(['Jan 2014', 291131.140000000]);
    data.addRow(['Feb 2014', 354750.680000000]);
    data.addRow(['Mar 2014', 312736.710000000]);
    var formatter = new google.visualization.NumberFormat({
        fractionDigits: 2,
        prefix: '£'
    });
    formatter.format(data, 1);
    var chartHeight = 400;
    var chartWidth = 500;
    var chartOptions = {
        trendlines: {
            {
                color: 'green',
            }
        },
        title: '1: TEST NAME',
        isStacked: true,
        width: chartWidth,
        height: chartHeight,
        colors: ['#0000D4'],
        hAxis: {
            title: 'Sales Value (£)',
            slantedText: true,
            slantedTextAngle: 45,
            textStyle: {
                fontSize: 10
            },
        },
        chartArea: {
            left: 100,
            top: 20,
            width: (chartWidth - 10),
            height: (chartHeight - 90)
        }
    };
    revenueChart_93 = new google.visualization.LineChart(document.getElementById('CustomerRevenue_93'));
    revenueChart_93.draw(data, chartOptions);
};
drawChartCustomerRevenue_93();

EDIT:

I have now attempted to put the code in suggested to me but it still shows no trendline:

function drawChartCustomerRevenue_93() {
    var revenueChart_93;
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Date');
    data.addColumn('number', 'Sales Value');
    data.addRow([new Date(2013, 4, 1), 184656.440000000]);
    data.addRow([new Date(2013, 5, 1), 420286.250000000]);
    data.addRow([new Date(2013, 6, 1), 431562.840000000]);
    data.addRow([new Date(2013, 7, 1), 446187.800000000]);
    data.addRow([new Date(2013, 8, 1), 320353.540000000]);
    data.addRow([new Date(2013, 9, 1), 313364.960000000]);
    data.addRow([new Date(2013, 10, 1), 252187.700000000]);
    data.addRow([new Date(2013, 11, 1), 303874.560000000]);
    data.addRow([new Date(2013, 12, 1), 224296.850000000]);
    data.addRow([new Date(2014, 1, 1), 297499.970000000]);
    data.addRow([new Date(2014, 2, 1), 354950.680000000]);
    data.addRow([new Date(2014, 3, 1), 315529.610000000]);
    data.addRow([new Date(2014, 4, 1), 145219.710000000]);
    var formatter = new google.visualization.NumberFormat({
        fractionDigits: 2,
        prefix: '£'
    });
    formatter.format(data, 1);
    var dateFormatter = new google.visualization.NumberFormat({
        pattern: 'MMM yyyy'
    });
    dateFormatter.format(data, 0);
    var chartHeight = 320;
    var chartWidth = 400;
    var chartOptions = {
        trendlines: {
            0: {
                color: 'green'
            }
        },
        title: '1: Test Name',
        isStacked: true,
        width: chartWidth,
        height: chartHeight,
        colors: ['#0000D4'],
        hAxis: {
            title: 'Sales Value (£)',
            slantedText: true,
            slantedTextAngle: 45,
            textStyle: {
                fontSize: 10
            },
            format: 'MMM yyyy',
        },
        chartArea: {
            left: 100,
            top: 20,
            width: (chartWidth - 8),
            height: (chartHeight - 72)
        }
    };
    revenueChart_93 = new google.visualization.LineChart(document.getElementById('CustomerRevenue_93'));
    revenueChart_93.draw(data, chartOptions);
};
drawChartCustomerRevenue_93();

Solution

  • First, as @mcgyver5 pointed out, trendlines are not supported for discrete (string-based) axes. Since your data is monthly, you can switch to a Date axis instead of a string axis, which would fix that issue. Also, your trendlines option is not specified correctly.

    Here's an example that fixes both issues:

    function drawChartCustomerRevenue_93() {
        var revenueChart_93;
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Sales Value');
        data.addRow([new Date(2013, 3, 1), 271176.940000000]);
        data.addRow([new Date(2013, 4, 1), 419031.540000000]);
        data.addRow([new Date(2013, 5, 1), 429155.540000000]);
        data.addRow([new Date(2013, 6, 1), 443780.400000000]);
        data.addRow([new Date(2013, 7, 1), 320353.540000000]);
        data.addRow([new Date(2013, 8, 1), 310640.910000000]);
        data.addRow([new Date(2013, 9, 1), 252187.700000000]);
        data.addRow([new Date(2013, 10, 1), 301414.560000000]);
        data.addRow([new Date(2013, 11, 1), 224296.850000000]);
        data.addRow([new Date(2014, 0, 1), 291131.140000000]);
        data.addRow([new Date(2014, 1, 1), 354750.680000000]);
        data.addRow([new Date(2014, 2, 1), 312736.710000000]);
        var formatter = new google.visualization.NumberFormat({
            fractionDigits: 2,
            prefix: '£'
        });
        formatter.format(data, 1);
        var dateFormatter = new google.visualization.NumberFormat({
            pattern: 'MMM yyyy'
        });
        dateFormatter.format(data, 0);
        var chartHeight = 400;
        var chartWidth = 500;
        var chartOptions = {
            trendlines: {
                0: {
                    color: 'green'
                }
            },
            title: '1: TEST NAME',
            isStacked: true,
            width: chartWidth,
            height: chartHeight,
            colors: ['#0000D4'],
            hAxis: {
                title: 'Sales Value (£)',
                slantedText: true,
                slantedTextAngle: 45,
                textStyle: {
                    fontSize: 10
                },
                format: 'MMM yyyy'
            },
            chartArea: {
                left: 100,
                top: 20,
                width: (chartWidth - 10),
                height: (chartHeight - 90)
            }
        };
        revenueChart_93 = new google.visualization.LineChart(document.getElementById('CustomerRevenue_93'));
        revenueChart_93.draw(data, chartOptions);
    }
    

    see it working here: http://jsfiddle.net/asgallant/j8N48/