C# 自定义 DevExpress 图表控件的系列点标签值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16404865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:57:02  来源:igfitidea点击:

Customize series point labels value of a DevExpress chart control

c#.netchartsdevexpress

提问by GoRoS

I would like to customize every series point values with a property obtained from the very same data source that is bound to the chart. To illustrate my problem I'll use the same example as DevExpress does in their websitefor chart data binding:

我想使用从绑定到图表的相同数据源获得的属性来自定义每个系列点值。为了说明我的问题,我将使用与 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; }
    }
}

To fill the chart it uses the following block of code:

要填充图表,它使用以下代码块:

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}";
}

The resulting chart of the code is:

代码的结果图表是:

Resulting chart

结果图表

My question is, how could I use for example the property IDto add extra information for every serie label points? (e.g {ID + " - " + Age}) In the previous graph we would get these label data points: "1 - 19", "2 - 30", "3 - 15" and "4 - 42".

我的问题是,我如何使用例如属性ID为每个系列标签点添加额外信息?(例如{ID + " - " + Age})在上图中,我们将获得这些标签数据点:“1 - 19”、“2 - 30”、“3 - 15”和“4 - 42”。

回答by Nevyn

From the look of the code this would be done from inside the Recordobject.

从代码的外观来看,这将从Record对象内部完成。

That Age parameter is an integer, and matches the chart label as well. To change that label, change what you are referring to.

该 Age 参数是一个整数,也与图表标签匹配。要更改该标签,请更改您所指的内容。

Make a new property withing the Record object that looks something like this:

使用 Record 对象创建一个新属性,如下所示:

public string ChartLabel
{  get { return String.Format("{0} - {1}", ID, Age); } }

its a get only property...then you would change the chart code like so:

它是一个仅获取属性...然后您可以像这样更改图表代码:

series1.ArgumentDataMember = "name";
series1.ValueDataMembers.AddRange(new string[] { "ChartLabel" });

That should change what is displayed in the chart.

这应该会改变图表中显示的内容。

回答by SidAhmed

I suggest you use the CustomDrawSeriesPointof your chart control, here is how :

我建议您使用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;
}

A link that may help : Click me

一个可能有帮助的链接:点击我

回答by user3209749

Use LegendPoint options it brings both argument and value in the legend text.

使用 LegendPoint 选项,它会在图例文本中带来参数和值。

series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;

series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;