Jump to content
Software FX Community

Adding to chart.Series throws an error


pixel3cs
 Share

Recommended Posts

  ChartFX.WPF.Chart chart = new ChartFX.WPF.Chart();   chart.Gallery = ChartFX.WPF.Gallery.Line;   chart.PointCount = dataTable.NumericDataLength;   ChartFX.WPF.SeriesAttributes series = new ChartFX.WPF.SeriesAttributes();   series.BindingPath = "NumericData";   series.Marker.Size = dataTable.NumericDataLength;    

        chart.Series.Add(series);// error, object not set to an instance of an object, why?

  chart.ItemsSource = dataTable;   grid.Children.Add(chart);

 

I am using this in XBAP but should be no problem. Please help. 

Link to comment
Share on other sites

If you are passing data you do not need to the set the chart.PointCount property.

 Also are you sure you want the size of the plotted points to be as big as the number of points in the chart? (This is what series.Marker.Size controls, the size of the rectangle/circle/etc used to represent each point).

If you are binding to a datatable you must pass the dataTable.Rows to the chart.ItemsSource property and "NumericData" should be the name of the dataTable column you want to plot.

Regards,

JuanC

Link to comment
Share on other sites

I have tried all of this but the problem still exists. Look what i am trying to do.

public partial class LineChartControl : UserControl   {   public LineChartControl(DataTable dataTable)   {   InitializeComponent();   for (int r = 0; r < dataTable.Rows.Count; r++)   {   ChartFX.WPF.SeriesAttributes series = new ChartFX.WPF.SeriesAttributes();   series.BindingPath = "NumericData";   series.AxisX = new ChartFX.WPF.Axis();   series.AxisY = new ChartFX.WPF.Axis();   chart.Series.Add(series);   }     chart.ItemsSource = dataTable.Rows;   // dataTable.Rows[0].NumericData is an array of Double elements   }   } 

So one line for each row and not for a column. The DataTable is not the one from System.Data, is own defined, but implements the IEnumerable interface.

Is there another way to program the graph to draw each row as a line and not use SeriesAttributes?

Link to comment
Share on other sites

I found the same problem and a simple hack to get around it.

...

chart.ItemsSource = null; // HACK: This is hack to ensure the chart.Series is initialized before accessing it

chart.Series.Add(series);

...

If the SoftwareFX guys has another way to handle this I would also very much like to here about.

Link to comment
Share on other sites

>> it appears that lines are drawn from columns values and not from rows

You are correct the default behavior is that a series reads data from a column/property and each series represents a line in a line chart. Each line will have as many points as items in the collection you pass to the chart.

There is a feature we support called DataTransform, the idea is that we can expand the way data is processed by the chart. One of the most useful transforms is crosstab, this allows you to read data like the following

Pepsi,Jan,10

CocaCola,Jan,14

Pepsi,Feb,20

CocaCola,Feb,17

Sprite,Feb,12

CocaCola,Mar,9

Sprite,Mar,15

And generate a chart with 3 series (Pepsi, CocaCola and Sprite) and 3 points for each series (Jan, Feb and Mar). You can view a sample here at the end of the thread.

In your case you will need a recently added transform called ListTransform. The class in question is in the ChartFX.WPF.Data assembly and it can be used as follows

  xmlns:cfx="clr-namespace:ChartFX.WPF;assembly=ChartFX.WPF"

  xmlns:cfxData="clr-namespace:ChartFX.WPF.Data;assembly=ChartFX.WPF.Data"

  <cfx:Chart Gallery="Line" x:Name="chart1" Grid.Row="2" Margin="10" ItemsSource="{Binding Source={StaticResource prodData}}">

  <cfx:Chart.DataTransforms>

  <cfxData:ListTransform ColumnPath="Sales" ValuePath="Value" />

  </cfx:Chart.DataTransforms>

  </cfx:Chart>

In this sample the collection passed to the chart contains CLR objects with a property named Sales that returns a collection of CLR objects with a numerical property called Value. In this scenario if the

Link to comment
Share on other sites

Hi

 I was wondering if you could tell me which version you are using - having had to revert to 0.8.2957.30556 build, while the hack of setting the ItemSource of the chart to Null does does get me over the immediate exception on the Series.Add, I do get the following exception thrown whan the charts are being displayed / painted:

 

System.NullReferenceException was unhandled

  Message="Object reference not set to an instance of an object."

  Source="ChartFX.WPF"

  StackTrace:

at j.a(Object A_0, Type A_1, String A_2)

at au.a(Object A_0, Object A_1, Type A_2, Int32 A_3)

at ca.a(Object A_0, Object A_1, Type A_2, Int32 A_3)

at ChartFX.WPF.SeriesAttributes.a(ICollection A_0, Int32 A_1, Int32 A_2, NotifyCollectionChangedAction A_3, DataValues A_4)

at ChartFX.WPF.SeriesAttributes.a(IEnumerable A_0, Int32 A_1, Int32 A_2, NotifyCollectionChangedAction A_3, DataValues A_4)

at ChartFX.WPF.SeriesAttributes.a(IEnumerable A_0, DataValues A_1)

at ChartFX.WPF.DataValues.a(ag A_0)

at ChartFX.WPF.Chart.ar()

at ChartFX.WPF.Chart.a(Object A_0, RoutedEventArgs A_1)

at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)

at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)

at System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args)

at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)

at MS.Internal.FrameworkObject.OnLoaded(RoutedEventArgs args)

at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)

at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)

at MS.Internal.LoadedOrUnloadedOperation.DoWork()

at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()

at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()

at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)

at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)

at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)

at System.Windows.Interop.HwndTarget.OnResize()

at System.Windows.Interop.HwndTarget.HandleMessage(Int32 msg, IntPtr wparam, IntPtr lparam)

at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)

at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)

at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)

at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)

at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at MS.Win32.UnsafeNativeMethods.ITfMessagePump.GetMessageW(MSG& msg, Int32 hwnd, Int32 msgFilterMin, Int32 msgFilterMax, Boolean& result)

at System.Windows.Threading.Dispatcher.GetMessage(MSG& msg, IntPtr hwnd, Int32 minMessage, Int32 maxMessage)

at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)

at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)

at System.Windows.Threading.Dispatcher.Run()

at System.Windows.Application.RunInternal(Window window)

at System.Windows.Application.Run(Window window)

at System.Windows.Application.Run()

at NTO.App.Main() in C:\Users\Neutrino_1\Documents\Projects\Neutrino\OBM\obj\Debug\App.g.vb:line 75

at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()

  InnerException:

Many thanks

 

Paul

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...