Click here to Skip to main content
15,889,200 members
Home / Discussions / C#
   

C#

 
GeneralRe: Running C# App over Network Pin
mbangh8-Sep-09 10:24
mbangh8-Sep-09 10:24 
GeneralRe: Running C# App over Network Pin
DaveyM698-Sep-09 10:26
professionalDaveyM698-Sep-09 10:26 
AnswerRe: Running C# App over Network Pin
darkelv8-Sep-09 15:31
darkelv8-Sep-09 15:31 
AnswerRe: Running C# App over Network Pin
Harvey Saayman8-Sep-09 20:52
Harvey Saayman8-Sep-09 20:52 
Questionc# help Pin
liveinweb2.08-Sep-09 8:14
liveinweb2.08-Sep-09 8:14 
AnswerRe: c# help Pin
dan!sh 8-Sep-09 8:29
professional dan!sh 8-Sep-09 8:29 
AnswerRe: c# help Pin
Christian Graus8-Sep-09 11:37
protectorChristian Graus8-Sep-09 11:37 
AnswerRe: c# help Pin
April Fans22-Sep-09 19:11
April Fans22-Sep-09 19:11 
What diagram do you want to generate?Here is an example, hope will help you.

using System;
using System.IO;

using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Insight_cs.WebCharts
{
public class PieChart
{
public PieChart()
{
}
public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
{
const int SIDE_LENGTH = 400;
const int PIE_DIAMETER = 200;
DataTable dt = chartData.Tables[0];


float sumData = 0;
foreach(DataRow dr in dt.Rows)
{
sumData += Convert.ToSingle(dr[1]);
}

Bitmap bm = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bm);

g.ScaleTransform((Convert.ToSingle(width))/SIDE_LENGTH,(Convert.ToSingle(height))/SIDE_LENGTH);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;


g.Clear(Color.White);
g.DrawRectangle(Pens.Black,0,0,SIDE_LENGTH-1,SIDE_LENGTH-1);

g.DrawString(title,new Font("Tahoma",24),Brushes.Black,new PointF(5,5));

g.DrawString(subTitle,new Font("Tahoma",14),Brushes.Black,new PointF(7,35));

float curAngle = 0;
float totalAngle = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
curAngle = Convert.ToSingle(dt.Rows[i][1]) / sumData * 360;

g.FillPie(new SolidBrush(ChartUtil.GetChartItemColor(i)),100,65,PIE_DIAMETER,PIE_DIAMETER,totalAngle,curAngle);
g.DrawPie(Pens.Black,100,65,PIE_DIAMETER,PIE_DIAMETER,totalAngle,curAngle);
totalAngle += curAngle;
}

g.DrawRectangle(Pens.Black,200,300,199,99);
g.DrawString("Legend",new Font("Tahoma",12,FontStyle.Bold),Brushes.Black,new PointF(200,300));


PointF boxOrigin = new PointF(210,330);
PointF textOrigin = new PointF(235,326);
float percent = 0;
for(int i=0;i<dt.Rows.Count;i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
percent = Convert.ToSingle(dt.Rows[i][1]) / sumData * 100;
g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString() + " (" + percent.ToString("0") + "%)",new Font("Tahoma",10),Brushes.Black,textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}

bm.Save(target, ImageFormat.Gif);

bm.Dispose();
g.Dispose();
}
}


public class BarChart
{
public BarChart()
{
}
public void Render(string title, string subTitle, int width, int height, DataSet chartData, Stream target)
{
const int SIDE_LENGTH = 400;
const int CHART_TOP = 75;
const int CHART_HEIGHT = 200;
const int CHART_LEFT = 50;
const int CHART_WIDTH = 300;
DataTable dt = chartData.Tables[0];


float highPoint = 0;
foreach(DataRow dr in dt.Rows)
{
if(highPoint<Convert.ToSingle(dr[1]))
{
highPoint = Convert.ToSingle(dr[1]);
}
}

Bitmap bm = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bm);

g.ScaleTransform((Convert.ToSingle(width))/SIDE_LENGTH,(Convert.ToSingle(height))/SIDE_LENGTH);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;


g.Clear(Color.White);
g.DrawRectangle(Pens.Black,0,0,SIDE_LENGTH-1,SIDE_LENGTH-1);

g.DrawString(title,new Font("Tahoma",24),Brushes.Black,new PointF(5,5));

g.DrawString(subTitle,new Font("Tahoma",14),Brushes.Black,new PointF(7,35));

float barWidth = CHART_WIDTH / (dt.Rows.Count * 2);
PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2),0);
float barHeight = dt.Rows.Count;
for(int i=0;i<dt.Rows.Count;i++)
{
barHeight = Convert.ToSingle(dt.Rows[i][1]) * 200 / highPoint;
barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin.Y,barWidth,barHeight);
barOrigin.X = barOrigin.X + (barWidth * 2);
}

