<1>中的不能动态缩放,将添加动态缩放功能
首先添加两个DateTimePicker控件显示Bottom的范围
开始的思想是,通过这个两个控件来控制TeeChart的绘图范围
private void tChart1_MouseWheel(object sender, MouseEventArgs e)
{
if (tChart1 != null)
{
if (e.Delta > 0)
{
if (DateTime.Compare(dateTimePicker3.Value, dateTimePicker4.Value) >= 0)
{
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(4);
}
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(-4);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(4);
this.button24_Click(sender, e);
}
else
{
if (DateTime.Compare(dateTimePicker3.Value, dateTimePicker4.Value) >= 0)
{
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(3);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(3);
}
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(4);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(-4);
this.button24_Click(sender, e);
}
}
}
private void zoombtnClick(object sender, EventArgs e)
{
if (DateTime.Compare(dateTimePicker3.Value, dateTimePicker4.Value) >= 0)
{
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(-4);
}
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(-6);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(6);
this.button24_Click(sender, e);
}
private void undobtnClick(object sender, EventArgs e)
{
if (DateTime.Compare(dateTimePicker3.Value, dateTimePicker4.Value) >= 0)
{
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(-4);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(4);
}
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(6);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(-6);
this.button24_Click(sender, e);
}
this.button24_Click(sender, e);这个调用drawchart(bigdate, enddate);//绘图
private void button24_Click(object sender, EventArgs e)
{
//检测是否在绘制中
// Console.Out.WriteLine(tChart1.Printing);
if (tChart1.Printing)
{
return;
}
//绘图参数设置,准备绘图
HideChartControl();
//获取时间
string bigdate;
string enddate;
bigdate = dateTimePicker3.Value.ToString("yyyy-MM-dd HH:mm:ss").Substring(0, 19);
enddate = dateTimePicker4.Value.ToString("yyyy-MM-dd HH:mm:ss").Substring(0, 19);
if (DateTime.Compare(dateTimePicker3.Value, dateTimePicker4.Value) >= 0)
{
enddate = dateTimePicker3.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss").Substring(0, 19);
}
CommOp.clearTools();//删除出了游标以外的所有注释工具
CommOp.ClearChart();//清空图表
drawchart(bigdate, enddate);//绘图
ShowChartControl();
}
private void drawchart(string sdate, string edate)
{
DBCM dbcm = new DBCM();//服务器类
String[] mps;
//生成每一条曲线
String wherestring = " and measuredt >= '" + sdate + "'and measuredt<= '" + edate + "' order by measuredt asc";
DataSet ds = new DataSet();
//先画出所有的纵轴
String f = " 1=1 ";
ds = dbcm.queryds("select * from tb_mp_" + mps[0] + " where " + f + wherestring);
CommOp.drawline(ds, ((SeriesObject)mpchartlist[i]));
tChart1.Refresh();
CommOp.adjustXvalues(sdate, edate);
setChartDate(sdate, edate);//之前有误差,把日期传入后精确
tChart1.Legend.CheckBoxes = true;//通过选择框来选择是否显示曲线
}
public static void drawline(Object ds, SeriesObject so)
{
if (ds != null)
{
DataTable dt = null;
int indextodraw = 0;
if (tchart.Series.Count > 0)
{
indextodraw = tchart.Series.Count;
}
if (ds.GetType().Name == "DataSet")
{
dt = ((DataSet)ds).Tables[0];
}
if (ds.GetType().Name == "DataTable")
{
dt = (DataTable)ds;
}
tchart.Series.Add(new Steema.TeeChart.Styles.FastLine());//画线
tchart.Series[indextodraw].DataSource = dt;
if (dt.Columns["parmvalue"] != null && dt.Columns["measuredt"] != null)
{
tchart.Series[indextodraw].YValues.DataMember = dt.Columns["parmvalue"].ToString();
tchart.Series[indextodraw].XValues.DataMember = dt.Columns["measuredt"].ToString();
tchart.Series[indextodraw].LabelMember = so.spanrange.ToString()+","+so.numtail.ToString();
}
tchart.Series[indextodraw].XValues.DateTime = true;
tchart.Series[indextodraw].Visible = so.visiable;
tchart.Series[indextodraw].Title = so.disname;
tchart.Series[indextodraw].CustomVertAxis = so.cva;
tchart.Refresh();
}
}
这样做虽然可以实现动态绘图但是会出现BUG而且放大和缩小的时间不好控制,有可能出现开始时间大于结束时间
所以换一种方法将绘好BOTTON中的值取出,重新进行放大缩小,然后根据放大缩小的值来查询服务器数据进行绘制
private void tChart1_MouseWheel(object sender, MouseEventArgs e)
{
if (tChart1 != null)
{
double XMid = ((CursorTool)tChart1.Tools[0]).XValue;
if (e.Delta > 0)
{
double OldXMin = tChart1.Axes.Bottom.Minimum;
double OldXMax = tChart1.Axes.Bottom.Maximum;
double NewXMin = (XMid * 0.5 + OldXMin) / (1.5);
double NewXMax = (XMid * 0.5 + OldXMax) / (1.5);
dateTimePicker3.Value = DateTime.FromOADate(NewXMin);
dateTimePicker4.Value = DateTime.FromOADate(NewXMax);
this.button24_Click(sender, e);
}
else
{
double OldXMin = tChart1.Axes.Bottom.Minimum;
double OldXMax = tChart1.Axes.Bottom.Maximum;
double NewXMin = (-XMid * 0.5 + OldXMin) / (0.5);
double NewXMax = (-XMid * 0.5 + OldXMax) / (0.5);
dateTimePicker3.Value = DateTime.FromOADate(NewXMin);
dateTimePicker4.Value = DateTime.FromOADate(NewXMax);
this.button24_Click(sender, e);
}
}
private void zoombtnClick(object sender, EventArgs e)
{
if (DateTime.Compare(dateTimePicker3.Value, dateTimePicker4.Value) >= 0)
{
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(-4);
}
dateTimePicker3.Value = dateTimePicker3.Value.AddHours(-6);
dateTimePicker4.Value = dateTimePicker4.Value.AddHours(6);
this.button24_Click(sender, e);
}
这样显示框的时间可能会超出绘制出的时间,将显示框的时间调整一下
dateTimePicker4.Value = DateTime.FromOADate(tChart1.Axes.Bottom.Maximum);
这样基本功能是可以完成了,但是这样绘制图形每次都需要连接服务器获取时间,一个初步的想法是一次取出一定量的数据放到缓存中,每次绘制检查时间,如果超出再连接服务器获取一定量的数据,后续再进行改进