* UILineChart:增加X轴为日期类型,曲线Y轴可分段显示颜色。

This commit is contained in:
Sunny 2020-10-07 11:59:37 +08:00
parent 1e09ae19b0
commit ef3896c50b
7 changed files with 128 additions and 11 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -76,6 +76,7 @@
this.uiImageButton3.TabStop = false;
this.uiImageButton3.Text = " Dark";
this.uiImageButton3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.uiImageButton3.Click += new System.EventHandler(this.uiImageButton3_Click);
//
// uiImageButton2
//
@ -89,6 +90,7 @@
this.uiImageButton2.TabStop = false;
this.uiImageButton2.Text = " Plain";
this.uiImageButton2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.uiImageButton2.Click += new System.EventHandler(this.uiImageButton2_Click);
//
// uiImageButton1
//
@ -102,6 +104,7 @@
this.uiImageButton1.TabStop = false;
this.uiImageButton1.Text = " Default";
this.uiImageButton1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.uiImageButton1.Click += new System.EventHandler(this.uiImageButton1_Click);
//
// uiLine1
//
@ -111,7 +114,7 @@
this.uiLine1.Name = "uiLine1";
this.uiLine1.Size = new System.Drawing.Size(670, 20);
this.uiLine1.TabIndex = 30;
this.uiLine1.Text = "UIBarChart";
this.uiLine1.Text = "UILineChart";
this.uiLine1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// LineChart

View File

@ -11,7 +11,7 @@ namespace Sunny.UI.Demo.Charts
InitializeComponent();
}
private void uiSymbolButton1_Click(object sender, System.EventArgs e)
private void uiSymbolButton1_Click(object sender, EventArgs e)
{
UILineOption option = new UILineOption();
option.Title = new UITitle();
@ -33,7 +33,7 @@ namespace Sunny.UI.Demo.Charts
series.SymbolLineWidth = 2;
series.SymbolColor = Color.Red;
series = option.AddSeries(new UILineSeries("Line2"));
series = option.AddSeries(new UILineSeries("Line2", Color.Lime));
series.Add(dt.AddHours(0.3), 3.3);
series.Add(dt.AddHours(0.4), 2.3);
series.Add(dt.AddHours(0.5), 2.3);
@ -51,6 +51,12 @@ namespace Sunny.UI.Demo.Charts
// option.XAxis.MaxAuto = false;
// option.XAxis.MinAuto = false;
option.GreaterWarningArea = new UILineWarningArea(3.5);
option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);
option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Red, Name = "上限", Value = 3.5 });
option.YAxisScaleLines.Add(new UIScaleLine() { Color = Color.Gold, Name = "下限", Value = 2.2 });
option.XAxis.Name = "数值";
option.YAxis.Name = "数值";
@ -68,5 +74,20 @@ namespace Sunny.UI.Demo.Charts
Console.WriteLine(sb.ToString());
}
private void uiImageButton1_Click(object sender, System.EventArgs e)
{
LineChart.ChartStyleType = UIChartStyleType.Default;
}
private void uiImageButton2_Click(object sender, System.EventArgs e)
{
LineChart.ChartStyleType = UIChartStyleType.Plain;
}
private void uiImageButton3_Click(object sender, System.EventArgs e)
{
LineChart.ChartStyleType = UIChartStyleType.Dark;
}
}
}

View File

