WPF 使用MSCHART 控件代码

本来想多写点 感觉有可能是废话直接看代码吧!

CHART支持缩放。,X轴是时间单位  注意浮点时间转换的方法。原来是数据可加载的数据 后来改成直接添加数据了要用数据库的自己去掉//

XML文件

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Charting="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization" x:Class="TESTCHART.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <WindowsFormsHost x:Name="WFHCHART" HorizontalAlignment="Left" Height="248" Margin="10,33,0,0" VerticalAlignment="Top" Width="497">
            <Charting:Chart  Name="mainChart" Text="测试CHART" >
                <Charting:Chart.Titles>
                    <Charting:Title Text="WTF"/>
                </Charting:Chart.Titles>
            </Charting:Chart>
        </WindowsFormsHost>
        <Button Content="Button" HorizontalAlignment="Left" Margin="90,297,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <TextBox x:Name="tbTEXT" HorizontalAlignment="Left" Height="23" Margin="75,5,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="407"/>

    </Grid>
</Window>
 

.s文件  

public partial class MainWindow : Window
    {
        //WFHCHART
        DataTable dt = new DataTable();
        Chart mainChart = null;
        public MainWindow()
        {
            InitializeComponent();
            mainChart = WFHCHART.Child as Chart;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dt.Columns.Add("Processor");
            dt.Columns.Add("Time", Type.GetType("System.DateTime"));
            for (int i = 0; i < 30; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Processor"] = i * DateTime.Now.Millisecond;
                dr["Time"] = DateTime.Now + new TimeSpan(0, i, 0);
                dt.Rows.Add(dr);
            }


            ChartArea ca = new ChartArea("ChartArea1");
            ca.Area3DStyle.Enable3D = false;
            ca.BackColor = System.Drawing.Color.FromArgb(0, 255, 255);
            // ca.AxisY.Maximum = 90000;
            mainChart.ChartAreas.Add(ca);

            Legend lgCPU = new Legend("Legend1");
            lgCPU.IsTextAutoFit = true;
            lgCPU.Docking = Docking.Bottom;
            mainChart.Legends.Add(lgCPU);


            Series seCPU = new Series("SeriesCPU");
            seCPU.ChartArea = "ChartArea1";
            seCPU.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            seCPU.IsVisibleInLegend = true;
            seCPU.Legend = "Legend1";
            seCPU.LegendText = "CPU";
            //   seCPU.XValueMember = "Time";
            //  seCPU.YValueMembers = "Processor";
            seCPU.Color = System.Drawing.Color.FromArgb(255, 0, 0);
            seCPU.XValueType = ChartValueType.DateTime;
          
            //  seCPU.IsValueShownAsLabel = true;//显示数据点的值
            // seCPU.MarkerStyle = MarkerStyle.Circle;
            mainChart.Series.Add(seCPU);
            //  mainChart.DataSource = dt;
            // mainChart.DataBind();
            seCPU.ToolTip = "X值:#VALX\nY值:#VALY";
            mainChart.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
            mainChart.ChartAreas[0].AxisX.LabelStyle.Angle =0;
            mainChart.ChartAreas[0].AxisY.LabelStyle.Format = "0.00";
          //  seCPU.MarkerSize = 5;
          //  seCPU.MarkerStyle = MarkerStyle.Circle;
            mainChart.MouseMove += mainChart_MouseMove;
            mainChart.MouseDown += mainChart_MouseDown;
            mainChart.MouseUp += mainChart_MouseUp;
        }
        private bool MouseDown = false;
        private Point ptDown;
        private Point ptUp;
        void mainChart_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                ptUp = new Point(e.X, e.Y);
                if (ptUp.X > ptDown.X)
                {
                    mainChart.ChartAreas[0].AxisY.Minimum = mainChart.ChartAreas[0].AxisY.PixelPositionToValue(ptUp.Y);
                    mainChart.ChartAreas[0].AxisY.Maximum = mainChart.ChartAreas[0].AxisY.PixelPositionToValue(ptDown.Y);

                    mainChart.ChartAreas[0].AxisX.Maximum = mainChart.ChartAreas[0].AxisX.PixelPositionToValue(ptUp.X);
                    mainChart.ChartAreas[0].AxisX.Minimum = mainChart.ChartAreas[0].AxisX.PixelPositionToValue(ptDown.X);
                }
                else
                {
                    mainChart.ChartAreas[0].AxisY.Minimum = System.Double.NaN;
                    mainChart.ChartAreas[0].AxisY.Maximum = System.Double.NaN;
                    mainChart.ChartAreas[0].AxisX.Maximum = System.Double.NaN;
                    mainChart.ChartAreas[0].AxisX.Minimum = System.Double.NaN;

                }
                MouseDown = false;
            }
            catch(Exception ex)
            {
                mainChart.ChartAreas[0].AxisY.Minimum = System.Double.NaN;
                    mainChart.ChartAreas[0].AxisY.Maximum = System.Double.NaN;
                    mainChart.ChartAreas[0].AxisX.Maximum = System.Double.NaN;
                    mainChart.ChartAreas[0].AxisX.Minimum = System.Double.NaN;
                MouseDown = false;
            }
        }

        void mainChart_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //throw new NotImplementedException();
            ptDown = new Point(e.X,e.Y);
            MouseDown = true;
        }

        void mainChart_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                var area = mainChart.ChartAreas[0];
                double xValue = 0;
                xValue = area.AxisX.PixelPositionToValue(e.X);
                double yValue = area.AxisY.PixelPositionToValue(e.Y);
                tbTEXT.Text = string.Format("{0:F0},{1:g}", yValue, DateTime.FromOADate(xValue));
            }
            catch(Exception r)
            {

            }
         //   throw new NotImplementedException();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
          //  mainChart.Scale(new System.Drawing.SizeF(600, 800));
            mainChart.Series[0].Points.Clear();
            for(int i = 0;i<100;i++)
            { //
                mainChart.Series[0].Points.AddXY(DateTime.Now+new TimeSpan(0,i,0), i);

            }
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF(Windows Presentation Foundation)是一种用于创建和管理Windows应用程序用户界面的框架。LVC(LiveCharts)是一个在WPF中创建动态、交互式和可视化图表的库。LVC曲线控件是LVC库的一部分,用于绘制各种类型的曲线图。 要使用WPF LVC曲线控件,首先需要在项目中引用LVC库。可以在Visual Studio中使用NuGet包管理器搜索并安装"LVC"。 在安装完成后,可以在XAML文件中创建曲线控件的实例。首先,需要添加正确的命名空间引用,例如: ```xaml xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" ``` 接下来,可以使用`lvc:CartesianChart`元素来创建曲线控件,并设置要显示的数据。例如,可以使用`SeriesCollection`属性添加曲线系列。 ```xaml <lvc:CartesianChart> <lvc:CartesianChart.Series> <lvc:LineSeries Title="Series 1" Values="10, 30, 15, 40" /> <lvc:LineSeries Title="Series 2" Values="20, 5, 25, 35" /> </lvc:CartesianChart.Series> </lvc:CartesianChart> ``` 在上面的示例中,创建了两个曲线系列,并设置了它们的标题和数值。可以通过添加更多的`LineSeries`元素来创建更多的曲线系列。 可以根据具体需求自定义曲线控件的外观和行为。可以设置各种属性,例如标题、坐标轴、图例、标签等等。还可以为曲线控件添加交互功能,例如缩放、平移、提示等。 通过这些步骤,就可以使用WPF LVC曲线控件创建动态、交互式和可视化的曲线图。使用LVC库的其他功能,还可以创建其他类型的图表,如饼图、柱状图、散点图等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值