473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.
Nov 17 '05 #1
5 8623
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:42**********************@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 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.

Nov 17 '05 #2
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="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

<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.FromImage(b);
p = myPage;
}

public void AddValue(int x, int y) {
datapoint myPoint;
myPoint.x=x;
myPoint.y=y;
myPoint.valid=true;
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(Color.Black);
Font axesFont = new Font("arial",10);

//first establish working area
p.Response.ContentType="image/jpeg";
g.FillRectangle(new
SolidBrush(Color.LightYellow),0,0,Width,Height);
int ChartInset = 50;
int ChartWidth = Width-(2*ChartInset);
int ChartHeight = Height-(2*ChartInset);
g.DrawRectangle(new
Pen(Color.Black,1),ChartInset,ChartInset,ChartWidt h,ChartHeight);

//must draw all text items before doing the rotate below
g.DrawString(Title, 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+ChartInset;
myLabel = (Xorigin + (ScaleX*i/Xdivs)).ToString();
g.DrawString(myLabel, axesFont, blackBrush, x-4,
y+10);
g.DrawLine(blackPen, x, y+2, x, y-2);
}
//draw Y axis labels
for(i=0; i<=Ydivs; i++) {
x=ChartInset;
y=ChartHeight+ChartInset-(i*ChartHeight/Ydivs);
myLabel = (Yorigin + (ScaleY*i/Ydivs)).ToString();
g.DrawString(myLabel, axesFont, blackBrush, 5, y-6);
g.DrawLine(blackPen, x+2, y, x-2, y);
}

//transform drawing coords to lower-left (0,0)
g.RotateTransform(180);
g.TranslateTransform(0,-Height);
g.TranslateTransform(-ChartInset,ChartInset);
g.ScaleTransform(-1, 1);

//draw chart data
datapoint prevPoint = new datapoint();
prevPoint.valid=false;
foreach(datapoint myPoint in chartValues) {
if(prevPoint.valid==true) {
x0=ChartWidth*(prevPoint.x-Xorigin)/ScaleX;
y0=ChartHeight*(prevPoint.y-Yorigin)/ScaleY;
x=ChartWidth*(myPoint.x-Xorigin)/ScaleX;
y=ChartHeight*(myPoint.y-Yorigin)/ScaleY;
g.DrawLine(blackPen,x0,y0,x,y);
g.FillEllipse(blackBrush,x0-2,y0-2,4,4);
g.FillEllipse(blackBrush,x-2,y-2,4,4);
}
prevPoint = myPoint;
}

//finally send graphics to browser
b.Save(p.Response.OutputStream, ImageFormat.Jpeg);
}

~LineChart() {
g.Dispose();
b.Dispose();
}
}
void Page_Load(Object 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,50);
c.AddValue(100,100);
c.AddValue(200,150);
c.AddValue(450,450);
c.Draw();
}

</script>

Zach.

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl...
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:42**********************@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 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.


Nov 17 '05 #3
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:42**********************@news.freeler.nl...
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="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

<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.FromImage(b);
p = myPage;
}

public void AddValue(int x, int y) {
datapoint myPoint;
myPoint.x=x;
myPoint.y=y;
myPoint.valid=true;
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(Color.Black);
Font axesFont = new Font("arial",10);

//first establish working area
p.Response.ContentType="image/jpeg";
g.FillRectangle(new
SolidBrush(Color.LightYellow),0,0,Width,Height);
int ChartInset = 50;
int ChartWidth = Width-(2*ChartInset);
int ChartHeight = Height-(2*ChartInset);
g.DrawRectangle(new
Pen(Color.Black,1),ChartInset,ChartInset,ChartWidt h,ChartHeight);

//must draw all text items before doing the rotate below
g.DrawString(Title, 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+ChartInset;
myLabel = (Xorigin + (ScaleX*i/Xdivs)).ToString();
g.DrawString(myLabel, axesFont, blackBrush, x-4,
y+10);
g.DrawLine(blackPen, x, y+2, x, y-2);
}
//draw Y axis labels
for(i=0; i<=Ydivs; i++) {
x=ChartInset;
y=ChartHeight+ChartInset-(i*ChartHeight/Ydivs);
myLabel = (Yorigin + (ScaleY*i/Ydivs)).ToString();
g.DrawString(myLabel, axesFont, blackBrush, 5, y-6);
g.DrawLine(blackPen, x+2, y, x-2, y);
}

//transform drawing coords to lower-left (0,0)
g.RotateTransform(180);
g.TranslateTransform(0,-Height);
g.TranslateTransform(-ChartInset,ChartInset);
g.ScaleTransform(-1, 1);

//draw chart data
datapoint prevPoint = new datapoint();
prevPoint.valid=false;
foreach(datapoint myPoint in chartValues) {
if(prevPoint.valid==true) {
x0=ChartWidth*(prevPoint.x-Xorigin)/ScaleX;
y0=ChartHeight*(prevPoint.y-Yorigin)/ScaleY;
x=ChartWidth*(myPoint.x-Xorigin)/ScaleX;
y=ChartHeight*(myPoint.y-Yorigin)/ScaleY;
g.DrawLine(blackPen,x0,y0,x,y);
g.FillEllipse(blackBrush,x0-2,y0-2,4,4);
g.FillEllipse(blackBrush,x-2,y-2,4,4);
}
prevPoint = myPoint;
}

//finally send graphics to browser
b.Save(p.Response.OutputStream, ImageFormat.Jpeg);
}

