Google Line Chart上的趋势线

时间:2014-04-03 15:49:47

标签: javascript google-visualization

我正在试图弄清楚如何将趋势线添加到我的谷歌折线图中但是我的失败很糟糕。有人能指出我正确的方向吗?我已经看到google没有提供在折线图上执行此操作的方法,但也看到可以在图表中添加新系列以实现类似的功能。到目前为止,除了没有趋势线之外,这是我的工作。我已经尝试了一些我在网上找到的建议,但没有一个能让数学变得清晰,让趋势线成为一条直线:

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();

编辑:

我现在试图把代码放在我的建议中,但它仍然没有显示趋势线:

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();

2 个答案:

答案 0 :(得分:6)

首先,正如@ mcgyver5指出的那样,离散(基于字符串)的轴不支持趋势线。由于您的数据是每月,您可以切换到日期轴而不是字符串轴,这将解决该问题。此外,未正确指定trendlines选项。

以下是修复这两个问题的示例:

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);
}

看到它在这里工作:http://jsfiddle.net/asgallant/j8N48/

答案 1 :(得分:2)

检查出来:Google charts trendline not showing

表示您的第一列不能是字符串,因为它不是连续的域轴。