c# chart control graph

c# chart control graph

SeriesChartType.Line polyline SeriesChartType.Spline curve

using System;
using System.Collections.Generic;
using System. ComponentModel;
using System.Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
using System. Windows. Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WindowsApp
{<!-- -->
    public partial class FormMain : Form
    {<!-- -->
        public FormMain()
        {<!-- -->
            InitializeComponent();
        }

        /// <summary>
        /// line chart
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLine_Click(object sender, EventArgs e)
        {<!-- -->
            // list
            List<int> ListPoint = new List<int>();
            Random random = new Random();

            int num = 0;
            for (int i = 0; i < 10; i ++ )
            {<!-- -->
                num = random.Next(0, 10); // random number
                ListPoint. Add(num);
            }

            // clear all nodes
            chartLine.Series[0].Points.Clear();

            for (int i = 0; i < ListPoint. Count; i ++ )
            {<!-- -->
                // add node
                chartLine.Series[0].Points.AddXY(i + 1, ListPoint[i]);
            }
        }

        /// <summary>
        /// line chart/pie chart
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPie_Click(object sender, EventArgs e)
        {<!-- -->
            string[] x = new string[] {<!-- --> "January", "February", "March", "April", "May" };
            double[] y = new double[] {<!-- --> 500, 900, 700, 650, 450 };

            // title
            chartView.Titles.Add("Polyline example");
            chartView.Titles[0].ForeColor = Color.Blue;
            chartView.Titles[0].Font = new Font("Microsoft Yahei", 12f, FontStyle.Regular);
            chartView.Titles[0].Alignment = ContentAlignment.TopCenter;
            chartView.Titles.Add("right title");
            chartView.Titles[1].ForeColor = Color.Blue;
            chartView.Titles[1].Font = new Font("Microsoft Yahei", 8f, FontStyle.Regular);
            chartView.Titles[1].Alignment = ContentAlignment.TopRight;

            // control background
            chartView.BackColor = Color.Transparent;
            // Chart area background
            chartView.ChartAreas[0].BackColor = Color.Transparent;
            chartView.ChartAreas[0].BorderColor = Color.Transparent;
            // X-axis label spacing
            chartView.ChartAreas[0].AxisX.Interval = 1;
            chartView.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;
            chartView.ChartAreas[0].AxisX.LabelStyle.Angle = -30;
            chartView.ChartAreas[0].AxisX.TitleFont = new Font("Microsoft Yahei", 14f, FontStyle.Regular);
            chartView.ChartAreas[0].AxisX.TitleForeColor = Color.Black;

            // X axis color
            //chartView.ChartAreas[0].AxisX.LineColor = ColorTranslator.FromHtml("#808080");
            chartView.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Black;
            chartView.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Microsoft Yahei", 10f, FontStyle.Regular);
            // X axis title
            chartView.ChartAreas[0].AxisX.Title = "Month";
            chartView.ChartAreas[0].AxisX.TitleFont = new Font("Microsoft Yahei", 10f, FontStyle.Regular);
            chartView.ChartAreas[0].AxisX.TitleForeColor = Color.Black;
            chartView.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
            // X-axis network line
            chartView.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
            //chartView.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#E6E6FA");

            // Y axis color
            //chartView.ChartAreas[0].AxisY.LineColor = ColorTranslator.FromHtml("#808080");
            chartView.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black;
            chartView.ChartAreas[0].AxisY.LabelStyle.Font = new Font("Microsoft Yahei", 10f, FontStyle.Regular);
            // Y axis title
            chartView.ChartAreas[0].AxisY.Title = "Quantity (unit)";
            chartView.ChartAreas[0].AxisY.TitleFont = new Font("Microsoft Yahei", 10f, FontStyle.Regular);
            chartView.ChartAreas[0].AxisY.TitleForeColor = Color.Black;
            chartView.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
            chartView.ChartAreas[0].AxisY.ToolTip = "Quantity (unit)";
            // Y axis grid lines
            chartView.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
            //chartView.ChartAreas[0].AxisY.MajorGrid.LineColor = ColorTranslator.FromHtml("#E6E6FA");

            //chartView.ChartAreas[0].AxisY2.LineColor = Color.Transparent;

            // background gradient
            chartView.ChartAreas[0].BackGradientStyle = GradientStyle.None;

            // legend style
            Legend legend = new Legend("Sales");
            legend.Title = "Legend";
            legend.TitleBackColor = Color.Transparent;
            legend.BackColor = Color.Transparent;
            legend.TitleForeColor = Color.Black;
            legend.TitleFont = new Font("Microsoft Yahei", 10f, FontStyle.Regular);
            legend.Font = new Font("Microsoft Yahei", 8f, FontStyle.Regular);
            legend.ForeColor = Color.Black;

            chartView.Series[0].XValueType = ChartValueType.String; // Set the value type on the X axis
            chartView.Series[0].Label = "#VAL"; // Set the value of X Y displayed
            chartView.Series[0].LabelForeColor = Color.Blue;
            chartView.Series[0].ToolTip = "#VALX:#VAL(Taiwan)"; // Move the mouse to the corresponding point to display the value
            chartView.Series[0].ChartType = SeriesChartType.Line; //Chart type Polyline Line Pie chart Pie

            chartView.Series[0].Color = Color.SkyBlue;
            chartView.Series[0].LegendText = legend.Name;
            chartView.Series[0].IsValueShownAsLabel = true;
            chartView.Series[0].LabelForeColor = Color.Black;
            chartView.Series[0].CustomProperties = "DrawingStyle = Cylinder";
            chartView.Series[0].CustomProperties = "PieLabelStyle = Outside";
            chartView.Legends.Add(legend);
            chartView.Legends[0].Position.Auto = true;
            chartView.Series[0].IsValueShownAsLabel = true;
            // Whether to display the legend
            chartView.Series[0].IsVisibleInLegend = true;
            chartView.Series[0].ShadowOffset = 0;

            // polyline
            //chartView.Series[0]["PieLineColor"] = "Blue";
            // bind data
            chartView.Series[0].Points.DataBindXY(x, y);
            chartView.Series[0].Points[0].Color = Color.Black;
            // bind color
            //chartView.Series[0].Palette = ChartColorPalette.BrightPastel;
 
        }

        /// <summary>
        /// Line chart highlights points
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLinePoint_Click(object sender, EventArgs e)
        {<!-- -->
            // clear all nodes
            chartLine.Series[0].Points.Clear();

            List<string> x = new List<string>() {<!-- --> "A", "B", "C", "D", "E", "F", "G" };
            List<int> y = new List<int>() {<!-- --> 10, 20, 30, 40, 35, 25, 15 };
            // line color
            chartLine.Series[0].Color = Color.Blue;
            // line thickness
            chartLine.Series[0].BorderWidth = 3;
            // mark border color
            chartLine.Series[0].MarkerBorderColor = Color.Black;
            // Size of border of marker point
            chartLine.Series[0].MarkerBorderWidth = 3;
            // mark point center color
            chartLine.Series[0].MarkerColor = Color.Red;
            // marker size
            chartLine.Series[0].MarkerSize = 8;
            // marker type
            chartLine.Series[0].MarkerStyle = MarkerStyle.Square;
            // move the text to the outside
            chartLine.Series[0]["PieLabelStyle"] = "Outside";
            // draw black lines
            chartLine.Series[0]["PieLineColor"] = "Black";
            chartLine.Series[0].Points.DataBindXY(x, y);
        }

        /// <summary>
        /// pie chart
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPieOnly_Click(object sender, EventArgs e)
        {<!-- -->
            List<string> x = new List<string>() {<!-- --> "A", "B", "C", "D", "E", "F", "G" };
            List<int> y = new List<int>() {<!-- --> 10, 20, 30, 40, 35, 25, 15 };

            chartPie.Series[0].ChartType = SeriesChartType.Pie; // chart type
            chartPie.Series[0]["PieLabelStyle"] = "Outside"; // Move the text to the outside
            chartPie.Series[0]["PieLineColor"] = "Black"; // Draw a black line
            chartPie.Series[0].IsValueShownAsLabel = true;
            chartPie.Series[0].Points.DataBindXY(x, y);

        }

        /// <summary>
        /// Histogram
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBar_Click(object sender, EventArgs e)
        {<!-- -->
            // clear all nodes
            chartBar.Series.Clear();
            //chartBar.Series[0].ChartType = SeriesChartType.Bar;

            // Create Series
            Series a = new Series();
            a.LegendText = "column a";

            Series b = new Series();
            b.LegendText = "column b";

            Series c = new Series();
            c.LegendText = "column c";

            // random number
            Random r = new Random();
            for (int i = 1; i < 10; i ++ )
            {<!-- -->
                // join the collection
                a.Points.AddXY(i, r.Next(1, 9));
                b.Points.AddXY(i, r.Next(10, 20));
                c.Points.AddXY(i, r.Next(21, 30));
            }

            // specify the color of the bar
            a.Color = Color.Green;
            b.Color = Color.Red;
            c.Color = Color.Black;

            // add to chartBar
            chartBar.Series.Add(a);
            chartBar.Series.Add(b);
            chartBar.Series.Add(c);

            // X-axis grid lines
            chartBar.ChartAreas[0].AxisX.MajorGrid.Enabled = false;

            //chartBar.Series[0].LegendText = "a";
            //chartBar.Series[1].LegendText = "b";
            //chartBar.Series[2].LegendText = "c";
        }

        /// <summary>
        /// Scatterplot
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPoint_Click(object sender, EventArgs e)
        {<!-- -->

            // list
            List<int> ListPoint = new List<int>();
            Random random = new Random();

            int num = 0;
            for (int i = 0; i < 10; i ++ )
            {<!-- -->
                num = random.Next(0, 10); // random number
                ListPoint. Add(num);
            }

            // clear all nodes
            chartPoint.Series[0].Points.Clear();

            for (int i = 0; i < ListPoint. Count; i ++ )
            {<!-- -->
                // add node
                chartPoint.Series[0].Points.AddXY(i + 1, ListPoint[i]);
            }

            // Whether to display the legend
            chartPoint.Series[0].IsVisibleInLegend = false;

            chartPoint.Series[0].ChartType = SeriesChartType.Point; // Chart type

            chartPoint.Series[0].MarkerColor = Color.Red; // Marker center color
            chartPoint.Series[0].MarkerStyle = MarkerStyle.Circle; // Marker type
        }

        /// <summary>
        /// Graph
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSPLine_Click(object sender, EventArgs e)
        {<!-- -->
            // list
            List<int> ListPoint = new List<int>();
            Random random = new Random();

            int num = 0;
            for (int i = 0; i < 10; i ++ )
            {<!-- -->
                num = random.Next(0, 10); // random number
                ListPoint. Add(num);
            }

            // clear all nodes
            chartSPLine.Series[0].Points.Clear();

            for (int i = 0; i < ListPoint. Count; i ++ )
            {<!-- -->
                // add node
                chartSPLine.Series[0].Points.AddXY(i + 1, ListPoint[i]);
            }

            // Whether to display the legend
            chartSPLine.Series[0].IsVisibleInLegend = false;

            chartSPLine.Series[0].ChartType = SeriesChartType.Spline; // Chart type

            chartSPLine.Series[0].MarkerColor = Color.Red; // Marker center color
            chartSPLine.Series[0].MarkerStyle = MarkerStyle.Circle; // Marker type
        }
    }
}