Click here to Skip to main content
15,894,017 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 42.1K   2.6K   27  
Adding scrollbars to the Microsoft Chart Control.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;


public class cls3DChart : UserControl
{
  private const int SCROLLBAROFFSET = 50;     // Padding for scrollbar
  private const int SCROLLBARWIDTH = 15;

                                              // Assumed property values
  private int width = 400;                    // Panel and chart size
  private int height = 400;
  private int chartDepth = 400;
  private int scrollBarWidth = SCROLLBARWIDTH;
  private int rotationValue = 15;             // How many degrees per click
  private int inclineValue = -15;
  private int smallDeltaX = 1;                // When thumb is dragged
  private int smallDeltaY = 1;
  private int largeDeltaX = SCROLLBARWIDTH;   // When scollbar track is clicked
  private int largeDeltaY = SCROLLBARWIDTH;
  private int numberOfSeries = 1;             // Only show one series of data
  private int currentSeries = 0;
  private int seriesSize = 10;                // Assume 10 data points in series
  private Color seriesColor = Color.CornflowerBlue;

  private string currentSeriesName;

  private double yInterval = .1;            // This value because I was doing probabilities
  private double xInterval = 1.0;

  Series series = new Series();
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.DataVisualization.Charting.Chart chartMain;

  VScrollBar myVScrollBar = new VScrollBar();
  HScrollBar myHScrollBar = new HScrollBar();

  #region Component Designer generated code

