Some Help Wrapper Class Example

Mar 14, 2013 at 2:39 PM
Here are 2 wrapper classes I created for a project - they don't do everything - but they do the stuff I needed for the project. They may be useful to anyone trying to use this with Windows Forms. One of the methods that might be very useful is plotting values in a GridView by passing the GridView to the method.
    public class Plot
    {
        #region Properties
        public DotNet.Highcharts.Highcharts Chart = new DotNet.Highcharts.Highcharts("chart");
        public DotNet.Highcharts.Options.Title Title = new DotNet.Highcharts.Options.Title();
        public DotNet.Highcharts.Options.XAxis XAxis = new DotNet.Highcharts.Options.XAxis
        {
            Title = new DotNet.Highcharts.Options.XAxisTitle()
        };
        
        public DotNet.Highcharts.Options.YAxis YAxis = new DotNet.Highcharts.Options.YAxis        
        {
            Title = new DotNet.Highcharts.Options.YAxisTitle() 
        };
 
        //        public DotNet.Highcharts.Options.PlotOptions Options = new DotNet.Highcharts.Options.PlotOptions();

        #endregion

        #region 
        public Plot(){
            Chart.SetTitle(Title);
            Chart.SetXAxis(XAxis);
            Chart.SetYAxis(YAxis);
       }

        #endregion 

        #region Set Values


        #endregion


        public string ToHtmlString()
        {
            return Chart.ToHtmlString();
        }

        #region Samples
        public void SetAsSample()
        {
            Chart.SetXAxis(new DotNet.Highcharts.Options.XAxis
            {
                Categories = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
                   
            });
            Chart.SetSeries(new DotNet.Highcharts.Options.Series
            {
                Data = new DotNet.Highcharts.Helpers.Data(new object[] { 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 })
            });
        }
        #endregion
    }

    public class ScatterPlot : Plot
    {
        public DotNet.Highcharts.Options.Series[] series = new DotNet.Highcharts.Options.Series[10];
        public int nSeries;

        public ScatterPlot():base()
        {
        }

        public void setSeries( int n, int increment, string title, double[] v)
        {
            object[] values = new object[n];
            for (int i = 0; i < n; i++)
            {
                values[i] = new DotNet.Highcharts.Options.Point { X = i * increment, Y = v[i] };
            }
 
            Chart.SetSeries(new DotNet.Highcharts.Options.Series{
                Type = DotNet.Highcharts.Enums.ChartTypes.Scatter,
                Data = new DotNet.Highcharts.Helpers.Data(values), 
                Name = title
            });
        }

        public string plotGrid(string title, int xcol, GridView g)
        {
            // xcol is the column with the x data
            int n = g.Rows[0].Cells.Count;
            DotNet.Highcharts.Options.Series[] series = new DotNet.Highcharts.Options.Series[n-1];
            
            int j = 0; // Alternate counter - you do not set the xcol as a series
            for (int i = 0; i < n; i++)
            {
                if (i != xcol)
                {
                    series[j] = new DotNet.Highcharts.Options.Series
                    {
                        Type = DotNet.Highcharts.Enums.ChartTypes.Scatter,
                        Data = new DotNet.Highcharts.Helpers.Data(getDataFromGridColumn(xcol, i, g)),
                        Name = g.HeaderRow.Cells[i].Text
                    };
                    j++;
                }
            }

            this.Title.Text = title;
            this.Chart.SetSeries(series);

            return ToHtmlString();
        }

        public string plotGrid(string title, int xcol, int[] cols, GridView g)
        {
            // Same as plotGrid, however only plots columns specified in cols
            int n = cols.Length;
            int n2 = g.Rows[0].Cells.Count;
            DotNet.Highcharts.Options.Series[] series = new DotNet.Highcharts.Options.Series[n];

            int j = 0; // Alternate counter - you do not set the xcol as a series
            for (int i = 0; i < n2; i++)
            {
                if (i != xcol && cols.Contains(i))
                {
                    series[j] = new DotNet.Highcharts.Options.Series
                    {
                        Type = DotNet.Highcharts.Enums.ChartTypes.Scatter,
                        Data = new DotNet.Highcharts.Helpers.Data(getDataFromGridColumn(xcol, i, g)),
                        Name = g.HeaderRow.Cells[i].Text
                    };
                    j++;
                }
            }

            this.Title.Text = title;
            this.Chart.SetSeries(series);

            return ToHtmlString();

        }

        private object[] getDataFromGridColumn(int xcol, int ycol, GridView g)
         {
            // Answers a Point Array that can be used as Data in a series
            // xcol is the column with the x data

            int n = g.Rows.Count;
            object[] v = new object[g.Rows.Count-1];
             for (int i = 1; i < n; i++)
             {
                 v[i - 1] = new DotNet.Highcharts.Options.Point
                 {
                     X = Convert.ToDouble(g.Rows[i].Cells[xcol].Text),
                     Y = Convert.ToDouble(g.Rows[i].Cells[ycol].Text)
                 };
             }
            return v;
         }
    }