简体   繁体   English

在ASP.NET MVC5视图中显示图表高图

[英]Display highchart figure in ASP.NET MVC5 view

I am trying to display a simple chart in my MVC view using highcharts but somehow my javascript is not running. 我正在尝试使用highcharts在我的MVC视图中显示一个简单的图表,但是我的javascript无法运行。 I want to add a chart in the following container. 我想在以下容器中添加图表。

    <div id="container" style="min-width:310px; height:400px; margin:0 auto"></div>

I have a separate javascript file that I add in my view. 我在视图中添加了一个单独的javascript文件。

<script src="~/Scripts/lineChart.js"></script>

Code for lineChart.js is: lineChart.js的代码是:

$('document').ready(function () {
$('#container').highcharts({
    title: {
        text: 'Monthly Average Temperature',
        x: -20 //center
    },
    subtitle: {
        text: 'Source: WorldClimate.com',
        x: -20
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    yAxis: {
        title: {
            text: 'Temperature (°C)'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        valueSuffix: '°C'
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle',
        borderWidth: 0
    },
    series: [{
        name: 'Tokyo',
        data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
    }, {
        name: 'New York',
        data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
    }, {
        name: 'Berlin',
        data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
    }, {
        name: 'London',
        data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
    }]
});

}); });

I have added all javascript and highchart references but when I run my application, it displays nothing 我已经添加了所有javascript和highchart引用,但是当我运行我的应用程序时,它什么也不显示

<script src="~/Scripts/highcharts.js"></script>
<script src="~/Scripts/highcharts.src.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery-1.10.2.js"></script>

I don't know where the problem is. 我不知道问题出在哪里。 I would appreciate any help. 我将不胜感激任何帮助。

Thanks 谢谢

Your code should work fine if you meet this conditions. 如果满足此条件,您的代码应该可以正常工作。 Mu guess is that you failed to do the #2 穆猜是你做不到#2

  1. In your layout file, you have defined a section for your page level scripts. 在布局文件中,已为页面级脚本定义了一个部分。 Something like @RenderSection("scripts", required: false) 类似于@RenderSection("scripts", required: false)
  2. In your specific page/view, you are calling/including your page level script which initialize the chart inside the scripts section. 在您的特定页面/视图中,您正在调用/包括页面级别脚本,该脚本将初始化scripts部分中的图表。

     <div id="container" style="min-width:310px; height:400px; margin:0 auto"></div> @section scripts { <script src="~/Scripts/highcharts.js"></script> <script src="~/Scripts/lineChart.js"></script> } 
  3. And you do not have any javascript errors in your page. 而且您的页面中没有任何JavaScript错误。 Check your browse console to see any possible script errors. 检查您的浏览控制台以查看任何可能的脚本错误。 Here is a working sample using your code & data. 是使用您的代码和数据的工作示例。 Compare it againist the markup produced by razor for your view. 再与剃刀产生的标记进行比较,以供您查看。

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

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