如何在ASP柱形图的列上显示其他“标签”?

时间:2016-08-16 17:26:27

标签: javascript c# asp.net html5 charts

我有一个ASP柱形图,它从两个下拉菜单中取值,“Station”和“Unit”并绘制它们的图表。这是我的图表的代码:

HomePage.aspx.cs:

void getChartData()
    {
        DataSet ds = new DataSet();
        // Read the data from XML file into DataSet
        ds.ReadXml(Server.MapPath("~/MWAvailability2.xml"));
        // Specify the column that contains values for X-AXIS
        ColumnChart.Series["Series1"].XValueMember = "AvailDate";
        // Specify the column that contains values for Y-AXIS
        ColumnChart.Series["Series1"].YValueMembers = "MW";
        // Set DataSet as the DataSource for the Chart control
        ColumnChart.DataSource = ds;
        // Finally call DataBind
        ColumnChart.DataBind();
    }

HomePage.aspx:

<asp:Chart ID="ColumnChart" runat="server" Height="250px" Width="500px">
                <Series>
                    <asp:Series Name="Series1" ChartType="Column" ChartArea="MainArea" IsValueShownAsLabel="true"></asp:Series>
                    <%--<asp:Series Name="Series2" ChartArea="MainArea" Enabled="true" Label='ddStation.SelectedValue' IsValueShownAsLabel="true"></asp:Series>--%>
                </Series>
                <ChartAreas>
                    <asp:ChartArea Name="MainArea" Area3DStyle-Enable3D="false"></asp:ChartArea>
                </ChartAreas>
            </asp:Chart>

MainForm.aspx.cs:

 void getChartData()
    {
        String ConString = ConfigurationManager.ConnectionStrings["cs"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(ConString))
        {
            String SQLText = "select AvailDate,MW from MWAvailability2 where UnitName = @UnitName and Station = @StationName and AvailDate between @AvailFrom and @AvailTo";
            SqlCommand cmd = new SqlCommand(SQLText, connection);
            connection.Open();
            cmd.Parameters.AddWithValue("@UnitName", ddUnit.SelectedValue);
            cmd.Parameters.AddWithValue("@StationName", ddStation.SelectedValue);
            if (TextBoxFrom.Text.Length > 0)
                {
                    cmd.Parameters.AddWithValue("@AvailFrom", TextBoxFrom.Text);
                }
            else
                {
                    cmd.Parameters.AddWithValue("@AvailFrom", "01-01-1900");
                }
            if (TextBoxTo.Text.Length > 0)
                { 
                    cmd.Parameters.AddWithValue("@AvailTo", TextBoxTo.Text);
                }
            else
                {
                    cmd.Parameters.AddWithValue("@AvailTo", "01-01-2050");
                }

            if (TextBoxFrom.Text.Length > 0 && TextBoxTo.Text.Length > 0)   // Text in from, text in to
            {
                chartTitle.Text = "Station " + ddStation.SelectedValue + " " + ddUnit.SelectedValue + " From: " + TextBoxFrom.Text + " To: " + TextBoxTo.Text;
            }
            else if (TextBoxFrom.Text.Length > 0 && TextBoxTo.Text.Length == 0)     // Text in from, no text in to
            {
                chartTitle.Text = "Station " + ddStation.SelectedValue + " " + ddUnit.SelectedValue + " From: " + TextBoxFrom.Text + " To: 01/01/2050";
            }
            else if (TextBoxFrom.Text.Length == 0 && TextBoxTo.Text.Length > 0)     // No text in from, text in to
            {
                chartTitle.Text = "Station " + ddStation.SelectedValue + " " + ddUnit.SelectedValue + " From: 01/01/1900 " + " To: " + TextBoxTo.Text;
            }
            else if (TextBoxFrom.Text.Length == 0 && TextBoxTo.Text.Length == 0)     // No text in from, no text in to
            {
                chartTitle.Text = "Station " + ddStation.SelectedValue + " " + ddUnit.SelectedValue + " From: 01/01/1900 To: 01/01/2050";
            }



            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                Series series = ColumnChart.Series["Series1"];
                if (reader.HasRows)
                {
                    series.Points.AddXY(reader["AvailDate"].ToString(), reader["MW"]);

                }
                else
                {
                    Console.WriteLine("No rows found.");
                }
            }

        }
    }

MainForm.aspx:

<asp:Chart ID="ColumnChart" runat="server" Height="250px" Width="500px">
                <Series>
                    <asp:Series Name="Series1" ChartType="Column" ChartArea="MainArea" IsValueShownAsLabel="true"></asp:Series>
                    <%--<asp:Series Name="Series2" ChartArea="MainArea" Enabled="true" Label='ddStation.SelectedValue' IsValueShownAsLabel="true"></asp:Series>--%>
                </Series>
                <ChartAreas>
                    <asp:ChartArea Name="MainArea" Area3DStyle-Enable3D="false"></asp:ChartArea>

                </ChartAreas>
            </asp:Chart>

这就是我的图表目前的样子: enter image description here

这就是我尝试添加的功能可能如下所示: enter image description here

我已经查看了Visual Studio文档,这个网站,谷歌,甚至Google图像,但似乎没有人,至少据我所知,已经做过类似的事情。我也尝试过ColumnChart.Series的所有功能,但这里没有任何东西适合我的需求。

我见过的大多数柱形图都有X和Y轴标签以及列顶部或顶部的标签,但是我需要第四个元素可以在每个列中共存,在这种情况下来自“站”下拉列表的值,但也可能来自“单位”。

我理解我所问的可能令人困惑,或者有一些明显的东西让我忽略,但我真的可以在这里使用一些帮助。

我非常愿意提供所需的任何其他信息,任何可以帮助我或者至少指出我正确方向的人都会非常感激。

谢谢!

0 个答案:

没有答案