0

我是 MVC 新手,第一次尝试使用 DotNet High Chart。我在 Visual Studios MVC 5 中关注教程。

我的捆绑包:

public class BundleConfig
{

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));


        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));

        bundles.Add(new Bundle("~/bundles/highchart").Include(
                    "~/Scripts/Highcharts-4.0.1/js/highchart.js"));
    }
}

}

我的模型:

namespace Charts2.Models
{
public class TransactionCount
{
    public string MonthName { get; set; }
    public int Count { get; set; }
}
}

我的控制器:

namespace Charts2.Controllers
{
     public class ChartSampleController : Controller
{
    // GET: ChartSample
    public ActionResult Index()
    {
        //create a collection of data
        var transactionCounts = new List<TransactionCount>{
                        new TransactionCount(){ MonthName="January", Count=30},
                        new TransactionCount(){ MonthName="Febuary", Count=40},
                        new TransactionCount(){ MonthName="March", Count=4},
                        new TransactionCount(){ MonthName="April", Count=35},
        };

        // modify data type to make it of array types
        var xDataMonths = transactionCounts.Select(i => i.MonthName).ToArray();
        var yDataCounts = transactionCounts.Select(i => new object[] { i.Count }).ToArray();

        //instanciate an object of the high charts type
        var chart = new Highcharts("chart")
                // define the type of chart
                .InitChart(new Chart { DefaultSeriesType = ChartTypes.Line})
                //overall title of the chart
                .SetTitle(new Title { Text = "Incoming Transactions per hour"})
                //small label below the main title
                .SetSubtitle(new Subtitle { Text = "Accounting"})
                // load the x values
                .SetXAxis(new XAxis {Categories = xDataMonths})
                // set the y title
                .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Number of Transactions"}})
                    .SetTooltip(new Tooltip{
                        Enabled = true,
                        Formatter = @"function() { return '<b>'+this.series.name +'</b><br/>'+this.x+': '+this.y;}"
                        })
                        .SetPlotOptions(new PlotOptions{
                        Line = new PlotOptionsLine {
                        DataLabels = new PlotOptionsLineDataLabels {
                        Enabled = true
                    },
                    EnableMouseTracking = false
                        } 
                  })
                     //load y values
.SetSeries(new []
    {
    new Series {Name = "Hour", Data = new Data(yDataCounts)},

});


        return View(chart);
    }
}
}

和我的观点:

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@model DotNet.Highcharts.Highcharts

<p> My Chart </p>
@(Model)

我共享的_Index:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/highchart")

</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            </ul>
            @Html.Partial("_LoginPartial")
        </div>
    </div>
</div>
<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
</div>


@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

每当我运行应用程序时,图表根本不会生成。我已按照教程进行操作,但无法理解我哪里出错了。在控制台上,我收到以下异常:

Uncaught ReferenceError: Highcharts is not defined

任何建议将不胜感激谢谢

4

2 回答 2

0

您能否发布完整的视图代码。在您当前的视图中,您没有添加高图表包来查看一些类似的东西

@Scripts.Render("~/bundles/highchart")
于 2015-08-13T11:50:48.273 回答
0

我已经意识到我哪里出错了。我在捆绑包名称的末尾缺少一个s ,我更改了捆绑包:

bundles.Add(new ScriptBundle("~/bundles/highcharts").Include(
             "~/Scripts/Highcharts-4.0.1/js/highcharts.js"
                ));

并根据更新了_index,现在它可以工作了。

于 2015-08-13T13:29:06.077 回答