Hi,
I'm looking for a way to sort data by a column as it is displayed in a BarChart.
At present I have the following:
DataTable table;table.Columns.Add("Key", typeof(int));table.Columns.Add("Labels", typeof(string)); // the Key in string formattable.Columns.Add("Value1", typeof(double));table.Columns.Add("Value2", typeof(double));
// ...Populate datatable...
chart.DataSource = table;chart.DataBind();
foreach (DataColumn col in table.Columns) { if(col.ColumnName.Equals("Key")) { chart.Data.IncludeColumn(col,false); } else { chart.Data.IncludeColumn(col, true); } }
At present the labels are appear (in no useful order) along one of the axes, and the "Value1" and "Value2" are shown as bars for each label.
I'd like to sort the table by "Key" and then have that reordering reflected in the order of the labels on the axis of the chart.
Does anyone know how to do this? I am using v8.2
Many thanks,Luke
Note that I have tried:
1)table.DefaultView.Sort = "Key"
2)table.DefaultView.Sort = "Key"chart.DataSource = table.DefaultView
3)DataView dv = new DataView(table)dv.Sort = "Key"chart.DataSource = dvSomething I didn't point out first time round: the data in the table continues to change after it is created, so I'd rather not have to recreate the table each time there is an update to its content!
Thanks again,Luke
I would have suggested using a DataView as you apparently have tried. Are the values coming out unsorted? Do you get the same unsorted results in a grid?
Try this code to help debugging:
Form f = new Form();
DataGrid g = new DataGrid();
g.DataSource = dv;
g.Dock = DockStyle.Fill
f.Controls.Add(g);
f.Show();
Thank you!
that may be true, but i think the default behavior was lacking a bit there. i entered it as a bug (BR35894) and fixed it. the issue should be resolved in a future hotfix. the workaround of calling DataBind() should work just as well, though.
I've been told this is the "default behaviour".
Hi David,
Many thanks for your reply. I have been playing with the code you supplied and it seems that the visual part of the chart was not being refreshed when data updates were made. I used the following code:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using Infragistics.Win.UltraWinGrid;using System.Windows.Forms;using Infragistics.Win.UltraWinChart;using System.Threading;namespace ConsoleApplication3{ class Program { static void Main(string[ args) { dt = new DataTable(); dt.Columns.Add("Key", typeof(int)); dt.Columns.Add("Labels", typeof(string)); dt.Columns.Add("Column1", typeof(double)); dt.Columns.Add("Column2", typeof(double)); DataView dv = new DataView(dt); dv.Sort = "Key"; uc = new UltraChart(); uc.ChartType = Infragistics.UltraChart.Shared.Styles.ChartType.LineChart; uc.Data.SwapRowsAndColumns = true; uc.DataSource = dv; uc.DataBind(); uc.Dock = DockStyle.Fill; uc.Data.IncludeColumn("Key", false); uc.Data.IncludeColumn("Labels", true); uc.Data.IncludeColumn("Column1", true); uc.Data.IncludeColumn("Column2", true); f = new Form(); f.Controls.Add(uc); f.Show(); Thread t = new Thread(new ThreadStart(Update)); t.Start(); Application.Run(); } static UltraChart uc; static DataTable dt; static Form f; static void Update() { int i = 200; while (true) { f.Invoke(new MethodInvoker( delegate() { DataRow dr = dt.NewRow(); dr["Key"] = i; dr["Labels"] = i.ToString(); dr["Column1"] = 0.5; dr["Column2"] = 0.7; dt.Rows.Add(dr); //uc.DataBind(); } )); Thread.Sleep(1000); i--; } } }}
Uncommenting the uc.DataBind() statement causes the chart area to redraw itself properly. If I fail to call DataBind() after each update, the chart only updates visually when I interact with it (eg moving the mouse over the chart!). Even more strangely, if I comment out both the IncludeColumn() statements and the DataBind() statement, the updates cause the chart to redraw itself as expected. Is this behaviour by design? If so, it might pay to make this fact more obvious in the documentation. On the other hand it's more likely I'm misunderstanding something that's going on here.
The DataBind() call is an adequate workaround in my example though, so I much appreciate your input so far.
All the best,Luke