Using HighCharts with Lambda Expression - c#

I am experiencing trouble getting what I am looking for, in respect to setting series for highcharts. I want to use my table in my database to post the number for the y-axis.
So in my table I have the properties, ID, TeamName, TotalWins.
I only have 2 records
ID = 1, TeamName = Boston Red Sox, TotalWins? = 0 nullable because the MLB season hasn't started yet
ID = 2, TeamName = Baltimore Orioles, TotalWins? = 0
Here is my ActionResult for my Chart:
public ActionResult Chart()
{
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Pie })
.SetTitle(new Title { Text = "Who Has more Wins?" })
.SetSubtitle(new Subtitle { Text = "Source: Sportscenter" })
.SetXAxis(new XAxis
{
Categories = new[] { "Boston Red Sox", "Baltimore Orioles" },
Title = new XAxisTitle { Text = "Teams" }
})
.SetYAxis(new YAxis
{
Min = 0,
Title = new YAxisTitle
{
Text = "Wins (Game)",
Align = AxisTitleAligns.High
}
})
.SetTooltip(new Tooltip { Formatter = "function() { return ''+ this.series.name +': '+ this.y +' millions'; }" })
.SetPlotOptions(new PlotOptions
{
Bar = new PlotOptionsBar
{
DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
}
})
.SetLegend(new Legend
{
Layout = Layouts.Horizontal,
Align = HorizontalAligns.Right,
VerticalAlign = VerticalAligns.Top,
X = -100,
Y = 100,
Floating = true,
BorderWidth = 1,
BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
Shadow = true
})
.SetCredits(new Credits { Enabled = false })
.SetSeries(new[]
{
new Series { Data = new Data(new object[] { db.Teams.Where(x => x.TeamName == "Boston Red Sox").Count(x => x.TotalWins) /*where the issue is */ }) },
});
return View(chart);
}
cannot implicitly convert type 'int?' to 'bool'
How do I set this lambda expression so that it retrieves the total wins for the boston red sox and then again for the baltimore orioles?

The problem is here:
.Count(x => x.TotalWins)
Count either takes no arguments (in which case it returns the total count of the results of the preceding query), or a lambda that returns a boolean expression, in which case it returns a count of items that meet the criteria.
Do you mean .Sum(x => x.TotalWins)?

Related

C# sort object list with start position and loop

