using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace ChartBuild
{
public class ChartBuild
{
public Bitmap GetChat()
{
/**/
/*自寫編碼*/
int ImgWidth = 600; //1.[總寬度] ***圖寬度
int ImgHeight = 300; //2.[總高度] ***圖高度
int ItemNum = 1; //3.[項目數量] ***圖表劃分的塊
int ChildNum = 7; //4.[塊數] ***大塊中劃分的子項的數量
float ChildRate = 0.3f; //5.[各塊總佔空間比率]
//int ChildSpace=15; //6.[各塊間的間距]
int ChartLeft = 80; //7.[圖表左邊距] ***圖表距圖的左邊距離
int ChartRight = 50; //8.[圖表右邊距] ***圖表距圖的右邊距離
int ChartTop = 50; //9.[圖表頂邊距] ***圖表距圖頂邊距離
int ChartBottom = 50; //10.[圖表底邊距] ***圖表距圖底邊距離
//int YMaxValue = 5000; //11.[縱座標標尺最大值] ***縱座標標尺的最大值
int YItemNum = 6; //12.[縱座標標尺段數] ***縱座標標尺的段數
int YTop = 15; //13.[距縱軸頂端間隔]
int YStrStart = 35; //14.[縱座標標尺文字起始X座標]
int XRight = 15; //15.[距橫軸右端間隔]
int XStrStart = 20; //16.[橫座標標尺文字起始Y座標]
//[圖表總寬度]=[總寬度]-[圖表左邊距]-[圖表右邊距]-[距橫軸右端間隔]
int chartwidth = ImgWidth - ChartLeft - ChartRight - XRight;
//[項目寬度]=[圖表總寬度]/[項目數量]
int itemwidth = chartwidth / ItemNum;
//[各塊總佔空間比率的實際寬度]=[項目寬度]*[各塊總佔空間比率]
int factwidth = Convert.ToInt32(Math.Floor(itemwidth * ChildRate));
//[各塊矩形寬度]=[各塊總佔空間比率的實際寬度]/[塊數]
int rectanglewidth = factwidth / ChildNum;
//[各塊間的間距]=([項目寬度]-[各塊總佔空間比率的實際寬度])/([塊數]+1)
int childspace = Convert.ToInt32(Math.Floor((Double)((itemwidth - factwidth) / (ChildNum + 1))));
Graphics objGps;//建立畫板對象
Bitmap objBitMap = new Bitmap(ImgWidth, ImgHeight);//建立位圖對象
objGps = Graphics.FromImage(objBitMap);//根據位圖對象建立畫板對象
objGps.Clear(Color.Silver);//設置畫板對象的背景色
//在繪畫圖面的指定位置,以指定的字體、指定的顏色繪製指定的字符串。即爲圖表標題
objGps.DrawString(" 問題統計圖", new Font("黑體", 10), Brushes.Black, new Point(1, 1));
//***以下代碼是是實現圖右上部
Point myRec = new Point(500, 30);
Point myDec = new Point(515, 30);
//以上是在圖01中爲下面繪製定位
//objGps.DrawString("單位:千棟", new Font("宋體", 9), Brushes.Black, new Point(500, 12));
string[] sBiaoshi ={ "未解決問題總數", "新增問題總數" };
for (int i = 0; i < 2; i++)
{
objGps.DrawRectangle(Pens.Black, myRec.X, myRec.Y, 10, 10);
//繪製小方塊
objGps.FillRectangle(new SolidBrush(GetColor(i)), myRec.X, myRec.Y, 10, 10);
//填充小方塊
objGps.DrawString(sBiaoshi[i].ToString(), new Font("宋體", 9), Brushes.Black, myDec);
//繪製小方塊右邊的文字
myRec.Y += 15;
myDec.Y += 15;
}
//數據數組
int[] arrValues ={ 0, 0, 0, 0, 0, 0 };
//處理顯示數據,進行圖表數值對應
arrValues[0] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2)));
arrValues[1] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 1.5)));
arrValues[2] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2.5)));
arrValues[3] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2)));
arrValues[4] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 3.5)));
arrValues[5] = Convert.ToInt32(Math.Floor((Double)((ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * 2)));
int[] arrValuesNow ={ 20, 28, 30, 20, 40, 10 };
//縱座標標碼
string[] arrValueNames = { "2", "4", "6", "8", "10", "12" };
string[] arrItemNames ={ "注約", "移行A", "移行B", "GLADOFFICE", "AAA", "DDDD" };
//得出矩形寬度,和畫圖X軸位置
//[項目寬度]=[總寬度]/[項目數量]
//======[各塊總佔空間比率]=([各塊矩形寬度]+[各塊間的間距])/[項目寬度]
//[各塊總佔空間比率的實際寬度]=[項目寬度]*[各塊總佔空間比率]
//[各塊矩形寬度]=([各塊總佔空間比率的實際寬度]-[各塊間的間距]*([塊數]))/[塊數]
//[一邊空餘空間寬度]=([項目寬度]-[各塊所佔空間比率的總寬度])/2
System.Drawing.Point[] pi = new Point[arrValues.Length]; //定義折線點的對象數組
System.Drawing.Point[] pit = new Point[3]; //定義座標三角點的對象數組
System.Drawing.Pen pe = new Pen(new SolidBrush(GetColor(7)), 1f); //定義畫直線的對象
//畫縱軸
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ChartLeft, ChartTop + YTop));
objGps.DrawLine(pe, new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom), new Point(ImgWidth - ChartRight, ChartTop + YTop));
////畫縱軸終點箭頭
//pit[0].X = ImgWidth - ChartRight; //確定三角形三點的位置
//pit[0].Y = ImgHeight - ChartBottom - 4;
//pit[1].X = ImgWidth - ChartRight;
//pit[1].Y = ImgHeight - ChartBottom + 4;
//pit[2].X = ImgWidth - ChartRight + 10;
//pit[2].Y = ImgHeight - ChartBottom;
//objGps.FillPolygon(new SolidBrush(GetColor(7)), pit);
//畫縱軸標尺和標尺描述
for (int i = 1; i <= YItemNum; i++)
{
//畫標尺
//objGps.DrawLine(pe, new PointF(ChartLeft, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i), new PointF(ChartLeft - 5, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i));
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i), new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i));
//畫描述
objGps.DrawString(arrValueNames[i - 1].ToString(), new Font("宋體", 10), Brushes.Black, new Point(YStrStart, ImgHeight - ChartBottom - (ImgHeight - ChartBottom - ChartTop - YTop) / YItemNum * i - 5));
}
//畫橫軸
objGps.DrawLine(pe, new Point(ChartLeft, ImgHeight - ChartBottom), new Point(ImgWidth - ChartRight, ImgHeight - ChartBottom));
////畫橫軸終點箭頭
//pit[0].X = ChartLeft - 4; //確定三角形三點的位置
//pit[0].Y = ChartTop;
//pit[1].X = ChartLeft + 4;
//pit[1].Y = ChartTop;
//pit[2].X = ChartLeft;
//pit[2].Y = ChartTop - 10;
//objGps.FillPolygon(new SolidBrush(GetColor(7)), pit);
//畫橫軸標尺和標尺描述
for (int i = 1; i <= arrValues.Length; i++)
{
//objGps.DrawLine(pe, new PointF(ChartLeft + itemwidth * i, ImgHeight - ChartBottom), new PointF(ChartLeft + itemwidth * i, ImgHeight - ChartBottom + 5));
objGps.DrawString((arrItemNames[i - 1].ToString()), (new Font("宋體", 9)), (Brushes.Black), new Point(ChartLeft + childspace + itemwidth * (i - 1) / 7, ImgHeight - ChartBottom + XStrStart));
}
for (int j = 0; j < arrValues.Length; j++)//畫矩形圖
{
objGps.FillRectangle(new SolidBrush(GetColor(4)), (j * (childspace + rectanglewidth)) + childspace + ChartLeft, ImgHeight - ChartBottom - arrValues[j], rectanglewidth, arrValues[j]);
objGps.FillRectangle(new SolidBrush(GetColor(3)), (j * (childspace + rectanglewidth)) + childspace + ChartLeft + rectanglewidth, ImgHeight - ChartBottom - arrValuesNow[j], rectanglewidth, arrValuesNow[j]);
objGps.DrawRectangle(Pens.Black, (j * (childspace + rectanglewidth)) + childspace + ChartLeft, ImgHeight - ChartBottom - arrValues[j], rectanglewidth, arrValues[j]);
objGps.DrawRectangle(Pens.Black, (j * (childspace + rectanglewidth)) + childspace + ChartLeft + rectanglewidth, ImgHeight - ChartBottom - arrValuesNow[j], rectanglewidth, arrValuesNow[j]);
////折線圖
//pi[j].X = (j * (childspace + rectanglewidth)) + childspace + ChartLeft;
//pi[j].Y = ImgHeight - ChartBottom - arrValues[j];
//pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor, System.Drawing.Drawing2D.LineCap.RoundAnchor, System.Drawing.Drawing2D.DashCap.Round);
//if (j > 0)
//{
// objGps.DrawLine(pe, pi[j - 1], pi[j]);
//}
}
return objBitMap;//該位圖對象以"GIF"格式輸出
}
/**/
/// <param name="itemIndex">系統定義的顏色,有效值0到7,分別爲(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
/// <returns></returns>
public static Color GetColor(int itemIndex)
{
Color objColor = new Color();
switch (itemIndex)
{
case 0:
objColor = Color.Blue;
break;
case 1:
objColor = Color.Red;
break;
case 2:
objColor = Color.Yellow;
break;
case 3:
objColor = Color.Orange;
break;
case 4:
objColor = Color.Purple;
break;
case 5:
objColor = Color.Brown;
break;
case 6:
objColor = Color.Pink;
break;
default:
objColor = Color.Black;
break;
}
return objColor;
}
/**/
/// <param name="red">自定義顏色紅色分量值,有效值0到255</param>
/// <param name="green">自定義顏色綠色分量值,有效值0到255</param>
/// <param name="blue">自定義顏色藍色分量值,有效值0到255</param>
/// <returns></returns>
public static Color GetColor(int red, int green, int blue)
{
Color objColor = new Color();
objColor = Color.FromArgb(red, green, blue);
return objColor;
}
}
}