【问题标题】:Formating tooltip data in Google Charts在 Google 图表中格式化工具提示数据
【发布时间】:2014-02-10 16:33:16
【问题描述】:

我有页面http://goodandevilbook.com/stats 我正在使用谷歌图表来显示数据。

在其中一个图表(面积图)上,当我将鼠标悬停在工具提示上时,它会以难以阅读的格式显示日期。我想将此日期格式化为更易读的方式。此外,工具提示显示 ga:visitors,我想将其更改为仅访客。我无法真正将 JSON 文件数据放入所需的格式,因为那是自动生成的

不知何故,我需要在收到这些数据后更改它。这是我生成面积图的代码:

google.setOnLoadCallback(drawGraph);
function drawGraph(){
    var graph = new google.visualization.ChartWrapper({
    "containerId":"last30chart",
    "dataSourceUrl":"http://good-and-evil-stats.appspot.com/query?id=ahVzfmdvb2QtYW5kLWV2aWwtc3RhdHNyFQsSCEFwaVF1ZXJ5GICAgICA14wKDA&format=data-table-response",
        "refreshInterval":0,
         "chartType":"AreaChart",
            "options":{
                "width":675,
                "height":400,
                "chartArea":{width:"95%", height:"83%"},
                "areaOpacity":0.1,
                "pointSize":4,
                "backgroundColor":"transparent",
                "colors":['#76BB72'],
                "legend":{position: 'none'},
                "tooltip":{textStyle: {fontSize:18}},
                "hAxis":{textPosition:"none", gridlines: {color: 'red', count: 7}},
                "dateFormat":{formatType: "long"}
            }
            });
    graph.draw();}

我已尝试插入用于格式化日期的代码。

https://developers.google.com/chart/interactive/docs/reference#dateformatter

这就是我在玩的东西:

var monthYearFormatter = new google.visualization.DateFormat({ 
     pattern: "MMM yyyy" 
}); 
monthYearFormatter.format(dataSourceUrl);

我已经尝试了很长时间,但无法让它工作。我是 Javascript/jquery 新手,所以不太确定自己在做什么。

【问题讨论】:

标签: javascript charts google-visualization


【解决方案1】:

使用 ChartWrapper 对象的dataSourceUrl 参数拉入数据时,您无法格式化数据。您必须改用 Query 对象,然后您可以格式化您的数据并根据需要更改列标签。由于您的日期实际上是字符串而不是 Date 对象,因此您必须使用 DataView 将它们转换为 Date 对象,然后才能对其进行格式化。这是一个例子:

function drawGraph(){
    var query = new google.visualization.Query("http://good-and-evil-stats.appspot.com/query?id=ahVzfmdvb2QtYW5kLWV2aWwtc3RhdHNyFQsSCEFwaVF1ZXJ5GICAgICA14wKDA&format=data-table-response");
    query.send(function (response) {
        if (response.isError()) {
            alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
            return;
        }

        var data = response.getDataTable();
        // set the label of column 1 to "Visitors"
        data.setColumnLabel(1, 'Visitors');

        // create a formatter for the dates
        var formatter = new google.visualization.DateFormat({pattern: "MMM yyyy"});

        var graph = new google.visualization.ChartWrapper({
            "containerId": "last30chart",
            "chartType":"AreaChart",
            dataTable: data,
            "options": {
                "width":675,
                "height":400,
                "chartArea": {width:"95%", height:"83%"},
                "areaOpacity":0.1,
                "pointSize":4,
                "backgroundColor":"transparent",
                "colors":['#76BB72'],
                "legend":{position: 'none'},
                "tooltip":{textStyle: {fontSize:18}},
                "hAxis":{textPosition:"none", gridlines: {color: 'red', count: 7}},
                "dateFormat":{formatType: "long"}
            },
            view: {
                columns: [{
                    type: 'date',
                    label: data.getColumnLabel(0),
                    calc: function (dt, row) {
                        var dateStr = dt.getValue(row, 0);
                        var year = parseInt(dateStr.substring(0, 4), 10);
                        var month = parseInt(dateStr.substring(4, 6), 10) - 1; // convert month to 0-based index
                        var day = parseInt(dateStr.substring(6, 8), 10);
                        var date = new Date(year, month, day);
                        return {v: date, f: formatter.formatValue(date)};
                    }
                }, 1]
            }
        });
        graph.draw();
    });
}

【讨论】:

  • 非常感谢您的回复....我现在了解如何使用不同的方法绘制图表。您给我的示例非常适合将标签从 ga:visitors 更改为访客,但它没有重新格式化日期。
  • 糟糕,我的错误,我没有正确构建视图(日期计算也有错误)。我在答案中修复了它。
  • 嗯,网站已经完成了我不知道你能不能看看这个,但是如果你在 Safari 中打开网站,面积图上的日期之一是关闭的,并且图表搞砸了。我对代码了解得不够多,无法找到问题所在....
  • 看起来 Safari 从 parseInt('08')parseInt('09') 返回 0 (由于前导 0,它将它们解析为八进制数字),所以你的日期字符串 '20140208' 和 @ 987654328@ 都变成了new Date(2014, 1, 0)。您可以通过在parseInt 调用parseInt(string, base) 中指定基数来解决此问题。我将通过修复更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 2018-12-07
相关资源
最近更新 更多