📄 compare-chart.aspx
字号:
oPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
oGraphics.DrawLine(oPen, 120, 261, 399, 261); // X Axis
oPen.EndCap = System.Drawing.Drawing2D.LineCap.NoAnchor;
oGraphics.DrawLine(oPen, 120, 17, 120, 261); // Y Axis
// draw the "key" boxes for each of the 6 possible
// values (gears) using column names from DataTable
for (int iLoop = 0; iLoop <= 5; iLoop++) {
// get column name to display
sText = oXData.Columns[iLoop + 1].ColumnName;
// calculate position
iKeyXTop = 295 + ((iLoop / 3) * 17);
int iMod3 = iLoop;
while (iMod3 >= 3) iMod3 -= 3;
iKeyXLeft = (int)(120 + (iMod3 * 110));
// set brush color and draw shadow for box
oBrush.Color = Color.FromArgb(102, 102, 102);
oGraphics.FillRectangle(oBrush, iKeyXLeft + 1, iKeyXTop + 1, 15, 13);
// set brush color and draw box and text
oBrush.Color = aColors[iLoop + 1];
oGraphics.FillRectangle(oBrush, iKeyXLeft, iKeyXTop, 15, 13);
DrawText(iKeyXLeft + 18, iKeyXTop, 115, 14, sText,
null, 0, "", StringAlignment.Near, 0);
}
// next, find maximum value from all rows
int iValue, iRowsMax, iMaxValue;
iRowsMax = 0;
foreach (DataRow oRow in oXData.Rows) {
for (int iLoop = 1; iLoop < oXData.Columns.Count; iLoop++) {
try {
iValue = (short)oRow[iLoop];
if (iValue > iRowsMax) {
iRowsMax = iValue;
}
}
catch (Exception e) {
}
}
}
// round up to next iYInterval
iMaxValue = ((iRowsMax + iYInterval) / iYInterval) * iYInterval;
// calculate number of values and pixel spacing
int iXValCount = (iMaxValue / iYInterval) + 1;
int iXSpacePX = 270 / (iXValCount - 1);
// see if the captions will overlap
while (iXSpacePX < 25) {
// double the interval size
iYInterval = iYInterval * 2;
// recalculate values based on this interval size
iMaxValue = ((iRowsMax + iYInterval) / iYInterval) * iYInterval;
iXValCount = (iMaxValue / iYInterval) + 1;
iXSpacePX = 270 / (iXValCount - 1);
}
// for each value on X axis
for (int iLoop = 0; iLoop < iXValCount; iLoop++) {
// get value to display along axis
iValue = iYInterval * iLoop;
// calculate horizontal position and write text
iTextPos = iLoop * iXSpacePX;
DrawText(96 + iTextPos, 263, 50, 12, iValue.ToString(),
null, 0, "", StringAlignment.Center, 0);
// draw light gray vertical line if not on Y axis
if (iLoop > 0) {
oPen.Color = Color.FromArgb(204, 204, 204); // light gray
oGraphics.DrawLine(oPen, 120 + iTextPos, 17, 120 + iTextPos, 260);
}
}
// write X axis caption aligned right
DrawText(40, 274, 355, 15, sXAxisText, null, 0, "", StringAlignment.Far, 0);
// calculate multiplier for X values per pixel
Single fXMultiplier = 270 / iMaxValue;
// create index for row to set vertical position
int iRowCount = 0;
// calculate vertical space for each row on Y axis
int iYSpacePX = 240 / oXData.Rows.Count;
// make each bar 3/4 of available space to provide
// gap between each one so scale rules are visible
int iYBarHeight = (int)(iYSpacePX * 0.75);
int iYBarAbove = (int)(iYSpacePX * 0.125);
// iterate through data rows
foreach (DataRow oRow in oXData.Rows) {
// calculate vertical position for row values
iRectYStart = 18 + (iRowCount * iYSpacePX);
// draw the vehicle name (caption) left of Y axis
DrawText(0, iRectYStart, 118, iYBarHeight + iYBarAbove, oRow[0].ToString(),
null, 0, "", StringAlignment.Far, 0);
// flag so that shadow is only drawn once
Boolean bDrawShadow = true;
// iterate through the columns of this row
for (int iLoop = (oXData.Columns.Count - 1); iLoop > 1; iLoop--) {
try {
iValue = (short)oRow[iLoop];
iRectWidth = (int)(iValue * fXMultiplier);
if (bDrawShadow) {
// if this is the first value box for this row
// then set brush color and draw shadow for bar
oBrush.Color = Color.FromArgb(102, 102, 102);
oGraphics.FillRectangle(oBrush, 121, iRectYStart + iYBarAbove, iRectWidth + 1, iYBarHeight + 1);
bDrawShadow = false; // clear flag
}
// set brush color and draw value bar
oBrush.Color = aColors[iLoop];
oGraphics.FillRectangle(oBrush, 121, iRectYStart + iYBarAbove, iRectWidth, iYBarHeight);
}
catch (Exception e) {
}
}
iRowCount += 1;
}
}
// -------------------------------------------------------------------
void FillColorArray() {
// fill the array of colors for chart items
// use browser-safe colors (multiples of #33)
aColors[1] = Color.FromArgb(255, 0, 0); // light red
aColors[2] = Color.FromArgb(0, 102, 204); // light blue
aColors[3] = Color.FromArgb(0, 204, 0); // light green
aColors[4] = Color.FromArgb(204, 204, 0); // gold
aColors[5] = Color.FromArgb(255, 153, 0); // orange
aColors[6] = Color.FromArgb(102, 0, 102); // purple
aColors[7] = Color.FromArgb(255, 0, 255); // magenta
aColors[8] = Color.FromArgb(153, 0, 0); // dark red
aColors[9] = Color.FromArgb(102, 153, 153); // teal
aColors[10] = Color.FromArgb(0, 0, 102); // dark blue
aColors[11] = Color.FromArgb(0, 0, 255); // cyan
aColors[12] = Color.FromArgb(102, 102, 102); // dark gray
}
// -------------------------------------------------------------------
// routine to draw error message on bitmap
void DrawErrorMessage(String sError) {
DrawText(35, 0, 365, 50, sError, FontStyle.Bold, 10, "Red", null, 0);
}
// -------------------------------------------------------------------
// routine to draw text string in rectangle on bitmap
void DrawText(int iTop, int iLeft, int iWidth,
int iHeight, String sText, object eFontStyle,
int iFontSize, String sColor, object eAlign,
StringFormatFlags eFlag) {
// set default values if not specified
if (eFontStyle == null) eFontStyle = FontStyle.Regular;
if (iFontSize == 0) iFontSize = 8;
if (sColor == "") sColor = "Black";
if (eAlign == null) eAlign = StringAlignment.Near;
// create the rectange to hold the text
RectangleF oRect = new RectangleF(iTop, iLeft, iWidth, iHeight);
// create a Font object for the text style
Font oFont = new Font("Arial", iFontSize, (FontStyle)eFontStyle);
// create the format object to define the format and style
StringFormat oFormat = new StringFormat(eFlag);
oFormat.Alignment = (StringAlignment)eAlign; // horizontal alignment
// always center vertically within rectangle area
oFormat.LineAlignment = StringAlignment.Center;
// create a brush object and draw the text
SolidBrush oBrush = new SolidBrush(Color.FromName(sColor));
oGraphics.DrawString(sText, oFont, oBrush, oRect, oFormat);
}
// -------------------------------------------------------------------
// function to create the DataSet of values
// does the same as in the compare.aspx page
DataSet GetCompareDS() {
String sConnect = ConfigurationSettings.AppSettings["XroxCarsConnectString"];
String sSQL = GetSqlStatement();
DataSet dsResult = new DataSet();
try {
SqlConnection oConn = new SqlConnection(sConnect);
SqlDataAdapter oDA = new SqlDataAdapter(sSQL, oConn);
oDA.Fill(dsResult, "Compare");
}
catch (Exception e) {
DrawErrorMessage("Database cannot be accessed");
}
return dsResult;
}
// -------------------------------------------------------------------
// function to create appropriate SQL statement
String GetSqlStatement() {
String sResult = "";
// see which comparison type is selected
// and create SELECT part of SQL statement
switch(sCompareIndex) {
case "0": // compare Performance
sCaption = "Time in seconds from standing start to reach speed (mph) of:";
sResult = "SELECT Caption = Model + ' ' + EngineName, "
+ "[30] = Perf30, [40] = Perf40, [50] = Perf50, "
+ "[60] = Perf60, [70] = Perf70, [80] = Perf80, "
+ "[90] = Perf90, [100] = Perf100 "
+ "FROM (tblCar JOIN tblCarEngines "
+ "ON tblCar.CarID = tblCarEngines.CarID) "
+ "JOIN tblEngine "
+ "ON tblEngine.EngineID = tblCarEngines.EngineID WHERE ";
break;
case "1": // compare Maximum Speed
sCaption = "Maximum speed (mph) in each gear:";
sResult = "SELECT Caption = Model + ' ' + EngineName, "
+ "[1st gear] = Speed1st, [2nd gear] = Speed2nd, "
+ "[3rd gear] = Speed3rd, [4th gear] = Speed4th, "
+ "[5th gear] = Speed5th, [6th gear] = Speed6th "
+ "FROM (tblCar JOIN tblCarEngines "
+ "ON tblCar.CarID = tblCarEngines.CarID) "
+ "JOIN tblEngine "
+ "ON tblEngine.EngineID = tblCarEngines.EngineID WHERE ";
break;
case "2": // compare Fuel Consumption
sCaption = "Fuel consumption (mpg) at a steady speed (mph) of:";
sResult = "SELECT Caption = Model + ' ' + EngineName, "
+ "[30] = Fuel30, [40] = Fuel40, [50] = Fuel50, "
+ "[60] = Fuel60, [70] = Fuel70, [80] = Fuel80, "
+ "[90] = Fuel90, [100] = Fuel100 "
+ "FROM (tblCar JOIN tblCarEngines "
+ "ON tblCar.CarID = tblCarEngines.CarID) "
+ "JOIN tblEngine "
+ "ON tblEngine.EngineID = tblCarEngines.EngineID WHERE ";
break;
}
// create WHERE clause section for each value in query string
// split list into an array, the extract each value
String[] aList = sModelList.Split(new Char[]{'*'});
String sModel, sEngine;
foreach (String sItem in aList) {
sModel = sItem.Substring(0, sItem.IndexOf(" "));
sEngine = sItem.Substring(sItem.IndexOf(" ") + 1);
sResult += "(Model = '" + sModel + "' AND EngineName = '"
+ sEngine + "') OR ";
}
// remove final "OR" from string
sResult = sResult.Substring(0, sResult.Length - 4);
// add ORDER BY clause and return result
return sResult + " ORDER BY Model, EngineName";
}
// -------------------------------------------------------------------
</script>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -