Use zedgraph to generate an indefinite Curve

Source: Internet
Author: User
My requirements are as follows:

There are a lot of suppliers whose inventory data needs to be compared by product by supplier by time period, and a chart is generated at the same time, that is
Use zedgraph to generate an indefinite Curve

I suggest you paste a picture.

I will summarize other implementation methods after this project is completed. What troubles me is:

How can we draw charts by selecting the number of suppliers and the time period? The number of suppliers is variable, and the time period is variable.

I have been using zedgraph. I have almost no articles on generating dynamic curves (the number of indefinite curves) on the Internet. It seems that you can only rely on yourself.

Looking back, let's take a look: to achieve the above, you need to understand these knowledge points.

1. C # use of interactive Arrays

(At the beginning, I chose an interactive array, but in the middle of the process, I felt I was wrong. Instead, I used a two-dimensional array to implement it. Although the data is Ko, however, I cannot draw a diagram. I finally carefully understood the usage of the C # array, Or I used the interactive array to achieve the desired effect)

2. statistical statements related to the stored procedure, mainly (Row-to-column, group by, sorting, dynamic SQL Construction)

The data in the gridview is obtained through the stored procedure, which is implemented through dynamic SQL. This will be completed later.

3. Split usage. Note the C # array length.
Facts have proved that, before implementation, there were mostly minor errors. It seems that my basic skills are not solid yet!

Now let's take a look at the implementation method:

Step 1. First, we enter the comparison of the supplier through a page.

Step2. compare data based on the selected object

The knowledge point here is how to transfer the id value of the selected object out? This requires the use of split.

Int numofchecked = 0; // calculate the number of selected objects

For (INT I = 0; I <this. gdvcompareobject. Rows. Count; I ++)
{
Bool ischecked = (checkbox) gdvcompareobject. Rows [I]. findcontrol ("chkselected"). checked;
If (ischecked)
{
Numofchecked ++;
}
} // Here is the total number of selected objects

If (numofchecked <2) // determines the number.

{
Page. clientscript. registerstartupscript (page. getType (), "", "<SCRIPT> alert ('sorry, You must select at least two objects for comparison. '); </SCRIPT> ");

}
Else
{

String compareobjectlist = string. empty;

For (INT I = 0; I <gdvcompareobject. Rows. Count; I ++)
{

Checkbox chkselectedagentid = (checkbox) gdvcompareobject. Rows [I]. findcontrol ("chkselected ");

If (chkselectedagentid. Checked = true)
{
Compareobjectlist + = gdvcompareobject. datakeys [I] ["object_id"] + ","; // gets the Object ID value to be compared and concatenates it into a token string

}
}
String url = "sysscomparebycompareobjectlistanddate. aspx? Aid = "+ compareobjectlist. substring (0, compareobjectlist. Length-1); // remove the last comma

Response. Redirect (URL );

}

Step3. after passing the value, you can write an article.

The dynamic SQL statement is used here. This stored procedure is like this.

Create procedure comparestoragebyobjectlistandcalculatedate

@ Compareobjectid varchar (1000 ),

@ Begindate datetime,

@ Enddate datetime

As

Declare @ SQL varchar (8000)

Set @ SQL = 'select object_id, ojbect_code'

Select @ SQL = @ SQL + ', max (Case convert (varchar (10), calculatedate, 120) When ''' + convert (varchar (10), calculatedate, 120) + '''then stocknum else 0 end) ['+ convert (varchar (10), calculatedate, 120) +']'

From (select distinct calculatedate from dezai_storage where calculatedate between @ begindate and @ enddate) as

Set @ SQL = @ SQL + 'from dezai_storage where object_id in (' + @ compareobjectid + ') group by object_id, ojbect_code'

Exec (@ SQL)
Go

Here, we note that if we use a general sum (), group by may not be able to get what we want. (If you set the time period to a field, the time here is dynamic.) Of course, the data obtained may be the same. Here we need to implement the requirements for Row-to-column conversion.

I recommend that you read this article. At least I have benefited a lot.

SQL row and column Conversion
Http://www.dezai.cn/article_show.asp? ArticleID = 32432

Step 4: generate data and charts

The field names and row data in the gridview are dynamically generated. Therefore, set the autogeneratecolumn attribute to true, you don't need to worry about anything else.

How can we get the passed supplier value? Program code

Get the agent ID Array
String agentidlist = pss. Common. encrypt. Decode (request. querystring ["aid"]. tostring (). Trim ());
String [] agentid = agentidlist. Split (',');
Int agentnum = agentid. length; // number of agents to be compared

Here I mainly want to talk about zedgraph and how to dynamically obtain cell values.

There are two methods to obtain the value here, but the C # array, especially the interactive array operation, is inevitable.

For the usage of the C # array, see the following articles.

C # 2D Array
Http://www.dezai.cn/article_show.asp? ArticleID = 32403

A simple example of two-dimensional array (getting the number of rows and columns)
Http://www.dezai.cn/article_show.asp? ArticleID = 32383

C # array tutorial
Http://www.dezai.cn/article_show.asp? ArticleID = 32433

(1) directly obtain data from the Business Layer

This method should also be desirable. When I first used this method, I ignored the length of the array (that is, the subscript of the array), which makes it more difficult to obtain the value from the gridview, I have not implemented this method.

(2) retrieve the data population interactive array from the gridview

The key here is how to fill the data in the gridview into the interactive array. Because zegraph does not support interactive arrays when drawing a graph, interactive arrays are the best way to achieve this.

There are also several key points to share with you:
Attention:

A. Why do I need to subtract 2?
Because the data in the generated gridview is the supplier ID and supplier code in the first two columns, it is not the data to be compared. In other words, it is not an integer or a specific number to be compared, so remove the array to make the length of the array compare with the obtained data.

B. xrowcount, What Is ycolumncount?
Xrowcount is the number of rows in the gridview. ycolumn is the number of columns. xrowcount does not include the header, but only the number of rows.

C. How to obtain the names of columns in the gridview
Here is the scale implementation of the X axis, that is, getting the column name (excluding the first two columns)

D. Does lineitem of zedgraph support arrays?
The pointpairlist lineitem of zedgraph supports arrays.
Pointpairlist [] ppl = new pointpairlist [xrowcount]
Lineitem [] line = new lineitem [xrowcount]

For (Int J = 2; j <ycolumncount; j ++)
{
X [J-2] = (double) New xdate (datetime. parse (convert. todatetime (gdvanalysisdata. headerrow. cells [J]. Text). to1_datestring ()));

// Xi = gdvanalysisdata. headerrow. cells [J]. Text. tostring ();

}

At first, I used a two-dimensional array, which is written as follows:

For (INT I = 0; I <xrowcount; I ++)
{
Double [,] storage = new double [xrowcount, ycolumncount-2]; // defines a two-dimensional array

For (int K = 2; k <ycolumncount; k ++)
{

Storage [I, K-2] = convert. todouble (gdvanalysisdata. rows [I]. cells [K]. text. tostring (); // fill the data in the cell into a two-dimensional array To Go To The K-2 is to remove the values of the first two columns not data

Response. write ("X [" + (K-2 ). tostring () + "] =" + X [K-2]. tostring () + "Stroage [" + I. tostring () + "," + (K-2 ). tostring () + "] =" + storage [I, K-2]. tostring () + "<br>"); // print the value detection data

}

The result of this Code is Ko, that is, to completely fill the data of the cells in the gridview into my two-dimensional array, but this zedgraph still cannot draw a graph through it. So after thinking for a while, I had to use the interactive array again. Because the interactive array is an array, the array can contain an array, so that my value can be used directly for zedgraph.

Interactive array implementation:

For (INT I = 0; I <xrowcount; I ++)
{
Double [] [] storage = new double [xrowcount] []; // defines an interactive Array

Storage [I] = new double [ycolumncount-2]; // defines the length of the array of the Interaction Array

For (int K = 2; k <ycolumncount; k ++) // performs a for loop on the columns in the gridview
{

Storage [I] [k-2] = convert. todouble (gdvanalysisdata. rows [I]. cells [K]. text. tostring (); fill in the cell value to the array of I corresponding to the interactive data


// Response. write ("X [" + (k-2 ). tostring () + "] =" + X [k-2]. tostring () + "storage [" + I. tostring () + "] [" + (k-2 ). tostring () + "] =" + storage [I] [k-2]. tostring () + "<br>"); print the data for correction


}

Mycurve [I] = mypane. addcurve (objectid [I]. tostring (), X, storage [I], color. fromargb (RA. next (1, 1,255), Ra. next (1, 1,255), Ra. next (1,255), symboltype. circle); // fill the values of the arrays X and Stroage [I] into zedgraph. Note that the X axis is the same, and the value of the X axis is the time segment, that is, the column name in the gridview.
Mycurve [I]. Line. isantialias = true;
Mycurve [I]. symbol. Fill = New fill (color. White );
Mycurve [I]. symbol. size = 7;
}

From the above value, we can get the desired effect, and the curve of zedgraph's uncertain number of curves and column names can be adjusted.

Curve implementation code;

Program code

Private void onrendergraph (zedgraphweb zgw, graphics g, masterpane)
{
// Set chart attributes
Graphpane mypane = masterpane [0];
// Set the title and axis labels
Mypane. Title. Text = "Total Inventory comparison curve ";
Mypane. xaxis. Title. Text = "year-month-day ";
Mypane. yaxis. Title. Text = "inventory ";
Mypane. yaxis. Scale. minauto = true;
Mypane. yaxis. Scale. maxauto = true;
Mypane. yaxis. Scale. majorstepauto = true;
Mypane. yaxis. Scale. isusetenpower = false;

// Obtain the statistical date
Datetime begindate = convert. todatetime (txtagentstoragebegindate. Text. Trim ());
Datetime enddate = convert. todatetime (txtagentstorageenddate. Text. Trim ());

// Obtain the supplier ID Array
String objectidlist = pss. Common. encrypt. Decode (request. querystring ["aid"]. tostring (). Trim ());
String [] agentid = objectidlist. Split (',');
Int agentnum = agentid. length; // number of suppliers to be compared

Mypane. xaxis. type = axistype. dateasordinal;
Mypane. xaxis. Scale. format = "yyyy-mm-dd ";

Lineitem [] mycurve = new lineitem [agentnum];

// Obtain the number of columns in the gridview, that is, the number of loops.

Pss. BLL. SYS. Statistical bllstatistic = new PSS. BLL. SYS. Statistical ();

Int ycolumncount = bllstatistic. syssumstoragescomparelist (objectidlist, begindate, enddate). fieldcount; // Number of Columns


Int xrowcount = gdvobjectstorage. Rows. Count; // number of rows

Random Ra = new random (1000 );

// X axis

Double [] x = new double [ycolumncount-2];

For (Int J = 2; j <ycolumncount; j ++)
{
X [J-2] = (double) New xdate (datetime. parse (convert. todatetime (gdvobjectstorage. headerrow. cells [J]. Text). to1_datestring ()));

// String xi = gdvobjectstorage. headerrow. cells [J]. Text. tostring ();

}

For (INT I = 0; I <xrowcount; I ++)
{
Double [] [] storage = new double [xrowcount] [];

Storage [I] = new double [ycolumncount-2];

For (int K = 2; k <ycolumncount; k ++)
{

Storage [I] [k-2] = convert. todouble (gdvobjectstorage. Rows [I]. cells [K]. Text. tostring ());

// Response. write ("X [" + (k-2 ). tostring () + "] =" + X [k-2]. tostring () + "storage [" + I. tostring () + "] [" + (k-2 ). tostring () + "] =" + storage [I] [k-2]. tostring () + "<br>"); print the data for correction

}

Mycurve [I] = mypane. addcurve (gdvobjectstorage. rows [I]. cells [1]. text. tostring (), X, storage [I], color. fromargb (RA. next (1, 1,255), Ra. next (1, 1,255), Ra. next (1,255), symboltype. diamond );
Mycurve [I]. Line. isantialias = true;
Mycurve [I]. symbol. Fill = New fill (color. White );
Mycurve [I]. symbol. size = 7;

}

}

My experience:

1. data is a process of data mining. It is easy to use data mining or (OLAP) for data organization. This is a small Bi application. It seems that the Bi market is great.
2. For statistical report analysis, the basic skills are SQL. SQL statistics only involve several fields and data. The results can be varied, but you still need to master SQL statements.
3. the problem I encountered should not be limited to how to transmit values to zedgraph. I think it is most important to obtain data and organize data according to relevant rules, the idea is the most important.

I would like to share with you a little bit of knowledge and experience in the project process. I would like to thank my colleagues who have helped me with this question. Thank you for your advice.

Use zedgraph to generate dynamic data curves (number of indefinite curves)
Http://www.dezai.cn/blog/article.asp? Id = 240

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.