/// <summary> /// Takes two lines and creates a polyon between them /// </summary> /// <param name="baseLine"></param> /// <param name="topLine"></param> /// <returns></returns> public static ChartPrimitive LineDiffToPolygon(ChartPrimitive baseLine, ChartPrimitive topLine) { var polygon = new ChartPrimitive(); if (baseLine != null && topLine != null) { IList <Point> baseLinePoints = baseLine.Points; IList <Point> topLinePoints = topLine.Points; for (int pointNo = baseLinePoints.Count - 1; pointNo >= 0; --pointNo) { polygon.AddPoint(baseLinePoints[pointNo]); } for (int pointNo = 0; pointNo < topLinePoints.Count; ++pointNo) { polygon.AddPoint(topLinePoints[pointNo]); } } polygon.Filled = true; return(polygon); }
/// <summary> /// Gets ChartLines and ChartPolygons for the population line, and /// the target line. /// </summary> /// <param name="populationLine"></param> /// <param name="results"></param> /// <param name="color"></param> /// <param name="label"></param> /// <returns></returns> public static LineAndPolygon ConvertResultsToTargetLineAndPolygon(ChartPrimitive populationLine, float[] results, Color color, string label) { // Calculate Target Primitives var targetLine = new ChartPrimitive(); targetLine.Color = color; targetLine.Dashed = true; targetLine.Label = label + " Target"; targetLine.ShowInLegend = false; targetLine.HitTest = true; if (populationLine.Points.Count == results.Length) { for (int monthNo = 0; monthNo < results.Length; monthNo += 2) { targetLine.AddPoint(new Point(monthNo * .5f, results[monthNo])); targetLine.AddPoint(new Point(monthNo * .5f + 1f, results[monthNo + 1])); } } else { for (int monthNo = 0; monthNo < results.Length; ++monthNo) { targetLine.AddPoint(new Point(monthNo, results[monthNo])); targetLine.AddPoint(new Point(monthNo + 1f, results[monthNo])); } } ChartPrimitive targetPolygon = ChartUtilities.LineDiffToPolygon(populationLine, targetLine); color.A = (byte)(alpha * color.A); targetPolygon.Color = color; targetPolygon.Dashed = true; targetPolygon.ShowInLegend = false; targetLine.HitTest = false; return(new LineAndPolygon(targetLine, targetPolygon)); }
/// <summary> /// Converts population level to a line and polygon /// </summary> /// <param name="results"></param> /// <param name="color"></param> /// <param name="label"></param> /// <returns></returns> public static LineAndPolygon ConvertResultsToPopulationLineAndPolygon(float[] results, Color color, string label) { var populationLine = new ChartPrimitive(); populationLine.Color = color; populationLine.Label = label; populationLine.ShowInLegend = true; populationLine.HitTest = true; for (int monthNo = 0; monthNo < results.Length; monthNo += 2) { populationLine.AddPoint(new Point(monthNo * .5f, results[monthNo])); populationLine.AddPoint(new Point(monthNo * .5f + 1f, results[monthNo + 1])); } ChartPrimitive populationPolygon = ChartUtilities.ChartLineToBaseLinedPolygon(populationLine); color.A = (byte)(alpha * color.A); populationPolygon.Color = color; populationPolygon.ShowInLegend = false; populationPolygon.HitTest = false; return(new LineAndPolygon(populationLine, populationPolygon)); }
//GetPlotRectangle /// <summary> /// Converts a ChartLine object to a ChartPolygon object that has /// one edge along the bottom Horizontal base line in the plot. /// </summary> /// <param name="chartLine"></param> /// <returns></returns> public static ChartPrimitive ChartLineToBaseLinedPolygon(ChartPrimitive chartLine) { ChartPrimitive chartPolygon = chartLine != null?chartLine.Clone() : new ChartPrimitive(); if (chartPolygon.Points.Count > 0) { Point firstPoint = chartPolygon.Points[0]; firstPoint.Y = 0; Point lastPoint = chartPolygon.Points[chartPolygon.Points.Count - 1]; lastPoint.Y = 0; chartPolygon.InsertPoint(firstPoint, 0); chartPolygon.AddPoint(lastPoint); } chartPolygon.Filled = true; return(chartPolygon); }
/// <summary> /// Gets ChartLines and ChartPolygons for the population line, and /// the target line. /// </summary> /// <param name="populationLine"></param> /// <param name="results"></param> /// <param name="color"></param> /// <param name="label"></param> /// <returns></returns> public static LineAndPolygon ConvertResultsToTargetLineAndPolygon(ChartPrimitive populationLine, float[] results, Color color, string label) { // Calculate Target Primitives var targetLine = new ChartPrimitive(); targetLine.Color = color; targetLine.Dashed = true; targetLine.Label = label + " Target"; targetLine.ShowInLegend = false; targetLine.HitTest = true; if (populationLine.Points.Count == results.Length) { for (int monthNo = 0; monthNo < results.Length; monthNo += 2) { targetLine.AddPoint(new Point(monthNo*.5f, results[monthNo])); targetLine.AddPoint(new Point(monthNo*.5f + 1f, results[monthNo + 1])); } } else { for (int monthNo = 0; monthNo < results.Length; ++monthNo) { targetLine.AddPoint(new Point(monthNo, results[monthNo])); targetLine.AddPoint(new Point(monthNo + 1f, results[monthNo])); } } ChartPrimitive targetPolygon = ChartUtilities.LineDiffToPolygon(populationLine, targetLine); color.A = (byte) (alpha*color.A); targetPolygon.Color = color; targetPolygon.Dashed = true; targetPolygon.ShowInLegend = false; targetLine.HitTest = false; return new LineAndPolygon(targetLine, targetPolygon); }
/// <summary> /// Adds a set of lines to the chart for test purposes /// </summary> /// <param name="xyLineChart"></param> public static void AddTestLines(XYLineChart xyLineChart) { if (xyLineChart == null) return; // Add test Lines to demonstrate the control xyLineChart.Primitives.Clear(); double limit = 5; double increment = .01; // Create 3 normal lines var lines = new ChartPrimitive[3]; for (int lineNo = 0; lineNo < 3; ++lineNo) { var line = new ChartPrimitive(); // Label the lines line.Filled = true; line.Dashed = false; line.ShowInLegend = false; line.AddPoint(0, 0); // Draw 3 sine curves for (double x = 0; x < limit + increment*.5; x += increment) { line.AddPoint(x, Math.Cos(x*Math.PI - lineNo*Math.PI/1.5)); } line.AddPoint(limit, 0); // Add the lines to the chart xyLineChart.Primitives.Add(line); lines[lineNo] = line; } // Set the line colors to Red, Green, and Blue lines[0].Color = Color.FromArgb(90, 255, 0, 0); lines[1].Color = Color.FromArgb(90, 0, 180, 0); lines[2].Color = Color.FromArgb(90, 0, 0, 255); for (int lineNo = 0; lineNo < 3; ++lineNo) { var line = new ChartPrimitive(); // Label the lines line.Label = "Test Line " + (lineNo + 1); line.ShowInLegend = true; line.HitTest = true; line.LineThickness = 1.5; // Draw 3 sine curves for (double x = 0; x < limit + increment*.5; x += increment) { line.AddPoint(x, Math.Cos(x*Math.PI - lineNo*Math.PI/1.5)); } // Add the lines to the chart xyLineChart.Primitives.Add(line); lines[lineNo] = line; } // Set the line colors to Red, Green, and Blue lines[0].Color = Colors.Red; lines[1].Color = Colors.Green; lines[2].Color = Colors.Blue; xyLineChart.Title = "Test Chart Title"; xyLineChart.XAxisLabel = "Test Chart X Axis"; xyLineChart.YAxisLabel = "Test Chart Y Axis"; xyLineChart.RedrawPlotLines(); }
/// <summary> /// Takes two lines and creates a polyon between them /// </summary> /// <param name="baseLine"></param> /// <param name="topLine"></param> /// <returns></returns> public static ChartPrimitive LineDiffToPolygon(ChartPrimitive baseLine, ChartPrimitive topLine) { var polygon = new ChartPrimitive(); if (baseLine != null && topLine != null) { IList<Point> baseLinePoints = baseLine.Points; IList<Point> topLinePoints = topLine.Points; for (int pointNo = baseLinePoints.Count - 1; pointNo >= 0; --pointNo) { polygon.AddPoint(baseLinePoints[pointNo]); } for (int pointNo = 0; pointNo < topLinePoints.Count; ++pointNo) { polygon.AddPoint(topLinePoints[pointNo]); } } polygon.Filled = true; return polygon; }
/// <summary> /// Adds a set of lines to the chart for test purposes /// </summary> /// <param name="xyLineChart"></param> public static void AddTestLines(XYLineChart xyLineChart) { if (xyLineChart == null) { return; } // Add test Lines to demonstrate the control xyLineChart.Primitives.Clear(); double limit = 5; double increment = .01; // Create 3 normal lines var lines = new ChartPrimitive[3]; for (int lineNo = 0; lineNo < 3; ++lineNo) { var line = new ChartPrimitive(); // Label the lines line.Filled = true; line.Dashed = false; line.ShowInLegend = false; line.AddPoint(0, 0); // Draw 3 sine curves for (double x = 0; x < limit + increment * .5; x += increment) { line.AddPoint(x, Math.Cos(x * Math.PI - lineNo * Math.PI / 1.5)); } line.AddPoint(limit, 0); // Add the lines to the chart xyLineChart.Primitives.Add(line); lines[lineNo] = line; } // Set the line colors to Red, Green, and Blue lines[0].Color = Color.FromArgb(90, 255, 0, 0); lines[1].Color = Color.FromArgb(90, 0, 180, 0); lines[2].Color = Color.FromArgb(90, 0, 0, 255); for (int lineNo = 0; lineNo < 3; ++lineNo) { var line = new ChartPrimitive(); // Label the lines line.Label = "Test Line " + (lineNo + 1); line.ShowInLegend = true; line.HitTest = true; line.LineThickness = 1.5; // Draw 3 sine curves for (double x = 0; x < limit + increment * .5; x += increment) { line.AddPoint(x, Math.Cos(x * Math.PI - lineNo * Math.PI / 1.5)); } // Add the lines to the chart xyLineChart.Primitives.Add(line); lines[lineNo] = line; } // Set the line colors to Red, Green, and Blue lines[0].Color = Colors.Red; lines[1].Color = Colors.Green; lines[2].Color = Colors.Blue; xyLineChart.Title = "Test Chart Title"; xyLineChart.XAxisLabel = "Test Chart X Axis"; xyLineChart.YAxisLabel = "Test Chart Y Axis"; xyLineChart.RedrawPlotLines(); }
private static void AppendLine(ChartPrimitive graph, double x, float y) { graph.AddPoint(new Point(x, y)); if (graph.Points.Count > MaxPlots) graph.Points.RemoveAt(0); }
private void updateGraph() { if (cmbGraphStat.SelectedIndex == -1 || cmbTeam.SelectedIndex == -1 || cmbGraphInterval.SelectedIndex == -1) { clearGraph(); return; } var intervalItem = cmbGraphInterval.SelectedItem.ToString(); var yearlyRows = _dtYea.Rows.Cast<DataRow>().Where(dr => dr[0].ToString().StartsWith("Season")).ToList(); var monthlyStats = MainWindow.SplitTeamStats[_curts.ID].Where(pair => pair.Key.StartsWith("M ")).ToList(); var orderedBSEList = _bseList.OrderBy(bse => bse.BS.GameDate).ToList(); Intervals interval; int count; switch (intervalItem) { case "Every Game": interval = Intervals.EveryGame; count = orderedBSEList.Count; break; case "Monthly": interval = Intervals.Monthly; count = monthlyStats.Count; break; case "Yearly": interval = Intervals.Yearly; count = yearlyRows.Count; break; default: throw new ArgumentOutOfRangeException(); } if (count < 2) { clearGraph(); return; } var propToGet = cmbGraphStat.SelectedItem.ToString(); propToGet = propToGet.Replace('3', 'T'); propToGet = propToGet.Replace('%', 'p'); propToGet = propToGet.Replace("TO", "TOS"); double sum = 0; double games = 0; chart.Primitives.Clear(); var cp = new ChartPrimitive { Label = cmbGraphStat.SelectedItem.ToString(), ShowInLegend = false }; switch (interval) { case Intervals.EveryGame: for (var i = 0; i < count; i++) { var bse = orderedBSEList[i]; bse.BS.PrepareForDisplay(_tst, _curts.ID); var isTeamAway = bse.BS.Team1ID == _curts.ID; var propToGetFinal = propToGet; if (propToGet == "PF") { propToGetFinal = isTeamAway ? "PTS1" : "PTS2"; } else if (propToGet == "PA") { propToGetFinal = isTeamAway ? "PTS2" : "PTS1"; } else { propToGetFinal += (isTeamAway ? 1 : 2).ToString(); } var value = bse.BS.GetValue<TeamBoxScore, double>(propToGetFinal); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; case Intervals.Monthly: monthlyStats = monthlyStats.OrderBy(ms => ms.Key).ToList(); if (TeamStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = TeamStatsHelper.TotalsToPerGame[propToGet]; } for (var i = 0; i < count; i++) { var ts = monthlyStats[i].Value; ts.CalcMetrics(new TeamStats()); var tsr = new TeamStatsRow(ts); var value = tsr.GetValue<double>(propToGet); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; case Intervals.Yearly: if (TeamStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = TeamStatsHelper.TotalsToPerGame[propToGet]; } for (var i = 0; i < count; i++) { var ts = new TeamStats(); createTeamStatsFromDataRow(ref ts, _dtYea.Rows.Cast<DataRow>().ToList()[i]); ts.CalcMetrics(new TeamStats()); var tsr = new TeamStatsRow(ts); var value = tsr.GetValue<TeamStatsRow, double>(propToGet); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; default: throw new ArgumentOutOfRangeException(); } if (cp.Points.Count > 0) { chart.Primitives.Add(cp); } if (chart.Primitives.Count > 0 && chart.Primitives.Sum(p => p.Points.Count) > 1) { //var average = sum / games; if (TeamStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = TeamStatsHelper.TotalsToPerGame[propToGet]; } double average; switch (interval) { case Intervals.EveryGame: case Intervals.Monthly: average = _curTSR.GetValue<double>(propToGet); break; case Intervals.Yearly: var ts = new TeamStats(); createTeamStatsFromDataRow(ref ts, _dtYea.Rows.Cast<DataRow>().ToList().Last()); ts.CalcMetrics(new TeamStats()); var tsr = new TeamStatsRow(ts); average = tsr.GetValue<double>(propToGet); break; default: throw new ArgumentOutOfRangeException(); } var cpavg = new ChartPrimitive(); for (var i = 0; i < count; i++) { cpavg.AddPoint(i + 1, average); } cpavg.Color = Color.FromRgb(0, 0, 100); cpavg.Dashed = true; cpavg.ShowInLegend = false; var cp2 = new ChartPrimitive(); cp2.AddPoint(chart.Primitives.First().Points.First().X, 0); cp2.AddPoint(chart.Primitives.Last().Points.Last().X, 1); chart.Primitives.Add(cpavg); chart.RedrawPlotLines(); chart.Primitives.Add(cp2); } else { chart.RedrawPlotLines(); var cp2 = new ChartPrimitive(); cp2.AddPoint(1, 0); cp2.AddPoint(2, 1); chart.Primitives.Add(cp2); } chart.ResetPanAndZoom(); }
/// <summary> /// Handles the SelectionChanged event of the cmbGraphStat control. /// Calculates and displays the player's performance graph for the newly selected stat. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e"> /// The <see cref="SelectionChangedEventArgs" /> instance containing the event data. /// </param> private void cmbGraphStat_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (cmbGraphStat.SelectedIndex == -1 || cmbTeam.SelectedIndex == -1 || cmbPlayer.SelectedIndex == -1 || _pbsList.Count < 1) { return; } var cp = new ChartPrimitive(); double i = 0; string propToGet = cmbGraphStat.SelectedItem.ToString(); propToGet = propToGet.Replace('3', 'T'); propToGet = propToGet.Replace('%', 'p'); double sum = 0; double games = 0; foreach (PlayerBoxScore pbs in _pbsList) { i++; double value = Convert.ToDouble(typeof (PlayerBoxScore).GetProperty(propToGet).GetValue(pbs, null)); if (!double.IsNaN(value)) { if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value*1000))/1000; } cp.AddPoint(i, value); games++; sum += value; } } cp.Label = cmbGraphStat.SelectedItem.ToString(); cp.ShowInLegend = false; chart.Primitives.Clear(); if (cp.Points.Count > 0) { double average = sum/games; var cpavg = new ChartPrimitive(); for (int j = 1; j <= i; j++) { cpavg.AddPoint(j, average); } cpavg.Color = Color.FromRgb(0, 0, 100); cpavg.Dashed = true; cpavg.ShowInLegend = false; chart.Primitives.Add(cpavg); chart.Primitives.Add(cp); } chart.RedrawPlotLines(); var cp2 = new ChartPrimitive(); cp2.AddPoint(1, 0); cp2.AddPoint(i, 1); chart.Primitives.Add(cp2); chart.ResetPanAndZoom(); }
/// <summary> /// Converts population level to a line and polygon /// </summary> /// <param name="results"></param> /// <param name="color"></param> /// <param name="label"></param> /// <returns></returns> public static LineAndPolygon ConvertResultsToPopulationLineAndPolygon(float[] results, Color color, string label) { var populationLine = new ChartPrimitive(); populationLine.Color = color; populationLine.Label = label; populationLine.ShowInLegend = true; populationLine.HitTest = true; for (int monthNo = 0; monthNo < results.Length; monthNo += 2) { populationLine.AddPoint(new Point(monthNo*.5f, results[monthNo])); populationLine.AddPoint(new Point(monthNo*.5f + 1f, results[monthNo + 1])); } ChartPrimitive populationPolygon = ChartUtilities.ChartLineToBaseLinedPolygon(populationLine); color.A = (byte) (alpha*color.A); populationPolygon.Color = color; populationPolygon.ShowInLegend = false; populationPolygon.HitTest = false; return new LineAndPolygon(populationLine, populationPolygon); }
private void updateGraph() { if (cmbGraphStat.SelectedIndex == -1 || cmbGraphInterval.SelectedIndex == -1 || cmbTeam.SelectedIndex == -1 || cmbPlayer.SelectedIndex == -1) { clearGraph(); return; } var intervalItem = cmbGraphInterval.SelectedItem.ToString(); var yearlyStats = _yearlyPSRList.Where(psr => psr.Type.StartsWith("Season ")).ToList(); var monthlyStats = MainWindow.SplitPlayerStats[_psr.ID].Where(pair => pair.Key.StartsWith("M ")).ToList(); int count; Intervals interval; switch (intervalItem) { case "Every Game": interval = Intervals.EveryGame; count = _pbsListWithOut.Count; break; case "Monthly": interval = Intervals.Monthly; count = monthlyStats.Count; break; case "Yearly": interval = Intervals.Yearly; count = yearlyStats.Count; break; default: throw new ArgumentOutOfRangeException(); } if (count < 2) { clearGraph(); return; } var propToGet = cmbGraphStat.SelectedItem.ToString(); propToGet = propToGet.Replace('3', 'T'); propToGet = propToGet.Replace('%', 'p'); propToGet = propToGet.Replace("TO", "TOS"); double sum = 0; double games = 0; chart.Primitives.Clear(); var cp = new ChartPrimitive { Label = cmbGraphStat.SelectedItem.ToString(), ShowInLegend = false }; switch (interval) { case Intervals.EveryGame: var orderedPBSList = _pbsListWithOut.OrderBy(pbs => pbs.RealDate).ToList(); for (var i = 0; i < orderedPBSList.Count; i++) { var pbs = orderedPBSList[i]; if (pbs.IsOut) { if (cp.Points.Count > 0) { chart.Primitives.Add(cp.CustomClone()); cp = new ChartPrimitive { Label = cmbGraphStat.SelectedItem.ToString(), ShowInLegend = false }; } continue; } var value = Convert.ToDouble(typeof(PlayerBoxScore).GetProperty(propToGet).GetValue(pbs, null)); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; case Intervals.Monthly: case Intervals.Yearly: if (interval == Intervals.Monthly) { monthlyStats = monthlyStats.OrderBy(ms => ms.Key).ToList(); } if (PlayerStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = PlayerStatsHelper.TotalsToPerGame[propToGet]; } for (var i = 0; i < count; i ++) { var ps = interval == Intervals.Monthly ? monthlyStats[i].Value : new PlayerStats(yearlyStats[i]); if (ps.Totals[PAbbrT.GP] == 0) { if (cp.Points.Count > 0) { chart.Primitives.Add(cp.CustomClone()); cp = new ChartPrimitive { Label = cmbGraphStat.SelectedItem.ToString(), ShowInLegend = false }; } continue; } var dummyTs = new TeamStats(); ps.CalcMetrics(dummyTs, dummyTs, dummyTs, GmScOnly: true); var psr = new PlayerStatsRow(ps, calcRatings: false); var value = psr.GetValue<double>(propToGet); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; default: throw new ArgumentOutOfRangeException(); } if (cp.Points.Count > 0) { chart.Primitives.Add(cp.CustomClone()); } if (chart.Primitives.Count > 0 && chart.Primitives.Sum(p => p.Points.Count) > 1) { if (PlayerStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = PlayerStatsHelper.TotalsToPerGame[propToGet]; } double average; switch (interval) { case Intervals.EveryGame: case Intervals.Monthly: average = _psr.GetValue<double>(propToGet); break; case Intervals.Yearly: average = _yearlyPSRList.Last().GetValue<double>(propToGet); break; default: throw new ArgumentOutOfRangeException(); } //var average = sum / games; var cpavg = new ChartPrimitive(); for (var i = 0; i < count; i++) { cpavg.AddPoint(i + 1, average); } cpavg.Color = Color.FromRgb(0, 0, 100); cpavg.Dashed = true; cpavg.ShowInLegend = false; var cp2 = new ChartPrimitive(); cp2.AddPoint(chart.Primitives.First().Points.First().X, 0); cp2.AddPoint(chart.Primitives.Last().Points.Last().X, 1); chart.Primitives.Add(cpavg); chart.RedrawPlotLines(); chart.Primitives.Add(cp2); } else { chart.RedrawPlotLines(); var cp2 = new ChartPrimitive(); cp2.AddPoint(1, 0); cp2.AddPoint(2, 1); chart.Primitives.Add(cp2); } chart.ResetPanAndZoom(); }