0

我正在关注本教程,该教程讨论了使用 WebForms 将数据绑定到 sql 服务器。在添加数据绑定之前,我输入了原始数据以显示图表正在工作。但是,当我运行项目时没有显示图表。我在 VS2012 中使用 NuGet 添加了 DotNet.HighCharts。知道为什么图表没有被渲染吗?这是我的 Default.aspx.cs 的代码

namespace HighChartsWebForms
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Render_Chart();
        }

        protected void Render_Chart()
        {
            Object[] chartValues = new Object[8]; // declare an object for the chart rendering

            DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
                .InitChart(new Chart { DefaultSeriesType = ChartTypes.Line })
                .SetTitle(new Title
                {
                    Text = "Data",
                    X = -20
                })
                .SetSubtitle(new Subtitle
                {
                    Text = "Source: Static data",
                    X = -20
                })
                .SetXAxis(new XAxis
                {
                    Categories = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug" }
                })
                .SetSeries(new[]
                {
                    new Series
                    {
                        Name = "# Pizza",
                        Data = new Data(new object[] { 2, 3, 5, 7, 6, 6, 7, 8 }),
                    },
                });

    ltChart1.Text = chart.ToHtmlString();
        }
    }
}

这是我的 Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HighChartsWebForms._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1><%: Title %>.</h1>
                <h2>Testing HighCharts with ASP.NET</h2>
            </hgroup>
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <h3>This should show a chart:</h3>
    <asp:Literal id="ltChart1" runat="server"></asp:Literal>
</asp:Content>
4

2 回答 2

2

如果您使用Site.Master文件添加 JavaScript 文件的路径,请确保您已添加这三个 JavaScript 文件的路径。

    <asp:ScriptManager runat="server">
        <Scripts>               
            <asp:ScriptReference Path="~/Scripts/highcharts.js" />
            <asp:ScriptReference Path="~/Scripts/jquery-1.5.1.min.js" />   
            <asp:ScriptReference Path="~/Scripts/exporting.js" />              
        </Scripts>
    </asp:ScriptManager>

查看本教程:http ://dotnethighcharts.codeplex.com/ 这里解释了 MVC 和 Web 窗体。

于 2017-04-03T06:46:01.330 回答
0

解决方法很简单。您忘记导入 highcharts.js 和 ajax 库。

    <asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1><%: Title %>.</h1>
                <h2>Testing HighCharts with ASP.NET</h2>
            </hgroup>
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <h3>This should show a chart:</h3>

  <!-- IMPORT THE JAVASCRIPT LIBRARIES HERE -->
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
  <script type="text/javascript" src="http://code.highcharts.com/modules/exporting.js"></script>

  <asp:Literal id="ltChart1" runat="server"></asp:Literal>
</asp:Content>
于 2015-03-23T19:11:24.733 回答