ZedGraph是一个不错的图表报表生成器,以前曾经用过,在asp.net2.0beta版里面。

现在忽然有可能用到了,但是却一时找不着以前的资料了,方才发现写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

 

1Asp页面上

   基本上没有什么技巧,放上这个控件就可以。
 


  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);

 


 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:几种图的使用


  1
  2/// <summary>
  3/// make all Stat info and put them into the GraphPane(ZedGraph)
  4/// </summary>

  5public 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     #region private class GroupScore for recode the group info
 51     private class GroupScore
 52    {
 53        public GroupScore(string groupName)
 54        {
 55           this.GroupName=groupName;
 56           Count = 0;
 57           Score = 0;
 58        }

 59        public string GroupName;
 60        public int Count;
 61        public int Score;
 62        public int Avg
 63        {
 64            get
 65            {
 66                if (Count != 0)
 67                    return Score / Count;
 68                else
 69                    return 0;
 70            }

 71        }

 72
 73    }

 74     #endregion

 75    /// <summary>
 76    /// a test,50-60;60-70 how many students in that scope
 77    /// by pie item
 78    /// </summary>
 79    /// <param name="courseId"></param>
 80    /// <param name="testId"></param>

 81    public void ScoreScopeCompare(int courseId, int testId)
 82    {
 83        //add 10 pies first
 84        PieItem[] items = new PieItem[10];
 85        for (int i = 0; i < 10; i++)
 86        {
 87           items[i]= this._pane.AddPieSlice(0this.GetRandomColor(), 0, (i*10).ToString() + "-" + ((i + 1* 10).ToString());
 88           items[i].LabelType = PieLabelType.Name_Value_Percent;
 89        }

 90        ObjectSet set = _page.BizManager.GetObjectSet(typeof(EtScore), "testId='" + testId + "'");
 91        for (int i = 0; i < set.Count; i++)
 92        {
 93            int grad = (set[i] as EtScore).Score/10;
 94            items[grad].Value++;
 95        }

 96    }

 97    /// <summary>
 98    /// all student, all test
 99    /// by curve line
100    /// </summary>
101    /// <param name="courseId"></param>

102    public void AllStudentsScore(int courseId)
103    {
104        //y: scores
105        //x: students
106        //multiline
107        ObjectSet set = _page.BizManager.GetObjectSet(typeof(EtSubscribe), "CourseId='" + courseId + "'");
108        for (int i = 0; i < set.Count; i++)
109        {
110            OneStudentScore(courseId, (set[i] as EtSubscribe).StudentId);
111        }

112    }

113
114    /// <summary>
115    /// all student at one test
116    /// by stick
117    /// </summary>
118    /// <param name="courseId"></param>
119    /// <param name="testId"></param>

120    public void AllStudentsScore(int courseId, int testId)
121    {
122        //y: score of a student
123        //x: student name, one by one
124        ObjectSet set = _page.BizManager.GetObjectSet(typeof(EtScore), "testId='" + testId + "'");
125        this._pane.XAxis.Max = set.Count + 1;
126        this._pane.XAxis.Step = 1;
127        this._pane.XAxis.Title = "students";
128        this._pane.YAxis.Title = "scores";
129        this._pane.Title = "socres in the test";
130        for(int i=0;i<set.Count;i++)
131        {
132            EtScore socre = set[i] as EtScore;
133            this._pane.AddStick(socre.StudentName, new double[] { i+1 }new double[] { socre.Score }, GetRandomColor());
134        }

135    }

136   
137    /// <summary>
138    /// a student,all test
139    /// </summary>
140    /// <param name="courseId"></param>
141    /// <param name="studentId"></param>

142    public void OneStudentScore(int courseId, int studentId)
143    {
144        //get all sorces in all test. 
145        //then get test name
146        //show
147        //y: score of a student
148        //x: test times, how to chang it test name and date?
149        ObjectSet set = _page.BizManager.GetObjectSet(typeof(EvScore), "courseId='" + courseId + "' and studentId='" + studentId + "'");
150        this._pane.XAxis.Max = set.Count + 1;
151        this._pane.XAxis.Step = 1;
152        this._pane.XAxis.Title = "tests";
153        this._pane.YAxis.Title = "scores";
154        this._pane.Title = "socres in all the test";
155        double[] tests = new double[set.Count];
156        double[] scores = new double[set.Count];
157        string name = "";
158        for (int i = 0; i < set.Count; i++)
159        {
160            EvScore socre = set[i] as EvScore;
161            tests[i] = i + 1;
162            scores[i] = socre.Score;
163            name = socre.StudentName;
164
165        }

166        LineItem line = this._pane.AddCurve(name + " score line.", tests, scores, GetRandomColor());
167        
168    }

169    private Random _random = new Random();
170    private Color GetRandomColor()
171    {
172       return Color.FromArgb(_random.Next()); 
173    }

174}

175
posted on 2007-03-28 10:06  冷火  阅读(718)  评论(1编辑  收藏  举报