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!
Simple line graph charting
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);
}
}
}
RevathiSRS 0 Newbie Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
RevathiSRS 0 Newbie Poster
RevathiSRS 0 Newbie Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
mcode12 0 Newbie Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
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.