Google 折线图上的趋势线

问题描述 投票:0回答:3

我试图弄清楚如何向我的谷歌折线图添加趋势线,但失败得很惨。有人能指出我正确的方向吗?我看到谷歌没有提供在线图上执行此操作的方法,但也看到可以在图表中添加新系列来实现类似的效果。这是我到目前为止所拥有的,除了没有趋势线之外,它的效果很好。我尝试了一些在网上找到的建议,但没有一个能清楚地说明数学原理,使趋势线成为一条直线:

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();
javascript google-visualization
3个回答
7
投票

首先,正如 @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/


2
投票

检查一下:谷歌图表趋势线未显示

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


0
投票

谢谢您(@asgallant 和 @mcgyver5)的帮助...但是是否可以使趋势线像这样开始和停止: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.