使用多个ColumnSeries以编程方式创建Infragistics XamDataChart [英] Creating an Infragistics XamDataChart Programatically with multiple ColumnSeries

查看:76
本文介绍了使用多个ColumnSeries以编程方式创建Infragistics XamDataChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以标题说明了一切.

So the title says it all.

我有一个循环,循环遍历发送给我的图.

I have a loop that loops through graphs that are sent to me.

每个图形对象中都有一个或多个系列的对象,这些对象包含标签,数据点和工具提示.

Each graph object has one or more series in it of objects that contain a label, datapoint and tool tip.

每个系列都必须在图表上使用单独的条形图.

Each series needs to be a separate bar on the graph.

例如,我可能有一个类似的系列对象

So for example I might have a series object like

系列1

标题:2013

对象

 label-Jan

 Value-200

 ToolTip-Value = 200   

对象

 label-Feb

 Value-400

 ToolTip-Value = 400

第二个系列

系列2

标题:2014

对象

 label-Jan

 Value-100

 ToolTip-Value = 100   

对象

 label-Feb

 Value-300

 ToolTip-Value = 300

每个系列都必须在图表上使用单独的条形颜色.

Each Series need to be a separate bar color on the graph.

尽管我尽了最大的努力,但我似乎无法以编程方式实现这一目标.

Despite my best efforts I cant seem to make this happen programmatically.

由于该控件用于多个位置,因此需要假定返回的数量不明的Series带有未知数量的数据点.

Need to assume there is an unknown amount of Series being returned with an unknown amount of data points because this control is used in multiple spots.

我存储每个点的对象

public class XamDataChartItem: INotifyPropertyChanged 
{
private String _label;

public String Label
{
    get { return _label; }
    set { _label = value;  }
}

private double _yPoint;

public double YPoint
{
    get { return _yPoint; }
    set { _yPoint = value; OnPropertyChanged("YPoint"); }
}

private String _ToolTip;

public String ToolTip
{
    get { return _ToolTip; }
    set { _ToolTip = value; OnPropertyChanged("ToolTip"); }
}   

void OnPropertyChanged(String prop)
{
    PropertyChangedEventHandler handler = PropertyChanged;

    if (handler != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}
public event PropertyChangedEventHandler PropertyChanged;

}

我将每个点存储在List<XamDataChartItem>

private List<XamDataChartItem> _dataCollection;
public List<XamDataChartItem> dataCollection
{
    get { return _dataCollection; }
    set { _dataCollection = value; OnPropertyChanged("dataCollection"); }
}

我的顶起方法不会创建多个条,并且不会在X轴上放置正确的标签

My jacked up method that doesn't create multiple bars, and doesn't put the correct labels on the X axis

            ChartControl control = o as ChartControl;
            if (control == null)
                return;

            ElementList list = args.NewValue as ElementList;
            if (list != null && list.Count >= 1)
            {
                control.dataCollection.Clear();

                foreach (Element data in list)
                {
                    List<XamDataChartItem> NewBarSeries = new List<XamDataChartItem>();

                    CategoryXAxis catX = new CategoryXAxis()
                    {
                        Name = "catX",
                        ItemsSource = NewBarSeries,
                        Label = "{}{Label}",
                        Gap = 10
                    };
                    NumericYAxis numY = new NumericYAxis()
                    {
                        Name = "numY",
                        ToolTip = "{Binding ToolTip}"
                    };

                    foreach (var point in (data["DataPoints"] as ElementList))
                    {
                        var label = point["Label"];
                        var value = point["Value"];
                        var toolTip = point["ToolTip"];
                        var legend = data["LegendLabel"];

                        if (legend != null)
                        {
                            control.Legend = legend.ToString();
                            control.isLegVis = true;
                        }
                        XamDataChartItem item = new XamDataChartItem()
                        {
                            Label = label.ToString(),
                            YPoint = double.Parse(value.ToString()),
                            ToolTip = toolTip.ToString()
                        };

                        NewBarSeries.Add(item);     

                    }


                    ColumnSeries cs = new ColumnSeries()
                    {
                        Title = data["LegendLabel"],
                        ItemsSource = NewBarSeries,
                        ValueMemberPath = "YPoint",
                        XAxis = catX,
                        YAxis = numY
                    };
                    control.masterCollection.Add(NewBarSeries);

                    if (control.chart.Axes.Count == 0)
                    {
                        control.chart.Axes.Add(catX);
                        control.chart.Axes.Add(numY);
                    }
                    control.chart.Series.Add(cs);

                    //Hack to refresh the DataContext
                    //For some reason Observable Collections don't update with this control
                    //You cannot set the Mode it's read only
                    //Invoking OnPropertyChanged from control doesn't work either 
                    control.DataContext = null;
                    control.DataContext = control;                   
                }

这是类似的预期结果

This is a similar intended outcome

推荐答案

因此,上述问题的答案非常简单.所需的元素中有99%已经存在

So the answer to the above problem was pretty simple. 99% of the elements needed are already there

与其尝试同时动态创建所有数据点和列的集合.分离它们效果很好.

Instead of attempting to dynamically create the collection of data points and the collumns all at the same time. Seperating them worked very well.

List<List<DataPointObjects>> MasterList 

method NewDataReceived(args){

    foreach Chart{
      new List<DataPointObjects> temp;
         Loop through data{
           Create new data point objects
           add them to temp
         }
      add temp to MasterList
    }

   // Now that we have all of our chart points
  Create xAxis
  Create yAxis
  Foreach chart in MasterList
  {
      Assign xAxis data
      Assign yAxis data

      Build a ColumnSeries
      assign it x and y axis

      if chart doesnt contain these axises then add them

      if chart series doesnt containt this new series then add it
  }
  chart.datacontext = null
  chart.datacontext = this

}

这篇关于使用多个ColumnSeries以编程方式创建Infragistics XamDataChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