首页 > 代码库 > Chart折线图自定义横轴实现阶梯折线效果
Chart折线图自定义横轴实现阶梯折线效果
#region
Chart Chart1 = new Chart();
Chart1.Width = 1000;
Chart1.Height = 400;
axisYMaximum = GetMax(dtsource);
double axisYMinimum = GetMin(dtsource);
Chart1.ChartAreas.Add("Series 1");
Chart1.ChartAreas["Series 1"].AxisY2.MajorGrid.Enabled = false;
Chart1.ChartAreas["Series 1"].AxisX.MajorGrid.Enabled = false;
Chart1.ChartAreas["Series 1"].AxisX.LabelStyle.IsEndLabelVisible = false;
Chart1.ChartAreas["Series 1"].AxisX.LabelStyle.Angle = 0;
Chart1.ChartAreas["Series 1"].Area3DStyle.Enable3D = false;
Chart1.ChartAreas["Series 1"].Area3DStyle.IsRightAngleAxes = false;
Chart1.ChartAreas["Series 1"].Area3DStyle.IsClustered = true;
Chart1.ChartAreas["Series 1"].Area3DStyle.Inclination = 80;
Chart1.ChartAreas["Series 1"].AxisY.IsStartedFromZero = false;
Chart1.ChartAreas["Series 1"].AxisY2.Enabled = AxisEnabled.False;//是否显示辅助轴
for (int k = 1; k < dtsource.Columns.Count; Chart1.Series.Add(dtsource.Columns[k].ColumnName), k++) ;
for (int i = 1; i < dtsource.Columns.Count; i++)
{
string curcol = dtsource.Columns[i].ColumnName;
DataRow[] drs = dtsource.Select("[" + curcol + "] is not null", "[time] asc");
if (drs.Count() == 0)
continue;
DataTable dtsourcecopy = drs.CopyToDataTable();
bool specialcols = new string[] { "PCI", "PSC" }.Contains(curcol);
if (specialcols)
{
DataTable DT = dtsourcecopy.Clone();
for (int ii = 1; ii < dtsourcecopy.Rows.Count; ii++)
{
DataRow dr = DT.NewRow();
dr["time"] = dtsourcecopy.Rows[ii]["time"]; // Convert.ToDateTime(dtsourcecopy.Rows[ii]["time"]).AddSeconds(0).ToString("hhmmss");
dr[curcol] = dtsourcecopy.Rows[ii - 1][curcol];
DT.Rows.Add(dr);
}
DataTable DTSrc = http://www.mamicode.com/dtsourcecopy.Copy();
dtsourcecopy.Clear();
for (int jj = 0; jj < (DT.Rows.Count + DTSrc.Rows.Count); jj++)
{
if (jj % 2 == 0)
dtsourcecopy.Rows.Add(DTSrc.Rows[(jj / 2)].ItemArray);
else
dtsourcecopy.Rows.Add(DT.Rows[((jj - 1) / 2)].ItemArray);
}
}
for (int j = 0; j < dtsourcecopy.Rows.Count; j++)
{
object x = dtsourcecopy.Rows[j]["time"], y = dtsourcecopy.Rows[j][curcol];
Chart1.Series[curcol].Points.AddXY((specialcols ? Convert.ToDouble(x.ToString().Replace(":", "")) : x), (specialcols ? Convert.ToDouble(y) : y));
Chart1.Series[curcol].IsValueShownAsLabel = false;
}
if (i != 1)
continue;
if (specialcols)
{
int c = Chart1.Series[curcol].Points.Count;
{
CustomLabel label = new CustomLabel();
label.Text = dtsourcecopy.Rows[0]["time"].ToString();
label.FromPosition = Chart1.Series[curcol].Points[0].XValue;
label.ToPosition = Chart1.Series[curcol].Points[0].XValue + ((Chart1.Series[curcol].Points[c-1].XValue - Chart1.Series[curcol].Points[0].XValue) <= 10 ? 3 : 10);
Chart1.ChartAreas["Series 1"].AxisX.CustomLabels.Add(label);
}
for (int z = 1; z < 3; z++)
{
int p = (int)Math.Floor(c * (0.4 * z)), lastp = (int)Math.Floor(c * (0.4 * (z - 1)));
double pv = Chart1.Series[curcol].Points[p].XValue, lastpv = Chart1.Series[curcol].Points[lastp].XValue;
if ((pv - lastpv) <= 10)
continue;
CustomLabel label = new CustomLabel();
label.Text = dtsourcecopy.Rows[p]["time"].ToString();
label.FromPosition = pv;
label.ToPosition = pv + 10;
Chart1.ChartAreas["Series 1"].AxisX.CustomLabels.Add(label);
}
}
axisYInterval = (((axisYMaximum - axisYMinimum) / (specialcols ? 10 : 0.1)) < 1 ? (specialcols ? 1 : 0.01) : ((axisYMaximum - axisYMinimum) / 10));
Chart1.ChartAreas["Series 1"].AxisY.Maximum = axisYMaximum + axisYInterval;
Chart1.ChartAreas["Series 1"].AxisY.Minimum = axisYMinimum - axisYInterval;
Chart1.ChartAreas["Series 1"].AxisY.Interval = axisYInterval;//设置刻度间隔为5%
Chart1.ChartAreas["Series 1"].AxisY.LabelStyle.Format = specialcols ? "0" : "N2";
}
for (int s = 0; s < Chart1.Series.Count; s++)
{
for (int i = 0; i < Chart1.Series[s].Points.Count; i++)
{
switch (s)
{
case 0:
Chart1.Series[0].Points[i].Color = Color.FromArgb(22, 0, 255);//蓝色
break;
case 1:
Chart1.Series[1].Points[i].Color = Color.FromArgb(255, 0, 0);//红色
break;
case 2:
Chart1.Series[2].Points[i].Color = Color.FromArgb(86, 38, 130);//紫色
break;
case 3:
Chart1.Series[3].Points[i].Color = Color.FromArgb(130, 86, 38);
break;
}
}
Color c = new Color();
switch (s)
{
case 0: c = Color.FromArgb(22, 0, 255); break;//蓝色
case 1: c = Color.FromArgb(255, 0, 0); break;
case 2: c = Color.FromArgb(86, 38, 130); break;
case 3: c = Color.FromArgb(130, 86, 38); break;
}
Chart1.Series[s].Color = c;
Chart1.Series[s].YAxisType = AxisType.Primary;
Chart1.Series[s].BorderWidth = 3;//线的宽度
Chart1.Series[s].ChartType = SeriesChartType.Line;
}
Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
Chart1.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
Chart1.BorderlineWidth = 2;
Chart1.Legends.Add("Legend1");
Chart1.Legends[0].Docking = Docking.Bottom;
Chart1.Legends[0].Alignment = StringAlignment.Center;
Chart1.Legends[0].BorderWidth = 1;
for (int a = 0; a < Chart1.Series.Count; a++)
if (Chart1.Series[a].Points.Count != 0)
{
Chart1.SaveImage(savePath, ChartImageFormat.Png);
break;
}
return true;
#endregion
Chart折线图自定义横轴实现阶梯折线效果