Recently, I got a program to display the number of votes in real time. A bar chart is required to dynamically display the number of votes of contestants. I wanted to use it. net's GDI + Ajax, but after a morning, it was very troublesome. I asked csdn to see if there was any good method and told me that zedgraph could be used. So Google gave me a look, put it down from SourceForge and start to study...
Create a winform project and set zedgraph. DLL is added to the bin, so that you can call its method. open the winform design interface and drag a zedgraphcontrol control from the toolbox. (Note: you must add the zedgraph control first ). let's start with a static global variable and a global method:
Private Static int num = 1;
Public double [] getarrvalues ()
{
Double [] arr = {100,135,115,125, 75,120 };
Arr [0] + = num;
Arr [1] + = num * 2;
Arr [2] + = num * 3;
Arr [3] + = num * 4;
Arr [4] + = num * 5;
Arr [5] + = num * 6;
Num ++;
Return arr;
}
Then add the following code to form_load:
This. zedgraphcontrol1.graphpane. Title. Text = "Happy male real-time voting display ";
This. zedgraphcontrol1.graphpane. xaxis. Title. Text = "contestant ";
This. zedgraphcontrol1.graphpane. yaxis. Title. Text = "votes ";
This. zedgraphcontrol1.graphpane. xaxis. type = zedgraph. axistype. dateasordinal;
Double [] arrvalues = getarrvalues ();
String [] xtitles = {"Wang xinxin", "jijie", "Wei Chen", "Wake up", "Zhang Jie", "Chen Chusheng "};
Baritem mybar = This. zedgraphcontrol1.graphpane. addbar ("votes", null, arrvalues, color. Red );
Mybar. Bar. Fill = New fill (color. Red, color. White, color. Red );
Mybar. Bar. Border. Color = color. transparent;
This. zedgraphcontrol1.graphpane. xaxis. Scale. textlabels = xtitles;
This. zedgraphcontrol1.graphpane. xaxis. Scale. fontspec. size = 10f;
This. zedgraphcontrol1.graphpane. xaxis. Scale. fontspec. fontcolor = color. blue;
This. zedgraphcontrol1.axischange ();
This. zedgraphcontrol1.graphpane. xaxis. type = axistype. text;
Run, then you can see the static Bar Chart display.
How can we dynamically display data? In winform, we can use timer controls or multithreading. Here we use timer controls.
Drag a tmier control from the toolbox to the form, set its enable to true, and add the following code to its timer1_tick event (note: generally, the database is read in real time as the bar chart data source ):
Double [] arrvalues = getarrvalues ();
// Shift the text items up by 5 user scale units above the bars
String [] xtitles = {"Wang xinxin", "jijie", "Wei Chen", "Wake up", "Zhang Jie", "Chen Chusheng "};
Const float shift = 5;
This. zedgraphcontrol1.graphpane. yaxis. Scale. maxauto = false;
This. zedgraphcontrol1.graphpane. yaxis. Size. max = 10000;
This. zedgraphcontrol1.graphpane. yaxis. Scale. min = 0;
This. zedgraphcontrol1.graphpane. yaxis. Scale. minorstep = 1;
This. zedgraphcontrol1.graphpane. yaxis. Color = color. transparent;
This. zedgraphcontrol1.graphpane. Chart. Fill = New fill (color. White, color. Green, 45.0f );
This. zedgraphcontrol1.graphpane. yaxis. isvisible = false;
For (INT I = 0; I <arrvalues. length; I ++)
{
// Format the label string to have 1 decimal place
String lab = arrvalues [I]. tostring ();
// Create the text item (assumes the X axis is ordinal or text)
// For negative bars, the label appears just above the zero value
Textobj text = new textobj (lab, (float) (I + 1), (float) (arrvalues [I] <0? 0.0: arrvalues [I]) + shift );
// Tell zedgraph to use user scale units for locating the textobj
Text. Location. coordinateframe = coordtype. axisxyscale;
// Alignh the left-Center of the text to the specified point
Text. Location. alignh = alignh. Center;
Text. Location. alignv = alignv. Center;
Text. fontspec. Border. isvisible = false;
Text. fontspec. Fill. isvisible = false;
// Rotate the text 90 degrees
Text. fontspec. angle = 0;
// Add the textobj to the list
This. zedgraphcontrol1.graphpane. graphobjlist. Add (text );
}
Baritem mybar = This. zedgraphcontrol1.graphpane. addbar (null, null, arrvalues, color. Red );
Mybar. Bar. Fill = New fill (color. Red, color. White, color. Red );
Mybar. Bar. Border. Color = color. transparent;
This. zedgraphcontrol1.graphpane. xaxis. Scale. textlabels = xtitles;
This. zedgraphcontrol1.graphpane. xaxis. Scale. fontspec. size = 4.5f;
This. zedgraphcontrol1.graphpane. barsettings. type = bartype. overlay;
This. zedgraphcontrol1.graphpane. xaxis. type = axistype. text;
Int num = 0;
Num = This. zedgraphcontrol1.graphpane. graphobjlist. count;
This. zedgraphcontrol1.graphpane. graphobjlist. removerange (0, num-6 );
This. zedgraphcontrol1.axischange ();
This. zedgraphcontrol1.refresh ();
After running, you can see the bar chart dynamically displayed, isn't it easy?