page contents

C#中Winform Chart图表控件使用

本文讲述了C#中Winform Chart图表控件使用!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-09-52tgU6Sx632286c14fb94.png本文讲述了C#中Winform Chart图表控件使用!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

根据网上的搜集的代码,自己在vs2019中生成的样式如图

attachments-2022-09-cqUimNi0632286745a13e.png下面是代码,欢迎需要的朋友拿去用!

首先是在工具箱-数据分组中我们就可以看到chart控件,使用时拖拽到界面即可。可以通过属性面板设置其样式,也可以通过代码来调整样式。

总结的部分设置chart样式功能的通用类:

public class ChartHelper

{

    /// <summary>

    /// Name:添加序列

    /// </summary>

    /// <param name="chart">图表对象</param>

    /// <param name="seriesName">序列名称</param>

    /// <param name="chartType">图表类型</param>

    /// <param name="color">颜色</param>

    /// <param name="markColor">标记点颜色</param>

    /// <param name="showValue">是否显示数值</param>

    public static void AddSeries(Chart chart, string seriesName, SeriesChartType chartType, Color color, Color markColor, bool showValue = false)

    {

        chart.Series.Add(seriesName);

        chart.Series[seriesName].ChartType = chartType;

        chart.Series[seriesName].Color = color;

        if (showValue)

        {

            chart.Series[seriesName].IsValueShownAsLabel = true;

            chart.Series[seriesName].MarkerStyle = MarkerStyle.Circle;

            chart.Series[seriesName].MarkerColor = markColor;

            chart.Series[seriesName].LabelForeColor = color;

            chart.Series[seriesName].LabelAngle = -90;

        }

    }


    /// <summary>

    /// Name:设置标题

    /// </summary>

    /// <param name="chart">图表对象</param>

    /// <param name="chartName">图表名称</param>

    public static void SetTitle(Chart chart, string chartName, Font font, Docking docking, Color foreColor)

    {

        chart.Titles.Add(chartName);

        chart.Titles[0].Font = font;

        chart.Titles[0].Docking = docking;

        chart.Titles[0].ForeColor = foreColor;

    }


    /// <summary>

    /// Name:设置样式

    /// </summary>

    /// <param name="chart">图表对象</param>

    /// <param name="backColor">背景颜色</param>

    /// <param name="foreColor">字体颜色</param>

    public static void SetStyle(Chart chart, Color backColor, Color foreColor)

    {

        chart.BackColor = backColor;

        chart.ChartAreas[0].BackColor = backColor;

        chart.ForeColor = Color.Red;

    }


    /// <summary>

    /// Name:设置图例

    /// </summary>

    /// <param name="chart">图表对象</param>

    /// <param name="docking">停靠位置</param>

    /// <param name="align">对齐方式</param>

    /// <param name="backColor">背景颜色</param>

    /// <param name="foreColor">字体颜色</param>

    public static void SetLegend(Chart chart, Docking docking, StringAlignment align, Color backColor, Color foreColor)

    {

        chart.Legends[0].Docking = docking;

        chart.Legends[0].Alignment = align;

        chart.Legends[0].BackColor = backColor;

        chart.Legends[0].ForeColor = foreColor;

    }


    /// <summary>

    /// Name:设置XY轴

    /// </summary>

    /// <param name="chart">图表对象</param>

    /// <param name="xTitle">X轴标题</param>

    /// <param name="yTitle">Y轴标题</param>

    /// <param name="align">坐标轴标题对齐方式</param>

    /// <param name="foreColor">坐标轴字体颜色</param>

    /// <param name="lineColor">坐标轴颜色</param>

    /// <param name="arrowStyle">坐标轴箭头样式</param>

    /// <param name="xInterval">X轴的间距</param>

    /// <param name="yInterval">Y轴的间距</param>

    public static void SetXY(Chart chart, string xTitle, string yTitle, StringAlignment align, Color foreColor, Color lineColor, AxisArrowStyle arrowStyle, double xInterval, double yInterval)

    {

        chart.ChartAreas[0].AxisX.Title = xTitle;

        chart.ChartAreas[0].AxisY.Title = yTitle;

        chart.ChartAreas[0].AxisX.TitleAlignment = align;

        chart.ChartAreas[0].AxisY.TitleAlignment = align;

        chart.ChartAreas[0].AxisX.TitleForeColor = foreColor;

        chart.ChartAreas[0].AxisY.TitleForeColor = foreColor;

        chart.ChartAreas[0].AxisX.LabelStyle = new LabelStyle() { ForeColor = foreColor };

        chart.ChartAreas[0].AxisY.LabelStyle = new LabelStyle() { ForeColor = foreColor };

        chart.ChartAreas[0].AxisX.LineColor = lineColor;

        chart.ChartAreas[0].AxisY.LineColor = lineColor;

        chart.ChartAreas[0].AxisX.ArrowStyle = arrowStyle;

        chart.ChartAreas[0].AxisY.ArrowStyle = arrowStyle;

        chart.ChartAreas[0].AxisX.Interval = xInterval;

        chart.ChartAreas[0].AxisY.Interval = yInterval;

    }


    /// <summary>

    /// Name:设置网格

    /// </summary>

    /// <param name="chart">图表对象</param>

    /// <param name="lineColor">网格线颜色</param>

    /// <param name="xInterval">X轴网格的间距</param>

    /// <param name="yInterval">Y轴网格的间距</param>

    public static void SetMajorGrid(Chart chart, Color lineColor, double xInterval, double yInterval)

