从Datatable / Excel创建图表

时间:2016-05-19 12:54:45

标签: c# asp.net charts datatable

我收到以下错误 - 执行ChartImg.axd的子请求时出错。

我正在尝试将数据表绑定到图表

aspx代码:

 <asp:Chart ID="Chart1" runat="server" Height="296px" Width="412px" BorderDashStyle="Solid"
            BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2px" BackColor="211, 223, 240"
            BorderColor="#1A3B69">
            <Titles>
                <asp:Title Text="Title of the Graph comes here" />
            </Titles>
            <Series>
                <asp:Series Name="Series1" BorderColor="180, 26, 59, 105">
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid"
                    BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent"
                    BackGradientStyle="TopBottom">
                    <Area3DStyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False"
                        WallWidth="0" IsClustered="False"></Area3DStyle>
                    <AxisY LineColor="64, 64, 64, 64">
                        <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                        <MajorGrid LineColor="64, 64, 64, 64" />
                    </AxisY>
                    <AxisX LineColor="64, 64, 64, 64">
                        <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                        <MajorGrid LineColor="64, 64, 64, 64" />
                    </AxisX>
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>

代码背后:

 protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtCloned = new DataTable();
        DataSet ds2;
        ds2 = (DataSet)Session["ds2"];
        dtCloned = ds2.Tables[0];
        dtCloned.Columns["SSN"].DataType = typeof(string);

        Chart1.DataSource = ds2.Tables[0];
        Chart1.ChartAreas[0].AxisX.Minimum = 0;
        Chart1.ChartAreas[0].AxisX.Maximum = 10;
        Chart1.ChartAreas[0].AxisX.Minimum = -5;
        Chart1.ChartAreas[0].AxisX.Maximum = 5;

        Chart1.Series["Series1"].YValueMembers = "Name";
        Chart1.Series["Series1"].XValueMember = "County";
        Chart1.DataBind();
}

调试时没有错误。但是一旦完成,我就会收到服务器错误。

提前致谢!

注意:我保留此问题,因为它对任何其他用户都是有建设性的。

1 个答案:

答案 0 :(得分:0)

问题在于缺少 web.config 设置。 一旦我添加它们,问题就解决了。

<configuration>
  <appSettings>
    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />
  </appSettings>
  <system.webServer>
    <handlers>
      <remove name="ChartImageHandler" />
      <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST"
        path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </handlers>
  </system.webServer>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting"
          assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </controls>
    </pages>
    <compilation debug="true" targetFramework="4.5">
        <assemblies>

        <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.6.1"/>
    <httpHandlers>
      <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    </httpHandlers>
  </system.web>
</configuration>