【问题标题】:Customize series point labels value of a DevExpress chart control自定义 DevExpress 图表控件的系列点标签值
【发布时间】:2013-05-06 18:28:13
【问题描述】:

我想使用从绑定到图表的相同数据源获得的属性来自定义每个系列的点值。为了说明我的问题,我将使用与 DevExpress 在他们的 website 中用于图表数据绑定的相同示例:

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”。

【问题讨论】:

    标签: c# .net charts devexpress


    【解决方案1】:

    我建议你使用图表控件的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;
    }
    

    一个可能有帮助的链接:Click me

    【讨论】:

    • 请再次阅读问题并注意我想要获得的内容。这不仅仅是在每个点之前添加“价值是”,为此我只使用LegendPointOptions.Pattern
    • 是的,我知道,为此我提到您可以获取点参数(记录名称)并使用它来获取记录的 ID,并将两者(ID 和年龄)连接到得到你想要的标签。如果有两条同名记录,可以遍历点值。
    • 好吧,假设我使用 Age 属性作为 ValueMember(和 Integer),它将永远不会对 Dictionary 中的唯一值有效,并且迭代也无济于事。同时,如果将任何唯一 id 用作 ValueMember,它将启用字典解决方案,但图表会将这些 id 作为值,因此也必须修改值本身。
    【解决方案2】:

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

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

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

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

    它是一个只能获取的属性...那么您可以像这样更改图表代码:

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

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

    【讨论】:

    • 这行不通,因为LineSeriesView 图表值数据成员的类型与数字刻度不兼容。
    【解决方案3】:

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

    series1.LegendPointOptions.PointView = PointView.ArgumentAndValues;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多