javascript - 如何将数据从 asp.net 传递到 javascript
问题描述
我正在尝试以饼图/甜甜圈图格式显示我的数据。我在互联网上找到的最简单的是简单的谷歌图表。然而,它并不像看起来那么简单。我仍然坚持传递数据来生成图表。非常感谢您对像我这样的菜鸟的帮助/建议/解决方案..提前致谢
我已经有了表格格式的数据,现在我试图将显示更改为饼图/甜甜圈图。
这是我显示数据的代码..(表格格式)
<table class="table table-striped table-bordered table-hover table-checkable dataTable no-footer" cellspacing="0" rules="all" border="1" style="border-collapse:collapse; margin-bottom: 0;">
<tr>
<td>Female</td>
<td class="text-center"><asp:Literal runat="server" ID="tFemaleOccupied"></asp:Literal></td>
</tr>
<tr>
<td>Mix</td>
<td class="text-center"><asp:Literal runat="server" ID="tMixOccupied"></asp:Literal></td>
</tr>
<tr>
<td>Queen</td>
<td class="text-center"><asp:Literal runat="server" ID="tQueenOccupied"></asp:Literal></td>
</tr>
<tr>
<td>Suite</td>
<td class="text-center"><asp:Literal runat="server" ID="tSuiteOccupied"></asp:Literal></td>
</tr>
</table>
我打算改成这个
<table class="table table-striped table-bordered table-hover table-checkable dataTable no-footer" cellspacing="0" rules="all" border="1" style="border-collapse:collapse; margin-bottom: 0;">
<tr>
<td>
<div id="piechart"></div>
</td>
</tr>
</table>
这是使用谷歌图表的“饼图”脚本
<asp:Content ID="Content4" ContentPlaceHolderID="cphPageScripts" runat="server">
<script src="/assets/pages/scripts/dashboard.min.js" type="text/javascript"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Room Type', 'No of Room'],
['Female', 'FemaleOccupied'],
['Mix', 'MixOccupied'],
['Queen', 'QueenOccupied'],
['Suite', 'SuiteOccupied']
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'Room Occupancy', 'width':'50%', 'height':'50%'};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
如何将值“tFemaleOccupied”传递给数据数组中的“FemaleOccupied”?
解决方案
您在这里缺少两件事:
- 该
arrayToDataTable
函数需要图形的实际数据(如数字),但您没有传递任何数据。 - 在您的表中,您通过后面的 ASP.NET 代码设置值。这是服务器端代码,即它在页面发送到您机器上的浏览器之前在服务器上执行。但是,您尝试使用的 Google 图表库是一个 JavaScript 库,因此它的代码在客户端执行,即在页面发送给您之后在您的浏览器中执行。在 ASP.NET Web 窗体中,在 ASP.NET 控件之外的服务器端和客户端之间交换数据并不总是那么简单。
在不过多更改代码的情况下,这是实现此目的的一种方法:
<table class="table table-striped table-bordered table-hover table-checkable dataTable no-footer" cellspacing="0" rules="all" border="1" style="border-collapse:collapse; margin-bottom: 0; display: none;">
<tr>
<td>Female</td>
<td class="text-center"><asp:Literal runat="server" ID="tFemaleOccupied"></asp:Literal></td>
</tr>
<tr>
<td>Mix</td>
<td class="text-center"><asp:Literal runat="server" ID="tMixOccupied"></asp:Literal></td>
</tr>
<tr>
<td>Queen</td>
<td class="text-center"><asp:Literal runat="server" ID="tQueenOccupied"></asp:Literal></td>
</tr>
<tr>
<td>Suite</td>
<td class="text-center"><asp:Literal runat="server" ID="tSuiteOccupied"></asp:Literal></td>
</tr>
</table>
<table class="table table-striped table-bordered table-hover table-checkable dataTable no-footer" cellspacing="0" rules="all" border="1" style="border-collapse:collapse; margin-bottom: 0;">
<tr>
<td>
<div id="piechart"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Room Type', 'No of Room'],
['Female', <%= tFemaleOccupied.Text%>],
['Mix', <%= tMixOccupied.Text%>],
['Queen', <%= tQueenOccupied.Text%>],
['Suite', <%= tSuiteOccupied.Text%>]
]);
// Optional; add a title and set the width and height of the chart
var options = { 'title': 'Room Occupancy', 'width': '50%', 'height': '50%' };
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
这假设您在代码后面或之前tFemaleOccupied.Text
的方法中设置了等。Page_Load
正如上面评论中提到的其他人所提到的,一种更简洁的编写方法是使用 Ajax 获取图表的数据,但这也需要您想出一种方法来提供该数据(例如,Web 服务),你现在可能没有。
推荐阅读
- c - 降序链表显示不按顺序显示
- javascript - 我有一个js出现效果问题
- amazon-web-services - aws runInstance 资源被拒绝显式允许所有资源
- django - Django ValueError:无法配置处理程序“文件”
- reactjs - 在 Laravel/React 项目中找不到 React 代码
- php - Laravel store multiple 只保存一个
- java - (Java Swing) 我的动画线程没有并行运行
- mongodb - Mongodb groupby日期和id范围
- sql-server - SQL 错误日志在 SQL Server 中不可见
- json - 如何在 Mulesoft 中比较 XML 中的 json 对象值