Jumat, 04 Mei 2012

Membuat Aplikasi Chart MSChart dengan C#.NET


Chart pada C#.NET

Pada Microsoft Visual Studio/Visual C#.NET tidak disediakan komponen Chart, komponen ini tersedia pada versi Visual Studio sebelumnya yaitu Visual Studio 6.0 dengan nama Microsoft Chart Control 6.0. Untuk memanggil komponen tersebut dapat dilakukan sama dengan ketika menambahkan komponen Windows Media Player, yaitu  pada Toolbox klik kanan Pilih Choose Items.


Setelah muncul dialog Choose Toolbox Items, kemudian pilih Tab COM Components. Kemudian cari dan cek Microsoft Chart Control 6.0. Klik OK.

 

Secara otomatis komponen Microsoft Chart Control akan ditambahkan pada Toolbox. Kemudian komponen MS Chart tersebut dapat ditambahkan pada Form. Untuk mengubah-ubah setting dari komponen MSChart yang  telah ditambahkan pada Form dapat dilakukan dengan mengubah-ubah properti dari MSChart pada Window Properties maupun dengan cara klik kanan komponen MSChart pada Form kemudian pilih ActiveX – Properties.

 

Akan muncul dialog Properties dari MSChart, pada dialog tersebut dapat diubah-ubah berbagai setting dari komponen MSChart antara lain Chart Type, Chart Options, Series, Axis, Text, Fonts, dsb. Setelah mengubah setting/properti selesai, klik OK.

 