~LineChart() {
g.Dispose();
b.Dispose();
}
}
void Page_Load(Object 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,50);
c.AddValue(100,100);
c.AddValue(200,150);
c.AddValue(450,450);
c.Draw();
}

</script>

Zach.

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl...
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:42**********************@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 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.



Nov 17 '05 #4
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)ROGERS(PERIOD)COM> wrote in message
news:uI**************@TK2MSFTNGP14.phx.gbl...
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:42**********************@news.freeler.nl...
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="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

<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.FromImage(b);
p = myPage;
}

public void AddValue(int x, int y) {
datapoint myPoint;
myPoint.x=x;
myPoint.y=y;
myPoint.valid=true;
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(Color.Black);
Font axesFont = new Font("arial",10);

//first establish working area
p.Response.ContentType="image/jpeg";
g.FillRectangle(new
SolidBrush(Color.LightYellow),0,0,Width,Height);
int ChartInset = 50;
int ChartWidth = Width-(2*ChartInset);
int ChartHeight = Height-(2*ChartInset);
g.DrawRectangle(new
Pen(Color.Black,1),ChartInset,ChartInset,ChartWidt h,ChartHeight);

//must draw all text items before doing the rotate below
g.DrawString(Title, 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+ChartInset;
myLabel = (Xorigin + (ScaleX*i/Xdivs)).ToString();
g.DrawString(myLabel, axesFont, blackBrush, x-4,
y+10);
g.DrawLine(blackPen, x, y+2, x, y-2);
}
//draw Y axis labels
for(i=0; i<=Ydivs; i++) {
x=ChartInset;
y=ChartHeight+ChartInset-(i*ChartHeight/Ydivs);
myLabel = (Yorigin + (ScaleY*i/Ydivs)).ToString();
g.DrawString(myLabel, axesFont, blackBrush, 5, y-6);
g.DrawLine(blackPen, x+2, y, x-2, y);
}

//transform drawing coords to lower-left (0,0)
g.RotateTransform(180);
g.TranslateTransform(0,-Height);
g.TranslateTransform(-ChartInset,ChartInset);
g.ScaleTransform(-1, 1);

//draw chart data
datapoint prevPoint = new datapoint();
prevPoint.valid=false;
foreach(datapoint myPoint in chartValues) {
if(prevPoint.valid==true) {
x0=ChartWidth*(prevPoint.x-Xorigin)/ScaleX;
y0=ChartHeight*(prevPoint.y-Yorigin)/ScaleY;
x=ChartWidth*(myPoint.x-Xorigin)/ScaleX;
y=ChartHeight*(myPoint.y-Yorigin)/ScaleY;
g.DrawLine(blackPen,x0,y0,x,y);
g.FillEllipse(blackBrush,x0-2,y0-2,4,4);
g.FillEllipse(blackBrush,x-2,y-2,4,4);
}
prevPoint = myPoint;
}

//finally send graphics to browser
b.Save(p.Response.OutputStream, ImageFormat.Jpeg);
}

~LineChart() {
g.Dispose();
b.Dispose();
}
}
void Page_Load(Object 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,50);
c.AddValue(100,100);
c.AddValue(200,150);
c.AddValue(450,450);
c.Draw();
}

</script>

Zach.

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl...
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:42**********************@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 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.
>
>



Nov 17 '05 #5
Try XYGraph, which is very simple to use:
http://www.componentxtra.com/html/Downloads.htm.

----
Bob
http://www.componentXtra.com
Components for .NET

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Frank | last post by:
I've written a jsp that retrieves data from a sybase table via the sql JSTL. Two colums of data are returned, date and value. Currently the data is placed in an HTML table. Does anyone know of...
1
by: Jim | last post by:
I have this chart on a form. I'm trying to get this chart to render only if the user chooses to do so. This because of that the chart is quite complex and takes some time to render. I know that I...
3
by: NickJ | last post by:
Dear all, I've encountered an unusual problem having just recently upgraded an Access 97 database to Access 2000 format and was hoping somebody could point me in the direction of a solution. ...
3
by: Serdar C | last post by:
hello there.. there used to be a usefull and simple control on vb6.0 which was a line chart.. it was quite easy to use.. but in c# theres a pani named "crystal reports" and its too much complicated...
7
by: Jacky Luk | last post by:
Does anyone know of a downloadable Line ActiveX control which allows me to plot straight lines on a VC++.NET form? Thanks Jack
5
by: Bruce Schechter | last post by:
I need to generate a series of line charts dynamically from ADO.NET data in a C#, ASP.NET application. I've read several articles about using GDI+ to render graphs into a bitmap image and then to...
1
by: diesel | last post by:
I have an OWC chart that works great Our client has requested that we add a vertical line at a specific point on the chart For example the horizontal axis contains 2,4,6,8 The vertical line...
1
by: Scott H. | last post by:
Hello: I am trying to use Crystal Reports from VS.NET 2003 to produce a simple line chart, where the x axis represents the number of measurements taken and the y axis represents the range of...
4
by: =?Utf-8?B?bWFydGluMQ==?= | last post by:
Hi, All, I want to draw line chart on the web using visual basic 2005, vb 2005 doesn't have chart components, so can anyone point out where I can start working this? Thanks, Ma
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.