Posted on 2007-03-23 17:52
随心所欲 阅读(2878)
评论(14) 编辑 收藏 网摘 所属分类:
Asp.net2.0 、
其他控件
ZedGraph是一个不错的图表报表生成器,以前曾经用过,在asp.net2.0的beta版里面。
现在忽然有可能用到了,但是却一时找不着以前的资料了,方才发现写blog的伟大作用。所以赶紧先把它记下来,以备后用。
1:这是for asp.net的版本。现在win版本的例子代码很多。
2:这里提供的只是一个基本的用法。
3:详细信息:http://zedgraph.org/wiki/index.php?title=Display_Dynamic_or_Real-Time_Data
版本:4.2.1.35091
1:Asp页面上
基本上没有什么技巧,放上这个控件就可以。

Asp Control
1
<zgw:ZedGraphWeb ID="zgw_graph" runat="server" Height="600" OnRenderGraph="zgw_graph_RenderGraph"
2
Width="800">
3
<XAxis Color="Black" Cross="0" CrossAuto="True" GridColor="Black" GridDashOff="5"
4
GridDashOn="1" GridPenWidth="1" IsInsideTic="True" IsMinorInsideTic="True" IsMinorOppositeTic="True"
5
IsMinorTic="True" IsOmitMag="False" IsOppositeTic="True" IsPreventLabelOverlap="False"
6
IsReverse="False" IsShowGrid="False" IsShowMinorGrid="False" IsShowTitle="True"
7
IsTic="True" IsTicsBetweenLabels="True" IsUseTenPower="False" IsVisible="True"
8
IsZeroLine="False" Max="0" MaxAuto="True" MaxGrace="0.1" Min="0" MinAuto="True"
9
MinGrace="0.1" MinorGridColor="Gray" MinorGridDashOff="10" MinorGridDashOn="1"
10
MinorGridPenWidth="1" MinorStepAuto="True" MinorTicSize="2.5" MinSpace="0" ScaleAlign="Center"
11
ScaleFormat="g" ScaleFormatAuto="True" ScaleMag="0" ScaleMagAuto="True" StepAuto="True"
12
TicPenWidth="1" TicSize="5" Title="" Type="Linear">
13
<TitleFontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
14
IsUnderline="False" Size="14" StringAlignment="Center">
15
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
16
RangeMax="0" RangeMin="0" Type="None" />
17
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
18
</TitleFontSpec>
19
<ScaleFontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
20
IsUnderline="False" Size="14" StringAlignment="Center">
21
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
22
RangeMax="0" RangeMin="0" Type="None" />
23
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
24
</ScaleFontSpec>
25
</XAxis>
26
<AxisBorder Color="Black" InflateFactor="0" IsVisible="True" PenWidth="1" />
27
<Y2Axis Color="Black" Cross="0" CrossAuto="True" GridColor="Black" GridDashOff="5"
28
GridDashOn="1" GridPenWidth="1" IsInsideTic="True" IsMinorInsideTic="True" IsMinorOppositeTic="True"
29
IsMinorTic="True" IsOmitMag="False" IsOppositeTic="True" IsPreventLabelOverlap="False"
30
IsReverse="False" IsShowGrid="False" IsShowMinorGrid="False" IsShowTitle="True"
31
IsTic="True" IsTicsBetweenLabels="True" IsUseTenPower="False" IsVisible="False"
32
IsZeroLine="True" Max="0" MaxAuto="True" MaxGrace="0.1" Min="0" MinAuto="True"
33
MinGrace="0.1" MinorGridColor="Gray" MinorGridDashOff="10" MinorGridDashOn="1"
34
MinorGridPenWidth="1" MinorStepAuto="True" MinorTicSize="2.5" MinSpace="0" ScaleAlign="Center"
35
ScaleFormat="g" ScaleFormatAuto="True" ScaleMag="0" ScaleMagAuto="True" StepAuto="True"
36
TicPenWidth="1" TicSize="5" Title="" Type="Linear">
37
<TitleFontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
38
IsUnderline="False" Size="14" StringAlignment="Center">
39
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
40
RangeMax="0" RangeMin="0" Type="None" />
41
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
42
</TitleFontSpec>
43
<ScaleFontSpec Angle="-90" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
44
IsUnderline="False" Size="14" StringAlignment="Center">
45
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
46
RangeMax="0" RangeMin="0" Type="None" />
47
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
48
</ScaleFontSpec>
49
</Y2Axis>
50
<AxisFill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
51
RangeMax="0" RangeMin="0" Type="Brush" />
52
<PaneBorder Color="Black" InflateFactor="0" IsVisible="True" PenWidth="1" />
53
<AxisRect Height="0" Width="0" X="0" Y="0" />
54
<PieRect Height="0" Width="0" X="0" Y="0" />
55
<YAxis Color="Black" Cross="0" CrossAuto="True" GridColor="Black" GridDashOff="5"
56
GridDashOn="1" GridPenWidth="1" IsInsideTic="True" IsMinorInsideTic="True" IsMinorOppositeTic="True"
57
IsMinorTic="True" IsOmitMag="False" IsOppositeTic="True" IsPreventLabelOverlap="False"
58
IsReverse="False" IsShowGrid="False" IsShowMinorGrid="False" IsShowTitle="True"
59
IsTic="True" IsTicsBetweenLabels="True" IsUseTenPower="False" IsVisible="True"
60
IsZeroLine="True" Max="0" MaxAuto="True" MaxGrace="0.1" Min="0" MinAuto="True"
61
MinGrace="0.1" MinorGridColor="Gray" MinorGridDashOff="10" MinorGridDashOn="1"
62
MinorGridPenWidth="1" MinorStepAuto="True" MinorTicSize="2.5" MinSpace="0" ScaleAlign="Center"
63
ScaleFormat="g" ScaleFormatAuto="True" ScaleMag="0" ScaleMagAuto="True" StepAuto="True"
64
TicPenWidth="1" TicSize="5" Title="" Type="Linear">
65
<TitleFontSpec Angle="-180" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
66
IsUnderline="False" Size="14" StringAlignment="Center">
67
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
68
RangeMax="0" RangeMin="0" Type="None" />
69
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
70
</TitleFontSpec>
71
<ScaleFontSpec Angle="90" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
72
IsUnderline="False" Size="14" StringAlignment="Center">
73
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
74
RangeMax="0" RangeMin="0" Type="None" />
75
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
76
</ScaleFontSpec>
77
</YAxis>
78
<PaneFill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
79
RangeMax="0" RangeMin="0" Type="Solid" />
80
<FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
81
IsUnderline="False" Size="16" StringAlignment="Center">
82
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
83
RangeMax="0" RangeMin="0" Type="None" />
84
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
85
</FontSpec>
86
<Legend IsHStack="True" IsVisible="True" Position="Top">
87
<Location AlignH="Left" AlignV="Center" CoordinateFrame="AxisFraction" Height="0"
88
Width="0" X="0" X1="0" Y="0" Y1="0">
89
<Rect Height="0" Width="0" X="0" Y="0" />
90
<TopLeft X="0" Y="0" />
91
<BottomRight X="0" Y="0" />
92
</Location>
93
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
94
RangeMax="0" RangeMin="0" Type="Brush" />
95
<Rect Height="0" Width="0" X="0" Y="0" />
96
<Border Color="Black" InflateFactor="0" IsVisible="True" PenWidth="1" />
97
<FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
98
IsUnderline="False" Size="12" StringAlignment="Center">
99
<Fill AlignH="Center" AlignV="Center" Color="White" IsScaled="True" IsVisible="True"
100
RangeMax="0" RangeMin="0" Type="Solid" />
101
<Border Color="Black" InflateFactor="0" IsVisible="False" PenWidth="1" />
102
</FontSpec>
103
</Legend>
104
</zgw:ZedGraphWeb>
2:连接到处理事件,
这个和Win的处理不一样。看这里传入的参数:System.Drawing.Graphics g, MasterPane pane,注意最后的更新方式:pane[0].AxisChange(g);

