Click here to Skip to main content
15,887,683 members
Articles / Web Development / ASP.NET

Generate graph using Nplot

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Sep 2012CPOL2 min read 32.1K   1.2K   8  
How to create a line and point chart for your ASP.NET application.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NPlot;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;

public partial class PointGraph : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreatePointGraph();
    }

    private void CreatePointGraph()
    {
        NPlot.Bitmap.PlotSurface2D npSurface = new NPlot.Bitmap.PlotSurface2D(700, 500);

        NPlot.PointPlot npPlot1 = new PointPlot();
        NPlot.PointPlot npPlot2 = new PointPlot();
        NPlot.PointPlot npPlot3 = new PointPlot();

        //Font definitions:
        Font TitleFont=new Font("Arial", 12);
        Font AxisFont=new Font("Arial", 10);
        Font TickFont=new Font("Arial", 8);



        //Legend definition:
        NPlot.Legend npLegend = new NPlot.Legend();

        DateTime[] X1 = new DateTime[50];
        DateTime[] X2 = new DateTime[50];
        int[] Y1 = new int[50];
        int[] Y2 = new int[50];

        Random r1 = new Random();
        Random r2 = new Random();



        for (int i = 0; i < 50; i++)
        {
            X1[i] = DateTime.Now.Date.AddDays(i);
            X2[i] = DateTime.Now.Date.AddDays(i);
            Y1[i] = r1.Next(100);
            Y2[i] = r2.Next(300);
        }


        //Prepare PlotSurface:
        npSurface.Clear();
        npSurface.Title = "Point Graph";
        npSurface.BackColor = System.Drawing.Color.White;


        //Left Y axis grid:
        NPlot.Grid p = new Grid();
        npSurface.Add(p, NPlot.PlotSurface2D.XAxisPosition.Bottom, NPlot.PlotSurface2D.YAxisPosition.Left);


        
        //Weight:
        npPlot1.AbscissaData = X1;
        npPlot1.DataSource = Y1;
        npPlot1.Label = "Weight (kg.)";
        npPlot1.Marker.Color = System.Drawing.Color.Blue;

        //Height
        npPlot2.AbscissaData = X2;
        npPlot2.DataSource = Y2;
        npPlot2.Label = "Height (cm)";
        npPlot2.Marker.Color = System.Drawing.Color.Green;


        npSurface.Add(npPlot1, NPlot.PlotSurface2D.XAxisPosition.Bottom, NPlot.PlotSurface2D.YAxisPosition.Left);
        npSurface.Add(npPlot2, NPlot.PlotSurface2D.XAxisPosition.Bottom, NPlot.PlotSurface2D.YAxisPosition.Left);



        //X axis
        npSurface.XAxis1.Label = "Date";
        npSurface.XAxis1.NumberFormat = "yyyy-MM-dd";
        npSurface.XAxis1.TicksLabelAngle = 90;
        npSurface.XAxis1.TickTextNextToAxis = true;
        npSurface.XAxis1.FlipTicksLabel = true;
        npSurface.XAxis1.LabelOffset = 110;
        npSurface.XAxis1.LabelOffsetAbsolute = true;
        npSurface.XAxis1.LabelFont = AxisFont;
        npSurface.XAxis1.TickTextFont = TickFont;
        

        //Y axis
        npSurface.YAxis1.Label = "Value";
        npSurface.YAxis1.NumberFormat = "{0:####0.0}";
        npSurface.YAxis1.LabelFont = AxisFont;
        npSurface.YAxis1.TickTextFont = TickFont;

        //Add legend:
        npLegend.AttachTo(NPlot.PlotSurface2D.XAxisPosition.Top, NPlot.PlotSurface2D.YAxisPosition.Right);
        npLegend.VerticalEdgePlacement = NPlot.Legend.Placement.Inside;
        npLegend.HorizontalEdgePlacement = NPlot.Legend.Placement.Outside;
        npLegend.BorderStyle = NPlot.LegendBase.BorderType.Line;
        npSurface.Legend = npLegend;


        //Update PlotSurface:
        npSurface.Refresh();


        //Save PlotSurface to MemoryStream, stream output as GIF file:
        Response.Buffer = true;
        Response.ContentType = "image/gif";

        MemoryStream memStream = new MemoryStream();

        npSurface.Bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Gif);
        memStream.WriteTo(Response.OutputStream);
        Response.End();
    }


}

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
Software Developer (Senior) icddr,b
Bangladesh Bangladesh
More than 8 years experience on Programming and Project implementation, I was primarily involved with projects for private organization,Govt.(Bangladesh Army,DG Health,RJSC), NGO (SEDF,WFP). Presently I am working at ICDDR,B and enhancing Hospital Management System developed by Microsoft Dynamic NAV and Windows Mobile Application 5.0

An active supporter of Open Source technology, my interested areas are ERP, IT Audit, Data warehouse, BI etc.

Playing Guitar for 15 years, my interested music style is Blues Rock,Neo Classical.

Certification

70-540:Microsoft® Windows Mobile® 5.0 - Application Development
MB7-514:Microsoft Dynamics™ NAV 5.0 C/SIDE Introduction
MB7-516:Microsoft Dynamics™ NAV 5.0 Solution Development
MB7-517:Microsoft Dynamics™ NAV 5.0 Installation and Configuration
MB7-515:Microsoft Dynamics™ NAV 5.0 Financials
70-432:Microsoft SQL Server 2008 - Implementation and Maintenance
70-450:PRO: Designing, Optimizing and Maintaining a Database Administrative Solution Using Microsoft SQL Server 2008
70-448:Microsoft SQL Server 2008, Business Intelligence Development and Maintenance
312-50:Certified Ethical Hacker

Web :http://masudparvezshabuz.appspot.com
Blog :http://masudparvezshabuz.wordpress.com
linkedin :http://www.linkedin.com/in/masudparvez

Comments and Discussions