I have a strange question :)
I have a object list looking like this:
var list = new []
{
new { Id = 1, Name = "Marcus" },
new { Id = 2, Name = "Mattias" },
new { Id = 3, Name = "Patric" },
new { Id = 4, Name = "Theodor" },
};
I would like to sort the list providing a "start id"
For example, if I provide "start id" 3, the result should look like this:
Id
Name
3
Patric
4
Theodor
1
Marcus
2
Mattias
I have no idea where to start, so I really need some help from you coding gods
The list is from a sql table, but it does not matter for me where the sort take place (in sql query or in c# code)
Try this:
var list = new []
{
new { Id = 1, Name = "Marcus" },
new { Id = 2, Name = "Mattias" },
new { Id = 3, Name = "Patric" },
new { Id = 4, Name = "Theodor" },
};
var start_id = 3;
var max_id = list.Max(y => y.Id);
var result =
from x in list
orderby (x.Id + max_id - start_id) % max_id
select x;
I get:
With LINQ to objects you can do something like that:
var list = new []
{
new { Id = 1, Name = "Marcus" },
new { Id = 2, Name = "Mattias" },
new { Id = 3, Name = "Patric" },
new { Id = 4, Name = "Theodor" },
};
var startId = 3;
var result = list
.GroupBy(i => i.Id >= startId ? 1 : 0) // split in two groups
.OrderByDescending(g => g.Key) // sort to have the group with startId first
.Select(g => g.OrderBy(i => i.Id)) // sort each group
.SelectMany(i => i) // combine result
.ToList();
Console.WriteLine(string.Join(", ", result.Select(i => i.Id))); // prints "3, 4, 1, 2"
You require 2 criteria to apply:
Order ascending by Id.
Return the Ids greater than threshold before the Ids less than threshold.
You can try:
var offset = 3;
var sorted1 = list
.OrderBy(item => item.Id < offset)
.ThenBy(item => item.Id);
The OrderBy condition yields true if Id is less than offset and false otherwise.
true is greater than false and therefore is returned later
A dirty way could also be:
var offset = 3;
var sorted2 = list
.OrderBy(item => unchecked((uint)(item.Id - offset)));
Here the offset is subtracted from Id and the result converted to unsigned int to make the negative values become very large positive ones. A little hacky. Might not work with queries against SQL providers.
Here's a toy Non-Linq Version
object[] ShiftList(int id)
{
var list = new dynamic[]
{
new { Id = 1, Name = "Marcus" },
new { Id = 2, Name = "Mattias" },
new { Id = 3, Name = "Patric" },
new { Id = 4, Name = "Theodor" },
};
Span<dynamic> listSpan = list;
int indexFound = -1;
for (int i = 0; i < list.Length; i++)
{
if (listSpan[i].Id == id)
{
indexFound = i;
}
}
if (indexFound is -1)
{
return list;
}
var left = listSpan.Slice(0, indexFound);
var right = listSpan[indexFound..];
object[] objs = new object[list.Length];
Span<object> objSpan = objs;
right.CopyTo(objSpan);
left.CopyTo(objSpan[right.Length..]);
return objs;
}
Try using foreach and iterate over each object in your list:
foreach (var item in list)
{
}
from here you should be able to use some of the collection methods for a list to reorder your list.

Chartsjs (blazor) BarChart not alinged

Im not sure im doing something fairly simple wrong. Im getting below plot when using the below code. I was expecting to get the B values in its own column like you would in excel.
EDIT: I have added my config in the post also, if there are some properties that im missing
/Thomas
BarDataset<double> _barDataSet3 = new BarDataset<double>
{
Label = "A",
BackgroundColor = ColorUtil.RandomColorString(),
BorderWidth = 0,
HoverBackgroundColor = ColorUtil.RandomColorString(),
HoverBorderColor = ColorUtil.RandomColorString(),
HoverBorderWidth = 1,
BorderColor = "#ffffff"
};
_barChartConfig.Data.Labels.AddRange(new[] { "A"});
_barDataSet3.Add(2.6);
_barChartConfig.Data.Datasets.Add(_barDataSet3);
BarDataset<double> _barDataSet4 = new BarDataset<double>
{
Label = "B",
BackgroundColor = ColorUtil.RandomColorString(),
BorderWidth = 0,
HoverBackgroundColor = ColorUtil.RandomColorString(),
HoverBorderColor = ColorUtil.RandomColorString(),
HoverBorderWidth = 1,
BorderColor = "#ffffff"
};
_barChartConfig.Data.Labels.AddRange(new[] { "B" });
_barDataSet4.Add(4.5);
_barChartConfig.Data.Datasets.Add(_barDataSet4);
EDIT: My config - is there a property that im missing?:
_barChartConfig = new BarConfig
{
Options = new BarOptions
{
Title = new OptionsTitle
{
Display = true,
Text = "Simple Bar Chart"
},
Scales = new BarScales
{
XAxes = new List<CartesianAxis>
{
new BarCategoryAxis
{
BarPercentage = 0.5,
BarThickness = BarThickness.Flex
}
},
YAxes = new List<CartesianAxis>
{
new BarLinearCartesianAxis
{
Ticks = new LinearCartesianTicks
{
BeginAtZero = true
}
}
}
}
}
};

Using Viewmodel and datepicker with Dotnet Highchart in MVC

I am using Dotnet Highchart that is based on C# code that has output code javascript.
Dotnet HighChart
This is almost how my chart looks like:
I am using data from my database to give this chart data by using LINQ queries.
I have a column "Completed Date" in my entity and I need to make two textboxes with datepicker so user can choose Start and end dates of Completed Date.Then the user should be able to click on a submit button and then the chart should update and show the statistic between the two dates. I have the complete LINQ Query for this
This is the LINQ query that I am going to use:
public List<CoreValueAndAverageGrade> GetAverageGradeForAllCoreValues(DateTime startDate, DateTime endDate)
{
return db.CoreValue
.Where(coreValue => coreValue.CoreValueQuestion
.Any(coreValueQuestion => coreValueQuestion.SubjectType.Ignored_Statistic == false))
.Select(coreValue => new CoreValueAndAverageGrade
{
CoreValue = coreValue,
AverageGrade = coreValue.CoreValueQuestion
.Where(coreValueQuestion => coreValueQuestion.SubjectType.Ignored_Statistic == false)
.Average(coreValueQuestion => coreValueQuestion.SelectedQuestions
.Where(selectedQuestion => selectedQuestion.GoalCardQuestionAnswer != null
&& selectedQuestion.GoalCardQuestionAnswer.Grade.HasValue
&& selectedQuestion.GoalCard.Completed_Date >= startDate
&& selectedQuestion.GoalCard.Completed_Date <= endDate
)
.Average(selectedQuestion => selectedQuestion.GoalCardQuestionAnswer.Grade.Value))
})
.ToList();
}
This is my ViewModel
public class OfficeStatisticNKIViewModel
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
In my GET action method I have following code:
public ActionResult OfficeStatistic()
{
{
OfficeStatisticNKIViewModel model = new OfficeStatisticNKIViewModel();
model.EndDate = DateTime.Now;
model.StartDate = DateTime.Now;
var averageGrades = OfficeStatisticRepository.GetAverageGradeForAllCoreValues(model.StartDate, model.EndDate);
var dataItems = (averageGrades.Select(averageGrade => averageGrade.AverageGrade).ToArray());
Data data = new Data(
dataItems.Select(y => new Point {Color = GetBarColour(y), Y = y}).ToArray());
Highcharts chart1 = new Highcharts("Chart")
.SetXAxis(new XAxis { Categories = averageGrades.Select(averageGrade => averageGrade.CoreValue.Name).ToArray() })
.SetYAxis(new YAxis { Min = 0, Max = 10, TickInterval = 1, Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetLegend(new Legend { Enabled = false })
.SetTitle(new Title { Text = "Örebro Statistik", })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });
return View(new Container(new[] { chart1 }));
}
The problem is that I have no idea how to do the post action method, The chart needs to update with the new the statistic, do I need some kind of ajax or can I do this on the serverside?
Any kind of help or tips is very appreciated!
Thanks in advance!
http://dotnethighcharts.codeplex.com/discussions/350721/

Change color of bars depending on value in Highchart bar-chart with MVC3

I am using Dotnet Highchart with MVC3
I am currently working with a diagram that looks like this:
I am trying to modify my code so I can change color on the bars depending on what number they have. I also wonder how I can remove the button "Snittbetyg" as you see can on the image.
This is my code:
public ActionResult OfficeStatistic()
{
{
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = new Data(new object[] { 1, 8, 9, 6 }), Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });
return View(chart1);
}
}
Any kind of help is appreciated.
Thanks in advance!
I haven't used Highchart but you can download examples from their codeplex page. It looks like both of your requirements can be achieved easily.
Remove the "Snittbetyg" button
Disable the legend:
.SetLegend(new Legend { Enabled = false });
Add Colours
For the series data use points instead of just the numbers:
Data data = new Data(new[]
{
new Point { Y = 1, Color = System.Drawing.Color.Red },
new Point { Y = 8, Color = System.Drawing.Color.Blue },
new Point { Y = 9, Color = System.Drawing.Color.Green },
new Point { Y = 6, Color = System.Drawing.Color.Black }
});
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetLegend(new Legend { Enabled = false });
There doesn't seem to be a built in way to make highchart automatically colour the bar based on the y-value. I believe you would have to pick the colour yourself, e.g:
private System.Drawing.Color GetBarColour(int value)
{
if (value < 5) return System.Drawing.Color.Red;
if (value > 7) return System.Drawing.Color.Green;
return System.Drawing.Color.Orange;
}
public ActionResult OfficeStatistic()
{
{
var dataItems = new[] {1, 8, 9, 6};
Data data = new Data(
dataItems.Select(y => new Point {Color = GetBarColour(y), Y = y}).ToArray()
);
Highcharts chart1 = new Highcharts("chart1")
.SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
.SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
.SetSeries(new Series { Data = data, Name = "Snittbetyg" })
.SetTitle(new Title { Text = "Örebro Statistik" })
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetLegend(new Legend { Enabled = false });
First, define a Tuple list first item is for color and second item point value
List<Tuple<string, Object>> dataItems = new List<Tuple<string, Object>>();
i am passing value with swtich it is not neccessary
SqlDataReader reader = myComm.ExecuteReader();
if (reader.HasRows)
{
string colorName ="";
while (reader.Read())
{
switch ((string)reader.GetValue(1))
{
case "Total Employee(s)":
colorName = "Blue";
break;
case "Present":
colorName = "Green";
break;
case "Late":
case"Absent":
case "During Less":
case "Early Going":
colorName = "Red";
break;
case "Leave":
colorName = "Orange";
break;
default:
colorName = "Gray";
break;
}
dataItems.Add(new Tuple<string, Object>(colorName, reader.GetValue(2)));
}
Now, Finally add Data into series object
new Series{
Name = "Employees",
Data = new Data(
dataItems.Select(y => new Point {
Color = System.Drawing.Color.FromName(y.Item1),
Y = (int)y.Item2 }).ToArray()
)
}

How to write a LINQ query combining group by and aggregates?

Given the following input, how do I write a LINQ query or expression to return an aggregated result set for the quantity?
Input:
var foo = new[] { new { PO = "1", Line = 2, QTY = 0.5000 },
new { PO = "1", Line = 2, QTY = 0.2500 },
new { PO = "1", Line = 2, QTY = 0.1000 },
new { PO = "1", Line = 2, QTY = -0.1000 }
}.ToList();
Desired result:
Something along the lines of
new { PO = "1", Line = 2, QTY = 0.7500 } // .5 + .25 + .1 + -.1
How would I write it for multiple lines as well (see the object model in foo)?
How about this:
var result = foo.GroupBy(x => x.Line)
.Select(g => new { PO = g.First().PO,
Line = g.Key,
QTY = g.Sum(x => x.QTY) });
In the case you just have one Line, just add a .Single() - result is an IEnumerable of the anonymous type defined when you set up foo.
Edit:
If both PO and Line should designate different groups (PO can have different values), they both have to be part of the group key:
var result = foo.GroupBy(x => new { x.PO, x.Line})
.Select(g => new {
PO = g.Key.PO,
Line = g.Key.Line,
QTY = g.Sum(x => x.QTY)
});
var query = (from t in foo
group t by new {t.PO, t.Line}
into grp
select new
{
grp.Key.PO,
grp.Key.Line,
QTY = grp.Sum(t => t.QTY)
}).ToList()

Categories

Resources