using LBFullProcessPlatfrom.Help;
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;
namespace LongStageApplication
{
public partial class ViewForm : Form
{
public ViewForm()
{
InitializeComponent();
}
//当前的总数据量
int dataCounts = 0;
//chart图显示的数据点数【默认值】
int chartShowPointCount = 100;
int minChartShowPointCount = 10;
//chart图上的数据点放大缩小时,采用的step
int sizeStep = 10;
/// <summary>
/// Chart初始化
/// </summary>
private void Init_Chart()
{
mainChart.Focus();
#region 横轴日期显示的设置
mainChart.Series[0].XValueType = ChartValueType.DateTime;//坐标轴type改为时间
mainChart.ChartAreas[0].AxisX.Interval = 1;
mainChart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
mainChart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//设置X轴的值的间隔大小
mainChart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Months;//设置x轴间隔值单位:秒
mainChart.ChartAreas[0].AxisX.LabelStyle.Format = "MM";//设置X轴的数据样式--只显示月
mainChart.ChartAreas[0].AxisX.MajorGrid.Interval = 1; //网格间隔
mainChart.ChartAreas[0].AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Years;
#endregion
#region x轴缩放
//防止数据点重叠
mainChart.Series[0].SmartLabelStyle.Enabled = true;
//超过图表界是否发生滚动
mainChart.ChartAreas["ChartArea1"].CursorX.AutoScroll = true;
//是否启用滚动条
mainChart.ChartAreas["ChartArea1"].AxisX.ScrollBar.Enabled = true;
//启用光标用户界面
mainChart.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
//启用用户选择范围--不用选择范围进行放大
//mainChart.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
//启用缩放
mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
//设置从哪个点(idx)开始显示
mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Position = dataCounts - chartShowPointCount;
//设置图表可视区域数据点数,即一次可以看到多少个X轴区域
mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Size = chartShowPointCount;
#endregion
}
private void ViewForm_Load(object sender, EventArgs e)
{
try
{
string sqlStr = "select price_dt,close_price from stat_day where stock_cd='000524' and price_dt>='2020-10-25' order by price_dt;";
DataRowCollection dataDrc = LocalMySqlHelper.GetDataSet(GlobalClass.ConnectStrPlatform, CommandType.Text, sqlStr, null).Tables[0].Rows;
//初始化界面
dataCounts = dataDrc.Count;
Init_Chart();
try
{
CustomLabel cusLabel = new CustomLabel();
for (int i = 0; i < dataDrc.Count; i++)
{
DataRow curDr = dataDrc[i];
mainChart.Series[0].Points.AddXY(Convert.ToDateTime(curDr["price_dt"]), Convert.ToDouble(curDr["close_price"]));
if (i == 0 || (i > 0 && Convert.ToDateTime(dataDrc[i - 1]["price_dt"]).Year != Convert.ToDateTime(curDr["price_dt"]).Year))
{
//新的一年了,把前一年的加入
if (i > 0)
{
cusLabel.ToPosition = i - 1;
mainChart.ChartAreas[0].AxisX.CustomLabels.Add(cusLabel);
}
cusLabel = new CustomLabel();
cusLabel.Text = Convert.ToDateTime(curDr["price_dt"]).ToString("yyyy");
cusLabel.FromPosition = i;
cusLabel.ForeColor = Color.Red;
cusLabel.RowIndex = 1;
}
}
//最后一次的
cusLabel.ToPosition = dataDrc.Count - 1;
mainChart.ChartAreas[0].AxisX.CustomLabels.Add(cusLabel);
}
catch (Exception ex)
{
throw;
}
}
catch (Exception ex)
{
throw;
}
}
private void mainChart_KeyDown(object sender, KeyEventArgs e)
{
string keyData = e.KeyData.ToString();
//获取当前的size
double curScaleViewSize = mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Size;
//获取当前的position
double curScaleViewPosition = 0;
//当前显示的结束位置
double curEndPosition = 0;
//存在x轴的缩放条
if (curScaleViewSize > 0)
{
curScaleViewPosition = mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Position;
curEndPosition = curScaleViewPosition + curScaleViewSize;
}
else
{
//不存在x先后的缩放条【数据全显示了】
curScaleViewSize = dataCounts;
curScaleViewPosition = 0;
curEndPosition = curScaleViewPosition + curScaleViewSize;
}
double newSize = 0;
//按键盘上的+-->放大
if (keyData == "Oemplus" || keyData == "Add")
{
//将size的值变小些
newSize = curScaleViewSize - sizeStep > minChartShowPointCount ? curScaleViewSize - sizeStep : minChartShowPointCount;
}
//按键盘上的--->缩小
else if (keyData == "OemMinus" || keyData == "Subtract")
{
//将size的值变大些
newSize = curScaleViewSize + sizeStep > dataCounts ? dataCounts : curScaleViewSize + sizeStep;
}
mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Size = newSize;
mainChart.ChartAreas["ChartArea1"].AxisX.ScaleView.Position = curEndPosition - newSize;
}
}
}
直接贴代码了,界面上只放了一个chart控件mainchart