Click here to Skip to main content
15,891,777 members
Articles / Programming Languages / C#

Rotating a Microsoft 3D Chart

Rate me:
Please Sign up or sign in to vote.
4.78/5 (6 votes)
12 Feb 2013CPOL6 min read 42K   2.6K   27  
Adding scrollbars to the Microsoft Chart Control.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

public class frmMain : Form
{
  private Button btnCalc;
  private Button btnClose;
  private Label label1;
  private TextBox txtWidth;
  private TextBox txtHeight;
  private Label label2;
  private Button btnClear;
  double[] series = new double[100];
  private TextBox txtY;
  private Label label3;
  private TextBox txtX;
  private Label label4;
  cls3DChart myChart;

  #region Windows Code
  private void InitializeComponent()
  {
      this.btnCalc = new System.Windows.Forms.Button();
      this.btnClose = new System.Windows.Forms.Button();
      this.label1 = new System.Windows.Forms.Label();
      this.txtWidth = new System.Windows.Forms.TextBox();
      this.txtHeight = new System.Windows.Forms.TextBox();
      this.label2 = new System.Windows.Forms.Label();
      this.btnClear = new System.Windows.Forms.Button();
      this.txtY = new System.Windows.Forms.TextBox();
      this.label3 = new System.Windows.Forms.Label();
      this.txtX = new System.Windows.Forms.TextBox();
      this.label4 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // btnCalc
      // 
      this.btnCalc.Location = new System.Drawing.Point(546, 12);
      this.btnCalc.Name = "btnCalc";
      this.btnCalc.Size = new System.Drawing.Size(75, 23);
      this.btnCalc.TabIndex = 0;
      this.btnCalc.Text = "Calculate";
      this.btnCalc.UseVisualStyleBackColor = true;
      this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
      // 
      // btnClose
      // 
      this.btnClose.Location = new System.Drawing.Point(546, 115);
      this.btnClose.Name = "btnClose";
      this.btnClose.Size = new System.Drawing.Size(75, 23);
      this.btnClose.TabIndex = 1;
      this.btnClose.Text = "&Close";
      this.btnClose.UseVisualStyleBackColor = true;
      this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
      // 
      // label1
      // 
      this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label1.Location = new System.Drawing.Point(29, 15);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(100, 20);
      this.label1.TabIndex = 2;
      this.label1.Text = "Width:";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // txtWidth
      // 
      this.txtWidth.Location = new System.Drawing.Point(135, 15);
      this.txtWidth.Name = "txtWidth";
      this.txtWidth.Size = new System.Drawing.Size(100, 20);
      this.txtWidth.TabIndex = 3;
      this.txtWidth.Text = "400";
      // 
      // txtHeight
      // 
      this.txtHeight.Location = new System.Drawing.Point(368, 14);
      this.txtHeight.Name = "txtHeight";
      this.txtHeight.Size = new System.Drawing.Size(100, 20);
      this.txtHeight.TabIndex = 5;
      this.txtHeight.Text = "400";
      // 
      // label2
      // 
      this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label2.Location = new System.Drawing.Point(262, 14);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(100, 20);
      this.label2.TabIndex = 4;
      this.label2.Text = "Height";
      this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // btnClear
      // 
      this.btnClear.Location = new System.Drawing.Point(546, 53);
      this.btnClear.Name = "btnClear";
      this.btnClear.Size = new System.Drawing.Size(75, 23);
      this.btnClear.TabIndex = 6;
      this.btnClear.Text = "C&lear";
      this.btnClear.UseVisualStyleBackColor = true;
      this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
      // 
      // txtY
      // 
      this.txtY.Location = new System.Drawing.Point(368, 34);
      this.txtY.Name = "txtY";
      this.txtY.Size = new System.Drawing.Size(100, 20);
      this.txtY.TabIndex = 10;
      this.txtY.Text = "25";
      // 
      // label3
      // 
      this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label3.Location = new System.Drawing.Point(262, 34);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(100, 20);
      this.label3.TabIndex = 9;
      this.label3.Text = "Y";
      this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // txtX
      // 
      this.txtX.Location = new System.Drawing.Point(135, 35);
      this.txtX.Name = "txtX";
      this.txtX.Size = new System.Drawing.Size(100, 20);
      this.txtX.TabIndex = 8;
      this.txtX.Text = "25";
      // 
      // label4
      // 
      this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.label4.Location = new System.Drawing.Point(29, 35);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(100, 20);
      this.label4.TabIndex = 7;
      this.label4.Text = "X";
      this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
      // 
      // frmMain
      // 
      this.ClientSize = new System.Drawing.Size(642, 547);
      this.Controls.Add(this.txtY);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.txtX);
      this.Controls.Add(this.label4);
      this.Controls.Add(this.btnClear);
      this.Controls.Add(this.txtHeight);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.txtWidth);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.btnClose);
      this.Controls.Add(this.btnCalc);
      this.Name = "frmMain";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "frmMain displayed";
      this.ResumeLayout(false);
      this.PerformLayout();

  }
  #endregion

  public frmMain()
  {
    InitializeComponent();
  }

  [STAThread]
  public static void Main()
  {
    frmMain main = new frmMain();
    Application.Run(main);
  }

  private void GenerateTestData(int n)
  {

    Random rnd = new Random(Guid.NewGuid().GetHashCode());  // Seed with GUID
    double temp = 0.0;
    int i;

    for (i = 0; i < n; i++)
    {
      temp = rnd.NextDouble();          // Generate a bunch of numbers between 0.0 and 1.0
      series[i] = rnd.NextDouble();
    }
  }

  private void btnCalc_Click(object sender, EventArgs e)
  {
    bool flag;
    int width;
    int height;
    int x;
    int y;

    flag = int.TryParse(txtWidth.Text, out width);                // Check for good numbers
    if (flag == false)
    {
      MessageBox.Show("Width must be numeric", "Input Erropr");
      txtWidth.Focus();
    }
    flag = int.TryParse(txtHeight.Text, out height);
    if (flag == false)
    {
      MessageBox.Show("Height must be numeric", "Input Erropr");
      txtHeight.Focus();
    }


    flag = int.TryParse(txtX.Text, out x);
    if (flag == false)
    {
      MessageBox.Show("X coordinate must be numeric", "Input Erropr");
      txtX.Focus();
    }
    flag = int.TryParse(txtY.Text, out y);
    if (flag == false)
    {
      MessageBox.Show("Y coordinate must be numeric", "Input Erropr");
      txtY.Focus();
    }

    try
    {
      myChart = new cls3DChart(width, height, x, y);

      myChart.SeriesSize = 10;              // The number of data points per plot
      myChart.NumberOfSeries = 2;           // The number of data plots
      myChart.CurrentSeriesName = "Probability";

      GenerateTestData(myChart.SeriesSize); // Fake some data

      this.Controls.Add(myChart);           // Add control to this form
      myChart.InitializeChart();            // Finish up the control
      myChart.SetSeriesData(series);        // Add data to chart

      GenerateTestData(myChart.SeriesSize); // Do it again...
      myChart.SeriesColor = Color.IndianRed;// ...using a different series color...
      myChart.CurrentSeriesName = "Data";   // ...and name.
      myChart.SetSeriesData(series);
    }
    catch (Exception ex)
    {
      MessageBox.Show("Something went terribly wrong: " + ex.Message);
    }


  }

  private void btnClose_Click(object sender, EventArgs e)
  {
    Close();
  }

  private void btnClear_Click(object sender, EventArgs e)
  {
    myChart.Dispose();    // Release all resources for chart
  }
}

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)


Written By
Retired Jack Purdum Associates
United States United States
Dr. Purdum is a retired professor from Purdue University's College of Technology and author of 17 programming books. He continues to do consulting and writing, and is also interested in programming embedded systems.

Comments and Discussions