How compare charts using a List<T> with LiveCharts? - c#

I'm trying to compare a lot of charts using Live Charts according with the numbers of classes inside my List.
I'm trying some like this:
class MyClass
{
IList<double> a;
IList<double> b;
}
And to make a chart:
List<MyClass> aLotOfCharts = new List<MyClass>
for(int i=0; i < aLotOfCharts.Count; i++)
{
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Values = new ChartValues<double> (aLotOfCharts[i].a)
},
};
}
I expect three charts with the "a" Data, but the actual output is only the last position of "aLotOfCharts.a".
I know I need to create more "new LineSeries" to input more charts, but I don't know how I can do this.

I do not know the Live Chart, but I think the answer is something like this:
List<MyClass> aLotOfCharts = new List<MyClass>()
SeriesCollection = new SeriesCollection()
for(int i=0; i < aLotOfCharts.Count; i++)
{
SeriesCollection.Add(new LineSeries
{
Values = new ChartValues<double> (aLotOfCharts[i].a)
});
}

Related

Binding Cartesian livecharts to a data table adaptor

I've been trying to to bind a data table adaptor to a Cartesian livecharts line series in c# winform with no luck
I've tried multiple method and none of them worked
Also there is no video on YouTube on how to bind the line series to data table adaptors only to simple data grid view so that data be inserted manually
I have done it with pie chart and I am in desperate need to do the same with line series
Could you help me to solve this problem
Best regards
cartesianChart2.Series.Clear();
SeriesCollection series = new SeriesCollection();
var types= (from o in brake1BindingSource.DataSource as List<Brake1TableAdapter>
select new { Braketype = o.Braketype }).Distinct();
foreach (var type in types)
{
List<double> values = new List<double>();
for (int vin= 1; vin<= 10000; vin++)
{
double value = 0;
var data = from o in brake1BindingSource.DataSource as List<Brake1TableAdapter>
where o.Braketype .Equals(type.Braketype ) && o.Braketype .Equals(vin)
orderby o.VIN ascending
select new { o.filling_amount, o.VIN };
if (data.SingleOrDefault() != null)
value = (double)data.SingleOrDefault().filling_amount;
values.Add(value);
}
series.Add(new LineSeries() { Title = type.Braketype .ToString(), Values = new ChartValues<double>(values) });
}
cartesianChart1.Series = series;

C# simply chart binding

The idea is to simply to plot arrD[i] in a chart called chart5 SeriesA. The issue is that nothing is plotted in the windows form. Maybe someone could help. Many thanks.
chart5 = new Chart();
Series SeriesA = new Series();
Dictionary<int, double> value5 = new Dictionary<int, double>();
for (int i = 0; i < monthCount; i++)
{
value5.Add(i, arrD[i]);
}
SeriesA.XValueMember = "Location";
SeriesA.YValueMembers = "Value";
chart5.DataSource = value5;
chart5.Series.Add("SeriesA");
You do not add the series you created to your chart.
Try this code :
Series SeriesA = new Series();
SeriesA.Points.DataBind(arrD, "Location", "Value", "");
chart5.Series.Add(SeriesA);
Note that we add SeriesA and not "SeriesA"
Ok, I simplified the binding (which works well now) as well as the loop for hiding the zero values. But how to print now the modified chart without zero values .. many thanks.
chart7.Series["Series3"].ChartType = SeriesChartType.Line;
chart7.Series["Series3"].Points.DataBindXY(xVal, arrDouble3);
foreach (Series series in chart7.Series)
{
foreach (DataPoint arrP in series.Points)
{
if (arrP.YValues.Length > 0 && (double)arrP.YValues.GetValue(0) == 0)
{
arrP.IsValueShownAsLabel = false;
}
}
}
chart7.Series["Series3"].Points.DataBindXY(xVal, arrP); ????

Fill combobox with a matrix

I want fill a combobox with a matrix
Matrix:
object[,] codes = new object[,] {
{ "1", "BANK S/A" },
{ "2", "BANK Center" },
{ "3", "BANK AMAZON S/A" }};
Fill my combobox:
comboBank.DataSource = codes;
comboBanco.DataBind();
but in my combobox appears the items
1
BANK S/A
2
Bank Center
3
Bank Amazon S/A
I want fill this combobox with just the names, not the ID's, and in the Value propertie of these combobox, I want the ID.
What I Want in my combobox
Bank S/A
Bank Center
Bank Amazon S/A
How can I do that ?
Here's a simple helper method you can use to get one column out of a two dimensional array:
public static IEnumerable<T> GetColumn<T>(T[,] array, int column)
{
for (int i = 0; i < array.GetLength(0); i++)
yield return array[i, column];
}
Using that you can just modify your code to:
comboBank.DataSource = codes.GetColumn(1);
Having said that, the real issue here is that you're using a multi-dimensional array when what you really should have is a single dimensional array of some object that contains both an int and a string.
public class Code
{
public int ID { get; set; }
public string Name { get; set; }
}
Code[] codes = new Code[]{
new Code(){ID=1, Name="BANK S/A"},
new Code(){ID=2, Name="BANK Center"},
new Code(){ID=3, Name="BANK AMAZON S/A"},
};
Once you have that strongly typed array you can either use Select to select the property to show, or set the DisplayMember of the combo box.
Maybe you could try something like this:
List<dynamic> list = new List<dynamic>();
for(int i = 0; i < codes.Lenght; i++)
list.Add(new { Value = code[i][0], Text = code[i][1] });
comboBox.DataSource = list;
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";

