Google图表格式日期不起作用

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

我有一个可以正常工作的Google折线图:

btcPriceHistoryGraphInit() {
    this.priceHistoryData.unshift(['Time', 'Bitcoin Price ($)']);
    var graphData = this.priceHistoryData;

    google.charts.setOnLoadCallback(drawMarketCapChart);
    function drawMarketCapChart() {
        var data = google.visualization.arrayToDataTable(graphData);
        var options = {
          animation: {
            duration: 1000,
            startup: true
          },
          chartArea: {
            width: '85%',
            height: '70%'
          },
          legend: {
            position: 'in'
          },
          explorer: {
            actions: ['dragToZoom', 'rightClickToReset']
          },
          hAxis: {
            titleTextStyle: {
                color: '#333'
            }
          },
          vAxis: {
            minValue: 0,
            format: '$#,###'
          }
        };

        var date_formatter = new google.visualization.DateFormat({ 
            pattern: "MMM dd, yyyy"
        }); 
        date_formatter.format(data, 0);
        var chart = new     google.visualization.AreaChart(document.getElementById('price-history-graph'));
        chart.draw(data, options);
    }       
}

现在我已将此函数/选项提取到我导入的函数中以简化它,并且google.visualization.DateFormat函数不再执行任何操作。该功能的代码如下:

export function Graph(chartDivId, chartData, xAxisLabel, yAxisLabel, vAxisFormat, chartWidthPercent, chartHeightPercent, title, animationDuration) {
    var goodData = [];

    chartData.forEach(function(elem) {
        var num = Number(elem[1]);
        goodData.push([elem[0], num])
    });

    var title = title || '';
    var chartWidthPercent = chartWidthPercent || '85%';
    var chartHeightPercent = chartHeightPercent || '70%';
    var duration = animationDuration || 1000;
    var vAxisFormat = vAxisFormat || '';
    var xAxisLabel = xAxisLabel || '';
    var yAxisLabel = yAxisLabel || '';

    goodData.unshift([xAxisLabel, yAxisLabel]);
    var graphData = goodData;

    google.charts.setOnLoadCallback(drawMarketCapChart);
    function drawMarketCapChart() {
        var data = google.visualization.arrayToDataTable(graphData);
        var options = {
          title: title,
          animation: {
            duration: duration,
            startup: true
          },
          chartArea: {
            width: chartWidthPercent,
            height: chartHeightPercent
          },
          legend: {
            position: 'in'
          },
          explorer: {
            actions: ['dragToZoom', 'rightClickToReset']
          },
          hAxis: {
            titleTextStyle: {
                color: '#333'
            }
          },
          vAxis: {
            minValue: 0,
            format: vAxisFormat
          }
        };

        var date_formatter = new google.visualization.DateFormat({ 
            pattern: "MMM dd, yyyy"
        }); 
        date_formatter.format(data, 0);
        var chart = new google.visualization.AreaChart(document.getElementById(chartDivId));
        chart.draw(data, options);
    }       
}

我这样调用函数:Graph(参数)

如何让日期格式化器工作?

谢谢你的建议

javascript charts date-format google-chartwrapper
1个回答
0
投票

我有问题,代码hAxis.format: 'dd.MM.yyyy无法正常工作。原因是explorer.actions: [dragToZoom', 'rightClickToReset']线。使用explorer.actions时,hAxis.format不起作用。

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