简体   繁体   中英

Customize series point labels value of a DevExpress chart control

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 website for chart data binding:

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:

结果图

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

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

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

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

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

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

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.

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

series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM