首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于WEBCHART的SQLSERVER数据库挂接有关问题

2012-01-16 
关于WEBCHART的SQLSERVER数据库挂接问题下面是我找到的源码,但源码中的数据源是一个手动创造的表ds,我的想

关于WEBCHART的SQLSERVER数据库挂接问题
下面是我找到的源码,但源码中的数据源是一个手动创造的表ds,我的想法是如果我想挂接SQLSERVER数据库中的一个表作为数据源,下面代码我该如何修改,我想主要修改DataSet   CreateDataSet()   中的部分就可以,我应该如何修改

<%@   Page   Language= "C# "   %>
<%@   Register   TagPrefix= "web "   Namespace= "WebChart "   Assembly= "WebChart "   %>
<%@   Import   Namespace= "System.Data "   %>
<%@   Import   Namespace= "System.Drawing "   %>   <script   runat= "server ">
 
        void   Page_Load(object   o,   EventArgs   e)
        {
                CreateChart();
        }
       
        void   CreateChart()  
        {
                DataSet   ds   =   CreateDataSet();
                ///DataSet   ds   =   SqlDataSource1;
                LineChart   chart5   =   new   LineChart();
                chart5.Line.Color   =   Color.Green;  
                chart5.Fill.Color   =   Color.Green;
                chart5.LineMarker   =   new   DiamondLineMarker(8,   Color.Green,   Color.Yellow);  
                chart5.Legend   =   "Goal ";
 
                foreach(DataRow   row   in   ds.Tables[0].Rows)  
                {
                    chart5.Data.Add(new   ChartPoint(row[ "date "].ToString(),   (int)row[ "goal "]));
                }

                chartControl1.Charts.Add(chart5);
                chartControl1.RedrawChart();
        }

        DataSet   CreateDataSet()   {
 
                DataSet   ds   =   new   DataSet();  
                DataTable   table   =   ds.Tables.Add( "Table ");
                table.Columns.Add( "date ");  
                table.Columns.Add( "pri0 ",   typeof(int));
                table.Columns.Add( "pri1 ",   typeof(int));
                table.Columns.Add( "pri2 ",   typeof(int));  
                table.Columns.Add( "pri3 ",   typeof(int));
                table.Columns.Add( "total ",   typeof(int));
                table.Columns.Add( "goal ",   typeof(int));
                Random   rnd   =   new   Random();


                for   (int   i   =   0;   i   <   15;   i++)  
                      {
                        DataRow   row   =   table.NewRow();
                        row[ "date "]   =   DateTime.Now.AddDays(i).ToString( "mm/dd ");
                        int   pri0   =   rnd.Next(200);
                        int   pri1   =   rnd.Next(200);
                        int   pri2   =   rnd.Next(200);
                        int   pri3   =   rnd.Next(200);
                        int   sum   =   pri0   +   pri1   +   pri2   +   pri3;
                        int   goal   =   rnd.Next(sum);
 
                        row[ "pri0 "]   =   pri0;
                        row[ "pri1 "]   =   pri1;
                        row[ "pri2 "]   =   pri2;
                        row[ "pri3 "]   =   pri3;
                        row[ "total "]   =   sum;
                        row[ "goal "]   =   goal;
                        table.Rows.Add(row);
                      }
                return   ds;
        }
</script>
<html>
<head>
        <title> Untitled   Page </title>
</head>
<body>
        <form   id= "form1 "   runat= "server ">
                <asp:SqlDataSource   ID= "SqlDataSource1 "   runat= "server "   ConnectionString= " <%$   ConnectionStrings:scadaConnectionString   %> "
                        SelectCommand= "SELECT   *   FROM   D20070103   WHERE   (TNAME   =   'ak01 ')   ORDER   BY   TIME ">
                </asp:SqlDataSource>
                <web:ChartControl   Runat= "server "   ID= "chartControl1 "   ChartPadding= "30 "   BottomChartPadding= "20 "
                        TopPadding= "20 "   Width= "800px "   Height= "450px "   BorderStyle= "Outset "   GridLines= "Both "  


                          Legend-Position= "Bottom "   Legend-Width= "30 "   HasChartLegend= "False "   ShowTitlesOnBackground= "False "   YCustomEnd= "0 "   YCustomStart= "0 "   YValuesInterval= "0 ">
                        <Background   Type= "LinearGradient "   Color= "CornflowerBlue "   EndPoint= "100,   400 "   Angle= "90 "   ForeColor= "#80FF80 "   />
                        <Border   Color= "CornflowerBlue "   />
                        <ChartTitle   Text= "My   Product   Bugs "   Font= "Tahoma,   10pt,   style=Bold "   ForeColor= "White "   StringFormat= "Center,Near,Character,LineLimit "   />
                        <XAxisFont   StringFormat= "Near,Center,Character,DirectionVertical "   />
                        <XTitle   Text= "Date "   Font= "Tahoma,   8pt,   style=Bold "   ForeColor= "SteelBlue "   StringFormat= "Center,Far,Character,LineLimit "   />
                        <YTitle   Text= "#   Bugs "   Font= "Tahoma,   8pt,   style=Bold "   StringFormat= "Near,Near,Character,DirectionVertical "   ForeColor= "SteelBlue "   />
                        <YAxisFont   StringFormat= "Far,Near,Character,LineLimit "   />
                        <PlotBackground   Angle= "90 "   EndPoint= "100,   400 "   ForeColor= "#FFFFC0 "   Type= "LinearGradient "   />
                        <Legend   Position= "Bottom "   Width= "30 "> </Legend>
                </web:ChartControl>
        </form>  