Assigning a button tag based on random items from an array

{
var buttonnameString = new List<string> { "button1", ... , "button12" };
for(int i = 0; i < 12; i++)
{
// Random car assignment to button
Random myRandom = new Random();
var carString = new List<string> { "Camaro", ... , "Model T" };
int index = myRandom.Next(carString.Count);
var name = carString[index];
carString.RemoveAt(index);
Tag = name.ToString();
}
}
In advance, thank you for any help that is provided. I'm a first year C# student so I know I have a lot to learn, but I've exhausted the extent of my google skills on trying to get this to work.
What I'm trying to do, is make a matching program. This program will have 12 buttons, labeled "button1", "button2"....etc. When the button is clicked, it will display it's tag, which is provided from a random array. I've gotten the random feature to work on assigning only a single buttons tag. Where I'm hung up at, is repeating this to all the buttons in a groupbox. I've tried the foreach venue, but couldn't get it to work successfully. Eventually I tried other methods as well. Below is where I've stopped at as I'm unsure on where to go. The two major questions that I have are
How do I assign the random string to a wildcard button tag?
What is the best way to accomplish randomly assigning 12 car names, to 12 buttons? The use of two arrays?
Try this:
{
var numOfButtons = 12;
var matchingButtonIndex = 0;
// Random car assignment to button
Random myRandom = new Random();
var buttons = new List<Button> { button1, ... , button12 };
var carString = new List<string> { "Camaro", ... , "Model T" };
while (matchingButtonIndex < numOfButtons)
{
int index = myRandom.Next(carString.Count);
var name = carString[index];
if (name != null)
{
buttons[matchingButtonIndex].Tag = name;
carString[index] = null;
matchingButtonIndex = matchingButtonIndex + 1;
}
}
}
Notice I have changed the button names array to an array of buttons. I have also emptied out the carString as I find unused car names.
Iterating over buttons is easy when you iterate through the form's Controls
var carString = new List<string> { "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T", "Camaro", "Mini Cooper", "Porsche 944", "Ford Focus", "Chevy Blazer", "Model T" };
foreach(Control c in Controls)
{
if (c is Button)
{
Random myRandom = new Random();
int index = myRandom.Next(carString.Count);
c.Tag = carString[index];
}
}

adding items to columns/rows in listview using foreach

I am into day 5 of learning c#, and am trying to figure out how to fill/re-fill a ListView control, containing 10 rows and 12 columns, using a foreach loop. I have coded the functionality I'm after in C.
void listPopulate(int *listValues[], int numberOfColumns, int numberOfRows)
{
char table[100][50];
for (int columnNumber = 0; columnNumber < numberOfColumns; ++columnNumber)
{
for (int rowNumber = 0; rowNumber < numberOfRows; ++rowNumber)
{
sprintf(&table[columnNumber][rowNumber], "%d", listValues[columnNumber][rowNumber]);
// ...
}
}
}
Here is what I have figured out so far:
public void listView1_Populate()
{
ListViewItem item1 = new ListViewItem("value1");
item1.SubItems.Add("value1a");
item1.SubItems.Add("value1b");
ListViewItem item2 = new ListViewItem("value2");
item2.SubItems.Add("value2a");
item2.SubItems.Add("value2b");
ListViewItem item3 = new ListViewItem("value3");
item3.SubItems.Add("value3a");
item3.SubItems.Add("value3b");
....
listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
}
I'm assuming that I would have to do the creation of the list items in a separate step. So my question is: there must be a way to do this in C# with a for or foreach loop, no?
I am not sure if I understood you correctly, but here's i think what you need...
Actually it depends on your DataSource which you are using to fill up the ListView.
Something like this (I am using Dictioanry as a DataSource here) -
// Dictinary DataSource containing data to be filled in the ListView
Dictionary<string, List<string>> Values = new Dictionary<string, List<string>>()
{
{ "val1", new List<string>(){ "val1a", "val1b" } },
{ "val2", new List<string>(){ "val2a", "val2b" } },
{ "val3", new List<string>(){ "val3a", "val3b" } }
};
// ListView to be filled with the Data
ListView listView = new ListView();
// Iterate through Dictionary and fill up the ListView
foreach (string key in Values.Keys)
{
// Fill item
ListViewItem item = new ListViewItem(key);
// Fill Sub Items
List<string> list = Values[key];
item.SubItems.AddRange(list.ToArray<string>());
// Add to the ListView
listView.Items.Add(item);
}
I have simplified the code for your understanding, since there several ways to iterate through a Dictionary...
Hope it helps!!
You do this almost exactly the same as in C. Just loop through the collection...
int i = 0;
foreach (var column in listValues)
{
var item = new ListViewItem("column " + i++);
foreach (var row in column)
{
item.SubItems.Add(row);
}
listView1.Items.Add(item);
}
It's hard to provide a real example without seeing what your collection looks like, but for an array of arrays this will work.

Categories