VS自带Chart控件——设置x轴为时间,并使用滚动条显示

目录

    • 1.初始化设置:
    • 2.添加曲线及数据:
    • 3.数据点提示x轴时间及y值

本文 完整例子打包:
https://download.csdn.net/download/Yyuanyuxin/12347571
效果:
VS自带Chart控件——设置x轴为时间,并使用滚动条显示_第1张图片

tchart和chart折线图实时显示 完整例子打包:
https://download.csdn.net/download/Yyuanyuxin/12347732
效果:
VS自带Chart控件——设置x轴为时间,并使用滚动条显示_第2张图片

1.初始化设置:

/*###############################################################################
//ps:尤其注意,下面的语句有的打断顺序会出现不同的效果····之前踩了好多雷···
###############################################################################*/

//设置滚动轴
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;//滚动轴上的按钮全显示
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;//滚动轴显示位置,x轴下面还是图外面
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 20;
//设置显示区域类型为秒
chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;

chart1.ChartAreas[0].AxisX.ScaleView.Size = 20;
chart1.ChartAreas[0].AxisX.ScaleView.MinSize = 15;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;
chart1.ChartAreas[0].AxisX.Interval = 0.5;//这个interval可以用来修改显示间隔,因为上面设置单位为秒,这里间隔为0.5秒
chart1.ChartAreas[0].AxisX.ScaleView.Size = 5;//设置显示区域x轴的长度,5s
chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;

chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM-dd HH:mm:ss:fff"; //x轴时间显示格式为时:分:秒
chart1.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;


//设置标题
this.chart1.Titles.Clear();
this.chart1.Titles.Add("title1");
this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.chart1.Titles[0].Text = string.Format("频组数据显示");

2.添加曲线及数据:

var table = getData().Tables[0].Select("line=0","x").CopyToDataTable();
if (table == null) return;
chart1.SuspendLayout();
//chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
//chart1.ChartAreas[0].AxisX.ScaleView.Size = 8;
this.chart1.Series.Clear();
this.chart1.DataSource = table;

Series series1 = new Series("y1");
//series1.IsValueShownAsLabel = true;
series1.ToolTip = "#VAL{N2}";
series1.ChartArea = "ChartArea1";
series1.XValueType = ChartValueType.DateTime;//坐标轴类型改为时间
series1.ChartType = SeriesChartType.Line;
series1.MarkerStyle = MarkerStyle.Circle;
series1.MarkerSize = 6;
series1.Points.Clear();
series1.Points.DataBind(table.AsEnumerable(), "x", "y1", "");
this.chart1.Series.Add(series1);

Series series2 = new Series("y2");
//series1.IsValueShownAsLabel = true;
series2.ToolTip = "#VAL{N2}";
series2.ChartArea = "ChartArea1";
series2.XValueType = ChartValueType.DateTime;
series2.ChartType = SeriesChartType.Line;
series2.MarkerStyle = MarkerStyle.Circle;
series2.MarkerSize = 6;
series2.Points.Clear();
series2.Points.DataBind(table.AsEnumerable(), "x", "y2", "");
this.chart1.Series.Add(series2);

chart1.ResumeLayout();

3.数据点提示x轴时间及y值

如果不是时间,或者说显示日期,下面的设置即可:

series1.ToolTip = "#VALX #VAL{N2}"

但对于要显示x轴时间格式自己设置的方式,目前找到以下的方式:
使用chart的GetToolTipText事件:

private void chart_GetToolTipText(object sender, ToolTipEventArgs e)
{
    var chart = sender as Chart;
    if (chart == null) return;
    HitTestResult hitTestResult = chart.HitTest(e.X, e.Y, ChartElementType.DataPoint);//获取命中测试的结果
    if (hitTestResult.ChartElementType == ChartElementType.DataPoint)
    {
        int i = hitTestResult.PointIndex;
        DataPoint dp = hitTestResult.Series.Points[i];
        string XValue = TimeHepler.ConvertDoubleToDateTime(dp.XValue).ToString("HH:mm:ss") ;//获取数据点的X值
        string YValue = dp.YValues[0].ToString();//获取数据点的Y值
        e.Text = "时间:" + XValue + "\r\n值:" + YValue;
    }
}

你可能感兴趣的:(c#,#,c#,控件操作)