Mschart绘制图表之X轴为时间的设置方式
生活随笔
收集整理的這篇文章主要介紹了
Mschart绘制图表之X轴为时间的设置方式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近使用C#開發(fā)圖表,比較了DirectorChart,DontNetCharting,TeeChart,最終選用微軟的mschart開發(fā),對(duì)于X軸作為時(shí)間軸探索了好久,終于實(shí)現(xiàn)了想要的效果。
界面效果:
核心源碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Windows.Forms.DataVisualization.Charting;
namespace Krypton440Test
{
public partial class Form4 : ComponentFactory.Krypton.Toolkit.KryptonForm
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
//清空原來數(shù)據(jù)緩存
chart1.Series[0].Points.Clear();
//定義圖表大小尺寸
chart1.Width = Width - 100;
chart1.Height = Height - 100;
//定義X軸、Y軸數(shù)據(jù)
double[] Ydata = { 20, 3, 23 ,6};
DateTime[] Xdate = new DateTime[] { DateTime.Parse("09:10:02"), DateTime.Parse("09:10:10"),DateTime.Parse("09:10:15"), DateTime.Parse("09:10:20") };
//以下按照先繪制chartArea、然后再繪制Series的步驟畫圖
//chartArea背景顏色
chart1.BackColor = Color.Azure;
//X軸設(shè)置
chart1.ChartAreas[0].AxisX.Title = "時(shí)間";
chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//不顯示豎著的分割線
/************************************************************************/
/* 本文重點(diǎn)講解時(shí)間格式的設(shè)置
* 如果想顯示原點(diǎn)第一個(gè)時(shí)間坐標(biāo),需要設(shè)置最小時(shí)間,時(shí)間間隔類型,時(shí)間間隔值等三個(gè)參數(shù)*/
/************************************************************************/
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; //X軸顯示的時(shí)間格式,HH為大寫時(shí)是24小時(shí)制,hh小寫時(shí)是12小時(shí)制
chart1.ChartAreas[0].AxisX.Minimum = DateTime.Parse("09:10:02").ToOADate();
chart1.ChartAreas[0].AxisX.Maximum = DateTime.Parse("09:10:21").ToOADate();
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;//如果是時(shí)間類型的數(shù)據(jù),間隔方式可以是秒、分、時(shí)
chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("00:00:02").Second;//間隔為2秒
//Y軸設(shè)置
chart1.ChartAreas[0].AxisY.Title = "數(shù)據(jù)點(diǎn)";
chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Center;
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//顯示橫著的分割線
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 25;
chart1.ChartAreas[0].AxisY.Interval = 5;
//Series繪制
chart1.Series[0].LegendText = "溫度點(diǎn)";
chart1.Series[0].ChartType = SeriesChartType.Spline;
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.Series[0].IsValueShownAsLabel = true;//顯示數(shù)據(jù)點(diǎn)的值
chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
//把數(shù)據(jù)點(diǎn)添加到Series圖表中
for (int i = 0; i < Xdate.Length; i++)
{
chart1.Series[0].Points.AddXY(Xdate[i], Ydata[i]);
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的Mschart绘制图表之X轴为时间的设置方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android系统设置在哪里,andro
- 下一篇: html 修改背景透明度,html –