格式化谷歌折线图工具提示数据以使用百分比

时间:2015-11-02 15:59:36

标签: javascript google-visualization

我有一个折线图,其数据来自测试JSON文件,如下所示:

{
  "cols": [
        {"id":"Day","label":"Day","type":"number"},
        {"id":"Failed","label":"Checks Failed","type":"number"},
        {"id":"Passed","label":"Checks Passed","type":"number"},
      ],
  "rows": [
        {"c":[{"v":"1","f":null},{"v": "25","f":null},{"v": "72","f":null}]},
        {"c":[{"v":"2","f":null},{"v": "6","f":null},{"v": "67","f":null}]},
        {"c":[{"v":"3","f":null},{"v": "6","f":null},{"v": "32","f":null}]},
        {"c":[{"v":"4","f":null},{"v": "6","f":null},{"v": "7","f":null}]},
        {"c":[{"v":"5","f":null},{"v": "6","f":null},{"v": "57","f":null}]},
      ]
}

我有我的图表脚本:

var graph_1 = $.ajax({
    url: "/linegraph.json",
    dataType: "json",
    async: false
}).responseText;

var data = new google.visualization.DataTable(graph_1);

var linechart_1 = new google.charts.Line(document.getElementById('lg1'));

var lg1_options = {
  legend: {position: 'none'},
  height: 100,
  vAxis: {format: '#%'}
};

linechart_1.draw(data, lg1_options);

格式选项似乎没有给我其他建议的结果。我的工具提示只显示一个普通数字,而我希望看到数字后跟%。我哪里错了?

1 个答案:

答案 0 :(得分:1)

  

材料图表处于测试阶段。外观和交互性在很大程度上是最终的,但声明选项的方式不是。如果要将经典折线图转换为材料折线图,您将要替换此线:

     

chart.draw(数据,选项);

     

......用这个:

     

chart.draw(data,google.charts.Line.convertOptions(options));

Docs

因此,您希望用以下代码替换您的代码:

linechart_1.draw(data, google.charts.Line.convertOptions(lg1_options));

请注意,#%格式化程序会强制它将您的值解释为百分比,即。 5 == 500%,而非5%,因此您可能希望使用format:'#\'%\''

JSFiddle