我正在使用Java和JSF。我转换为传递给google.visualization.arrayToDataTable
方法的JSON的对象是一个地图列表。
List<Map<String, Object>> listOfMaps = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("Name", "Value");
map.put("A", new Random().nextInt());
map.put("B", new Random().nextInt());
listOfMaps.add(map);
Gson gson = new Gson();
String arrayJson = gson.toJson(listOfMaps);
RequestContext.getCurrentInstance().execute("google.setOnLoadCallback(drawChart(" + arrayJson + "));");
RequestContext.getCurrentInstance().update("piechart");
HTML是:
<h:head>
<title>Facelet Title</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
//<![CDATA[
google.load("visualization", "1", {packages:["corechart"]});
function drawChart(mapList) {
var gdata = google.visualization.arrayToDataTable(mapList);
var options = {
title: 'Teste'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(gdata, options);
}
//]]>
</script>
</h:head>
<h:body>
<h:form>
<p:growl id="growl" autoUpdate="true" globalOnly="true" showDetail="true" sticky="true" />
<p:commandButton id="button" value="test" action="#{testMB.test()}" />
<br />
<div id="piechart" style="width: 900px; height: 600px" />
</h:form>
</h:body>
页面中错误的屏幕截图:
JavaScript控制台中没有任何内容。
编辑:
将List
从Map
更改为String[]
后,会显示图表。当我发出JavaScript警报时,会打印数组长度,但不会打印值。