Simple line graph charting

ddanbe 0 Tallied Votes 3K Views Share

It was tinstaafl who put me on the right track. Click Here I did not know that .NET as of VS 2010 had a charting control!
OK, let’s get started! But the learning curve, is as always, a bit steep. MSDN does not seem to provide too much code, so I looked around on the net, but here they often make use of special graphics packages. So not much help either… After some fumbling around, I came up with the following easy ( I think ) code. Just start up a new windows form app and fill in the provided code in the Form.cs file. Enjoy!

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 Graph = System.Windows.Forms.DataVisualization.Charting;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Graph.Chart chart;

        public Form1()
        {
            InitializeComponent();
            this.ClientSize = new System.Drawing.Size(750, 750);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            const int MaxX = 20;

            // Create new Graph
            chart = new Graph.Chart();
            chart.Location = new System.Drawing.Point(10, 10);
            chart.Size = new System.Drawing.Size(700, 700);

            // Add a chartarea called "draw", add axes to it and color the area black
            chart.ChartAreas.Add("draw");

            chart.ChartAreas["draw"].AxisX.Minimum = 0;
            chart.ChartAreas["draw"].AxisX.Maximum = MaxX;
            chart.ChartAreas["draw"].AxisX.Interval = 1;
            chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = Color.White;
            chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;

            chart.ChartAreas["draw"].AxisY.Minimum = -0.4;
            chart.ChartAreas["draw"].AxisY.Maximum = 1;
            chart.ChartAreas["draw"].AxisY.Interval = 0.2;
            chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.White;
            chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;
            
            chart.ChartAreas["draw"].BackColor = Color.Black;
            
            // Create a new function series
            chart.Series.Add("MyFunc");

            // Set the type to line      
            chart.Series["MyFunc"].ChartType = Graph.SeriesChartType.Line;

            // Color the line of the graph light green and give it a thickness of 3
            chart.Series["MyFunc"].Color = Color.LightGreen;
            chart.Series["MyFunc"].BorderWidth = 3; 

            //This function cannot include zero, and we walk through it in steps of 0.1 to add coordinates to our series
            for (double x = 0.1; x < MaxX; x += 0.1)
            {
                chart.Series["MyFunc"].Points.AddXY(x, Math.Sin(x) / x);
            }

            chart.Series["MyFunc"].LegendText = "sin(x) / x";

            // Create a new legend called "MyLegend".
            chart.Legends.Add("MyLegend");
            chart.Legends["MyLegend"].BorderColor = Color.Tomato; // I like tomato juice!

            Controls.Add(this.chart);           
        }
    }
}
TnTinMN 418 Practically a Master Poster

Hi ddanbe,

I'm glad you found a new toy to play with. :)

You may find these examples useful: Samples Environment for Microsoft Chart ControlsClick Here

The chart control was also made available for .Net 3.5 as a separate library.

Microsoft Chart Controls for Microsoft .NET Framework 3.5Click Here

commented: A little late, but great advise! +14
RevathiSRS 0 Newbie Poster

Can we able to use this MS Visual studio 2005 and 2008..???

ddanbe 2,724 Professional Procrastinator Featured Poster

@RevathiSRS: Welcome to DaniWeb!
If you don't have VS2010 or later, read the post by TnTinMN(see last two lines)

RevathiSRS 0 Newbie Poster

okay fine ddanbe Thank you...

RevathiSRS 0 Newbie Poster

Hai... Can we able to make any pictorial representation in Microsoft office 2005 .net 2.0 windows application..?? Without crystal report. please let me know.

ddanbe 2,724 Professional Procrastinator Featured Poster

From C# you could draw a chart in excel see example here C# 4.0 has improved interop syntax, but I could not find an example right away.

mcode12 0 Newbie Poster

Can you offer any advice on using this code as a basis to plot data from a SQL server?

ddanbe 2,724 Professional Procrastinator Featured Poster

Never tried that myself, but this tutorial might help you out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.