绘制两条折线图

使用微软Chart控件,绘制两条折线图。

 

绘制两条折线图_第1张图片

 

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 图表练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // 设置曲线的样式 
            Series series1 = new Series();
            series1 = chart1.Series[0];
            series1.ChartType = SeriesChartType.Spline;
            series1.BorderWidth = 2;
            series1.Color = Color.Red;
            series1.LegendText = "第一条线";

            Series series2 = new Series();
            series2 = chart1.Series[1];
            series2.ChartType = SeriesChartType.Line;
            series2.BorderWidth = 2;
            series2.Color = Color.Green;
            series2.LegendText = "第二条线";


            // 准备数据 
            int[] myValues1 = { 3, 4, 5, 6, 7, 8, 8, 4, 3, 2, 9, 6, 8, 4, 2, 9, 1, 2, 5, 6, 8, 9, 1, 2, 3, 4, 4, 8 };

            int[] myValues2 = { 13, 13, 14, 15, 16, 17, 18, 19, 10, 14, 17, 19, 14, 13, 19, 11, 12, 15, 16, 17, 19, 11 };

            // 在chart中显示数据 

            int xValue1 = 0;
            int xValue2 = 0;
            foreach (int yValue1 in myValues1)
            {
                series1.Points.AddXY(xValue1, yValue1);
                xValue1++;
            }

            foreach (int yValue2 in myValues2)
            {
                series2.Points.AddXY(xValue2, yValue2);
                xValue2++;

            }

            // 设置显示范围 
            ChartArea myChartArea = chart1.ChartAreas[0];
            myChartArea.AxisX.Minimum = 0;
            myChartArea.AxisX.Maximum = 30;
            myChartArea.AxisY.Minimum = 0;
            myChartArea.AxisY.Maximum = 20;
        }
    }
}

 

 

 

 

 

你可能感兴趣的:(绘制两条折线图)