Question

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

Series 1

Title:2013

Object

 label-Jan

 Value-200

 ToolTip-Value = 200   

Object

 label-Feb

 Value-400

 ToolTip-Value = 400

And a second series

Series 2

Title:2014

Object

 label-Jan

 Value-100

 ToolTip-Value = 100   

Object

 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.

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.

My object that I store each point in

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;

}

I store each point in a List<XamDataChartItem>

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

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 enter image description here

Was it helpful?

Solution

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top