private void DrawData(Cairo.Context canvas) { int colorIndex = 0; int baseLine = this.DataOrgPosition.Y; int[][] normSeries = this.NormalizeData(); for (int numSerie = 0; numSerie < normSeries.Length; ++numSerie) { var p = this.DataOrgPosition; int numValues = normSeries[numSerie].Length; int xGap = this.GraphWidth / (numValues + 1); // Set drawing colors and sizes colorIndex = colorIndex % SeriesColors.Count(); canvas.LineWidth = this.DataLineWidth; canvas.SetSourceColor(this.SeriesColors[colorIndex++]); // Draw the series for (int i = 0; i < numValues; ++i) { int val = normSeries[numSerie][i]; string tag = this.Values[numSerie][i].ToString(); var nextPoint = new Gdk.Point(p.X + xGap, baseLine - val); if (this.Type == ChartType.Bars) { p = new Gdk.Point(nextPoint.X, baseLine); } this.DrawLine(canvas, p, nextPoint); this.DrawString(canvas, new Gdk.Point(nextPoint.X, nextPoint.Y), tag); p = nextPoint; } canvas.Stroke(); } }
private PlotModel RenderExposure(string selectedChart) { //===== get plot data var chartData = PlotData[selectedChart]; string xLabel = chartData .First() // first row is as good as any .First().Key; // first column is x-axis object xValue = chartData .First() // first row is as good as any .First().Value; // first column is x-axis //===== initialize plot model PlotModel plotModel = new PlotModel(); plotModel.Title = selectedChart; plotModel.LegendPosition = LegendPosition.LeftTop; plotModel.Axes.Clear(); Axis xAxis = xValue.GetType() == typeof(DateTime) ? new DateTimeAxis() : new LinearAxis(); xAxis.Title = xLabel; xAxis.Position = AxisPosition.Bottom; xAxis.Key = "x"; var yAxis = new LinearAxis(); yAxis.Title = "Exposure [%]"; yAxis.Position = AxisPosition.Right; yAxis.Key = "y"; plotModel.Axes.Add(xAxis); plotModel.Axes.Add(yAxis); //===== create series Dictionary <string, AreaSeries> allSeries = new Dictionary <string, AreaSeries>(); foreach (var row in chartData) { xValue = row[xLabel]; foreach (var col in row) { if (col.Key == xLabel) { continue; } if (col.Value.GetType() != typeof(double) || double.IsInfinity((double)col.Value) || double.IsNaN((double)col.Value)) { continue; } string yLabel = col.Key; double yValue = (double)col.Value; if (!allSeries.ContainsKey(yLabel)) { var color = SeriesColors[allSeries.Count % SeriesColors.Count()]; var newSeries = new AreaSeries { Title = yLabel, IsVisible = true, XAxisKey = "x", YAxisKey = "y", Color = color, Fill = color, //ConstantY2 = 0.0, }; allSeries[yLabel] = newSeries; } double yStacked = 0.0; foreach (var col2 in row) { if (col2.Key == xLabel) { continue; } if (col2.Key == col.Key) { break; } yStacked += (double)col2.Value; } allSeries[col.Key].Points.Add(new DataPoint( xValue.GetType() == typeof(DateTime) ? DateTimeAxis.ToDouble(xValue) : (double)xValue, 100.0 * (yStacked + (double)yValue))); allSeries[col.Key].Points2.Add(new DataPoint( xValue.GetType() == typeof(DateTime) ? DateTimeAxis.ToDouble(xValue) : (double)xValue, 100.0 * (yStacked /*+ (double)yValue*/))); } } //===== add series to plot model foreach (var series in allSeries) { plotModel.Series.Add(series.Value); } return(plotModel); }