    {

        chart.ChartAreas[0].AxisX.MajorGrid.LineColor = lineColor;

        chart.ChartAreas[0].AxisY.MajorGrid.LineColor = lineColor;

        chart.ChartAreas[0].AxisX.MajorGrid.Interval = xInterval;

        chart.ChartAreas[0].AxisY.MajorGrid.Interval = yInterval;

    }

}

在form_load事件中加入下面代码


chart1.Series.Clear();

ChartHelper.AddSeries(chart1, "柱状图", SeriesChartType.Column, Color.Lime, Color.Red, true);

ChartHelper.AddSeries(chart1, "曲线图", SeriesChartType.Spline, Color.Red, Color.Red);

ChartHelper.SetTitle(chart1, "柱状图与曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.White);

ChartHelper.SetStyle(chart1, Color.Transparent, Color.White);

ChartHelper.SetLegend(chart1, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);

ChartHelper.SetXY(chart1, "序号", "数值", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);

ChartHelper.SetMajorGrid(chart1, Color.Gray, 20, 2);


chart2.Series.Clear();

ChartHelper.AddSeries(chart2, "饼状图", SeriesChartType.Pie, Color.Lime, Color.Red, true);

ChartHelper.SetStyle(chart2, Color.Transparent, Color.White);

ChartHelper.SetLegend(chart2, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);


chart3.Series.Clear();

ChartHelper.AddSeries(chart3, "曲线图", SeriesChartType.SplineRange, Color.FromArgb(100, 46, 199, 201), Color.Red, true);

ChartHelper.SetTitle(chart3, "曲线图", new Font("微软雅黑", 12), Docking.Bottom, Color.FromArgb(46, 199, 201));

ChartHelper.SetStyle(chart3, Color.Transparent, Color.White);

ChartHelper.SetLegend(chart3, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);

ChartHelper.SetXY(chart3, "序号", "数值", StringAlignment.Far, Color.White, Color.White, AxisArrowStyle.SharpTriangle, 1, 2);

ChartHelper.SetMajorGrid(chart3, Color.Gray, 20, 2);


chart4.Series.Clear();

ChartHelper.AddSeries(chart4, "饼状图", SeriesChartType.Funnel, Color.Lime, Color.Red, true);

ChartHelper.SetStyle(chart4, Color.Transparent, Color.White);

ChartHelper.SetLegend(chart4, Docking.Top, StringAlignment.Center, Color.Transparent, Color.White);


RefreshData();

添加一些方法


public void RefreshData()

{

    List<int> x1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

    List<int> y1 = new List<int>();

    Random ra = new Random();

    y1 = new List<int>() {

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10),

        ra.Next(1, 10)

    };

    RefreshChart(x1, y1, "chart1");

    RefreshChart(x1, y1, "chart2");

    RefreshChart(x1, y1, "chart3");

    RefreshChart(x1, y1, "chart4");

}


public delegate void RefreshChartDelegate(List<int> x, List<int> y, string type);

public void RefreshChart(List<int> x, List<int> y, string type)

{

    if (type == "chart1")

    {

        if (this.chart1.InvokeRequired)

        {

            RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);

            this.Invoke(stcb, new object[] { x, y, type });

        }

        else

        {

            chart1.Series[0].Points.DataBindXY(x, y);

            chart1.Series[1].Points.DataBindXY(x, y);

        }

    }

    else if (type == "chart2")

    {

        if (this.chart2.InvokeRequired)

        {

            RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);

            this.Invoke(stcb, new object[] { x, y, type });

        }

        else

        {

            chart2.Series[0].Points.DataBindXY(x, y);

            List<Color> colors = new List<Color>() {

                Color.Red,

                Color.DarkRed,

                Color.IndianRed,

                Color.MediumVioletRed,

                Color.OrangeRed,

                Color.PaleVioletRed,

                Color.Purple,

                Color.DarkOrange,

                Color.Maroon,

                Color.LightCoral,

                Color.LightPink,

                Color.Magenta

            };

            DataPointCollection points = chart2.Series[0].Points;

            for (int i = 0; i < points.Count; i++)

            {

                points[i].Color = colors[i];

            }

        }

    }

    else if (type == "chart3")

    {

        if (this.chart3.InvokeRequired)

        {

            RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);

            this.Invoke(stcb, new object[] { x, y, type });

        }

        else

        {

            chart3.Series[0].Points.DataBindXY(x, y);

        }

    }

    else if (type == "chart4")

    {

        if (this.chart4.InvokeRequired)

        {

            RefreshChartDelegate stcb = new RefreshChartDelegate(RefreshChart);

            this.Invoke(stcb, new object[] { x, y, type });

        }

        else

        {

            chart4.Series[0].Points.DataBindXY(x, y);

            List<Color> colors = new List<Color>() {

                Color.Red,

                Color.DarkRed,

                Color.IndianRed,

                Color.MediumVioletRed,

                Color.OrangeRed,

                Color.PaleVioletRed,

                Color.Purple,

                Color.DarkOrange,

                Color.Maroon,

                Color.LightCoral,

                Color.LightPink,

                Color.Magenta

            };

            DataPointCollection points = chart4.Series[0].Points;

            for (int i = 0; i < points.Count; i++)

            {

                points[i].Color = colors[i];

            }

        }

    }

}

最后加个定时器


private void timer1_Tick(object sender, EventArgs e)

{

    new Thread(new ThreadStart(RefreshData)).Start();

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-09-15 09:58
  • 阅读 ( 1373 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章