制作報表的時候結果出現畫紅線處的信息太散,
如果沒必要全部顯示出來,我們可以使用這種效果:
注意和前面的區分,這個功能叫做Collect Pie Slices(收集分區)
要實現此功能,應先了解相關信息
1.收集的閥值(Collected Threshold)
比如這張圖我們設置當百分比小於多少5%時集中。
2.集合區要顯示的文本(Collected Label)
比如我們這裡集合區域(餅狀圖綠色部分)內顯示的Other
3.圖例文本(標簽)(CollectedLegendText)
即我們餅狀圖的圖例(右上角),顯示的文本
4.合並區域顯示的顏色(CollectedColor)
效果
5.合並後的區域是否突出(CollectedSliceExploded)
效果:
6.效果:
實現紅線處上面為數量,下面為所占百分比:
var servers = Chart1.Series[0];
servers.Label = "#VAL\r\n#PERCENT{P1}";
實例:
html:
<asp:Chart ID="Chart1" runat="server" Palette="BrightPastel" BackColor="#D3DFF0" BorderDash BackSecondaryColor="White"
BackGradient BorderWidth="2" BorderColor="26, 59, 105" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" ImageType="Png">
<Titles>
<asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3" Text="Pie Chart" Name="Title1" ForeColor="26, 59, 105"></asp:Title>
</Titles>
<Legends>
<asp:Legend Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold" Enabled="True" ></asp:Legend>
</Legends>
<BorderSkin Skin></BorderSkin>
<Series>
<asp:Series Name="Default" ChartType="Pie" XValueType="Double" BorderColor="180, 26, 59, 105" ShadowOffset="5"
Font="Trebuchet MS, 8.25pt, style=Bold" YValueType="Double" ></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BackSecondaryColor="White" BackColor="Transparent" ShadowColor="" BorderWidth="0">
<Area3DStyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False" WallWidth="0" IsClustered="False"></Area3DStyle>
<AxisY LineColor="64, 64, 64, 64">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="64, 64, 64, 64" />
</AxisY>
<AxisX LineColor="64, 64, 64, 64">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="64, 64, 64, 64" />
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
後台代碼:
protected void Page_Load(object sender, EventArgs e)
{
// Create pie chart helper class,
pieHelper = new PieCollectedDataHelper(Chart1);
pieHelper.CollectedLabel = String.Empty;
// Set chart type ,設置圖標類型,餅狀圖Pie 環裝圖:Doughnut
//Chart1.Series["Default"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), ChartTypeList.SelectedItem.Text, true);
Chart1.Series["Default"].ChartType = SeriesChartType.Pie;
// Set chart type and title
//需要配合Titles
Chart1.Titles[0].Text = "後整產量" + " Chart";
//ShowLegend顯示標簽,Disabled隱藏 Inside內部顯示,Outside外部顯示
Chart1.Series["Default"]["PieLabelStyle"] = "Outside";
// Set Doughnut hole size,甜甜圈寬度,,只有當 Chart1.Series["Default"].ChartType=="Doughnut"才生效
Chart1.Series["Default"]["DoughnutRadius"] = "60";
//Docking
//需要配合Legends
//調整Legends位置
Chart1.Legends[0].Docking = Docking.Bottom;
//Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Legend
//LegendStyle
// Enable 3D
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
//甜甜圈圖樣式Default SoftEdge Concave,,當啟動3D模式時設置不生效
Chart1.Series[0]["PieDrawingStyle"] = "SoftEdge";
//// Pie drawing style
//if (this.CheckboxShow3D.Checked)
// this.Dropdownlist1.Enabled = false;
//else
// this.Dropdownlist1.Enabled = true;
// Populate series data
double[] yValues = { 9042, 558, 2160, 4051, 2151, 1761 };
string[] xValues = { "A64", "A65", "A66", "A67", "A68", "A69"};
DataTable dt = CreateData();
var v = ToList<HZProducte>(dt);
//double[] yValues = { 65.62, 2.1, 85.73, 11.42, 34.45, 75.54, 5.7, 4.1 };
//string[] xValues = { "France", "Japan", "USA", "Italy", "Germany", "Canada", "Russia", "Spain" };
//具有綁定規則的其他數據點屬性,格式為:PointProperty=Field[{Format}] [,PointProperty=Field[{Format}]]。例如:“Tooltip=Price{C1},Url=WebSiteName”。
//Chart1.Series["Default"].Points.DataBindXY(xValues, yValues);<!--Label="#PERCENT{P1}-->"
/*
MSChart的Label的值的轉義符,如下:
#VALX 顯示當前圖例的X軸的對應文本(或數據)
#VAL, #VALY, 顯示當前圖例的Y軸的對應文本(或數據)
#VALY2, #VALY3, 顯示當前圖例的輔助Y軸的對應文本(或數據)
#SER: 顯示當前圖例的名稱
#LABEL 顯示當前圖例的標簽文本
#INDEX 顯示當前圖例的索引
#PERCENT 顯示當前圖例的所占的百分比
#TOTAL 總數量
#LEGENDTEXT 圖例文本
當LegendText為#AXISLABEL時,如果Label為#PERCENT,並且綁定的ValueMember為數值時,LegendText顯示的和Label一樣,存在Bug。
*/
var servers = Chart1.Series[0];
servers.Label = "#VAL\r\n#PERCENT{P1}";
//Chart1.DataSource = dt;
//servers.XValueMember = "CenterName";
//servers.YValueMembers = "Qty";
//servers.LegendText = "#VALX";
//Chart1.DataBind();
//servers.LabelBackColor = Color.Red;
//servers.LabelUrl = "http://www.baidu.com";
servers.Points.DataBind(v, "CenterName", "Qty", "LegendText=CenterName");
pieHelper.SupplementedAreaSizeRatio = 3.0f;
}
public class HZProducte
{
public int Qty { set; get; }
public string CenterName { get; set; }
}
DataTable CreateData()
{
DataTable dt = new DataTable();
#region 准備Dt
dt.Columns.Add("Qty", typeof(System.Int32));
dt.Columns.Add("CenterName", typeof(System.String));
DataRow row1 = dt.Rows.Add();
row1["Qty"] = 9042;
row1["CenterName"] = "後整一廠";
DataRow row2 = dt.Rows.Add();
row2["Qty"] = 558;
row2["CenterName"] = "後整二廠";
DataRow row3 = dt.Rows.Add();
row3["Qty"] = 2160;
row3["CenterName"] = "後整三廠";
DataRow row4 = dt.Rows.Add();
row4["Qty"] = 4051;
row4["CenterName"] = "後整四廠";
DataRow row5 = dt.Rows.Add();
row5["Qty"] = 2151;
row5["CenterName"] = "後整五廠";
DataRow row6 = dt.Rows.Add();
row6["Qty"] = 1761;
row6["CenterName"] = "後整六廠";
#endregion
return dt;
}
/// <summary>
/// DataTable 轉換為List 集合
/// </summary>
/// <typeparam name="TResult">類型</typeparam>
/// <param name="dt">DataTable</param>
/// <returns></returns>
public static List<TResult> ToList<TResult>(DataTable dt) where TResult : class, new()
{
//創建一個屬性的列表
List<PropertyInfo> prlist = new List<PropertyInfo>();
//獲取TResult的類型實例 反射的入口
Type t = typeof(TResult);
//獲得TResult 的所有的Public 屬性 並找出TResult屬性和DataTable的列名稱相同的屬性(PropertyInfo) 並加入到屬性列表
Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
//創建返回的集合
List<TResult> oblist = new List<TResult>();
foreach (DataRow row in dt.Rows)
{
//創建TResult的實例
TResult ob = new TResult();
//找到對應的數據 並賦值
prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
//放入到返回的集合中.
oblist.Add(ob);
}
return oblist;
}方便的話就用vs2008吧,省力多了MSCHART.exe裝下就好了。如果項目剛開始,直接用2008打開轉下,對你原有程序影響不大。
2005下試用方法:
在VS2005下添加mschart控件,添加方法有多種:
方法一:工具(T)-》選擇工具箱項(X)-》COM組件 在Microsoft Chart Control, version 6.0(OLEDB)前面打上對勾