C# 自定义DevExpress图表控件的系列点标签值
我想使用从绑定到图表的同一数据源获得的特性自定义每个系列的点值。为了说明我的问题,我将在图表数据绑定中使用与DevExpress相同的示例:C# 自定义DevExpress图表控件的系列点标签值,c#,.net,charts,devexpress,C#,.net,Charts,Devexpress,我想使用从绑定到图表的同一数据源获得的特性自定义每个系列的点值。为了说明我的问题,我将在图表数据绑定中使用与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 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”。从代码的外观看,这将从记录
对象内部完成
该年龄参数是一个整数,并且与图表标签匹配。要更改该标签,请更改您所指的内容
使用如下所示的记录对象创建一个新属性:
public string ChartLabel
{ get { return String.Format("{0} - {1}", ID, Age); } }
这是一个get only属性…然后您将更改图表代码,如下所示:
series1.ArgumentDataMember = "name";
series1.ValueDataMembers.AddRange(new string[] { "ChartLabel" });
这将改变图表中显示的内容。从代码的外观看,这将从
记录
对象内部完成
该年龄参数是一个整数,并且与图表标签匹配。要更改该标签,请更改您所指的内容
使用如下所示的记录对象创建一个新属性:
public string ChartLabel
{ get { return String.Format("{0} - {1}", ID, Age); } }
这是一个get only属性…然后您将更改图表代码,如下所示:
series1.ArgumentDataMember = "name";
series1.ValueDataMembers.AddRange(new string[] { "ChartLabel" });
这应该会改变图表中显示的内容。我建议您使用图表控件的
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;
}
一个可能有帮助的链接:我建议您使用图表控件的
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;
}
一个可能有帮助的链接:使用LegendPoint选项它在图例文本中提供参数和值
series1.LegendPointOptions.PointView=PointView.ArgumentAndValues 使用LegendPoint选项它在图例文本中同时显示参数和值
series1.LegendPointOptions.PointView=PointView.ArgumentAndValues 这不起作用,因为
LineSeriesView
图表值数据成员的类型与数字刻度不兼容。这不起作用,因为LineSeriesView
图表值数据成员的类型与数字刻度不兼容。请重新阅读问题,并注意我想要获得的内容。这不仅仅是在每个点之前添加“value is”,为此,我只会使用LegendPointOptions.Pattern
。是的,我知道,对于这一点,我提到了可以获取点参数(记录名称)并使用它来获取记录的Id,并将这两个参数(Id和年龄)合并以获得所需的标签。如果有两条记录具有相同的名称,则可以遍历这些点值。好吧,假设我使用Age
属性作为ValueMember(和Integer),它对于字典中的唯一值将永远无效,并且迭代也不会有帮助。同时,如果任何唯一id用作ValueMember,它将启用字典解决方案,但图表将这些id作为值,因此值本身也必须修改。请再次阅读问题,并注意我想要获得的内容。这不仅仅是在每个点之前添加“value is”,为此,我只会使用LegendPointOptions.Pattern
。是的,我知道,对于这一点,我提到了可以获取点参数(记录名称)并使用它来获取记录的Id,并将这两个参数(Id和年龄)合并以获得所需的标签。如果有两条记录具有相同的名称,则可以遍历这些点值。好吧,假设我使用Age
属性作为ValueMember(和Integer),它对于字典中的唯一值将永远无效,并且迭代也不会有帮助。同时,如果将任何唯一id用作ValueMember,它将启用字典解决方案,但图表将这些id作为值,因此也必须修改值本身。