zoukankan      html  css  js  c++  java
  • 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;   //设置是否交错显示,比如数据多的时间分成两行来显示 

  • 相关阅读:
    c++单例设计模式---17
    c++友元函數---16
    c++const关键字---15
    c++浅拷贝和深拷贝---14
    linux shell 基本语法
    Linux静态库生成
    alsa wav
    Android Butterknife使用方法总结 IOC框架
    利用cglib给javabean动态添加属性,不用在建VO
    钢铁雄心三 通过事件做修改器
  • 原文地址:https://www.cnblogs.com/w-honey/p/10141646.html
Copyright © 2011-2022 走看看