g.DrawLine(new Pen(Color.Black,2),new Point(CHART_LEFT,CHART_TOP),new Point(CHART_LEFT,CHART_TOP + CHART_HEIGHT));
g.DrawLine(new Pen(Color.Black,2),new Point(CHART_LEFT,CHART_TOP + CHART_HEIGHT),new Point(CHART_LEFT + CHART_WIDTH,CHART_TOP + CHART_HEIGHT));

g.DrawRectangle(new Pen(Color.Black,1),200,300,199,99);
g.DrawString("Legend",new Font("Tahoma",12,FontStyle.Bold),Brushes.Black,new PointF(200,300));


PointF boxOrigin = new PointF(210,330);
PointF textOrigin = new PointF(235,326);
for(int i=0;i<dt.Rows.Count;i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString(),new Font("Tahoma",10),Brushes.Black,textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}

bm.Save(target, ImageFormat.Gif);


bm.Dispose();
g.Dispose();
}
}
public class ChartUtil
{
public ChartUtil()
{
}
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch(itemIndex)
{
case 0:
selectedColor = Color.Blue;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.Purple;
break;
default:
selectedColor = Color.Green;
break;
}
return selectedColor;
}
}
}
April

Comm100 - Leading Live Chat Software Provider


modified 27-May-14 21:58pm.

Questionthanks Pin
tanweer8-Sep-09 6:47
tanweer8-Sep-09 6:47 
Questionrecord and play mp4 file Pin
Markandaiya Harsh8-Sep-09 6:27
Markandaiya Harsh8-Sep-09 6:27 
AnswerRe: record and play mp4 file Pin
Henry Minute8-Sep-09 10:57
Henry Minute8-Sep-09 10:57 
QuestionOverwrite an image Pin
vandel2128-Sep-09 5:55
vandel2128-Sep-09 5:55 
AnswerRe: Overwrite an image [modified] Pin
Luc Pattyn8-Sep-09 6:08
sitebuilderLuc Pattyn8-Sep-09 6:08 
GeneralRe: Overwrite an image Pin
harold aptroot8-Sep-09 6:10
harold aptroot8-Sep-09 6:10 
AnswerRe: Overwrite an image Pin
Luc Pattyn8-Sep-09 7:58
sitebuilderLuc Pattyn8-Sep-09 7:58 
QuestionTaking a snapshot from a local webcam... Pin
to_aru_shiroi_neko8-Sep-09 5:37
to_aru_shiroi_neko8-Sep-09 5:37 
QuestionWorking with very large numbers. Pin
JTKRIST8-Sep-09 5:13
JTKRIST8-Sep-09 5:13 
AnswerRe: Working with very large numbers. Pin
Luc Pattyn8-Sep-09 5:22
sitebuilderLuc Pattyn8-Sep-09 5:22 
AnswerRe: Working with very large numbers. Pin
Richard MacCutchan8-Sep-09 5:26
mveRichard MacCutchan8-Sep-09 5:26 
AnswerRe: Working with very large numbers. Pin
harold aptroot8-Sep-09 5:39
harold aptroot8-Sep-09 5:39 
AnswerRe: Working with very large numbers. Pin
DaveyM698-Sep-09 5:40
professionalDaveyM698-Sep-09 5:40 
QuestionHow to notify all instance of a class that a change is happened Pin
Nitheesh George8-Sep-09 4:48
Nitheesh George8-Sep-09 4:48 
AnswerRe: How to notify all instance of a class that a change is happened Pin
monstale8-Sep-09 5:16
monstale8-Sep-09 5:16 
Questioncompile Pin
sanforjackass8-Sep-09 4:27
sanforjackass8-Sep-09 4:27 
AnswerRe: compile Pin
stancrm8-Sep-09 4:30
stancrm8-Sep-09 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.