Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Simple Column Chart Generator

, 26 Jan 2008 CPOL
With this library, it’s possible to generate some column chart images easily
forlogic.columnchartgenerator.bin.zip
ForLogic.ColumnChartGenerator.bin
ForLogic.ColumnChartGenerator.ObjectModel.dll
ForLogic.ColumnChartGenerator.Tests.exe
forlogic.columnchartgenerator.source.zip
ForLogic.ColumnChartGenerator.source
ForLogic.ColumnChartGenerator.ObjectModel
bin
Debug
ForLogic.ColumnChartGenerator.ObjectModel.dll
ForLogic.ColumnChartGenerator.suo
ForLogic.ColumnChartGenerator.Tests
bin
Debug
ForLogic.ColumnChartGenerator.ObjectModel.dll
ForLogic.ColumnChartGenerator.Tests.exe
ForLogic.ColumnChartGenerator.Tests.vshost.exe
using System;
using System.Data;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ForLogic.ColumnChartGenerator.ObjectModel;

namespace ForLogic.ColumnChartGenerator.Tests
{
	public partial class MainForm : Form
	{
		List<ColumnChartItem> _items = new List<ColumnChartItem>(); // load chart with list of columns
		DataTable _source = new DataTable(); // load chart with SetDataSource method
		
		public MainForm()
		{
			InitializeComponent();
			initValues();
		}
		
		#region events methods
		void BtnUpdateChartClick(object sender, EventArgs e)
		{
			updateChart();
		}
		
		void BtnAddColumnClick(object sender, EventArgs e)
		{
			addColumn();
		}
		
		void LvwColumnsSelectedIndexChanged(object sender, EventArgs e)
		{
			btnRemoveColumn.Enabled = (lvwColumns.SelectedItems.Count > 0);
		}
		
		void BtnRemoveColumnClick(object sender, EventArgs e)
		{
			removeColumn();
		}
		
		void MnuSaveAsClick(object sender, EventArgs e)
		{
			saveAs();
		}
		#endregion
		
		#region private methods
		void initValues()
		{
			// you can use it with a list of items (columns)
			_items.Add(new ColumnChartItem(Color.DarkOrange, "Data Column1", 5.3m));
			_items.Add(new ColumnChartItem(Color.DarkBlue, "Column 2", 10.7m));
			_items.Add(new ColumnChartItem(Color.Red, "Some More Data", 15.9m));

			// or with datatables (related to the line 105)
			//_source.Columns.Add("id", typeof(int));
			//_source.Columns.Add("name", typeof(string));
			//_source.Columns.Add("price", typeof(decimal));
			//_source.Rows.Add(1, "Product 1", 5.6m);
			//_source.Rows.Add(2, "Product 2", 7.85m);
			//_source.Rows.Add(3, "Product 3", 15.3m);
			//_source.Rows.Add(4, "Product 4", 9.2m);
			
			cboColumnColor.SelectedIndex = 3;
			updateListView();
			updateChart();
		}
		
		void updateListView()
		{
			lvwColumns.BeginUpdate();
			lvwColumns.Items.Clear();
			foreach (ColumnChartItem item in _items){
				ListViewItem lvwItem = new ListViewItem(item.Value.ToString());
				lvwItem.SubItems.Add(item.Description);
				lvwColumns.Items.Add(lvwItem);
			}
			lvwColumns.EndUpdate();
		}
		
		void updateChart()
		{
			ColumnChart cc = new ColumnChart();
			cc.BackgroundColor = Color.White;
			cc.ShowGuideLines = chkShowGuideLines.Checked;
			cc.ValueFont = new Font("Arial", Convert.ToSingle(nudFontSize.Value));
			cc.DescriptionFont = new Font("Arial", Convert.ToSingle(nudFontSize.Value));
			cc.Width = Convert.ToInt32(nudWidth.Value);
			cc.Height = Convert.ToInt32(nudHeight.Value);
			cc.ColumnSpacing = Convert.ToInt32(nudColumnSpacing.Value);
			cc.Margins = Convert.ToInt32(nudMargins.Value);
			cc.Depth = Convert.ToInt32(nudDepth.Value);
			cc.ValueFormat = txtValueFormat.Text;
			cc.GuideLineFormat = txtValueFormat.Text;
			cc.GuideLinesCount = Convert.ToInt32(nudGuideLinesCount.Value);
			cc.ShowDescriptions = chkShowDescriptions.Checked;
			cc.ShowValues = chkShowValues.Checked;
			
			// you can use it with a list of items (columns)
			foreach (ColumnChartItem item in _items)
				cc.Items.Add(item);
			// or with datatables
			//cc.SetDataSource(_source, "price", "name");
			
			pct.Image = cc.GetChart();
		}
		
		void addColumn()
		{
			ColumnChartItem item = new ColumnChartItem();
			item.Value = Convert.ToDecimal(nudColumnValue.Value);
			item.Description = txtColumnDescription.Text;
			item.FillColor = Color.FromName(cboColumnColor.Text);
			_items.Add(item);
			updateListView();
			updateChart();
		}
		
		void removeColumn()
		{
			String v = lvwColumns.SelectedItems[0].SubItems[0].Text;
			String d = lvwColumns.SelectedItems[0].SubItems[1].Text;
			for (int x=0; x<_items.Count; x++)
				if (_items[x].Value.ToString() == v && _items[x].Description == d)
					_items.RemoveAt(x);
			updateListView();
			updateChart();

			if (lvwColumns.Items.Count > 0)
				lvwColumns.Items[0].Selected = true;
			else
				btnRemoveColumn.Enabled = false;
		}
		
		void saveAs()
		{
			if (sfd.ShowDialog() != DialogResult.OK)
				return;
			pct.Image.Save(sfd.FileName, ImageFormat.Png);
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Diogo Arenhart
Software Developer ForLogic Software
Brazil Brazil
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 26 Jan 2008
Article Copyright 2008 by Diogo Arenhart
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid