绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
winform Chart x轴以当前时间开始(格式:月-日 小时:分钟)
2019-10-25 10:20:20

这是用户控件的代码:


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

{

publicpartialclass UcChartInfo : UserControl

{

private System.Windows.Forms.DataVisualization.Charting.Chart chart1;

privateSystem.ComponentModel.Container components =null;

public UcChartInfo()

{

InitializeComponent();

}

protectedoverridevoidDispose(bool disposing)

{

if (disposing)

{

if(components !=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#regionComponentprivatevoid 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 =newSystem.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 =newSystem.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 =newSystem.Drawing.Font("Trebuchet MS",8.25F, System.Drawing.FontStyle.Bold);

legend1.IsTextAutoFit =false;

legend1.Name ="Default";

this.chart1.Legends.Add(legend1);

this.chart1.Location =newSystem.Drawing.Point(0,0);

this.chart1.Margin =newSystem.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 =newSystem.Drawing.Size(533,258);

this.chart1.TabIndex =1;

//// UcChartInfo

//this.BackColor = System.Drawing.Color.Transparent;

this.Controls.Add(this.chart1);

this.Font =newSystem.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.Margin =newSystem.Windows.Forms.Padding(0);

this.Name ="UcChartInfo";

this.Size =newSystem.Drawing.Size(533,258);

((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();

this.ResumeLayout(false);

}

#endregionprivatevoid 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; }

privatevoidUcChartInfo_Load(object sender, EventArgs e)

{

}

publicvoidBindDataInfo(List lst)

{

DateTime baseDate = DateTime.Today;

for(inti =0; i < lst.Count; i++)

{

chart1.Series["Series1"].Points.AddXY(lst[i].XDate, lst[i].YValue);

}

UpdateChartSettings();

}

}

publicclass ChartLactionInfo

{

publicDateTime XDate {get;set; }

publicdoubleYValue {get;set; }

}

}

这是主窗体的代码:

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

{

publicpartialclass FrmChartInfo : Form

{

public FrmChartInfo()

{

InitializeComponent();

}

privatevoidFrmChartInfo_Load(object sender, EventArgs e)

{

List lst =newList();

Random r =new Random();

DateTime dtNow = DateTime.Now;

for(inti =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);

}

}

}

 

界面运行图片:

上面箭头指的这个地方应该是当前的时间,但是没有显示。我所写的代码都在上面。请帮我看看,里面哪里设置有问题?

分享好友

分享这个小栈给你的朋友们,一起进步吧。

长沙IT圈
创建时间:2019-10-25 09:32:10
结识长沙IT圈朋友、一起打酱油。
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • abc
    栈主

小栈成员

查看更多
  • 栈栈
  • 54xx
  • amadan
  • abcjob
戳我,来吐槽~