</body>
</html>

[解决办法]
弄个SqlDataAdapter 上去。。然后填充DataSet不就可以了么?
[解决办法]
SqlConnection conn =null;
SqlDataAdapter dapt = null;
DataSet ds = new DataSet();
string strConnectionString = "Data Source=.;initial catalog=数据库;uid=sa;pwd=xxx ";
string strSQL = "SELECT 字段 from 表 "
try
{
conn = new SqlConnection(strConnectionString);
conn.Open();
dapt = new SqlDataAdapter(strSQL,conn);
dapt.Fill(ds, "table1 ");
DataTable dt = ds.Tabls[0];
if(dt.Rows.Count > 0)
{
this.Text1.Text = dt.Rows[0][ "字段 "].ToString();
}

}
catch(System.Exception e)
{
this.Text1.Text = "错误 "+e.Message;
}
finally


{
if(conn!=null)
conn.Dispose();
if(dapt!=null)
dapt.Dispose();
}

[解决办法]
直接读取数据库,然后设定控件的X,Y,Z轴对应的字段名称就可以了

参考
protected WebChart.ChartControl ChartControl1;


string strStar= " ";
string strEnd= " ";
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BLL_Login MyRole = new BLL_Login();
MyRole.CheckWorkPower( "效益分析 ");

new BLL_Charts().DelChart();
strStar=Request.QueryString[ "start "]+ " ";
strEnd=Request.QueryString[ "end "]+ " 23:59:59 ";

string strwhere= " companyid= "+Request[ "companyid "]+ " and DateTime between ' "+strStar+ " ' and ' "+strEnd+ " ' ";

DAL_benefit Myclass=new DAL_benefit();

DataSet ds = Myclass.Bind(strwhere);
if (ds.Tables[0].Rows.Count <=0)
{
Response.Write( "没有数据可供分析! ");
this.ChartControl1.Visible=false;
return ;
}

DataView dv = ds.Tables[0].DefaultView ;

LineChart chart = new LineChart( ); //图表类型--这个是曲线图
chart.Line.Color = Color.Blue;
chart.Legend = "收入(元) ";
chart.DataSource = dv;
chart.DataXValueField = "DateTime ";

chart.DataYValueField = "Freight1 ";
chart.DataLabels.Visible=true;
chart.DataLabels.ShowZeroValues=true;
chart.DataBind();
ChartControl1.Charts.Add(chart);

LineChart chart1 = new LineChart( ); //图表类型--这个是曲线图
chart1.Line.Color = Color.Tomato;
chart1.Legend = "成本(元) ";
chart1.DataSource = dv;
chart1.DataXValueField = "DateTime ";
chart1.DataYValueField = "LongCarriage ";
chart1.DataLabels.Visible=true;
chart1.DataLabels.ShowZeroValues=true;
chart1.DataBind();
ChartControl1.Charts.Add(chart1);

LineChart chart2 = new LineChart( ); //图表类型--这个是曲线图
chart2.Line.Color = Color.DarkSlateGray;
chart2.Legend = "利润(元) ";
chart2.DataSource = dv;
chart2.DataXValueField = "DateTime ";
chart2.DataYValueField = "gain ";
chart2.DataLabels.Visible=true;
chart2.DataLabels.ShowZeroValues=true;
chart2.DataBind();
ChartControl1.Charts.Add(chart2);

ConfigureColors();
ChartControl1.RedrawChart();
}
}

private void ConfigureColors()//图表的配置
{
//被注释掉的是因为在页面上设置属性更方便
//ChartControl1.Background.Color = Color.FromArgb(75, Color.SteelBlue);//80表示背景色的渐变程度
//
//ChartControl1.Background.Type = InteriorType.Solid;//背景的花样。
//
//ChartControl1.Background.ForeColor = Color.SteelBlue;//背景色

ChartControl1.Background.EndPoint = new Point(500, 350) ;//不知道


ChartControl1.Legend.Position = LegendPosition.Right;//注释栏的位置
ChartControl1.Legend.Width = 80;//注释栏的宽度

ChartControl1.YAxisFont.ForeColor = Color.SteelBlue;//Y轴的字体颜色SteelBlue
ChartControl1.XAxisFont.ForeColor = Color.SteelBlue;//X轴的字体颜色SteelBlue

//ChartControl1.XTitle.Text= "X轴注释 ";
//ChartControl1.XTitle.ForeColor=Color.SteelBlue;
//ChartControl1.YTitle.Text= "Y轴注释 ";
//ChartControl1.YTitle.ForeColor=Color.SteelBlue;

ChartControl1.ChartTitle.Text = "“ "+Request[ "Name "]+ "”的 "+ strStar + "-- "+ Request[ "end "] + " 消费走势 ";


ChartControl1.ChartTitle.ForeColor = Color.White;

//ChartControl1.Border.Color = Color.SteelBlue;
//ChartControl1.BorderStyle = BorderStyle.None;
}

[解决办法]
上面有人回了,应当可以解决了吧,现在消息不闪,我没看到

也不要叫我老师,不好意思,呵呵

热点排行