Oxyplot column series - Add items by using a loop - c#

I have a oxyplot column series which I have masked as a histogram using a linear axis. I get my values from a list called frequency with 20 elements.
I wonder if there is a smarter way to do this:
this.Items = new Collection<Item>
{
new Item {Label = "1", Value=frequency[0]},
new Item {Label = "2", Value=frequency[1]},
new Item {Label = "3", Value=frequency[2]},
...
new Item {Label = "18", Value=frequency[17]},
new Item {Label = "19", Value=frequency[18]},
new Item {Label = "20", Value=frequency[19]},
};
I have tried to create a for-loop inside like this:
this.Items = new Collection<Item>
{
for (int i = 0; i < 20; i++)
{
Items.Add(new Item { Label = i.ToString(), Value = frequency[i]});
}
};
But it does not work.
Does anyone have an idea on how to do this?

You can't put the for loop in the object initializer.
Create the collection.
this.Items = new Collection<Item>();
Populate it:
for (int i = 0; i< 20; i++)
{
Items.Add(new Item { Label = i.ToString(), Value = frequency[i] });
}

Related

How can ı remove from duplicated items in C#?

I have a list in the below it has 18 value.I have another list(include unique 6 value) created by random numbers(1,18), and they are unique.
When i do these for example elidenkiKartlar list shows me 6 unique values, but i have 2 list more in other lists, I have same values for example in elindekiKartlar=K1,K3,S4,RD-1,S2,M4 in another list K1,K4,RD-2,S4,K3, i have same values (K1,K3) e.g.
How can i remove them ? How can I share this big list to 3 equal(6,6,6) part ?
Thanks for your advise from now..
public List<string> kartlar = new List<string>
{
"S1",
"S2",
"S3",
"S4",
"S5",
"M1",
"M2",
"M3",
"M4",
"M5",
"K1",
"K2",
"K3",
"K4",
"K5",
"RD-1",
"RD-2",
"RD-3",
};
//Creating uniqe random list
int [] sayilar=new int[6];
for (int i = 0; i < sayilar.Length; i++)
{
essiz:
sayilar[i] = random.Next(0, 18);
for (int j = 0; j < i; j++)
{
if (sayilar[i]==sayilar[j])
{
goto essiz;
}
}
}
//Adding string items by randomList index
foreach (var sayi in sayilar)
{
elindekiKartlar.Add(kartlar[sayi]);
}
Use Distinct Method
var uniqueValues = kartlar.Distinct().ToList();
Try this
elindekiKartlar.Distinct();

Blank chart when using candlesticks

When trying to plot candlestick chart using Oxyplot library, it is empty, despite the fact that I assigned model to the plot view.
var plotModel1 = new PlotModel { Title = "Large Data Set (wide window)" };
var timeSpanAxis1 = new DateTimeAxis { Position = AxisPosition.Bottom };
plotModel1.Axes.Add(timeSpanAxis1);
var linearAxis1 = new LinearAxis { Position = AxisPosition.Left };
plotModel1.Axes.Add(linearAxis1);
var n = 10000;
var items = HighLowItemGenerator.MRProcess(n).ToArray();
var series = new CandleStickSeries
{
Color = OxyColors.Black,
IncreasingColor = OxyColors.DarkGreen,
DecreasingColor = OxyColors.Red,
DataFieldX = "Time",
DataFieldHigh = "H",
DataFieldLow = "L",
DataFieldOpen = "O",
DataFieldClose = "C",
TrackerFormatString =
"High: {2:0.00}\nLow: {3:0.00}\nOpen: {4:0.00}\nClose: {5:0.00}",
ItemsSource = items
};
timeSpanAxis1.Minimum = items[n - 200].X;
timeSpanAxis1.Maximum = items[n - 130].X;
linearAxis1.Minimum = items.Skip(n - 200).Take(70).Select(x => x.Low).Min();
linearAxis1.Maximum = items.Skip(n - 200).Take(70).Select(x => x.High).Max();
plotModel1.Series.Add(series);
timeSpanAxis1.AxisChanged += (sender, e) => AdjustYExtent(series, timeSpanAxis1, linearAxis1);
var controller = new PlotController();
controller.UnbindAll();
controller.BindMouseDown(OxyMouseButton.Left, PlotCommands.PanAt);
plotView1.Model = plotModel1;
Strange thing is that I've just copied few things from the Oxyplot series example. I've also created minimal project with the problem described.
The objects generated by the HighLowItemGenerator have different names of properties than defined in the CandleStickSeries definition. Check the items objects in the debugger to see it. Maybe the sample is a bit out of date. The solution is to change the series definition to use the correct properties this is how it should look like:
var series = new CandleStickSeries
{
Color = OxyColors.Black,
IncreasingColor = OxyColors.DarkGreen,
DecreasingColor = OxyColors.Red,
DataFieldX = "X",
DataFieldHigh = "High",
DataFieldLow = "Low",
DataFieldOpen = "Open",
DataFieldClose = "Close",
TrackerFormatString =
"High: {2:0.00}\nLow: {3:0.00}\nOpen: {4:0.00}\nClose: {5:0.00}",
ItemsSource = items
};

Find variables in a list of array