  private void InitializeComponent()
  {
      System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
      System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
      System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
      this.panel1 = new System.Windows.Forms.Panel();
      this.chartMain = new System.Windows.Forms.DataVisualization.Charting.Chart();
      this.panel1.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.chartMain)).BeginInit();
      this.SuspendLayout();
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.chartMain);
      this.panel1.Name = "panel1";
      this.panel1.TabIndex = 0;
      // 
      // chartMain
      // 
      chartArea1.Name = "ChartArea1";
      this.chartMain.ChartAreas.Add(chartArea1);
      legend1.Name = "Legend1";
      this.chartMain.Legends.Add(legend1);
      this.chartMain.Location = new System.Drawing.Point(0, 0);
      this.chartMain.Name = "chartMain";
      this.chartMain.Size = new System.Drawing.Size(300, 300);
      this.chartMain.TabIndex = 0;
      this.chartMain.Text = "chart1";
      // 
      // MyMSChartControl
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.AutoScroll = true;
      this.Controls.Add(this.panel1);
      this.Location = new System.Drawing.Point(50, 50);
      this.Name = "MyMSChartControl";
      this.Size = new System.Drawing.Size(500, 500);
      this.panel1.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.chartMain)).EndInit();
      this.ResumeLayout(false);
  }

  #endregion

  //============================= Constructor ===========================
  public cls3DChart(int w, int h, int x, int y)
  {
    Size cSize = new Size();
    
    InitializeComponent();          // The Windows-generated code

    width = w;                                            // Panel and chart size
    height = h;
    cSize = this.Size;                                    // default max size is 500x500
    panel1.Location = new System.Drawing.Point(x, y);     // Where to pin it

    if (height >= cSize.Height || width >= cSize.Width)   // See if it's too big
    {
      width = cSize.Width - SCROLLBAROFFSET;              // Scale it down if it is...
      height = cSize.Height - SCROLLBAROFFSET;
    }
    this.panel1.Size = new System.Drawing.Size(width + SCROLLBAROFFSET, height + SCROLLBAROFFSET);
    this.chartMain.Size = new System.Drawing.Size(width, height);
  }


  //======================== Property Methods =========================

  public Color SeriesColor
  {
    get { return seriesColor; }
    set { seriesColor = value; }
  }
  public string CurrentSeriesName
  {
    get { 
      return currentSeriesName; }
    set
    {
      if (value != "")
      {
        currentSeriesName = value;
      }
    }
  }

  public int NumberOfSeries
  {
    get { return numberOfSeries; }
    set { if (value > 0 && value < 10) numberOfSeries = value; }
  }

  public int ScrollbarLargeDeltaY
  {
    get { return largeDeltaY; }
    set { if (value > 0 && value < 1000) largeDeltaY = value; }
  }
  public int ScrollbarSmallDeltaY
  {
    get { return smallDeltaY; }
    set { if (value > 0 && value < 1000) smallDeltaY = value; }
  }
  public int ScrollbarLargeDeltaX
  {
    get { return largeDeltaX; }
    set { if (value > 0 && value < 1000) largeDeltaX = value; }
  }
  public int ScrollbarSmallDeltaX
  {
    get { return smallDeltaX; }
    set { if (value > 0 && value < 1000) smallDeltaX = value; }
  }
  public int StartingInclineValue
  {
    get { return inclineValue; }
    set { if (value > -91 && value < 91) inclineValue = value; }
  }
  public int StartingRotationValue
  {
    get { return rotationValue; }
    set { if (value > 0 && value < 180) rotationValue = value; }
  }
  public int ScrollBarWidth
  {
    get { return scrollBarWidth; }
    set { if (value > 0 && value < 50) scrollBarWidth = value; }
  }
  public int SeriesSize
  {
    get { return seriesSize; }
    set { if (value > 0 && value < 100) seriesSize = value; }
  }

  public int ChartDepth
  {
    get { return chartDepth; }
    set { if (value > 0 && value < 1000) chartDepth = value; }
  }
  public int ChartHeight
  {
    get { return height; }
    set { 
      if (value > 0 && value < 800) 
        height = value;
      this.Size = new System.Drawing.Size(width, height);
    }
  }
  public int ChartWidth
  {
    get { return width; }
    set { 
      if (value > 0 && value < 1000) 
        width = value;
      this.Size = new System.Drawing.Size(width, height);
    }
  }
 
  //========================= General Methods ====================
  public void SetSeriesData(double[] sentData)
  {
    int i;
    Series series = new Series();

    series.Name = currentSeriesName;
    series.Color = seriesColor;
    series.ChartType = SeriesChartType.Column;    // Could make this a changable property

    for (i = 0; i < seriesSize; i++)
    {
      series.Points.Add(sentData[i]);
    }
    chartMain.Series.Add(series);
    currentSeries++;
  }

  public void InitializeChart()
  {
    chartMain.ChartAreas[0].Area3DStyle.Enable3D = true;          // Turn on 3D
    chartMain.ChartAreas[0].Area3DStyle.PointDepth = chartDepth;  // How deep is the chart

    Axis axisY = chartMain.ChartAreas[0].AxisY;                   // Set up for the axis data
    Axis axisX = chartMain.ChartAreas[0].AxisX;
    axisY.Interval = yInterval;
    axisX.Interval = xInterval;
 
                                                                  // Do all the scrollbar stuff
    myVScrollBar.Scroll += new ScrollEventHandler(this.myVScrollBar_Scroll);
    myHScrollBar.Scroll += new ScrollEventHandler(this.myHScrollBar_Scroll);

    myVScrollBar.Height = height;
    myVScrollBar.Width = scrollBarWidth;

    myHScrollBar.Height = scrollBarWidth;
    myHScrollBar.Width = width;

    myHScrollBar.Location = new System.Drawing.Point(0, height);  // Where to pin the scrollbars
    myVScrollBar.Location = new System.Drawing.Point(width, 0);


    myVScrollBar.SetBounds(width, 0, scrollBarWidth, height);     // How big are they...
    myHScrollBar.SetBounds(0, height, width, scrollBarWidth);
    
    myVScrollBar.Maximum = 90;                // Set limits and starting values, these could be properties
    myVScrollBar.Minimum = -90;
    myVScrollBar.SmallChange = smallDeltaY;   // How far when dragging
    myVScrollBar.LargeChange = largeDeltaY;   // How far when clicking
    myVScrollBar.Value = -15;                 // Starting angle

    myHScrollBar.Maximum = 180;               // Same stuff for horizontal scrollbar
    myHScrollBar.Minimum = 0;
    myHScrollBar.SmallChange = smallDeltaX;
    myHScrollBar.LargeChange = largeDeltaX;
    myHScrollBar.Value = 15;

    panel1.Controls.Add(myVScrollBar);    // Place the bars on the panel, at chart edges
    panel1.Controls.Add(myHScrollBar);
  }

  private void AddMyScrollEventHandlers()
  {
    // Event handlers
    myVScrollBar.Scroll += new ScrollEventHandler(this.myVScrollBar_Scroll);
    myHScrollBar.Scroll += new ScrollEventHandler(this.myHScrollBar_Scroll);
  }

  // Create the Scroll event handler.
  private void myVScrollBar_Scroll(Object sender,ScrollEventArgs e)
  {
    if (myVScrollBar.Value > inclineValue)
    {
      chartMain.ChartAreas[0].Area3DStyle.Inclination = (int)myVScrollBar.Value;
      inclineValue += (int)myVScrollBar.SmallChange;
    }
    else
    {
      chartMain.ChartAreas[0].Area3DStyle.Inclination = (int)myVScrollBar.Value;
      inclineValue -= (int)myVScrollBar.SmallChange;
    }
  }

  private void myHScrollBar_Scroll(Object sender, ScrollEventArgs e)
  {

    if (myHScrollBar.Value > rotationValue)
    {
      chartMain.ChartAreas[0].Area3DStyle.Rotation = (int)myHScrollBar.Value;
      rotationValue += (int)myHScrollBar.SmallChange;
      if (rotationValue == 45 || rotationValue == 90) // Done to soften "jump" when scrolled
        rotationValue++;
    }
    else
    {
      chartMain.ChartAreas[0].Area3DStyle.Rotation = (int)myHScrollBar.Value;
      rotationValue -= (int)myHScrollBar.SmallChange;
      if (rotationValue == 45 || rotationValue == 90) // Done to soften "jump" when scrolled
        rotationValue--;
    }
  }

}

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