line chart example pls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zach

    line chart example pls

    Hi,

    I am completely new to VC#.NET graphics code and would
    like some help to stop me wasting my time up blind alleys.

    I am looking for a line charting code example. Nothing posh or airy
    fairy, just a simple area with annotated axis and a graph.

    I am not looking for code to create a multi applicable graphic contol and
    I don't want to buy a library. I have been around the Code Project and I
    do have forms literature (which I do not find to be so helpful on the
    subject
    at hand).

    Many thanks,
    Zach.


  • Tim Wilson

    #2
    Re: line chart example pls

    Although I haven't had a need to use it, ZedGraph may be something to look
    into.


    --
    Tim Wilson
    ..Net Compact Framework MVP

    "Zach" <00@00.00> wrote in message
    news:42c16a5d$0 $2448$9f6128d4@ news.freeler.nl ...[color=blue]
    > Hi,
    >
    > I am completely new to VC#.NET graphics code and would
    > like some help to stop me wasting my time up blind alleys.
    >
    > I am looking for a line charting code example. Nothing posh or airy
    > fairy, just a simple area with annotated axis and a graph.
    >
    > I am not looking for code to create a multi applicable graphic contol and
    > I don't want to buy a library. I have been around the Code Project and I
    > do have forms literature (which I do not find to be so helpful on the
    > subject
    > at hand).
    >
    > Many thanks,
    > Zach.
    >
    >[/color]


    Comment

    • Zach

      #3
      Re: line chart example pls

      Thanks for your suggestion, however,

      I had had a look at that article already.
      The code is quite sophisticated, besides
      when I loaded it to the VC# IDE it wouldn't run.
      The code complexity is such that I cannot
      extract just the essentials I need.

      My last attempt was to convert the following code I found to a normal cs
      file
      but I ran aground. If you have time, could you help me out on this?

      <%@ Import Namespace="Syst em" %>
      <%@ Import Namespace="Syst em.Drawing" %>
      <%@ Import Namespace="Syst em.Drawing.Draw ing2D" %>
      <%@ Import Namespace="Syst em.Drawing.Imag ing" %>

      <script language="C#" runat="server">

      class LineChart
      {
      //Sample ASPX C# LineChart Class, Steve Hall, 2002
      public Bitmap b;
      public string Title="Default Title";
      public ArrayList chartValues = new ArrayList();
      public float Xorigin=0, Yorigin=0;
      public float ScaleX, ScaleY;
      public float Xdivs=2, Ydivs=2;

      private int Width, Height;
      private Graphics g;
      private Page p;

      struct datapoint {
      public float x;
      public float y;
      public bool valid;
      }

      //initialize
      public LineChart(int myWidth, int myHeight, Page myPage) {
      Width = myWidth; Height = myHeight;
      ScaleX = myWidth; ScaleY = myHeight;
      b = new Bitmap(myWidth, myHeight);
      g = Graphics.FromIm age(b);
      p = myPage;
      }

      public void AddValue(int x, int y) {
      datapoint myPoint;
      myPoint.x=x;
      myPoint.y=y;
      myPoint.valid=t rue;
      chartValues.Add (myPoint);
      }

      public void Draw() {
      int i;
      float x, y, x0, y0;
      string myLabel;
      Pen blackPen = new Pen(Color.Black ,1);
      Brush blackBrush = new SolidBrush(Colo r.Black);
      Font axesFont = new Font("arial",10 );

      //first establish working area
      p.Response.Cont entType="image/jpeg";
      g.FillRectangle (new
      SolidBrush(Colo r.LightYellow), 0,0,Width,Heigh t);
      int ChartInset = 50;
      int ChartWidth = Width-(2*ChartInset);
      int ChartHeight = Height-(2*ChartInset);
      g.DrawRectangle (new
      Pen(Color.Black ,1),ChartInset, ChartInset,Char tWidth,ChartHei ght);

      //must draw all text items before doing the rotate below
      g.DrawString(Ti tle, new Font("arial",14 ), blackBrush,
      Width/3, 10);
      //draw X axis labels
      for(i=0; i<=Xdivs; i++) {
      x=ChartInset+(i *ChartWidth)/Xdivs;
      y=ChartHeight+C hartInset;
      myLabel = (Xorigin + (ScaleX*i/Xdivs)).ToStrin g();
      g.DrawString(my Label, axesFont, blackBrush, x-4,
      y+10);
      g.DrawLine(blac kPen, x, y+2, x, y-2);
      }
      //draw Y axis labels
      for(i=0; i<=Ydivs; i++) {
      x=ChartInset;
      y=ChartHeight+C hartInset-(i*ChartHeight/Ydivs);
      myLabel = (Yorigin + (ScaleY*i/Ydivs)).ToStrin g();
      g.DrawString(my Label, axesFont, blackBrush, 5, y-6);
      g.DrawLine(blac kPen, x+2, y, x-2, y);
      }

      //transform drawing coords to lower-left (0,0)
      g.RotateTransfo rm(180);
      g.TranslateTran sform(0,-Height);
      g.TranslateTran sform(-ChartInset,Char tInset);
      g.ScaleTransfor m(-1, 1);

      //draw chart data
      datapoint prevPoint = new datapoint();
      prevPoint.valid =false;
      foreach(datapoi nt myPoint in chartValues) {
      if(prevPoint.va lid==true) {
      x0=ChartWidth*( prevPoint.x-Xorigin)/ScaleX;
      y0=ChartHeight* (prevPoint.y-Yorigin)/ScaleY;
      x=ChartWidth*(m yPoint.x-Xorigin)/ScaleX;
      y=ChartHeight*( myPoint.y-Yorigin)/ScaleY;
      g.DrawLine(blac kPen,x0,y0,x,y) ;
      g.FillEllipse(b lackBrush,x0-2,y0-2,4,4);
      g.FillEllipse(b lackBrush,x-2,y-2,4,4);
      }
      prevPoint = myPoint;
      }

      //finally send graphics to browser
      b.Save(p.Respon se.OutputStream , ImageFormat.Jpe g);
      }

      ~LineChart() {
      g.Dispose();
      b.Dispose();
      }
      }


      void Page_Load(Objec t sender, EventArgs e) {
      LineChart c = new LineChart(640, 480, Page);
      c.Title="My Line Chart";
      c.Xorigin=0; c.ScaleX=500; c.Xdivs=5;
      c.Yorigin=0; c.ScaleY=1000; c.Ydivs=5;
      c.AddValue(50,5 0);
      c.AddValue(100, 100);
      c.AddValue(200, 150);
      c.AddValue(450, 450);
      c.Draw();
      }

      </script>

      Zach.

      "Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote in message
      news:O16lUyAfFH A.3036@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Although I haven't had a need to use it, ZedGraph may be something to look
      > into.
      > http://www.codeproject.com/csharp/ZedGraph.asp
      >
      > --
      > Tim Wilson
      > .Net Compact Framework MVP
      >
      > "Zach" <00@00.00> wrote in message
      > news:42c16a5d$0 $2448$9f6128d4@ news.freeler.nl ...[color=green]
      > > Hi,
      > >
      > > I am completely new to VC#.NET graphics code and would
      > > like some help to stop me wasting my time up blind alleys.
      > >
      > > I am looking for a line charting code example. Nothing posh or airy
      > > fairy, just a simple area with annotated axis and a graph.
      > >
      > > I am not looking for code to create a multi applicable graphic contol[/color][/color]
      and[color=blue][color=green]
      > > I don't want to buy a library. I have been around the Code Project and I
      > > do have forms literature (which I do not find to be so helpful on the
      > > subject
      > > at hand).
      > >
      > > Many thanks,
      > > Zach.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Tim Wilson

        #4
        Re: line chart example pls

        Have you tried using the ZedGraph Web Control version? I develop using
        Windows Forms, not Web Forms, so I can't say this for sure, since I haven't
        used it, but I would imagine that it would just be a matter of setting
        properties on an object in the code behind. The control should take care of
        drawing the chart to an image using the information you provide it. In the
        long run it might be worth the time to figure out how to use the control
        rather than trying to figure out how to draw it all yourself.

        --
        Tim Wilson
        ..Net Compact Framework MVP

        "Zach" <00@00.00> wrote in message
        news:42c196af$0 $1583$9f6128d4@ news.freeler.nl ...[color=blue]
        > Thanks for your suggestion, however,
        >
        > I had had a look at that article already.
        > The code is quite sophisticated, besides
        > when I loaded it to the VC# IDE it wouldn't run.
        > The code complexity is such that I cannot
        > extract just the essentials I need.
        >
        > My last attempt was to convert the following code I found to a normal cs
        > file
        > but I ran aground. If you have time, could you help me out on this?
        >
        > <%@ Import Namespace="Syst em" %>
        > <%@ Import Namespace="Syst em.Drawing" %>
        > <%@ Import Namespace="Syst em.Drawing.Draw ing2D" %>
        > <%@ Import Namespace="Syst em.Drawing.Imag ing" %>
        >
        > <script language="C#" runat="server">
        >
        > class LineChart
        > {
        > //Sample ASPX C# LineChart Class, Steve Hall, 2002
        > public Bitmap b;
        > public string Title="Default Title";
        > public ArrayList chartValues = new ArrayList();
        > public float Xorigin=0, Yorigin=0;
        > public float ScaleX, ScaleY;
        > public float Xdivs=2, Ydivs=2;
        >
        > private int Width, Height;
        > private Graphics g;
        > private Page p;
        >
        > struct datapoint {
        > public float x;
        > public float y;
        > public bool valid;
        > }
        >
        > //initialize
        > public LineChart(int myWidth, int myHeight, Page myPage) {
        > Width = myWidth; Height = myHeight;
        > ScaleX = myWidth; ScaleY = myHeight;
        > b = new Bitmap(myWidth, myHeight);
        > g = Graphics.FromIm age(b);
        > p = myPage;
        > }
        >
        > public void AddValue(int x, int y) {
        > datapoint myPoint;
        > myPoint.x=x;
        > myPoint.y=y;
        > myPoint.valid=t rue;
        > chartValues.Add (myPoint);
        > }
        >
        > public void Draw() {
        > int i;
        > float x, y, x0, y0;
        > string myLabel;
        > Pen blackPen = new Pen(Color.Black ,1);
        > Brush blackBrush = new SolidBrush(Colo r.Black);
        > Font axesFont = new Font("arial",10 );
        >
        > //first establish working area
        > p.Response.Cont entType="image/jpeg";
        > g.FillRectangle (new
        > SolidBrush(Colo r.LightYellow), 0,0,Width,Heigh t);
        > int ChartInset = 50;
        > int ChartWidth = Width-(2*ChartInset);
        > int ChartHeight = Height-(2*ChartInset);
        > g.DrawRectangle (new
        > Pen(Color.Black ,1),ChartInset, ChartInset,Char tWidth,ChartHei ght);
        >
        > //must draw all text items before doing the rotate below
        > g.DrawString(Ti tle, new Font("arial",14 ), blackBrush,
        > Width/3, 10);
        > //draw X axis labels
        > for(i=0; i<=Xdivs; i++) {
        > x=ChartInset+(i *ChartWidth)/Xdivs;
        > y=ChartHeight+C hartInset;
        > myLabel = (Xorigin + (ScaleX*i/Xdivs)).ToStrin g();
        > g.DrawString(my Label, axesFont, blackBrush, x-4,
        > y+10);
        > g.DrawLine(blac kPen, x, y+2, x, y-2);
        > }
        > //draw Y axis labels
        > for(i=0; i<=Ydivs; i++) {
        > x=ChartInset;
        > y=ChartHeight+C hartInset-(i*ChartHeight/Ydivs);
        > myLabel = (Yorigin + (ScaleY*i/Ydivs)).ToStrin g();
        > g.DrawString(my Label, axesFont, blackBrush, 5, y-6);
        > g.DrawLine(blac kPen, x+2, y, x-2, y);
        > }
        >
        > //transform drawing coords to lower-left (0,0)
        > g.RotateTransfo rm(180);
        > g.TranslateTran sform(0,-Height);
        > g.TranslateTran sform(-ChartInset,Char tInset);
        > g.ScaleTransfor m(-1, 1);
        >
        > //draw chart data
        > datapoint prevPoint = new datapoint();
        > prevPoint.valid =false;
        > foreach(datapoi nt myPoint in chartValues) {
        > if(prevPoint.va lid==true) {
        > x0=ChartWidth*( prevPoint.x-Xorigin)/ScaleX;
        > y0=ChartHeight* (prevPoint.y-Yorigin)/ScaleY;
        > x=ChartWidth*(m yPoint.x-Xorigin)/ScaleX;
        > y=ChartHeight*( myPoint.y-Yorigin)/ScaleY;
        > g.DrawLine(blac kPen,x0,y0,x,y) ;
        > g.FillEllipse(b lackBrush,x0-2,y0-2,4,4);
        > g.FillEllipse(b lackBrush,x-2,y-2,4,4);
        > }
        > prevPoint = myPoint;
        > }
        >
        > //finally send graphics to browser
        > b.Save(p.Respon se.OutputStream , ImageFormat.Jpe g);
        > }
        >
        > ~LineChart() {
        > g.Dispose();
        > b.Dispose();
        > }
        > }
        >
        >
        > void Page_Load(Objec t sender, EventArgs e) {
        > LineChart c = new LineChart(640, 480, Page);
        > c.Title="My Line Chart";
        > c.Xorigin=0; c.ScaleX=500; c.Xdivs=5;
        > c.Yorigin=0; c.ScaleY=1000; c.Ydivs=5;
        > c.AddValue(50,5 0);
        > c.AddValue(100, 100);
        > c.AddValue(200, 150);
        > c.AddValue(450, 450);
        > c.Draw();
        > }
        >
        > </script>
        >
        > Zach.
        >
        > "Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote in message
        > news:O16lUyAfFH A.3036@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Although I haven't had a need to use it, ZedGraph may be something to[/color][/color]
        look[color=blue][color=green]
        > > into.
        > > http://www.codeproject.com/csharp/ZedGraph.asp
        > >
        > > --
        > > Tim Wilson
        > > .Net Compact Framework MVP
        > >
        > > "Zach" <00@00.00> wrote in message
        > > news:42c16a5d$0 $2448$9f6128d4@ news.freeler.nl ...[color=darkred]
        > > > Hi,
        > > >
        > > > I am completely new to VC#.NET graphics code and would
        > > > like some help to stop me wasting my time up blind alleys.
        > > >
        > > > I am looking for a line charting code example. Nothing posh or airy
        > > > fairy, just a simple area with annotated axis and a graph.
        > > >
        > > > I am not looking for code to create a multi applicable graphic contol[/color][/color]
        > and[color=green][color=darkred]
        > > > I don't want to buy a library. I have been around the Code Project and[/color][/color][/color]
        I[color=blue][color=green][color=darkred]
        > > > do have forms literature (which I do not find to be so helpful on the
        > > > subject
        > > > at hand).
        > > >
        > > > Many thanks,
        > > > Zach.
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Zach

          #5
          Re: line chart example pls

          Hi Tim,

          When I loaded the code to my IDE it produced *errors*
          so there wasn't a lot I could do with it.
          I have now come across www.devx.com/dotnet/Article/20077
          This is a tutorial. I haven't started on it yet, but it seems quite OK,
          so I will dig into that, and hope it will provide me with what I am
          after.

          Zach.


          "Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote in message
          news:uIxvlLBfFH A.3040@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Have you tried using the ZedGraph Web Control version? I develop using
          > Windows Forms, not Web Forms, so I can't say this for sure, since I[/color]
          haven't[color=blue]
          > used it, but I would imagine that it would just be a matter of setting
          > properties on an object in the code behind. The control should take care[/color]
          of[color=blue]
          > drawing the chart to an image using the information you provide it. In the
          > long run it might be worth the time to figure out how to use the control
          > rather than trying to figure out how to draw it all yourself.
          >
          > --
          > Tim Wilson
          > .Net Compact Framework MVP
          >
          > "Zach" <00@00.00> wrote in message
          > news:42c196af$0 $1583$9f6128d4@ news.freeler.nl ...[color=green]
          > > Thanks for your suggestion, however,
          > >
          > > I had had a look at that article already.
          > > The code is quite sophisticated, besides
          > > when I loaded it to the VC# IDE it wouldn't run.
          > > The code complexity is such that I cannot
          > > extract just the essentials I need.
          > >
          > > My last attempt was to convert the following code I found to a normal cs
          > > file
          > > but I ran aground. If you have time, could you help me out on this?
          > >
          > > <%@ Import Namespace="Syst em" %>
          > > <%@ Import Namespace="Syst em.Drawing" %>
          > > <%@ Import Namespace="Syst em.Drawing.Draw ing2D" %>
          > > <%@ Import Namespace="Syst em.Drawing.Imag ing" %>
          > >
          > > <script language="C#" runat="server">
          > >
          > > class LineChart
          > > {
          > > //Sample ASPX C# LineChart Class, Steve Hall, 2002
          > > public Bitmap b;
          > > public string Title="Default Title";
          > > public ArrayList chartValues = new ArrayList();
          > > public float Xorigin=0, Yorigin=0;
          > > public float ScaleX, ScaleY;
          > > public float Xdivs=2, Ydivs=2;
          > >
          > > private int Width, Height;
          > > private Graphics g;
          > > private Page p;
          > >
          > > struct datapoint {
          > > public float x;
          > > public float y;
          > > public bool valid;
          > > }
          > >
          > > //initialize
          > > public LineChart(int myWidth, int myHeight, Page myPage) {
          > > Width = myWidth; Height = myHeight;
          > > ScaleX = myWidth; ScaleY = myHeight;
          > > b = new Bitmap(myWidth, myHeight);
          > > g = Graphics.FromIm age(b);
          > > p = myPage;
          > > }
          > >
          > > public void AddValue(int x, int y) {
          > > datapoint myPoint;
          > > myPoint.x=x;
          > > myPoint.y=y;
          > > myPoint.valid=t rue;
          > > chartValues.Add (myPoint);
          > > }
          > >
          > > public void Draw() {
          > > int i;
          > > float x, y, x0, y0;
          > > string myLabel;
          > > Pen blackPen = new Pen(Color.Black ,1);
          > > Brush blackBrush = new SolidBrush(Colo r.Black);
          > > Font axesFont = new Font("arial",10 );
          > >
          > > //first establish working area
          > > p.Response.Cont entType="image/jpeg";
          > > g.FillRectangle (new
          > > SolidBrush(Colo r.LightYellow), 0,0,Width,Heigh t);
          > > int ChartInset = 50;
          > > int ChartWidth = Width-(2*ChartInset);
          > > int ChartHeight = Height-(2*ChartInset);
          > > g.DrawRectangle (new
          > > Pen(Color.Black ,1),ChartInset, ChartInset,Char tWidth,ChartHei ght);
          > >
          > > //must draw all text items before doing the rotate below
          > > g.DrawString(Ti tle, new Font("arial",14 ), blackBrush,
          > > Width/3, 10);
          > > //draw X axis labels
          > > for(i=0; i<=Xdivs; i++) {
          > > x=ChartInset+(i *ChartWidth)/Xdivs;
          > > y=ChartHeight+C hartInset;
          > > myLabel = (Xorigin + (ScaleX*i/Xdivs)).ToStrin g();
          > > g.DrawString(my Label, axesFont, blackBrush, x-4,
          > > y+10);
          > > g.DrawLine(blac kPen, x, y+2, x, y-2);
          > > }
          > > //draw Y axis labels
          > > for(i=0; i<=Ydivs; i++) {
          > > x=ChartInset;
          > > y=ChartHeight+C hartInset-(i*ChartHeight/Ydivs);
          > > myLabel = (Yorigin + (ScaleY*i/Ydivs)).ToStrin g();
          > > g.DrawString(my Label, axesFont, blackBrush, 5, y-6);
          > > g.DrawLine(blac kPen, x+2, y, x-2, y);
          > > }
          > >
          > > //transform drawing coords to lower-left (0,0)
          > > g.RotateTransfo rm(180);
          > > g.TranslateTran sform(0,-Height);
          > > g.TranslateTran sform(-ChartInset,Char tInset);
          > > g.ScaleTransfor m(-1, 1);
          > >
          > > //draw chart data
          > > datapoint prevPoint = new datapoint();
          > > prevPoint.valid =false;
          > > foreach(datapoi nt myPoint in chartValues) {
          > > if(prevPoint.va lid==true) {
          > > x0=ChartWidth*( prevPoint.x-Xorigin)/ScaleX;
          > > y0=ChartHeight* (prevPoint.y-Yorigin)/ScaleY;
          > > x=ChartWidth*(m yPoint.x-Xorigin)/ScaleX;
          > > y=ChartHeight*( myPoint.y-Yorigin)/ScaleY;
          > > g.DrawLine(blac kPen,x0,y0,x,y) ;
          > > g.FillEllipse(b lackBrush,x0-2,y0-2,4,4);
          > > g.FillEllipse(b lackBrush,x-2,y-2,4,4);
          > > }
          > > prevPoint = myPoint;
          > > }
          > >
          > > //finally send graphics to browser
          > > b.Save(p.Respon se.OutputStream , ImageFormat.Jpe g);
          > > }
          > >
          > > ~LineChart() {
          > > g.Dispose();
          > > b.Dispose();
          > > }
          > > }
          > >
          > >
          > > void Page_Load(Objec t sender, EventArgs e) {
          > > LineChart c = new LineChart(640, 480, Page);
          > > c.Title="My Line Chart";
          > > c.Xorigin=0; c.ScaleX=500; c.Xdivs=5;
          > > c.Yorigin=0; c.ScaleY=1000; c.Ydivs=5;
          > > c.AddValue(50,5 0);
          > > c.AddValue(100, 100);
          > > c.AddValue(200, 150);
          > > c.AddValue(450, 450);
          > > c.Draw();
          > > }
          > >
          > > </script>
          > >
          > > Zach.
          > >
          > > "Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote in[/color][/color]
          message[color=blue][color=green]
          > > news:O16lUyAfFH A.3036@TK2MSFTN GP10.phx.gbl...[color=darkred]
          > > > Although I haven't had a need to use it, ZedGraph may be something to[/color][/color]
          > look[color=green][color=darkred]
          > > > into.
          > > > http://www.codeproject.com/csharp/ZedGraph.asp
          > > >
          > > > --
          > > > Tim Wilson
          > > > .Net Compact Framework MVP
          > > >
          > > > "Zach" <00@00.00> wrote in message
          > > > news:42c16a5d$0 $2448$9f6128d4@ news.freeler.nl ...
          > > > > Hi,
          > > > >
          > > > > I am completely new to VC#.NET graphics code and would
          > > > > like some help to stop me wasting my time up blind alleys.
          > > > >
          > > > > I am looking for a line charting code example. Nothing posh or airy
          > > > > fairy, just a simple area with annotated axis and a graph.
          > > > >
          > > > > I am not looking for code to create a multi applicable graphic[/color][/color][/color]
          contol[color=blue][color=green]
          > > and[color=darkred]
          > > > > I don't want to buy a library. I have been around the Code Project[/color][/color][/color]
          and[color=blue]
          > I[color=green][color=darkred]
          > > > > do have forms literature (which I do not find to be so helpful on[/color][/color][/color]
          the[color=blue][color=green][color=darkred]
          > > > > subject
          > > > > at hand).
          > > > >
          > > > > Many thanks,
          > > > > Zach.
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • componentxtra@yahoo.com

            #6
            Re: line chart example pls

            Try XYGraph, which is very simple to use:
            http://www.componentxtra.com/html/Downloads.htm.

            ----
            Bob

            Components for .NET

            Comment

            Working...