Lecture – Basics – Arrays and Plotting

Prerequisites

Lecture – Creating a Popup Plot with Highcharts – Just another plotting lecture – does show Highcharts, not really required for this lecture.

Summary

Demonstrates plotting the quadratic equation using Highcharts.Net and arrays.

Video 

Reference Materials

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="PlotExample.aspx.cs" Inherits="SampleApplicationCOP4834.Pages.PlotExample" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" src="../Scripts/jquery-1.7.1.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
    <table><tr><td>
    a:<asp:TextBox ID="tbA" runat="server" Text="1"></asp:TextBox><br/>
    b:<asp:TextBox ID="tbB" runat="server" Text="1"></asp:TextBox><br/>
    c:<asp:TextBox ID="tbC" runat="server" Text="1"></asp:TextBox><br/>
        </td>
        <td>
    
    Last Number:<asp:TextBox ID="tbLast" runat="server" Text="10" /><br/>
    Step:<asp:TextBox ID="tbStep" runat="server" Text ="1" /><br/>
   
    </td>
    </tr></table>
    <highchart:LineChart runat="server" ID="hcLine" Width="500" Height="400" />
    <br/>
    <asp:Button ID="btnPlot" runat="server" Text="RePlot" OnClick="btnPlot_Click" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Highchart.UI;
using Highchart.Core;
using Highchart.Core.Data.Chart;
using Highchart.Core.PlotOptions;

namespace SampleApplicationCOP4834.Pages
{
    public partial class PlotExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Defining Axis

            hcLine.YAxis.Add(new YAxisItem { title = new Title("Future") });
//            hcLine.XAxis.Add(new XAxisItem { categories = new[] { "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002" } });
            hcLine.XAxis.Add(new XAxisItem { categories = xValues() });

            //New data collection
            var series = new List<Serie>();
 //           series.Add(new Serie { data = new object[] { 400, 435, 446, 479, 554, 634, 687, 750, 831 } });
            series.Add(new Serie { data = yValues(xValues())});
            //bind 
            hcLine.DataSource = series;
            hcLine.DataBind();
        }

        private string[] xValues()
        {
            int first = 0;
            int last = Convert.ToInt32(tbLast.Text);
            int step = Convert.ToInt32(tbStep.Text);

            int n = (last - first)/step;

            var x = new string[n];
            for (int i = 0; i < n; i++)
            {
                x[i] = Convert.ToString(first + i*step);
            }
            return x;
        }

        protected object[] yValues(string[] xValues)
        {
            double a = Convert.ToDouble(tbA.Text);
            double b = Convert.ToDouble(tbB.Text);
            double c = Convert.ToDouble(tbC.Text);

            var y = new object[xValues.Length];
            for (int i = 0; i < xValues.Length; i++)
            {
                double x = Convert.ToDouble(xValues[i]);
                y[i] = Quadratic(x, a, b, c);
            }
            return y;
        }

        protected double Quadratic(double x, double a, double b, double c)
        {
            return a*x*x + b*x + c;
        }

        protected void btnPlot_Click(object sender, EventArgs e)
        {

        }

    }
} 

Additional Information

COP 4834 Lectures Page