Upload
manoj-kumar-g
View
54
Download
0
Embed Size (px)
DESCRIPTION
C# Chart_ Windows Forms
Citation preview
C# ChartThe Chart control visualizes your data. Itdisplays data in your Windows Forms programas a bar graph or chart. With Chart and theSystem.Windows.FormsDataVisualization.Charting namespace, you canquickly display your data in a colorful graphiccontrolled by C# code.
Get startedFirst, you must beusing a newer versionof the .NETFramework. Older versions will not have theChart control available. Open the Toolbox anddrag the Chart item to the Form.
Next:Select Properties after right-clicking on theChart. You can add Series and Titles thisway.
And:For our example, remove the defaultSeries1 that was added by Visual Studio.
C#: Windows
C# Chart: Windows Forms http://www.dotnetperls.com/chart
1 of 5 25/05/2013 01:18
CodeexampleIn the next step, wewill use the Form1_Load eventhandler to initialize the Chartwe just added. To addForm1_Load, double-click onthe Form window in VisualStudio.
In Form1_Load, we assign an array of stringsand an array of integers. These strings are theSeries and the integers are the Points. In thefor-loop, we add the strings to the Seriescollection and add the integers to the Pointscollections on those Series.For
Example that sets Chart control up: C#using System;using System.Windows.Forms;using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApplication1{ public partial class Form1 : Form {
public Form1(){ InitializeComponent();}
private void Form1_Load(object sender, EventArgs e){ // Data arrays. string[] seriesArray = { "Cats", "Dogs" }; int[] pointsArray = { 1, 2 };
// Set palette. this.chart1.Palette = ChartColorPalette.SeaGreen;
// Set title. this.chart1.Titles.Add("Pets");
// Add series. for (int i = 0; i < seriesArray.Length; i++) {
// Add series.Series series = this.chart1.Series.Add(seriesArray[i]);
C# Chart: Windows Forms http://www.dotnetperls.com/chart
2 of 5 25/05/2013 01:18
// Add point.series.Points.Add(pointsArray[i]);
}}
}}
Output See screenshot at top.
The loop results in two Series: a Cats serieswith a Point of 1, and a Dogs series with aPoint of 2. These are reflected in a bar graph,which is shown in the screenshot at the top ofthis article.
When you call the Series.Add method, youcan pass in a string which becomes the nameof the Series. A Series object is returned byAdd. At this point, you can assign a localvariable to the result of Add and then accessthe Points collection on the Series.
And:By calling Points.Add, you can set thevalues.
Palette propertySome computerprogrammersmay not beartisticallyinclined. ThePalette property on the Chart type can helpwith this: you can use a ChartColorPaletteenumerated constant in an assignmentexpression. When you work with a Chart, trychanging your Palette and experimenting withthe ChartColorPalette constants.
ChartColorPalette enumerated constants: C#ChartColorPalette.BerryChartColorPalette.BrightChartColorPalette.BrightPastelChartColorPalette.ChocolateChartColorPalette.EarthTonesChartColorPalette.ExcelChartColorPalette.Fire
C# Chart: Windows Forms http://www.dotnetperls.com/chart
3 of 5 25/05/2013 01:18
ChartColorPalette.GrayScaleChartColorPalette.LightChartColorPalette.NoneChartColorPalette.PastelChartColorPalette.SeaGreenChartColorPalette.SemiTransparent
SaveImageSometimes youwill need to saveyour chart to animage file. TheSaveImagemethod takes the graphical representation ofyour chart as it appears on the screen andwrites it to a file or Stream you specify. In theabove example, try adding this statement andthen check out the chart.png file.
Code that saves chart to image: C#this.chart1.SaveImage("C:\\chart.png", ChartImageFormat.Png);
SummaryWe looked at theChart type in the.NET Frameworkand Windows Formslibrary. With Chart, you can automaticallygenerate graphs based on data. These bargraphs can then be used in a variety of placesor displayed directly in a Windows Formsprogram.
C# Chart: Windows Forms http://www.dotnetperls.com/chart
4 of 5 25/05/2013 01:18
C# Chart: Windows Forms http://www.dotnetperls.com/chart
5 of 5 25/05/2013 01:18