protected override void CreateEmptyOption() { if (emptyOption != null) { return; } UIBarOption option = new UIBarOption(); option.Title = new UITitle(); option.Title.Text = "SunnyUI"; option.Title.SubText = "BarChartEx"; //设置Legend option.Legend = new UILegend(); option.Legend.Orient = UIOrient.Horizontal; option.Legend.Top = UITopAlignment.Top; option.Legend.Left = UILeftAlignment.Left; option.Legend.AddData("Bar1"); option.Legend.AddData("Bar2"); option.Legend.AddData("Bar3"); var series = new UIBarSeries(); series.Name = "Bar1"; series.AddData(1); option.Series.Add(series); series = new UIBarSeries(); series.Name = "Bar2"; series.AddData(2); series.AddData(3); option.Series.Add(series); series = new UIBarSeries(); series.Name = "Bar2"; series.AddData(4); series.AddData(5); series.AddData(6); option.Series.Add(series); option.XAxis.Data.Add("Mon"); option.XAxis.Data.Add("Tue"); option.XAxis.Data.Add("Wed"); option.XAxis.Data.Add("Thu"); option.XAxis.Data.Add("Fri"); option.ToolTip = new UIBarToolTip(); option.ToolTip.AxisPointer.Type = UIAxisPointerType.Shadow; emptyOption = option; }
protected override void CalcData(UIOption option) { Bars.Clear(); NeedDraw = false; UIBarOption o = (UIBarOption)option; if (o == null || o.Series == null || o.SeriesCount == 0) { return; } DrawOrigin = new Point(BarOption.Grid.Left, Height - BarOption.Grid.Bottom); DrawSize = new Size(Width - BarOption.Grid.Left - BarOption.Grid.Right, Height - BarOption.Grid.Top - BarOption.Grid.Bottom); if (DrawSize.Width <= 0 || DrawSize.Height <= 0) { return; } if (o.XAxis.Data.Count == 0) { return; } NeedDraw = true; DrawBarWidth = DrawSize.Width * 1.0f / o.XAxis.Data.Count; double min = double.MaxValue; double max = double.MinValue; foreach (var series in o.Series) { min = Math.Min(min, series.Data.Min()); max = Math.Max(max, series.Data.Max()); } if (min > 0 && max > 0 && !o.YAxis.Scale) { min = 0; } if (min < 0 && max < 0 && !o.YAxis.Scale) { max = 0; } if (!o.YAxis.MaxAuto) { max = o.YAxis.Max; } if (!o.YAxis.MinAuto) { min = o.YAxis.Min; } if ((max - min).IsZero()) { max = 100; min = 0; } UIChartHelper.CalcDegreeScale(min, max, o.YAxis.SplitNumber, out int start, out int end, out double interval); YAxisStart = start; YAxisEnd = end; YAxisInterval = interval; float x1 = DrawBarWidth / ((o.SeriesCount * 2) + o.SeriesCount + 1); float x2 = x1 * 2; for (int i = 0; i < o.SeriesCount; i++) { float barX = DrawOrigin.X; var series = o.Series[i]; Bars.TryAdd(i, new List <BarInfo>()); for (int j = 0; j < series.Data.Count; j++) { Color color = ChartStyle.GetColor(i); if (series.Colors.Count > 0 && j >= 0 && j < series.Colors.Count) { color = series.Colors[j]; } if (YAxisStart >= 0) { float h = Math.Abs((float)(DrawSize.Height * (series.Data[j] - start * interval) / ((end - start) * interval))); Bars[i].Add(new BarInfo() { Rect = new RectangleF( barX + x1 * (i + 1) + x2 * i, DrawOrigin.Y - h, x2, h), Color = color }); } else if (YAxisEnd <= 0) { float h = Math.Abs((float)(DrawSize.Height * (end * interval - series.Data[j]) / ((end - start) * interval))); Bars[i].Add(new BarInfo() { Rect = new RectangleF( barX + x1 * (i + 1) + x2 * i, BarOption.Grid.Top + 1, x2, h - 1), Color = color }); } else { float lowH = 0; float highH = 0; float DrawBarHeight = DrawSize.Height * 1.0f / (YAxisEnd - YAxisStart); float lowV = 0; float highV = 0; for (int k = YAxisStart; k <= YAxisEnd; k++) { if (k < 0) { lowH += DrawBarHeight; } if (k > 0) { highH += DrawBarHeight; } if (k < 0) { lowV += (float)YAxisInterval; } if (k > 0) { highV += (float)YAxisInterval; } } lowH.ConsoleWriteLine(); highH.ConsoleWriteLine(); if (series.Data[j] >= 0) { float h = Math.Abs((float)(highH * series.Data[j] / highV)); Bars[i].Add(new BarInfo() { Rect = new RectangleF( barX + x1 * (i + 1) + x2 * i, DrawOrigin.Y - lowH - h, x2, h), Color = color }); } else { float h = Math.Abs((float)(lowH * series.Data[j] / lowV)); Bars[i].Add(new BarInfo() { Rect = new RectangleF( barX + x1 * (i + 1) + x2 * i, DrawOrigin.Y - lowH + 1, x2, h - 1), Color = color }); } } barX += DrawBarWidth; } } if (BarOption.ToolTip != null) { for (int i = 0; i < BarOption.XAxis.Data.Count; i++) { string str = BarOption.XAxis.Data[i]; foreach (var series in BarOption.Series) { str += '\n'; str += series.Name + " : " + series.Data[i].ToString(BarOption.ToolTip.ValueFormat); } Bars[0][i].Tips = str; } } }