Event to Draw the Chart
1
protected void zgw_graph_RenderGraph(System.Drawing.Graphics g, MasterPane pane)
2
{
3
if (Request.Params["type"] == null)
4
return;
5
//get params
6
int courseId=0,testId=0,studentId=0;
7
if (Request.Params["cid"] != null && Request.Params["cid"].ToString()!="")
8
courseId = Convert.ToInt32(Request.Params["cid"]);
9
if (Request.Params["tid"] != null && Request.Params["tid"].ToString() != "")
10
testId = Convert.ToInt32(Request.Params["tid"]);
11
if (Request.Params["sid"] != null && Request.Params["sid"].ToString() != "")
12
studentId = Convert.ToInt32(Request.Params["sid"]);
13
14
SorceStat stat = new SorceStat(pane[0]);
15
switch (Request.Params["type"].ToLower())
16
{
17
case "all_students_score_in_one_test":
18
stat.AllStudentsScore(courseId,testId);
19
break;
20
case "one_students_score_in_all_test":
21
stat.OneStudentScore(courseId, studentId);
22
break;
23
case "all_students_score_in_all_test":
24
stat.AllStudentsScore(courseId);
25
break;
26
case "scope_score_in_one_test":
27
stat.ScoreScopeCompare(courseId, testId);
28
break;
29
case "group_avg_score_in_one_test":
30
stat.GroupAvgCompare(courseId, testId);
31
break;
32
case "group_avg_score_in_all_test":
33
//stat.GroupAvgCompare(courseId); null now
34
break;
35
default:
36
break;
37
}
38
39
pane[0].AxisChange(g);
40
}
41
3:几种图的使用

Code to Draw every chart
1
2
/**//// <summary>
3
/// make all Stat info and put them into the GraphPane(ZedGraph)
4
/// </summary>
5
public class SorceStat
6

{
7
private GraphPane _pane;
8
private BasePage _page = new BasePage();
9
public SorceStat(GraphPane pane)
10
{
11
this._pane = pane;
12
}
13
/**//// <summary>
14
/// all the test
15
/// by curve line
16
/// </summary>
17
/// <param name="courseId"></param>
18
public void GroupAvgCompare(int courseId)
19
{
20
21
}
22
/**//// <summary>
23
/// just a test
24
/// by baritem
25
/// </summary>
26
/// <param name="courseId"></param>
27
/// <param name="testId"></param>
28
public void GroupAvgCompare(int courseId, int testId)
29
{
30
//
31
//get all then get the ave of it. as it can not use sql derectly
32
ObjectSet scores=_page.BizManager.GetObjectSet(typeof(EvGroupScore),"courseId='"+courseId+"' and testId='"+testId+"'");
33
Hashtable group = new Hashtable();
34
for (int i = 0; i < scores.Count; i++)
35
{
36
EvGroupScore score = scores[i] as EvGroupScore;
37
if (!group.Contains(score.GroupName))
38
group.Add(score.GroupName, new GroupScore(score.GroupName));
39
(group[score.GroupName] as GroupScore).Count++;
40
(group[score.GroupName] as GroupScore).Score += score.Score;
41
}
42
int index = 0;
43
foreach (DictionaryEntry de in group)
44
{
45
GroupScore score = de.Value as GroupScore;
46
this._pane.AddBar(score.GroupName, new double[]
{ index++ } , new double[]
{ score.Avg }, GetRandomColor());
47
}
48
49
}
50
private class GroupScore for recode the group info#region private class GroupSc