正好做个WINFROM 的小软件,用到chart 图。在网上看到Zedgraph这个开源的
控件比较好。有个基本的问题弄了半天没解决。就是在 form_load 调用creatgraph 之后,图表正常出来了。或者是根据条件第一次选择。
再根据条件筛选数据之后,图表里面原来生成的图去不掉,而是第一次生成的一起显示。
看了相关的介绍。说要 CurveList.Clear();但是我实验了半天了。也没搞定。希望做个给帮个忙。再次感谢。代码如下:
 
//在btnSearchByDate_Click事件里怎么先清空原来的图,还是在别的地方?
private void btnSearchByDate_Click(object sender, EventArgs e)
        {            zedGraphControl1.GraphPane.CurveList.Clear();
            CreateGraph(zedGraphControl1, comboBox1.Text.ToString());
            //SetSize();        
            //zedGraphControl1.AxisChange();             
            
            
        }        private void CurrentRate_Load(object sender, EventArgs e)
        {
            lb = new lBusiness();
            totalNum = (int)lb.GetTotalRecord("Library_Book");
            int index = comboBox1.FindString(DateTime.Now.ToShortDateString().Substring(0, 4));
            comboBox1.SelectedIndex = index;            //GraphPane pane = zedGraphControl1.GraphPane;
            //pane.XAxis.Min = 40;
            //pane.XAxis.Max = 160; 
           // Setup the graph
           // CreateGraph(zedGraphControl1, comboBox1.Text.ToString());
            //// Size the control to fill the form with a margin
           // SetSize();
            
        }        private void zedGraphControl1_Resize(object sender, EventArgs e)
        {
            //SetSize();
        }        private void SetSize()
        {
            //zedGraphControl1.GraphPane.CurveList.Clear();
            //zedGraphControl1.Refresh();
            zedGraphControl1.Location = new Point(0, 5);
            // Leave a small margin around the outside of the control
            zedGraphControl1.Size = new Size(ClientRectangle.Width,ClientRectangle.Height - 180);
        }        private void CreateGraph(ZedGraphControl zg1,string year)
        {
            // get a reference to the GraphPane
   
           //zg1.MasterPane.PaneList.Clear();
            GraphPane myPane = zg1.GraphPane;
           myPane.CurveList.Clear();
           //PointPairList list = new PointPairList();            myPane.Title.Text = "图书流通率";
            myPane.XAxis.Title.Text = "月份";
            myPane.YAxis.Title.Text = " 图书相对册数";
            //string[] labels new string[];;// = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
            ArrayList al = new ArrayList();
            al = lb.GetCurrentRateByYear(year);
            string[] labels = new string[al.Count];
            double[] y = new double[al.Count];
            for (int i = 0; i < al.Count; i++)
            {
                y[i] = Convert.ToDouble(al[i]);
                //list.Add(1,y[i]);
                labels[i] = "("+Convert.ToString(i+1)+")"+Convert.ToString(Math.Round(Convert.ToDouble(al[i])*100 / Convert.ToDouble(totalNum),3))+"%";
            }
                        // Generate a red bar with "Curve 1" in the legend
            BarItem myBar = myPane.AddBar("当月流通册数", null,y, Color.Red);
            myBar.Bar.Fill = new Fill(Color.Red, Color.White,Color.Red);            // Generate a blue bar with "Curve 2" in the legend
            myBar = myPane.AddBar("总册数", null,  Color.Blue);
            myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);            myPane.XAxis.Scale.TextLabels = labels;
            myPane.XAxis.Type = AxisType.Text;
            //myPane.XAxis.Scale.Max = 110;
            myPane.Chart.Fill = new Fill(Color.White,Color.FromArgb(255, 255, 166), 90F);
            //myPane.Legend.Fill = new Fill(Color.White,Color.FromArgb(255, 255, 250), 90F);
            zg1.AxisChange();
            SetSize();
            // expand the range of the Y axis slightly to accommodate the labels
            myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;
              // Create TextObj's to provide labels for each bar
            BarItem.CreateBarLabels(myPane, false, "f0");        }

解决方案 »

  1.   

    哈哈哈,刚好碰上我了,你要首先把myPane 里面的图像对象清空掉// Get a reference to the GraphPane instance in the ZedGraphControl
    GraphPane myPane = zgWaveChart.GraphPane;// Set the titles and axis labels
    myPane.Title =  
    myPane.XAxis.Title =  
    myPane.YAxis.Title = //清空绘制区
    myPane.CurveList.Clear();
    myPane.GraphItemList.Clear();// Generate a red curve with diamond symbols, and "Alpha" in the legend
    LineItem myCurve = 
    不管怎么样先把mypane清空,再画