Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Adding Scrollbar to MS Chart Component

$
0
0

I have created an MS Chart component which work just fine in my application. However, I now need to add a horizontal scroll feature to the chart. I have review numerous post and am still striggling with this.

The following is my function which creates the chart:

      private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
          double minVal = -1;
          double maxVal = -1;
          double mminVal = -1;
          double mmaxVal = -1;
          int rowIndex = e.RowIndex;
          dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightBlue;
          dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
          dataGridView1.Refresh();
          DataGridViewRow roww = dataGridView1.Rows[rii];
          if (rii % 2 != 0)
             roww.DefaultCellStyle.BackColor = Color.White;
          else
             roww.DefaultCellStyle.BackColor = Color.LightBlue; //  .Cyan;
         DataGridViewRow row = dataGridView1.Rows[rowIndex];
         rii = rowIndex;
         row.DefaultCellStyle.BackColor = Color.Cyan;
         lblElement.Text = row.Cells[0].Value.ToString();
         Element.Series.Clear();
         Element.ChartAreas.Clear();
         Element.ChartAreas.Add("area");
         Element.ChartAreas["area"].Position.X = 0;
         Element.ChartAreas["area"].Position.Width = 100;
         Element.ChartAreas["area"].Position.Y = 0;
         Element.ChartAreas["area"].Position.Height = 100;

//added following code to add scrollbar feature
         Element.ChartAreas["area"].CursorX.AutoScroll = true;
         Element.ChartAreas["area"].AxisX.ScrollBar.IsPositionedInside = true;
         Element.ChartAreas["area"].CursorX.IsUserEnabled = true;
         Element.ChartAreas["area"].CursorX.IsUserSelectionEnabled = true;
         Element.ChartAreas["area"].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;

         foreach (DataRow trow in table.Rows)
         {
             if (trow["Element"].ToString() == row.Cells[0].Value.ToString())
             {
                 lblRange.Text = "Normal Range: " + trow["Range"].ToString();
                 try
                 {
                    string [] values =  trow["Range"].ToString().Split(new Char[] {'-'});
                     minVal =  Convert.ToDouble(values[0].ToString());
                     maxVal =  Convert.ToDouble(values[1].ToString());
                     mminVal = minVal;
                     mmaxVal = maxVal;
                     lblRange.Text = "Normal Range: " + minVal.ToString("#0.00") + " - " + maxVal.ToString("#0.00") ; //   min   trow["Range"].ToString();
                     Element.Palette = ChartColorPalette.None;
                     Element.PaletteCustomColors = new Color[] { Color.DarkOrchid };
                    break;
                 }
                 catch
                 {
                     return;
                 }
             }
         }

          int counter = 0;
          foreach (DataGridViewColumn col in dataGridView1.Columns)
          {
//adding this code since scrollbar feature not working
              if (counter > 9)
                  break;

              if (counter == 0)
              {
                Element.Series.Add(row.Cells[0].Value.ToString());
                Element.Series[row.Cells[0].Value.ToString()].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                Element.Series[row.Cells[0].Value.ToString()].BorderWidth = 3;
                Element.Series[row.Cells[0].Value.ToString()].MarkerStyle = MarkerStyle.Circle;
                Element.Series[row.Cells[0].Value.ToString()].MarkerSize = 10; // =  ma  .MarkerStyle = MarkerStyle.Circle;
              }
              else
              {
                  if (row.Cells[counter].Value != null)
                  {
                      this.Element.Series[row.Cells[0].Value.ToString()].Points.AddXY("(" + col.HeaderText + ")", row.Cells[counter].Value.ToString());
                      try
                      {
                          if (Convert.ToDouble(row.Cells[counter].Value.ToString()) < minVal)
                          {
                              minVal = Convert.ToDouble(row.Cells[counter].Value.ToString());
                          }

                          if (Convert.ToDouble(row.Cells[counter].Value.ToString()) > maxVal)
                          {
                              maxVal = Convert.ToDouble(row.Cells[counter].Value.ToString());
                          }
                      }
                      catch
                      {

                      }
                  }

              }
              counter += 1;
          }
          Element.ChartAreas["area"].AxisY.Minimum = minVal;
          Element.ChartAreas["area"].AxisY.Maximum = maxVal;
          StripLine stripLine = new StripLine();
          stripLine.IntervalOffset = mminVal;
          stripLine.StripWidth = stripLine.StripWidth = (mmaxVal - mminVal);
          stripLine.TextAlignment = StringAlignment.Near;
          stripLine.ForeColor = Color.Red;
          stripLine.Text = "Normal Range";
          stripLine.BackColor = Color.LightPink;
          stripLine.BorderColor = Color.Red;
          stripLine.BorderDashStyle = ChartDashStyle.Dash;
          stripLine.BorderWidth = (int)1.5;
          stripLine.BackHatchStyle = ChartHatchStyle.LightDownwardDiagonal;
          Element.ChartAreas["area"].AxisY.StripLines.Add(stripLine);
      }

No scroll bar show on my Chart. Please tell me what I need to add or change to make this work.

Thank you.


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>