How to make a WinForms line chart

Need help in implementing some specific function to your LightningChart Ultimate powered application? Post a question and get code snippets from other LightningChart Ultimate community members.

Moderator: Arction_LasseP

User avatar
ArctionPasi
Posts: 1366
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

How to make a WinForms line chart

Post by ArctionPasi » Tue Oct 04, 2016 1:46 pm

To make a very simple WinForms line chart, here is how...

Just add empty Form in the application, and apply the following code in it. CreateChart method creates the chart instance, and also creates the series. Chart line style is defined, and also points style. Both line and points are shown in this method.

Code: Select all

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 Arction.WinForms.Charting;
using Arction.WinForms.Charting.Axes;
using Arction.WinForms.Charting.Views;
using Arction.WinForms.Charting.Views.ViewXY;
using Arction.WinForms.Charting.SeriesXY; 

 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        LightningChartUltimate _chart; 


        public Form1()
        {
            InitializeComponent(); //This row is created already by Visual Studio. 

            CreateChart();   
        }


       
        void CreateChart()
        {
            _chart = new LightningChartUltimate();
            _chart.Parent = this;
            _chart.Dock = DockStyle.Fill; 

            //BeginUpdate...EndUpdate is recommended to be used around chart property settings, 
            //so that the chart doesn't refresh every time when setting is changed. We only 
            //need to update it once. 
            _chart.BeginUpdate();

            //Set axis labels to numbers
            _chart.ViewXY.XAxes[0].ValueType = AxisValueType.Number; 

            //Create point line series
            PointLineSeries pls = new PointLineSeries(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
            
            //Set line style
            pls.LineStyle.Width = 1.5f;
            pls.LineStyle.Color = Color.Yellow;
            pls.LineVisible = true;

            //Set point style 
            pls.PointStyle.Shape = Shape.Circle;
            pls.PointStyle.GradientFill = GradientFillPoint.Solid;
            pls.PointStyle.Color1 = Color.OrangeRed;
            pls.PointStyle.BorderColor = Color.Orange;
            pls.PointStyle.BorderWidth = 1.5f;
            pls.PointsVisible = true;

            pls.Points = GeneratePoints(); 

            _chart.ViewXY.PointLineSeries.Add(pls);

             //Zoom to fit
            _chart.ViewXY.ZoomToFit(); 

            _chart.EndUpdate(); 
        }


        SeriesPoint[] GeneratePoints()
        {
            int count = 100;
            SeriesPoint[] points = new SeriesPoint[count]; 
            Random rand = new Random(); 

            for (int i = 0; i < count; i++)
            {
                points[i].X = i;
                points[i].Y = rand.NextDouble() * 10.0; 
            }
            return points; 
        }
    }
}
The line chart application shows like this
WinForms line chart example
WinForms line chart example
WinformsLineChart.jpg (123.67 KiB) Viewed 7402 times
LightningChart Support Team, PT