为了账号安全,请及时绑定邮箱和手机立即绑定

winform Chart x轴以当前时间开始(格式:月-日 小时:分钟)

winform Chart x轴以当前时间开始(格式:月-日 小时:分钟)

千巷猫影 2018-12-06 17:30:58
这是用户控件的代码: View Code using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WinformControls { public partial class UcChartInfo : UserControl { private System.Windows.Forms.DataVisualization.Charting.Chart chart1; private System.ComponentModel.Container components = null; public UcChartInfo() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Component private void InitializeComponent() { System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend(); System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.SuspendLayout(); // // chart1 // this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(223)))), ((int)(((byte)(193))))); this.chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; this.chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(181)))), ((int)(((byte)(64)))), ((int)(((byte)(1))))); this.chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid; this.chart1.BorderlineWidth = 2; this.chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss; chartArea1.Area3DStyle.Inclination = 40; chartArea1.Area3DStyle.IsClustered = true; chartArea1.Area3DStyle.IsRightAngleAxes = false; chartArea1.Area3DStyle.LightStyle = System.Windows.Forms.DataVisualization.Charting.LightStyle.Realistic; chartArea1.Area3DStyle.Perspective = 9; chartArea1.Area3DStyle.Rotation = 25; chartArea1.Area3DStyle.WallWidth = 3; chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.BackColor = System.Drawing.Color.OldLace; chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom; chartArea1.BackSecondaryColor = System.Drawing.Color.White; chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid; chartArea1.Name = "Default"; chartArea1.ShadowColor = System.Drawing.Color.Transparent; chartArea1.AxisX.MajorGrid.Enabled = false;//隐藏竖线。 chartArea1.AxisX.IntervalType = DateTimeIntervalType.Hours; chartArea1.AxisX.Interval = 100; chartArea1.AxisX.Minimum = DateTime.Now.ToOADate(); chartArea1.AxisX.Maximum = DateTime.Now.AddHours(800).ToOADate(); chartArea1.AxisX.LabelStyle.Format = "MM-dd\nHH:mm";//时间格式。 chartArea1.AxisY.Interval = 5; chartArea1.AxisY.Minimum = 0; chartArea1.AxisY.Maximum = 20; this.chart1.ChartAreas.Add(chartArea1); this.chart1.Dock = System.Windows.Forms.DockStyle.Fill; legend1.BackColor = System.Drawing.Color.Transparent; legend1.Enabled = false; legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold); legend1.IsTextAutoFit = false; legend1.Name = "Default"; this.chart1.Legends.Add(legend1); this.chart1.Location = new System.Drawing.Point(0, 0); this.chart1.Margin = new System.Windows.Forms.Padding(0); this.chart1.Name = "chart1"; series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105))))); series1.ChartArea = "Default"; series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240))))); series1.Legend = "Default"; series1.MarkerSize = 8; series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle; series1.Name = "Series1"; series1.ShadowColor = System.Drawing.Color.Black; series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double; //设置显示的时间类型。 series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime; this.chart1.Series.Add(series1); this.chart1.Size = new System.Drawing.Size(533, 258); this.chart1.TabIndex = 1; // // UcChartInfo // this.BackColor = System.Drawing.Color.Transparent; this.Controls.Add(this.chart1); this.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Margin = new System.Windows.Forms.Padding(0); this.Name = "UcChartInfo"; this.Size = new System.Drawing.Size(533, 258); ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.ResumeLayout(false); } #endregion private void UpdateChartSettings() { chart1.Series["Series1"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Line", true); chart1.Series["Series1"].SmartLabelStyle.Enabled = true; chart1.Series["Series1"].IsValueShownAsLabel = true; chart1.Series["Series1"].IsXValueIndexed = false; chart1.ChartAreas["Default"].AxisX.IsMarginVisible = false; chart1.ChartAreas["Default"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None; chart1.ChartAreas["Default"].AxisX.LabelStyle.IsStaggered = false; //chart1.ChartAreas[0].AxisX.Crossing = 0; } private void UcChartInfo_Load(object sender, EventArgs e) { } public void BindDataInfo(List lst) { DateTime baseDate = DateTime.Today; for (int i = 0; i < lst.Count; i++) { chart1.Series["Series1"].Points.AddXY(lst[i].XDate, lst[i].YValue); } UpdateChartSettings(); } } public class ChartLactionInfo { public DateTime XDate { get; set; } public double YValue { get; set; } } } 这是主窗体的代码: View Code using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using WinformControls; namespace WinformTest { public partial class FrmChartInfo : Form { public FrmChartInfo() { InitializeComponent(); } private void FrmChartInfo_Load(object sender, EventArgs e) { List lst = new List(); Random r = new Random(); DateTime dtNow = DateTime.Now; for (int i = 0; i < 50; i++) { ChartLactionInfo c = new ChartLactionInfo() { XDate = dtNow, YValue = r.Next(0, 20) }; lst.Add(c); dtNow = dtNow.AddHours(i + 12); } ucChartInfo1.BindDataInfo(lst); } } }   界面运行图片: 上面箭头指的这个地方应该是当前的时间,但是没有显示。我所写的代码都在上面。请帮我看看,里面哪里设置有问题?
查看完整描述

5 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

chartArea1.AxisX.Minimum = DateTime.Now.ToOADate();这个值不要大于 第一个点的X坐标值。 要不然会不显示
查看完整回答
反对 回复 2018-12-09
?
烙印99

TA贡献1829条经验 获得超13个赞

博主,能提供一下源代码吗 非常感谢啊
查看完整回答
反对 回复 2018-12-09
?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

上面的代码包含了所有的了,这已经是4年以前的项目了,现在早就没有这个了;你把上面的代码复制进去应该就可以了。
查看完整回答
反对 回复 2018-12-09
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

@ArnoldZhang: 直接复制就可以用吗
查看完整回答
反对 回复 2018-12-09
  • 5 回答
  • 0 关注
  • 1219 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信