Untuk memasukkan data yang akan ditampilkan komponen MSChart harus dilakukan dari kode program. Berikut ini contoh kode program yang dapat dipakai untuk mengolah MSChart secara dinamis dari kode program, baik untuk Chart yang berbentuk Pie, Line, Bar, dll.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Chart
{
    public partial class FChart : Form
    {
        public FChart()
        {
            InitializeComponent();
        }

        private void bPie_Click(object sender, EventArgs e)
        {
            cChart.chartType = MSChart20Lib.VtChChartType.VtChChartType2dPie;
            //cChart.chartType = MSChart20Lib.VtChChartType.VtChChartType3dPie;
            int[,] MyArray;
            MyArray = new int[1, 5];
            MyArray[0, 0] = 5;
            MyArray[0, 1] = 4;
            MyArray[0, 2] = 3;
            MyArray[0, 3] = 2;
            MyArray[0, 4] = 1;
            cChart.ChartData = MyArray;

            cChart.Row = 1;
            cChart.RowLabel = "Hasil Pemilu";

            cChart.Column = 1;
            cChart.ColumnLabel = "Partai Merah";
            cChart.Column = 2;
            cChart.ColumnLabel = "Partai Kuning";
            cChart.Column = 3;
            cChart.ColumnLabel = "Partai Biru";
            cChart.Column = 4;
            cChart.ColumnLabel = "Partai Hijau";
            cChart.Column = 5;
            cChart.ColumnLabel = "Partai Putih";

            cChart.Title.Text = "Chart Pie";
            cChart.ShowLegend = true;
        }

        private void bLine_Click(object sender, EventArgs e)
        {
            cChart.chartType = MSChart20Lib.VtChChartType.VtChChartType2dLine;
            //cChart.chartType = MSChart20Lib.VtChChartType.VtChChartType3dLine
            double[,] MyArray;
            MyArray = new double[3, 5];
            Random rnd = new Random();

            //MyArray[0, 0] = rnd.Next(11);
            //MyArray[0, 1] = rnd.Next(11);
            //MyArray[0, 2] = rnd.Next(11);
            //MyArray[0, 3] = rnd.Next(11);
            //MyArray[0, 4] = rnd.Next(11);
            //MyArray[1, 0] = rnd.Next(11);
            //MyArray[1, 1] = rnd.Next(11);
            //MyArray[1, 2] = rnd.Next(11);
            //MyArray[1, 3] = rnd.Next(11);
            //MyArray[1, 4] = rnd.Next(11);
            //MyArray[2, 0] = rnd.Next(11);
            //MyArray[2, 1] = rnd.Next(11);
            //MyArray[2, 2] = rnd.Next(11);
            //MyArray[2, 3] = rnd.Next(11);
            //MyArray[2, 4] = rnd.Next(11);

            int i, j;

            for (i = 0; i < MyArray.GetLength(0); i++)
            {
                for (j = 0; j < MyArray.GetLength(1); j++)
                {
                    MyArray[i, j] = rnd.NextDouble () * 10;
                }
            }

            cChart.ChartData = MyArray;

            //int i, j;

            //cChart.RowCount = 3;
            //cChart.ColumnCount = 5;
            //for (i = i; i <= MyArray.GetLength(0); i++)
            //{
            //    for (j = i; j <= MyArray.GetLength(1); j++)
            //    {
            //        cChart.Row = (short)(i);
            //        cChart.Column = (short)(j);
            //        cChart.Data = (rnd.NextDouble () * 10).ToString();
            //    }
            //}
           
            cChart.Row = 1;
            cChart.RowLabel = "Satu";
            cChart.Row = 2;
            cChart.RowLabel = "Dua";
            cChart.Row = 3;
            cChart.RowLabel = "Tiga";

            cChart.Column = 1;
            cChart.ColumnLabel = "Nilai A";
            cChart.Column = 2;
            cChart.ColumnLabel = "Nilai B";
            cChart.Column = 3;
            cChart.ColumnLabel = "Nilai C";
            cChart.Column = 4;
            cChart.ColumnLabel = "Nilai D";
            cChart.Column = 5;
            cChart.ColumnLabel = "Nilai E";

            cChart.Title.Text = "Chart Line";
            cChart.ShowLegend = true;
        }

        private void bBar_Click(object sender, EventArgs e)
        {
            cChart.chartType = MSChart20Lib.VtChChartType.VtChChartType2dBar;
            //cChart.chartType = MSChart20Lib.VtChChartType.VtChChartType3dBar;
            double[,] MyArray;
            MyArray = new double[3, 5];
            Random rnd = new Random();

            //MyArray[0, 0] = rnd.NextDouble() * 10;
            //MyArray[0, 1] = rnd.NextDouble() * 10;
            //MyArray[0, 2] = rnd.NextDouble() * 10;
            //MyArray[0, 3] = rnd.NextDouble() * 10;
            //MyArray[0, 4] = rnd.NextDouble() * 10;
            //MyArray[1, 0] = rnd.NextDouble() * 10;
            //MyArray[1, 1] = rnd.NextDouble() * 10;
            //MyArray[1, 2] = rnd.NextDouble() * 10;
            //MyArray[1, 3] = rnd.NextDouble() * 10;
            //MyArray[1, 4] = rnd.NextDouble() * 10;
            //MyArray[2, 0] = rnd.NextDouble() * 10;
            //MyArray[2, 1] = rnd.NextDouble() * 10;
            //MyArray[2, 2] = rnd.NextDouble() * 10;
            //MyArray[2, 3] = rnd.NextDouble() * 10;
            //MyArray[2, 4] = rnd.NextDouble() * 10;

            //int i, j;

            //for (i = 0; i < MyArray.GetLength(0); i++)
            //{
            //    for (j = 0; j < MyArray.GetLength(1); j++)
            //    {
            //        MyArray[i, j] = rnd.Next(11);
            //    }
            //}

            //cChart.ChartData = MyArray;

            int i, j;

            cChart.RowCount = 3;
            cChart.ColumnCount = 5;
            for (i = 1; i <= MyArray.GetLength(0); i++)
            {
                for (j = 1; j <= MyArray.GetLength(1); j++)
                {
                    cChart.Row = (short)(i);
                    cChart.Column = (short)(j);
                    cChart.Data = (rnd.Next(11)).ToString();
                }
            }

            cChart.Row = 1;
            cChart.RowLabel = "Satu";
            cChart.Row = 2;
            cChart.RowLabel = "Dua";
            cChart.Row = 3;
            cChart.RowLabel = "Tiga";

            cChart.Column = 1;
            cChart.ColumnLabel = "Nilai A";
            cChart.Column = 2;
            cChart.ColumnLabel = "Nilai B";
            cChart.Column = 3;
            cChart.ColumnLabel = "Nilai C";
            cChart.Column = 4;
            cChart.ColumnLabel = "Nilai D";
            cChart.Column = 5;
            cChart.ColumnLabel = "Nilai E";

            cChart.Title.Text = "Chart Line";
            cChart.ShowLegend = true;
        }
    }
}

Setelah dijalankan, hasilnya dapat dilihat pada tampilan berikut ini :