[英]Customize series point labels value of a DevExpress chart control
我想使用從綁定到圖表的完全相同的數據源獲得的屬性來自定義每個系列點值。 為了說明我的問題,我將使用與DevExpress在其網站中進行的圖表數據綁定相同的示例:
public class Record {
int id, age;
string name;
public Record(int id, string name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int ID {
get { return id; }
set { id = value; }
}
public string Name {
get { return name; }
set { name = value; }
}
public int Age {
get { return age; }
set { age = value; }
}
}
要填充圖表,它使用以下代碼塊:
private void Form1_Load(object sender, EventArgs e) {
// Create a list.
ArrayList listDataSource = new ArrayList();
// Populate the list with records.
listDataSource.Add(new Record(1, "Jane", 19));
listDataSource.Add(new Record(2, "Joe", 30));
listDataSource.Add(new Record(3, "Bill", 15));
listDataSource.Add(new Record(4, "Michael", 42));
// Bind the chart to the list.
ChartControl myChart = chartControl1;
myChart.DataSource = listDataSource;
// Create a series, and add it to the chart.
Series series1 = new Series("My Series", ViewType.Bar);
myChart.Series.Add(series1);
// Adjust the series data members.
series1.ArgumentDataMember = "name";
series1.ValueDataMembers.AddRange(new string[] { "age" });
// Access the view-type-specific options of the series.
((BarSeriesView)series1.View).ColorEach = true;
series1.LegendPointOptions.Pattern = "{A}";
}
代碼的結果圖為:
我的問題是,例如,如何使用屬性ID
為每個意向標簽點添加額外的信息? (例如{ID +“-” + Age})在上一張圖中,我們將獲得以下標簽數據點:“ 1-19”,“ 2-30”,“ 3-15”和“ 4-42”。
我建議您使用圖表控件的CustomDrawSeriesPoint
,方法如下:
private void chartControl1_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
{
// Get the value of your point (Age in your case)
var pointValue = e.SeriesPoint.Values[0];
// You can get the argument text using e.SeriesPoint.Argument
// Set the label text of your point
e.LabelText = "value is " + pointValue;
}
可能有幫助的鏈接: 單擊我
從代碼的外觀來看,這將在Record
對象內部完成。
該Age參數是一個整數,並且也與圖表標簽匹配。 要更改該標簽,請更改所指的內容。
使用Record對象創建一個新屬性,如下所示:
public string ChartLabel
{ get { return String.Format("{0} - {1}", ID, Age); } }
它只有一個屬性...然后您將更改圖表代碼,如下所示:
series1.ArgumentDataMember = "name";
series1.ValueDataMembers.AddRange(new string[] { "ChartLabel" });
那應該改變圖表中顯示的內容。
使用LegendPoint選項可以在圖例文本中同時包含參數和值。
series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.