Let's say I have a list of arrays with contains as below:
var listArray = new List<string[]>():
1st array = {code, ID_1, PK_1, ID_2, PK_2} //Somehow like a header
2nd array = {85734, 32343, 1, 66544, 2}
3rd array = {59382, 23324, 1, 56998, 2}
4rd array = {43234, 45334, 1, 54568, 2}
and these arrays will be added into 'listArray'.
listArray.Add(array);
what should I do for matching the variable inside the list?
e.g: if ID_1 of the array is '32343', ID_2 = '66544'.
// create
var listArray = new List<string[]>():
string whatIWantToFind = "1234";
string[] mySearchArray = new string[] {"1234", "234234", "324234"};
// fill your array here...
// search
foreach(string[] listItem in listArray)
{
// if you want to check a single item inside...
foreach(string item in listItem)
{
// you can compare
if(item == whatIWantToFind)
{
}
// or check if it contains
if(item.Contains(whatIWantToFind))
{
}
}
// to compare everything..
bool checked = true;
for(int i = 0; i < listItem.lenght; i++)
{
if(!listItem[i].Equals(mySearchArray[i])
{
checked = false; break;
}
}
// aha! this is the one
if(checked) {}
}
If you create a class that contains all the data for one array, you can make a master array of those objects. For instance:
public class ListItem {
public string code, ID_1, PK_1, ID_2, PK_2;
}
And then you can use this class:
var listArray = new List<ListItem>();
listArray.add(new ListItem(){ code = 85734, ID_1 = 32343, PK_1 = 1, ID_2 = 66544, PK_2 = 2});
listArray.add(......);
Then, to find the data, you can use a field accessor on the objects in the array:
foreach(var item in listArray)
{
if (item.ID_1.equals("32343") && item.ID_2.equals("66544"))
Console.WriteLine("Found item.");
}
var listArray = new List<string[]>
{
new []{ "code", "ID_1", "PK_1", "ID_2", "PK_2"},
new []{ "85734", "32343", "1", "66544", "2"},
new []{"59382", "23324", "1", "56998", "2"}
};
var index = listArray.First().ToList().IndexOf("ID_1");
var result = listArray.Where((a, i) => i > 0 && a[index] == "32343").ToList();

C# Google Chart JSON, Not enough columns given to draw the requested chart

I'm trying to get Google Charts working with data from a DataContext (db).
With this code:
public JsonResult getYearChart(clientId)
{
DataDataContext db = new DataDataContext();
var alldata = (from deba in db.debViews
where deba.ClientID == clientId
group deba by deba.Date.Value.Month
into grp
select new
{
Month = grp.Key,
Price = grp.Sum(x => x.Price)
}).ToList();
var cols = new[] { new { Month ="Month", Price ="Total" } };
var ret = new[]
{
new { data = cols.Select(x => new string[] { x.Month, x.Price }) },
new { data = alldata.Select(x => new string[] { x.Month.ToString(), Convert.ToString(x.Price) }) }
};
And returning ret with Json.
This doesn't work, it just gives me "Not enough columns given to draw the requested chart". what would be the best practice to make a google chart dynamic with data from a DataContext?
This is the JavaScript I use to draw the chart with the Json response.
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
$.post('Ex/getYearChart', {},
function (data) {
var tdata = new google.visualization.DataTable();
var rows = data.length;
var cols = data[0].length;
tdata.addColumn('string', data[0][0]);
for (var i = 0; i < cols; i++) {
tdata.addColumn('number', data[0][i]);
}
tdata.addRows(data.length);
for (var i = 1; i < data.length; i++) {
tdata.setCell(i, 0, data[i][0]);
for (var j = 1; j < cols; j++) {
var value = parseInt(data[i][j]);
tdata.setCell(i, j, value);
}
}
var options = {
title: 'Test Chart',
isStacked: true,
width: 500,
height: 400,
vAxis: { title: "More Text" },
hAxis: { title: "Date" }
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(tdata, options);
}
)}
Until you give an example of the contents of data, I can't say what the problem is for certain, but I know that this will cause problems for you, even absent problems with data:
tdata.addColumn('string', data[0][0]);
for (var i = 0; i < cols; i++) {
tdata.addColumn('number', data[0][i]);
}
You should start incrementing i from 1 instead of 0, since you are already adding a column for the first element in each row.

How to create a List of Anonymous types?

I am trying to create a List of Anonymous types as shown below but I am making mistake somewhere
for (int i = 0; i < 10; i++)
{
var list = new[]
{
new { Number = i, Name = string.Concat("name",i) }
};
}
E.g.
var o1 = new { Id = 1, Name = "Name1" };
var o2 = new { Id = 2, Name = "Name2" };
var list = new[] { o1, o2 }.ToList();
how to do the same at runtime?
no error...but the collection is always 1
That is because your are creating a new list in each iteration
You can try it like:
var list = new[] { new { Number = 0, Name = "Name1" } }.ToList(); //just to create a
//list of anonymous type object
list.Clear();
for (int i = 0; i < 10; i++)
{
list.Add(new { Number = i, Name = string.Concat("name",i) });
}
Or one way to do that would be to use List<Object> like:
List<object> list = new List<object>();
for (int i = 0; i < 10; i++)
{
list.Add(new { Number = i, Name = string.Concat("name",i) });
}
Or you can use Enumerable.Range like
var list = Enumerable.Range(0, 10)
.Select(i => new { Number = i, Name = "SomeName" })
.ToList();
Were you thinking of something like the following (using LINQ):
var anonList = Enumerable
.Range(1, 10)
.Select(i => new {
ID = i,
Name = String.Format("Name{0}", i)
});
You could of course replace Enumerable.Range() with anything that give you a list to select from.
You need a List object to store it.
List<Object> o = new List<Object>();
for (int i = 0; i < 10; i++)
{
o.Add(new { Number = i, Name = string.Concat("name",i) });
}
o.Dump();
Define a list first then store the value in For loop
List<Object> NewList = new List<Object>();
for (int i = 0; i < 10; i++)
{
NewList.Add(
{
new { Number = i, Name = string.Concat("name",i) }
});
}
int i = 0;
while(i < 10)
{
list.Add(new { Number = i, Name = string.Concat("name",i) });
i++;
}

Categories

Resources