Creating Bar Graphs

  • Thread starter Thread starter robert.q.johnson
  • Start date Start date
R

robert.q.johnson

Help. I am trying to create a web page in C# to display a row of data
with bar graphs (an image 1px). I get the max value of the row and use
the following formula to size the height of the image, but the bar
graphs are not sizing correctly. Can anyone provide me with a solution
or any ideas.

rowValue / maxRowValue * maxBarHeight
 
Are rowValue and maxRowValue both integer values? You need to make sure at
least on of those values is a floating point number otherwise you are just
going to end up with 0's.

When you say not being sized correctly, what exactly do you mean?

Mark.
 
Help. I am trying to create a web page in C# to display a row of data
with bar graphs (an image 1px). I get the max value of the row and use
the following formula to size the height of the image, but the bar
graphs are not sizing correctly. Can anyone provide me with a solution
or any ideas.

rowValue / maxRowValue * maxBarHeight

Try this:

(rowValue / maxRowValue) * maxBarHeight
 
In this case that would not make a difference since \ has a higher order of
precedence than * and will be evaluated first.

Mark
 
Here is my current code. The bars are showing a blank image if the
value is the same as the maxvalue which makes since since 60/60 = 0.
The maxHieght is a constant of 120. The other graphs don't size
correctly to the data value 4 value / 60 maxValue * 120 = 8 but the
image is close to the top of the cell. Image width is a constant of
30.

private void PaintPlacements(DataTable dtPlacements)
{
DataRow[] rows = dtPlacements.Select();

double maxPlacements = Double.Parse(dtPlacements.Compute

("Max(PLACEMENTS_NBR)", "").ToString());
double maxPerformer = Double.Parse(dtPlacements.Compute

("Max(BEST_PERFORMER_NBR)", "").ToString());
double[] maxValue = {maxPlacements, maxPerformer};
tblPlacements.Rows.Add(AddPlacementGraphs(rows, maxValue,
ScorecardData.ImageWidth));
}

private TableRow AddPlacementGraphs(DataRow[] rows, double[] maxValue,
int barWidth)
{
double graphData;
double graphHeight;
TableRow tr = new TableRow();

foreach(DataRow row in rows)
{
TableCell tc = new TableCell();
graphData = Double.Parse(row["PLACEMENTS_NBR"].ToString());
graphHeight = (graphData / maxValue[0] *
ScorecardData.MaximumHeight);
tc.Text = "<img border=0 src=../images/spacer1.gif" +
" height=" + Math.Round(graphHeight, 0) +
" width=" + barWidth + ">";
tc.BackColor = Color.FromName("Blue");
tc.VerticalAlign = VerticalAlign.Top;
tr.Cells.Add(tc);
tc = null;

tc = new TableCell();
graphData = Double.Parse(row["BEST_PERFORMER_NBR"].ToString());
graphHeight = (graphData / maxValue[1] *
ScorecardData.MaximumHeight);
tc.Text = "<img border=0 src=../images/spacer1.gif" +
" height=" + Math.Round(graphHeight, 0) +
" width=" + barWidth + ">";
tc.BackColor = Color.FromName("CornflowerBlue");
tc.VerticalAlign = VerticalAlign.Top;
tr.Cells.Add(tc);
tc = null;
}
return tr;
}
 
Hi Robert,
The bars are showing a blank image if the
value is the same as the maxvalue which makes since since 60/60 = 0

I would hope 60\60 == 1 :-)

I am no web expert but I would think you want to align your images to the
bottom of the cell, you have:
tc.VerticalAlign = VerticalAlign.Top;

which would align the images to the top of the cell which is okay if you
want an inverted graph, but you probably want to align them to the bottom?
--
http://www.markdawson.org


Here is my current code. The bars are showing a blank image if the
value is the same as the maxvalue which makes since since 60/60 = 0.
The maxHieght is a constant of 120. The other graphs don't size
correctly to the data value 4 value / 60 maxValue * 120 = 8 but the
image is close to the top of the cell. Image width is a constant of
30.

private void PaintPlacements(DataTable dtPlacements)
{
DataRow[] rows = dtPlacements.Select();

double maxPlacements = Double.Parse(dtPlacements.Compute

("Max(PLACEMENTS_NBR)", "").ToString());
double maxPerformer = Double.Parse(dtPlacements.Compute

("Max(BEST_PERFORMER_NBR)", "").ToString());
double[] maxValue = {maxPlacements, maxPerformer};
tblPlacements.Rows.Add(AddPlacementGraphs(rows, maxValue,
ScorecardData.ImageWidth));
}

private TableRow AddPlacementGraphs(DataRow[] rows, double[] maxValue,
int barWidth)
{
double graphData;
double graphHeight;
TableRow tr = new TableRow();

foreach(DataRow row in rows)
{
TableCell tc = new TableCell();
graphData = Double.Parse(row["PLACEMENTS_NBR"].ToString());
graphHeight = (graphData / maxValue[0] *
ScorecardData.MaximumHeight);
tc.Text = "<img border=0 src=../images/spacer1.gif" +
" height=" + Math.Round(graphHeight, 0) +
" width=" + barWidth + ">";
tc.BackColor = Color.FromName("Blue");
tc.VerticalAlign = VerticalAlign.Top;
tr.Cells.Add(tc);
tc = null;

tc = new TableCell();
graphData = Double.Parse(row["BEST_PERFORMER_NBR"].ToString());
graphHeight = (graphData / maxValue[1] *
ScorecardData.MaximumHeight);
tc.Text = "<img border=0 src=../images/spacer1.gif" +
" height=" + Math.Round(graphHeight, 0) +
" width=" + barWidth + ">";
tc.BackColor = Color.FromName("CornflowerBlue");
tc.VerticalAlign = VerticalAlign.Top;
tr.Cells.Add(tc);
tc = null;
}
return tr;
}



Are rowValue and maxRowValue both integer values? You need to make sure at
least on of those values is a floating point number otherwise you are just
going to end up with 0's.

When you say not being sized correctly, what exactly do you mean?

Mark.
 
Back
Top