c# - How can I bind an axis of a chart to a specific index of a List()? -
i'm attempting create chart using wpf toolkit y axis being updated values in list()
. i'm trying access value via specific index. currently, binding index of list()
rather int
creates "no suitable axis available plotting dependent value." exception.
here's have far, note attempt dependentvaluepath
access index:
<charting:lineseries verticalalignment="stretch" horizontalalignment="stretch" itemssource="{binding path=memorystats}" independentvaluepath="timestamp" dependentvaluepath="bytecount[0]" title="data points">
this memorystats
values consist of in code behind:
public list<int> bytecount { get; set; } public datetime timestamp { get; set; }
the chart works fine when lineseries in xaml has property dependentvaluepath="bytecount"
, when codebehind uses simple int:
public int bytecount { get; set; } public datetime timestamp { get; set; }
how bind index of list()
rather int
?
edit
i have been able grab specific value in list code behind naming index, there multiple lineseries
generated dynamically when chart created. bind each 1 index of list<int>()
, recreate every second or so.
here full method memorystats
uses update ui. working updating of lineseries
y values single bytecount int
, of lines same.
public class memorysample { public static memorysample generate(list<int> datapoints) { return new memorysample { bytecount = datapoints[0], timestamp = datetime.now }; } public int bytecount { get; set; } public datetime timestamp { get; set; } }
of course, i'd of lineseries
different. have x axis of each lineseries
of chart timestamp
(so have same timestamp) , various lineseries
have y axis values updated list()
of integers, each using separate index of list()
i'll try implement type converter, i'm not entirely sure when/where that.
edit 2
i got work way wanted to! found lot of from s.o. question regarding using multiple series in line chart.
it appears if type converter work well, shimrod has got question answered. however, ended doing set binding of lineseries' itemsource
index , retrieved data of content of index.
so, here's lineseries
looks like:
<charting:lineseries verticalalignment="stretch" horizontalalignment="stretch" itemssource="{binding [0]}" independentvaluepath="x" dependentvaluepath="y" title="data points"> </charting:lineseries>
note index in itemsource
binding. in code behind, set datacontext
of control observablecollection
, holds object inherits 'ilist' (which use object so), , object holds object contains properties x
, y
properties.
public observablecollection<inheritsfromilist<objectwithxandyproperties>> variabledatacontextissetto { get; set; }
accessing specific index of observablecollection
return list. then, items in list display on chart.
the easiest way think add property in class int
value of list
.
e.g.
int val { { return bytecount[0]; } }
or create converter take list binding , index parameter, , return desired value.
for example (i haven't tried this):
public class elementoflistconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value ilist && parameter int) { var lst = value ilist; int pos = (int)parameter; if (lst.count >= pos) { return lst[pos]; } } return null; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
edit
these steps use converter:
- create class (elementoflistconverter), shown before.
- add new xml namespace pointing towards location of converter. e.g.
xmlns:local="clr-namespace:wpfapplication2"
in resources of window (or usercontrol), add reference converter, this:
<window.resources> <local:elementoflistconverter x:key="elemoflist" /> </window.resources>
in binding, specify converter use (using key)
{binding yourelement, converter={staticresource elemoflist}, converterparameter=0}
the pro method can specify index of element directly in xaml. bind value other value using multibinding
, multivalueconverter
. (see this question on s.o. if need more information.
Comments
Post a Comment