@ -184,10 +184,10 @@ namespace Sunny.UI
// if (BarOption.ToolTip != null && BarOption.ToolTip.AxisPointer.Type == UIAxisPointerType.Shadow) DrawToolTip(g);
DrawAxis(g);
DrawTitle(g, LineOption.Title);
DrawAxisScales(g);
DrawSeries(g);
// if (BarOption.ToolTip != null && BarOption.ToolTip.AxisPointer.Type == UIAxisPointerType.Line) DrawToolTip(g);
DrawLegend(g, LineOption.Legend);
DrawAxisScales(g);
}
private void DrawAxis(Graphics g)
@ -308,16 +308,83 @@ namespace Sunny.UI
Color color = series.Color;
if (!series.CustomColor) color = ChartStyle.GetColor(idx);
using (Pen pen = new Pen(color, series.Width))
if (LineOption.GreaterWarningArea == null && LineOption.LessWarningArea == null)
{
g.SetHighQuality();
if (series.Smooth)
g.DrawCurve(pen, series.Points.ToArray());
else
g.DrawLines(pen, series.Points.ToArray());
g.SetDefaultQuality();
using (Pen pen = new Pen(color, series.Width))
{
g.SetHighQuality();
if (series.Smooth)
g.DrawCurve(pen, series.Points.ToArray());
else
g.DrawLines(pen, series.Points.ToArray());
g.SetDefaultQuality();
}
}
else
{
Bitmap bmp = new Bitmap(Width, Height);
Bitmap bmpGreater;
Bitmap bmpLess;
float wTop = 0;
float wBottom = Height;
using (Pen pen = new Pen(color, series.Width))
{
Graphics graphics = bmp.Graphics();
graphics.SetHighQuality();
if (series.Smooth)
graphics.DrawCurve(pen, series.Points.ToArray());
else
graphics.DrawLines(pen, series.Points.ToArray());
graphics.SetDefaultQuality();
}
if (LineOption.GreaterWarningArea != null)
{
using (Pen pen = new Pen(LineOption.GreaterWarningArea.Color, series.Width))
using (bmpGreater = new Bitmap(Width, Height))
{
bmpGreater = new Bitmap(Width, Height);
Graphics graphics = bmpGreater.Graphics();
graphics.SetHighQuality();
if (series.Smooth)
graphics.DrawCurve(pen, series.Points.ToArray());
else
graphics.DrawLines(pen, series.Points.ToArray());
graphics.SetDefaultQuality();
wTop = (float)((LineOption.GreaterWarningArea.Value - YAxisStart * YAxisInterval) * 1.0f * DrawSize.Height / YAxisInterval / (YAxisEnd - YAxisStart));
wTop = DrawOrigin.Y - wTop;
g.DrawImage(bmpGreater, new Rectangle(0, 0, Width, (int)wTop),
new Rectangle(0, 0, Width, (int)wTop), GraphicsUnit.Pixel);
}
}
if (LineOption.LessWarningArea != null)
{
using (Pen pen = new Pen(LineOption.LessWarningArea.Color, series.Width))
using (bmpLess = new Bitmap(Width, Height))
{
Graphics graphics = bmpLess.Graphics();
graphics.SetHighQuality();
if (series.Smooth)
graphics.DrawCurve(pen, series.Points.ToArray());
else
graphics.DrawLines(pen, series.Points.ToArray());
graphics.SetDefaultQuality();
wBottom = (float)((LineOption.LessWarningArea.Value - YAxisStart * YAxisInterval) * 1.0f * DrawSize.Height / YAxisInterval / (YAxisEnd - YAxisStart));
wBottom = DrawOrigin.Y - wBottom;
g.DrawImage(bmpLess, new Rectangle(0, (int)wBottom, Width, Height - (int)wBottom),
new Rectangle(0, (int)wBottom, Width, Height - (int)wBottom), GraphicsUnit.Pixel);
}
}
g.DrawImage(bmp, new Rectangle(0, (int)wTop, Width, (int)wBottom - (int)wTop),
new Rectangle(0, (int)wTop, Width, (int)wBottom - (int)wTop), GraphicsUnit.Pixel);
bmp.Dispose();
}
if (series.Symbol != UILinePointSymbol.None)
{

View File

@ -30,6 +30,9 @@ namespace Sunny.UI
public readonly List<UIScaleLine> YAxisScaleLines = new List<UIScaleLine>();
public UILineWarningArea GreaterWarningArea { get; set; }
public UILineWarningArea LessWarningArea { get; set; }
public UILineSeries AddSeries(UILineSeries series)
{
if (series.Name.IsNullOrEmpty()) return null;
@ -278,4 +281,27 @@ namespace Sunny.UI
public double Y { get; set; }
}
public class UILineWarningArea
{
public double Value { get; set; }
public Color Color { get; set; } = Color.Red;
public UILineWarningArea()
{
}
public UILineWarningArea(double value)
{
Value = value;
}
public UILineWarningArea(double value, Color color)
{
Value = value;
Color = color;
}
}
}