zoukankan      html  css  js  c++  java
  • Demo of Microsoft Chart Controls for Microsoft .NET Framework 3.5

    控件介绍

    Demo展示

    主要代码

    代码
    /*
    * author:Joey Zhao
    * date:2010-8-18
    * describe: a simple demo of MsChart control
    */
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.DataVisualization.Charting;

    namespace MsChartDemo
    {
    public partial class FormChart : Form
    {
    private int _maxXValue;//横坐标最大值
    private int _maxYValue;//纵坐标最大值
    private SeriesChartType _chartType = SeriesChartType.Column;//显示样式
    private bool _show3dStyle;//是否显示3D效果
    private string _labelStyle = "Auto";//lableStyle用户属性值

    private List<Entity> m_listXrayFlux = new List<Entity>();//假数据,测试用


    //构造函数
    public FormChart()
    {
    this.InitializeComponent();
    _maxXValue
    = 10;
    _maxYValue
    = 100;
    this.InitControls();
    this.BindData();
    }

    //绑定数据
    private void BindData()
    {
    //绑定Series1
    Random random = new Random();
    for (int i = 0; i < 10; i++)
    {
    this.chart1.Series[0].Points.AddY(random.Next(0, 100));
    }

    //绑定Series3,Series3
    List<MyEntity> list = new List<MyEntity>();
    MyEntity ent
    = null;
    for (int i = 0; i < 20; i++)
    {
    ent
    = new MyEntity();
    ent.X
    = i;
    ent.Y
    = random.Next(0, 100);
    ent.Z
    = random.Next(0, 100);
    list.Add(ent);
    }
    this.chart1.DataSource = list;
    this.chart1.Series[2].XValueMember = "X";
    this.chart1.Series[2].YValueMembers = "Y";
    this.chart1.Series[1].XValueMember = "X";
    this.chart1.Series[1].YValueMembers = "Z";
    }

    //初始化控件
    private void InitControls()
    {
    //
    //trackBarX控件
    //
    this.trackBarX.Minimum = _maxXValue;
    this.trackBarX.Maximum = 100;
    this.trackBarX.ValueChanged += delegate(object o, EventArgs e)
    {
    _maxXValue
    = this.trackBarX.Value;
    this.UpDateSetting();
    };
    //
    //trackBarY控件
    //
    this.trackBarY.Minimum = _maxYValue;
    this.trackBarY.Maximum = 500;
    this.trackBarY.ValueChanged += delegate(object o, EventArgs e)
    {
    _maxYValue
    = this.trackBarY.Value;
    this.UpDateSetting();
    };
    //
    //comboBoxStyle控件
    //
    this.comboBoxStyle.DropDownStyle = ComboBoxStyle.DropDownList;
    string[] array = Enum.GetNames(typeof(SeriesChartType));
    for (int i = 0; i < array.Length; i++)
    {
    this.comboBoxStyle.Items.Add(array[i]);
    }
    this.comboBoxStyle.Text = SeriesChartType.Column.ToString();
    this.comboBoxStyle.SelectedIndexChanged += delegate(object o, EventArgs e)
    {
    if (this.comboBoxStyle.Text == string.Empty)
    {
    return;
    }
    _chartType
    = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), this.comboBoxStyle.Text);
    this.UpDateSetting();
    };
    //
    //ComboBoxLableStyle控件
    //
    this.comboBoxLableStyle.Items.AddRange(
    new object[] {"Auto", "Top", "Bottom", "Right",
    "Left", "TopLeft", "TopRight", "BottomLeft", "BottomRight", "Center" });
    this.comboBoxLableStyle.SelectedIndex = 0;
    this.comboBoxLableStyle.DropDownStyle = ComboBoxStyle.DropDownList;

    this.comboBoxLableStyle.SelectedIndexChanged += delegate(object o, EventArgs e)
    {
    _labelStyle
    = this.comboBoxLableStyle.Text;
    this.UpDateSetting();
    };
    //
    //Chart控件
    //
    this.chart1.Series[0].IsValueShownAsLabel = true;
    this.chart1.Series[1].IsValueShownAsLabel = true;

    this.chart1.ChartAreas[0].AxisX.Title = "ChartArea1";
    this.chart1.ChartAreas[1].AxisX2.Title = "ChartArea2";

    this.chart1.Series[0].XAxisType = AxisType.Secondary;//坐标样式
    this.chart1.Series[0].YAxisType = AxisType.Secondary;//坐标样式
    //
    //3D按钮
    //
    this.button3D.Click += delegate(object o, EventArgs e)
    {
    _show3dStyle
    = !_show3dStyle;
    this.UpDateSetting();
    };
    }

    //更新Chart控件
    private void UpDateSetting()
    {
    foreach (var item in this.chart1.Series)
    {
    item.ChartType
    = _chartType;
    item[
    "LabelStyle"] = _labelStyle;
    }
    this.chart1.ChartAreas[0].AxisX2.Maximum = _maxXValue;
    this.chart1.ChartAreas[0].AxisY2.Maximum = _maxYValue;

    this.chart1.ChartAreas[1].AxisX.Maximum = _maxXValue;
    this.chart1.ChartAreas[1].AxisY.Maximum = _maxYValue;

    this.chart1.ChartAreas[0].Area3DStyle.Enable3D = _show3dStyle;
    this.chart1.ChartAreas[1].Area3DStyle.Enable3D = _show3dStyle;
    }
    }
    class MyEntity
    {
    public int X
    {
    get;
    set;
    }
    public int Y
    {
    get;
    set;
    }
    public int Z
    {
    get;
    set;
    }
    }
    }
    主要概念

    • ChartArea:包括一组到两组横纵坐标,一个或多个Series放在CharArea上。一个Chart控件可以包含多个ChartArea
    • Series:一组数据,包含了构成图形的一系列坐标点
  • 相关阅读:
    [无聊测试赛] T12 道路
    [无聊测试赛] T9 矩阵覆盖
    [无聊测试赛] T10 所驼门王的宝藏
    [无聊测试赛] T8 佳佳的魔法药水
    [无聊测试赛] T7 豪华游轮
    [无聊测试赛] T6 排行榜
    【模板】三分求函数极值
    树的重心模板
    倍增最近公共祖先(LCA)
    ccf201612题解
  • 原文地址:https://www.cnblogs.com/zzy0471/p/1802808.html
Copyright © 2011-2022 走看看