简体   繁体   English

使用数据库数据在Google图表上显示报告

[英]Using database data to display reports on google charts

I am very new to reporting and mvc. 我对报告和MVC非常陌生。 Im trying to get my database data to populate my reports. 我试图获取我的数据库数据来填充我的报告。 The reports i have so far are hard coded but i have tried to use a raw query function which got from Return count using raw query, using Entity Framework and MVC to assist me. 到目前为止,我已经对报告进行了硬编码,但是我尝试使用原始查询功能,该功能是通过使用原始查询返回计数中获取的,并使用Entity Framework和MVC来帮助我。 However i am still struggling to get the chart to display the database data. 但是,我仍然在努力使图表显示数据库数据。

report view: 报告视图:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);

    var sql = "SELECT COUNT(*) FROM dbo.School WHERE (SchoolName = Accountancy)";
    var total = _context.Database.SqlQuery < int > (sql).single();

    function drawChart() {                                
        var data = google.visualization.arrayToDataTable([
                       ['Task', 'Hours per Day'],
                       ['Accountancy', total],
                       ['Economics and Business', 2],
                       ['Law', 2],
                       ['Governance', 2],
                       ['Business', 7]
                   ]);

        var options = {
                          title: 'Sample (Until we figure out how to get database data)'
                      };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
    }
</script>

<div id="piechart" style="width:800px; height: 600px;"></div>
</div>

I tried to get the "Accounting" field to display but the chart didn't display the accounting total, it only displays the totals for the others (which are hard-coded) 我试图显示“会计”字段,但该图表未显示会计总数,它仅显示其他(硬编码)的总数

Is there anything specific that needs to be coded to get this data ie like in the controller?.. just to remind I am very new to this 有什么特别的东西需要编码以获取此数据,例如在控制器中吗?..提醒我,我对此很陌生

The question you're referencing is showing C# code. 您所引用的问题正在显示C#代码。 I know this can be confusing with the use of var (I'm not a big fan of var in C#). 我知道这可能与var的使用相混淆(我不是C#中var的忠实拥护者)。 I'm not sure what the MVC 3 way of doing this would be, but you can't mix client and server-side code directly. 我不确定MVC 3的实现方式,但是您不能直接混合客户端和服务器端代码。 In later versions of MVC, I think you can use Razor syntax to bind the server-side query result to your client-side javascript code. 在更高版本的MVC中,我认为您可以使用Razor语法将服务器端查询结果绑定到客户端javascript代码。

An additional piece of advice: Take baby steps. 另一条建议:采取婴儿的步骤。 You're jumping into a lot of concepts all at once if you've never done anything like this. 如果您从未做过这样的事情,那么您会一次跳入很多概念。 You need to learn SQL, Entity Framework, Asp.net-mvc, Javascript, and the Google Charts API all at once to pull this off. 您需要一次学习SQL,实体框架,Asp.net-mvc,Javascript和Google Charts API,才能实现这一目标。

First try just to display the number on the page before trying to display a complicated interactive chart. 首先尝试在页面上显示数字,然后再显示复杂的交互式图表。 For that matter, before you can do that you need to learn how to pull data from SQL into memory on your server. 为此,您必须先学习如何将数据从SQL提取到服务器的内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM