4
<asp:Chart ID="chartSellThru" runat="server" Height="400px" Width="1200px" >
    <Series>                       
        <asp:Series Name="ActualsQTD">
        </asp:Series>
        <asp:Series Name="ForecastQTD">
        </asp:Series>
        <asp:Series Name="QTDRatio" ChartType="Line">
        </asp:Series>
        <asp:Series Name="TargetAttain" ChartType="Line">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="SellThruChartArea">
        </asp:ChartArea>
    </ChartAreas>

chartSellThru.Series["ActualsQTD"].XValueMember = "ProductGroup";
chartSellThru.Series["ActualsQTD"].YValueMembers = "ActualsQTD";

我在default.aspx页面中写了上面几行代码,最后两行代码写在default.aspx.cs页面中。数据绑定到图表。

如何编写折线图的代码。确保值是从数据库绑定的。如何将值从 sql server 绑定到折线图。

4

3 回答 3

5

我想你是否需要两个系列应该绑定到图表如果我放置下面的代码

在 aspx 页面中

  <asp:Chart ID="ChartDemo" runat="server" Height="400px" Width="1200px">
                <Series>
                    <asp:Series Name="ActualsQTD">
                    </asp:Series>
                    <asp:Series Name="ForecastQTD">
                    </asp:Series>
                    <asp:Series Name="QTDAttainMent">
                    </asp:Series>
                    <asp:Series Name="QTDRatio">
                    </asp:Series>
                </Series>
                <ChartAreas>
                    <asp:ChartArea Name="SellThruChartArea">
                    </asp:ChartArea>
                </ChartAreas>
            </asp:Chart>

并在 aspx.cs 页面中

      DataSet ds = new DataSet();
        ds = SrcObj.GetSellThruChartData(ddlFiscalMonth.SelectedValue.ToString(), ddlReportWeek.SelectedValue.ToString(), ddlArea.SelectedValue.ToString());
        //Column Chart
        ChartDemo.Series["ActualsQTD"].ChartType = SeriesChartType.Column;
        ChartDemo.Series["ActualsQTD"]["PointWidth"] = "0.9";            
        ChartDemo.Series["ActualsQTD"]["BarLabelStyle"] = "Center";
        ChartDemo.Series["ActualsQTD"]["PixelPointDepth"] = "99";
        ChartDemo.Series["ActualsQTD"]["DrawingStyle"] = "Cylinder";
        ChartDemo.Series["ForecastQTD"].ChartType = SeriesChartType.Column;
        ChartDemo.Series["ForecastQTD"]["PointWidth"] = "0.9";            
        ChartDemo.Series["ForecastQTD"]["BarLabelStyle"] = "Center";
        ChartDemo.Series["ForecastQTD"]["PixelPointDepth"] = "99";
        ChartDemo.Series["ForecastQTD"]["DrawingStyle"] = "Cylinder";
        ChartDemo.ChartAreas["SellThruChartArea"].AxisX.Interval = 1;            
        ChartDemo.ChartAreas["SellThruChartArea"].AxisX.MajorGrid.Enabled = false;
        //ChartDemo.ChartAreas["SellThruChartArea"].AxisY.MajorGrid.Enabled = false;

        ChartDemo.Series["ActualsQTD"].XValueMember = "ProductGroup";
        ChartDemo.Series["ActualsQTD"].YValueMembers = "ActualsQTD";            
        ChartDemo.Series["ForecastQTD"].XValueMember = "";
        ChartDemo.Series["ForecastQTD"].YValueMembers = "ForecastQTD";
        ChartDemo.Series["ActualsQTD"].IsValueShownAsLabel = true;            
        ChartDemo.Series["ForecastQTD"].IsValueShownAsLabel = true;

        //Line Chart
        ChartDemo.Series["QTDAttainMent"].ChartType = SeriesChartType.Line;
        ChartDemo.Series["QTDRatio"].ChartType = SeriesChartType.Line;           

        ChartDemo.Series["QTDAttainMent"].XValueMember = "";
        ChartDemo.Series["QTDAttainMent"].YValueMembers = "QTDAttainMent";
        ChartDemo.Series["QTDRatio"].XValueMember = "";
        ChartDemo.Series["QTDRatio"].YValueMembers = "QTDRatio";

        ChartDemo.Series["QTDAttainMent"].IsValueShownAsLabel = true;
        ChartDemo.Series["QTDRatio"].IsValueShownAsLabel = true;

        ChartDemo.Series["QTDAttainMent"].BorderWidth = 3;
        ChartDemo.Series["QTDAttainMent"].Color = Color.Green;
        ChartDemo.Series["QTDRatio"].BorderWidth = 3;
        ChartDemo.Series["QTDRatio"].Color = Color.Red;

        ChartDemo.Series["QTDAttainMent"].YAxisType = AxisType.Secondary;
        ChartDemo.Series["QTDRatio"].YAxisType= AxisType.Secondary;           

        ChartDemo.DataSource = ds;
        ChartDemo.DataBind();
于 2013-02-15T09:51:35.013 回答
0

代码非常简单:

using (SqlConnection conn = new SqlConnection("OMG look a connection string"))
{
    using (SqlCommand cmd = new SqlCommand("SELECT xValue, yValue FROM chartPoints"))
    {
        try
        {
            conn.Open()

            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                chartSellThru.Series["QTDRatio"].Points.DataBindXY(rdr, "xValue", rdr, "yValue");
            }
        }
        catch(Exception ex)
        {
            //handle errors
        }
    }
}

您还可以将此命令设置为SelectCommandfor anSqlDataSource并将其绑定到图表,然后以与之前相同的方式选择带有XValueMemberand的点:YValueMembers

<asp:SqlDataSource runat="server" ID="dsLinePoints" ConnectionString="OMG look a connection string" SelectCommand="SELECT xValue, yValue FROM chartPoints" />

<asp:Chart ID="chartSellThru" runat="server" Height="400px" Width="1200px" DataSourceID="dsLinePoints">
    <Series>                       
        <asp:Series Name="ActualsQTD">
        </asp:Series>
        <asp:Series Name="ForecastQTD">
         </asp:Series>
        <asp:Series Name="QTDRatio" ChartType="Line">
        </asp:Series>
        <asp:Series Name="TargetAttain" ChartType="Line">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="SellThruChartArea">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

chartSellThru.Series["QTDRatio"].XValueMember = "xValue";
chartSellThru.Series["QTDRatio"].YValueMembers = "yValue";
于 2013-02-14T12:40:55.257 回答
0

您可以先简单地完成,然后再进行多个系列。

代码后面的页面:

Values[]={10,20,30,40,50}

weekworkline1.Series.Add(new AjaxControlToolkit.LineChartSeries { Data = values, LineColor = "#2fd1f9", Name = "Working Hours" });

ASP 代码:

<asp:LineChart ID="weekworkline1" runat="server" ChartHeight="300" ChartWidth="900" ChartType="Stacked"></asp:LineChart>
于 2013-10-04T13:47:40.803 回答