using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Data; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using System.Windows.Forms.DataVisualization.Charting.Utilities; namespace WinFormsChartSamples { /// /// Summary description for HistogramChart. /// public class HistogramChart : System.Windows.Forms.UserControl { private System.Windows.Forms.Label labelSampleComment; private System.Windows.Forms.Panel panel1; private bool loadingData = false; private System.Windows.Forms.DataVisualization.Charting.Chart chart1; private System.Windows.Forms.ComboBox comboBoxIntervalNumber; private System.Windows.Forms.Label label1; private System.Windows.Forms.CheckBox checkBoxShowPercents; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public HistogramChart() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitForm call } /// /// Clean up any resources being used. /// protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Component Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = 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(); System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.labelSampleComment = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); this.checkBoxShowPercents = new System.Windows.Forms.CheckBox(); this.label1 = new System.Windows.Forms.Label(); this.comboBoxIntervalNumber = new System.Windows.Forms.ComboBox(); this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.panel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.SuspendLayout(); // // labelSampleComment // this.labelSampleComment.Dock = System.Windows.Forms.DockStyle.Top; this.labelSampleComment.Font = new System.Drawing.Font("Verdana", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.labelSampleComment.Location = new System.Drawing.Point(0, 0); this.labelSampleComment.Name = "labelSampleComment"; this.labelSampleComment.Size = new System.Drawing.Size(1460, 104); this.labelSampleComment.TabIndex = 0; this.labelSampleComment.Text = "This sample demonstrates how to create a histogram chart that uses a given number" + " of intervals. "; this.labelSampleComment.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // panel1 // this.panel1.Controls.Add(this.checkBoxShowPercents); this.panel1.Controls.Add(this.label1); this.panel1.Controls.Add(this.comboBoxIntervalNumber); this.panel1.Dock = System.Windows.Forms.DockStyle.Right; this.panel1.Location = new System.Drawing.Point(904, 104); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(556, 760); this.panel1.TabIndex = 0; // // checkBoxShowPercents // this.checkBoxShowPercents.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; this.checkBoxShowPercents.Checked = true; this.checkBoxShowPercents.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBoxShowPercents.Location = new System.Drawing.Point(6, 85); this.checkBoxShowPercents.Name = "checkBoxShowPercents"; this.checkBoxShowPercents.Size = new System.Drawing.Size(520, 51); this.checkBoxShowPercents.TabIndex = 2; this.checkBoxShowPercents.Text = "Show &Percent Axis:"; this.checkBoxShowPercents.CheckedChanged += new System.EventHandler(this.checkBoxShowPercents_CheckedChanged); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(6, 11); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(335, 36); this.label1.TabIndex = 0; this.label1.Text = "&Number of Intervals:"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // comboBoxIntervalNumber // this.comboBoxIntervalNumber.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxIntervalNumber.Items.AddRange(new object[] { "4", "10", "20", "30"}); this.comboBoxIntervalNumber.Location = new System.Drawing.Point(366, 8); this.comboBoxIntervalNumber.Name = "comboBoxIntervalNumber"; this.comboBoxIntervalNumber.Size = new System.Drawing.Size(160, 44); this.comboBoxIntervalNumber.TabIndex = 1; this.comboBoxIntervalNumber.SelectedIndexChanged += new System.EventHandler(this.comboBoxIntervalNumber_SelectedIndexChanged); // // chart1 // this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(223)))), ((int)(((byte)(193))))); this.chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; this.chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(181)))), ((int)(((byte)(64)))), ((int)(((byte)(1))))); this.chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid; this.chart1.BorderlineWidth = 2; this.chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss; chartArea1.AlignWithChartArea = "HistogramArea"; chartArea1.Area3DStyle.Inclination = 15; chartArea1.Area3DStyle.IsClustered = true; chartArea1.Area3DStyle.IsRightAngleAxes = false; chartArea1.Area3DStyle.Perspective = 10; chartArea1.Area3DStyle.Rotation = 10; chartArea1.Area3DStyle.WallWidth = 0; chartArea1.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True; chartArea1.AxisX.LabelAutoFitStyle = ((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles)(((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.IncreaseFont | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont) | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.WordWrap))); chartArea1.AxisX.LabelStyle.Enabled = false; chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisX.MajorTickMark.LineColor = System.Drawing.Color.Transparent; chartArea1.AxisX.MajorTickMark.Size = 1.5F; chartArea1.AxisX.Title = "One axis data distribution chart"; chartArea1.AxisX.TitleFont = new System.Drawing.Font("Trebuchet MS", 8F); chartArea1.AxisY.IsReversed = true; chartArea1.AxisY.LabelStyle.Enabled = false; chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisY.MajorGrid.Enabled = false; chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisY.MajorTickMark.Enabled = false; chartArea1.AxisY.Maximum = 2D; chartArea1.AxisY.Minimum = 0D; chartArea1.AxisY2.IsLabelAutoFit = false; chartArea1.AxisY2.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea1.BackColor = System.Drawing.Color.OldLace; chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; chartArea1.BackSecondaryColor = System.Drawing.Color.White; chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid; chartArea1.Name = "Default"; chartArea1.Position.Auto = false; chartArea1.Position.Height = 15F; chartArea1.Position.Width = 96F; chartArea1.Position.X = 3F; chartArea1.Position.Y = 4F; chartArea1.ShadowColor = System.Drawing.Color.Transparent; chartArea2.Area3DStyle.Inclination = 15; chartArea2.Area3DStyle.IsClustered = true; chartArea2.Area3DStyle.IsRightAngleAxes = false; chartArea2.Area3DStyle.Perspective = 10; chartArea2.Area3DStyle.Rotation = 10; chartArea2.Area3DStyle.WallWidth = 0; chartArea2.AxisX.LabelAutoFitStyle = ((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles)((((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.IncreaseFont | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont) | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.LabelsAngleStep90) | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.WordWrap))); chartArea2.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea2.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea2.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea2.AxisX.Title = "Histogram (Frequency Diagram)"; chartArea2.AxisX.TitleFont = new System.Drawing.Font("Trebuchet MS", 8F); chartArea2.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea2.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea2.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea2.AxisY2.IsLabelAutoFit = false; chartArea2.AxisY2.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea2.BackColor = System.Drawing.Color.OldLace; chartArea2.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; chartArea2.BackSecondaryColor = System.Drawing.Color.White; chartArea2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea2.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid; chartArea2.Name = "HistogramArea"; chartArea2.Position.Auto = false; chartArea2.Position.Height = 77F; chartArea2.Position.Width = 93F; chartArea2.Position.X = 3F; chartArea2.Position.Y = 18F; chartArea2.ShadowColor = System.Drawing.Color.Transparent; this.chart1.ChartAreas.Add(chartArea1); this.chart1.ChartAreas.Add(chartArea2); this.chart1.Dock = System.Windows.Forms.DockStyle.Fill; legend1.BackColor = System.Drawing.Color.Transparent; legend1.Enabled = false; legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); legend1.IsTextAutoFit = false; legend1.Name = "Default"; this.chart1.Legends.Add(legend1); this.chart1.Location = new System.Drawing.Point(0, 104); this.chart1.Name = "chart1"; series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105))))); series1.ChartArea = "Default"; series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(120)))), ((int)(((byte)(252)))), ((int)(((byte)(180)))), ((int)(((byte)(65))))); series1.Enabled = false; series1.Legend = "Default"; series1.MarkerSize = 9; series1.Name = "RawData"; series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(110)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105))))); series2.ChartArea = "Default"; series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(120)))), ((int)(((byte)(252)))), ((int)(((byte)(180)))), ((int)(((byte)(65))))); series2.Legend = "Default"; series2.MarkerSize = 8; series2.Name = "DataDistribution"; series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series3.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105))))); series3.ChartArea = "HistogramArea"; series3.Color = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(64)))), ((int)(((byte)(10))))); series3.IsValueShownAsLabel = true; series3.Legend = "Default"; series3.Name = "Histogram"; series3.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; series3.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; this.chart1.Series.Add(series1); this.chart1.Series.Add(series2); this.chart1.Series.Add(series3); this.chart1.Size = new System.Drawing.Size(904, 760); this.chart1.TabIndex = 1; // // HistogramChart // this.BackColor = System.Drawing.Color.White; this.Controls.Add(this.chart1); this.Controls.Add(this.panel1); this.Controls.Add(this.labelSampleComment); this.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "HistogramChart"; this.Size = new System.Drawing.Size(1460, 864); this.Load += new System.EventHandler(this.PieChartType_Load); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.ResumeLayout(false); } #endregion private void UpdateChartSettings() { if (!this.loadingData) { // Create a histogram series HistogramChartHelper histogramHelper = new HistogramChartHelper(); histogramHelper.SegmentIntervalNumber = int.Parse(comboBoxIntervalNumber.Text); histogramHelper.ShowPercentOnSecondaryYAxis = checkBoxShowPercents.Checked; // NOTE: Interval width may be specified instead of interval number //histogramHelper.SegmentIntervalWidth = 15; histogramHelper.CreateHistogram(chart1, "RawData", "Histogram"); // Set same X axis scale and interval in the single axis data distribution // chart area as in the histogram chart area. chart1.ChartAreas["Default"].AxisX.Minimum = chart1.ChartAreas["HistogramArea"].AxisX.Minimum; chart1.ChartAreas["Default"].AxisX.Maximum = chart1.ChartAreas["HistogramArea"].AxisX.Maximum; chart1.ChartAreas["Default"].AxisX.Interval = chart1.ChartAreas["HistogramArea"].AxisX.Interval; } } private void PieChartType_Load(object sender, System.EventArgs e) { // Populate chart with random data Random rand = new Random(); for (int index = 1; index < 70; index++) { int maxValue = (int)Math.Pow(rand.Next(100, 1000) / 100.0, 2.0); double newVal = 100 + rand.Next(0, (int)maxValue); chart1.Series["RawData"].Points.AddY(newVal); newVal = 100 + rand.Next(-(int)maxValue, 0); chart1.Series["RawData"].Points.AddY(newVal); } // Populate single axis data distribution series. Show Y value of the // data series as X value and set all Y values to 1. foreach (DataPoint dataPoint in chart1.Series["RawData"].Points) { chart1.Series["DataDistribution"].Points.AddXY(dataPoint.YValues[0], 1); } // Set current selection this.loadingData = true; comboBoxIntervalNumber.SelectedIndex = 1; this.loadingData = false; // Update chart UpdateChartSettings(); } private void comboBoxIntervalNumber_SelectedIndexChanged(object sender, System.EventArgs e) { // Update chart UpdateChartSettings(); } private void checkBoxShowPercents_CheckedChanged(object sender, System.EventArgs e) { // Update chart UpdateChartSettings(); } } }