所以标题说明了一切。
我有一个循环遍历发送给我的图表。
每个图形对象都有一个或多个包含标签,数据点和工具提示的对象系列。
每个系列都需要是图表上的单独栏。
例如,我可能有一个像
这样的系列对象系列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
每个系列都需要在图表上显示单独的条形颜色。
尽管我付出了最大的努力,但似乎无法以编程方式实现这一目标。
需要假设返回的未知数量的系列具有未知数量的数据点,因为此控件用于多个位置。
我在
中存储每个点的对象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轴上放置正确的标签
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;
}
这是一个类似的预期结果
答案 0 :(得分:1)
所以上述问题的答案非常简单。 99%的元素已经存在
而不是尝试同时动态创建数据点和collumns的集合。分离它们非常有效。
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
}