ASP.Net Webform折线图显示为空白图像

时间:2013-12-12 04:59:02

标签: c# asp.net charts asp.net-charts

我使用折线图显示人们日期申请的工作数量。

我使用折线图来显示它

  <asp:Chart ID="chrtDateWise" runat="server" Width="500px">
        <Series>
            <asp:Series ChartType="Line" Name="Series1">
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

代码背后

 protected void ShowDateWise()
    {
        String strSql = string.Empty;
        strSql = "SELECT COUNT(ApplicationDate) AS Total, ApplicationDate FROM [Jobs] GROUP BY ApplicationDate, ApplicationDate order by ApplicationDate ";

        DataSet ds = DataProvider.Connect_Select(strSql);

        DataTable dt = ds.Tables[0];
        chrtDateWise.DataSource = dt;
        chrtDateWise.Series["Series1"].XValueMember = "ApplicationDate";
        chrtDateWise.Series["Series1"].YValueMembers = "Total";

        //chrtDateWise.Series["Series1"]["PieLabelStyle"] = "Outside";
        chrtDateWise.Series["Series1"].ToolTip = "#VALY";
        chrtDateWise.Series["Series1"].Label = "#VALX";
        chrtDateWise.DataBind();

    }

更新:

我将代码更改为

  <asp:Chart ID="chrtDateWise" runat="server" Width="600px">
        <Series>
            <asp:Series ChartType="Line" Name="Series1"> </asp:Series>
        </Series>
        <ChartAreas>
             <asp:ChartArea Name="chrtAreaSupporters">
                    <AxisY LineColor="White">
                        <LabelStyle Font="Trebuchet MS, 8.25pt" />
                        <MajorGrid LineColor="#e6e6e6" />
                        <MinorGrid Enabled="false" LineColor="#e6e6e6" />
                    </AxisY>
                    <AxisX LineColor="White">
                        <LabelStyle Font="Trebuchet MS, 8.25pt" />
                        <MajorGrid LineColor="#e6e6e6" />
                        <MinorGrid Enabled="false" LineColor="#e6e6e6" />
                    </AxisX>
                </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>



chrtDateWise.Series["Series1"].ChartType = SeriesChartType.Line;

    chrtDateWise.Series["Series1"].XValueMember = "Total";
    chrtDateWise.Series["Series1"].YValueMembers = "ApplicationDate";


    chrtDateWise.Series["Series1"].IsValueShownAsLabel = true;


    chrtDateWise.Series["Series1"].BorderWidth = 3;
    chrtDateWise.Series["Series1"].Color = System.Drawing.Color.Green;

    chrtDateWise.Series["Series1"].YAxisType = AxisType.Secondary;

这也不起作用......

我不确定代码有什么问题,因为查询从表中获取数据,除此之外我还能显示饼图。

我认为我搞砸了图表属性。

1 个答案:

答案 0 :(得分:1)

通过这样一个简单的查询,您可以在下面的代码中完成所有操作:

<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
<Series>
    <asp:Series Name="Series1" ChartType="Line" XValueMember="Total" 
        YValueMembers="ApplicationDate">
    </asp:Series>
</Series>
<ChartAreas>
    <asp:ChartArea Name="ChartArea1">
    </asp:ChartArea>
</ChartAreas>
</asp:Chart>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="yourDbConnectionString" 
    SelectCommand="SELECT COUNT(ApplicationDate) AS Total, ApplicationDate FROM [Jobs] GROUP BY ApplicationDate, ApplicationDate order by ApplicationDate">
</asp:SqlDataSource>

ConnectionString是web-config中的一个属性,VisualStudio中有一个向导可以编写它,但是这里有一个关于how to write it的指南。

如果您需要修改图表外观,可以在下面的代码中添加属性,或者在代码隐藏中添加简单的foreach循环

(codebelow)<asp:Series Name="Series1" ChartType="Line" BorderWidth="3" XValueMember="Total" 
        YValueMembers="orderdate">
    </asp:Series>

(codebehind)
foreach(Series s in Chart1.Series){
    s.BorderWidth=3;
    s.IsValueShownAsLabel = true;
    ...
}