winform程序中chart圖的使用經驗(chart圖的更新)


  • 如何讓chart圖進行刷新並且根據數值重新繪制

首先初始化一個chart

1                 chart1.Titles.Add("柱狀圖數據分析");
2                 chart1.ChartAreas[0].AxisX.Title = "時間(月)";
3                 chart1.ChartAreas[0].AxisY.Title = "單位(元)";
4                 //chart1
5                 Series series = chart1.Series["Series1"];
6                 series.LegendText = "銷售分析";
7                 chart1.DataSource = list;
8                 series.XValueMember = "time";
9                 series.YValueMembers = "value";

進行重新繪制

 1             chart1.Series["Series1"].Points.Clear();//清除之前的圖
 2             Series series = chart1.Series["Series1"];
 3             // 圖示上的文字
 4             series.LegendText = "銷售分析";
 5             //InitializeComponent();
 6             //獲取當前年份查詢
 7             List<LirunModel> list = new LirunDao().getList(year,type);
 8             if (list != null)
 9             {
10                 chart1.DataSource = list;
11                 series.XValueMember = "time";
12                 series.YValueMembers = "value";
13             }
  • time和value是怎么回事?

這是給chart賦值的過程;

首先有一個模型類

    class LirunModel
    {
        private double _value;

        public double Value
        {
            get { return _value; }
            set { _value = value; }
        }
        private string _time;

        public string Time
        {
            get { return _time; }
            set { _time = value; }
        }

    }

然后用一個List保存多個模型類的對象,最后通過這個list給chart的橫縱坐標賦值。

 1         //年份和類型 1是銷售額   2是利潤
 2         public List<LirunModel> getList(int year,int type)
 3         {
 4             List<LirunModel> list=new List<LirunModel>();
 5             
 6             LirunModel m=null;
 7             for (int i = 1; i < 13; i++)
 8             {
 9                 m = new LirunModel();
10                 m.Time = i + "";
11                 if (type == 1)
12                 {
13                     m.Value = getInByMonth(i, year);
14                 }
15                 else if (type == 2)
16                 {
17                     m.Value = getInByMonth(i, year) - getOutByMonth(i, year);
18                 }
19                 list.Add(m);
20             }
21             return list;
22         }

 

  • C#獲取年份

DateTime.Now.Year.ToString();         獲取年份  // 2008

DateTime.Now.ToString();            // 2008-9-4 20:02:10

DateTime.Now.ToString("yyyy-MM-dd");        // 2008-09-04

DateTime.Now.Month.ToString();      獲取月份   // 9

DateTime.Now.DayOfWeek.ToString(); 獲取星期   // Thursday

DateTime.Now.DayOfYear.ToString(); 獲取第幾天   // 248

 

  • c#如何讓chart中的每個橫坐標都顯示

首先,通過chart空間屬性,找到 “ChartAreas集合” ,並且點開

於是來到了ChartAreas集合編輯器,在右邊ChartAreas1屬性中找到 “Axes集合",並點開,如圖
因為我們要設置的是x軸,所以在 ”Axis集合編輯器“ 左邊中選 ”x axis“,
在右邊屬性中選擇 ”IntervalAutoMode“ 在下來項中選中 ”VariableCount“,設定x軸的間隔是可變的,
設定x軸間隔可變
這時,如果x軸標簽過多,可能還不會使得x軸標簽全部顯示出來,這就需要把x軸標簽分為上下兩層顯示
還是在 ”Axis集合編輯器“ 中找到 ”IsStaggered屬性“ 設其值為 ”True“,
在 ”Axis集合編輯器“ 中找到 ”IsStaggered屬性“ 設其值為 ”True“,
接着在運行,成功顯示x軸全部標簽
當然,還有另一種方法,使x軸標簽旋轉90度角顯示,
在設置x軸可變后,在 ”Axis集合編輯器“ 選中 ”Angle“ 選項,設置值為90,
點擊 ”確定“ 退出設置

或者可以添加這句代碼

Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;   //設置是否交錯顯示,比如數據多的時間分成兩行來顯示 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM