之前一直用GDI绘图,后面公司要求使用WPF,网上WPF资料太少(可能自己没找到吧),自己写了个测试用,可以拖动。

前端代码

  1. <Window x:Class="Wpf绘图.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Window1" Height="600" Width="800" Loaded="Window_Loaded_1">
  5. <Grid>
  6. <Grid.ColumnDefinitions>
  7. <ColumnDefinition/>
  8. </Grid.ColumnDefinitions>
  9. <Grid.RowDefinitions>
  10. <RowDefinition Height="30"/>
  11. <RowDefinition Height="*"/>
  12. </Grid.RowDefinitions>
  13. <ToolBar Grid.Row="0">
  14. <Label Name="moushPonit" Foreground="Red">11</Label>
  15. </ToolBar>
  16. <Canvas Grid.Row="1" Name="MainCanvas" Background="#FFBBBCBF"
  17. MouseMove="MainCanvas_MouseMove"
  18. MouseLeftButtonDown="MainCanvas_MouseLeftButtonDown" MouseLeftButtonUp="MainCanvas_MouseLeftButtonUp" SizeChanged="MainCanvas_SizeChanged" ClipToBounds="True">
  19. <Canvas.RenderTransform>
  20. <TransformGroup>
  21. <ScaleTransform x:Name="sfr" />
  22. <TranslateTransform x:Name="tlt" />
  23. </TransformGroup>
  24. </Canvas.RenderTransform>
  25. </Canvas>
  26. </Grid>
  27. </Window>

  

后台代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14.  
  15. namespace Wpf绘图
  16. {
  17. /// <summary>
  18. /// Window1.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class Window1 : Window
  21. {
  22. /// <summary>
  23. /// 画板宽度
  24. /// </summary>
  25. double BoardWidth { get; set; }
  26. /// <summary>
  27. /// 画板高度
  28. /// </summary>
  29. double BoardHeight { get; set; }
  30. /// <summary>
  31. /// 垂直(纵向)边距(画图区域距离左右两边长度)
  32. /// </summary>
  33. double VerticalMargin { get; set; }
  34. /// <summary>
  35. /// 平行(横向)边距(画图区域距离左右两边长度)
  36. /// </summary>
  37. double HorizontalMargin { get; set; }
  38. /// <summary>
  39. /// 水平刻度间距像素
  40. /// </summary>
  41. double horizontalBetween { get; set; }
  42. /// <summary>
  43. /// 垂直刻度间距像素
  44. /// </summary>
  45. double verticalBetween { get; set; }
  46.  
  47. /// <summary>
  48. /// x轴最大值
  49. /// </summary>
  50. public double MaxX { get; set; }
  51.  
  52. /// <summary>
  53. /// y轴最大值
  54. /// </summary>
  55. public double MaxY { get; set; }
  56.  
  57. /// <summary>
  58. /// x轴最小值
  59. /// </summary>
  60. public double MinX { get; set; }
  61.  
  62. /// <summary>
  63. /// y轴最小值
  64. /// </summary>
  65. public double MinY { get; set; }
  66.  
  67. /// <summary>
  68. /// 图表区域宽度
  69. /// </summary>
  70. double ChartWidth;
  71. /// <summary>
  72. /// 图表区域高度
  73. /// </summary>
  74. double CharHeight;
  75. /// <summary>
  76. /// 画图区域起点
  77. /// </summary>
  78. Point StartPostion;
  79. /// <summary>
  80. /// 画图区域终点
  81. /// </summary>
  82. Point EndPostion;
  83. /// <summary>
  84. /// 数据源
  85. /// </summary>
  86. PointCollection DataSourse;
  87.  
  88. double MapLocationX = 0;
  89. double MapLocationY = 0;
  90. //鼠标按下去的位置
  91. Point startMovePosition;
  92. TranslateTransform totalTranslate = new TranslateTransform();
  93. TranslateTransform tempTranslate = new TranslateTransform();
  94. ScaleTransform totalScale = new ScaleTransform();
  95. Double scaleLevel = 1;
  96.  
  97. public Window1()
  98. {
  99. InitializeComponent();
  100. DataSourse = GetCollPoint();
  101. }
  102.  
  103. private void MainCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  104. {
  105. startMovePosition = e.GetPosition((Canvas)sender);
  106. }
  107.  
  108. private void MainCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  109. {
  110. /*
  111. Point endMovePosition = e.GetPosition((Canvas)sender);
  112.  
  113. totalTranslate.X += (endMovePosition.X - startMovePosition.X) / scaleLevel;
  114. totalTranslate.Y += (endMovePosition.Y - startMovePosition.Y) / scaleLevel;
  115. */
  116. }
  117.  
  118. private void MainCanvas_MouseMove(object sender, MouseEventArgs e)
  119. {
  120. Point currentMousePosition = e.GetPosition((UIElement)sender);
  121. moushPonit.Content = currentMousePosition.X.ToString() + "," + currentMousePosition.Y.ToString();
  122.  
  123. if (e.LeftButton == MouseButtonState.Pressed)
  124. {
  125. MapLocationX = MapLocationX + currentMousePosition.X - startMovePosition.X;
  126. MapLocationY = MapLocationY + currentMousePosition.Y - startMovePosition.Y;
  127.  
  128. startMovePosition = currentMousePosition;
  129.  
  130. Refresh();
  131. /*
  132. Point deltaPt = new Point(0, 0);
  133. deltaPt.X = (currentMousePosition.X - startMovePosition.X) / scaleLevel;
  134. deltaPt.Y = (currentMousePosition.Y - startMovePosition.Y) / scaleLevel;
  135.  
  136. tempTranslate.X = totalTranslate.X + deltaPt.X;
  137. tempTranslate.Y = totalTranslate.Y + deltaPt.Y;
  138.  
  139. TransformGroup tfGroup = new TransformGroup();
  140. tfGroup.Children.Add(tempTranslate);
  141. tfGroup.Children.Add(totalScale);
  142. foreach (UIElement ue in MainCanvas.Children)
  143. {
  144. ue.RenderTransform = tfGroup;
  145. }*/
  146. }
  147.  
  148. }
  149.  
  150. private void MainCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
  151. {
  152. Refresh();
  153. }
  154.  
  155. private void Refresh()
  156. {
  157. InitCanvas();
  158.  
  159. //获取y最大值
  160. if (MaxY < 0.0001)
  161. {
  162. MaxY = DataSourse.Max(m => m.Y);
  163. }
  164. //MinY = DataSourse.Min(m => m.Y);
  165.  
  166. if (MaxX < 0.0001)
  167. {
  168. MaxX = DataSourse.Max(m => m.X);
  169. }
  170. //MinX = DataSourse.Min(m => m.X);
  171. if (Math.Abs(MaxX) < 0.000001 || Math.Abs(MaxY) < 0.000001)
  172. {
  173. return;
  174. }
  175.  
  176. DrawAxis();
  177. DrawXAxisTicks();
  178. DrawYAxisTicks();
  179. DrawPolyline();
  180. }
  181.  
  182. private void InitCanvas()
  183. {
  184. MainCanvas.Children.Clear();
  185.  
  186. BoardWidth = MainCanvas.ActualWidth - SystemParameters.VerticalScrollBarWidth;
  187. BoardHeight = MainCanvas.ActualHeight - SystemParameters.HorizontalScrollBarHeight;
  188. HorizontalMargin = 40;
  189. VerticalMargin = 40;
  190. //horizontalBetween = 50;
  191. //verticalBetween = 50;
  192. ChartWidth = BoardWidth - 2 * HorizontalMargin;//画图区域宽度
  193. CharHeight = BoardHeight - 2 * VerticalMargin; //画图区域高度
  194.  
  195. StartPostion = new Point(HorizontalMargin, VerticalMargin);
  196. EndPostion = new Point(BoardWidth - HorizontalMargin, BoardHeight - VerticalMargin);
  197.  
  198. }
  199.  
  200. private void DrawPolyline()
  201. {
  202. var polyline = new Polyline();
  203. foreach (var t in DataSourse)
  204. {
  205. polyline.Points.Add(GetRealPoint(t));
  206. }
  207. polyline.Stroke = Brushes.Blue;
  208. MainCanvas.Children.Add(polyline);
  209. }
  210.  
  211. private Point GetRealPoint(Point point)
  212. {
  213. var realX = StartPostion.X + (point.X - MinX) * ChartWidth / (MaxX - MinX) + MapLocationX;
  214. var realY = StartPostion.Y + (MaxY - point.Y) * CharHeight / (MaxY - MinY) + MapLocationY;
  215. return new Point(realX, realY);
  216. }
  217.  
  218. /// <summary>
  219. /// 画y轴刻度
  220. /// </summary>
  221. private void DrawYAxisTicks()
  222. {
  223. if (MinY >= MaxY)
  224. {
  225. return;
  226. }
  227. if (verticalBetween < 0.0001)
  228. {
  229. verticalBetween = (MaxY - MinY) / 10;
  230. }
  231. for (var i = MinY; i <= MaxY + 0.01; i += verticalBetween)
  232. {
  233. var y = EndPostion.Y - i * CharHeight / (MaxY - MinY) + MapLocationY;
  234. var marker = new Line
  235. {
  236. X1 = StartPostion.X - 5,
  237. Y1 = y,
  238. X2 = StartPostion.X,
  239. Y2 = y,
  240. Stroke = Brushes.Red
  241. };
  242. MainCanvas.Children.Add(marker);
  243.  
  244. //画y轴字符
  245. var markText = new TextBlock
  246. {
  247. Text = i.ToString(CultureInfo.InvariantCulture),
  248. Width = 30,
  249. Foreground = Brushes.Yellow,
  250. FontSize = 10,
  251. HorizontalAlignment = HorizontalAlignment.Right,
  252. TextAlignment = TextAlignment.Right
  253. };
  254. MainCanvas.Children.Add(markText);
  255. Canvas.SetTop(markText, y - 10);
  256. Canvas.SetLeft(markText, 00);
  257. }
  258. }
  259.  
  260. /// <summary>
  261. /// 画x轴标签
  262. /// </summary>
  263. private void DrawXAxisTicks()
  264. {
  265. if (MinX >= MaxX)
  266. {
  267. return;
  268. }
  269. if (horizontalBetween < 0.0001)
  270. {
  271. horizontalBetween = (MaxX - MinX) / 10;
  272. }
  273. for (var i = MinX; i <= MaxX + 0.01; i += horizontalBetween)
  274. {
  275. var x = StartPostion.X + i * ChartWidth / (MaxX - MinX) + MapLocationX;
  276. var marker = new Line
  277. {
  278. X1 = x,
  279. Y1 = EndPostion.Y,
  280. X2 = x,
  281. Y2 = EndPostion.Y+4,
  282. Stroke = Brushes.Red
  283. };
  284. MainCanvas.Children.Add(marker);
  285.  
  286. var gridLine = new Line
  287. {
  288. X1 = x,
  289. Y1 = StartPostion.Y,
  290. X2 = x,
  291. Y2 = EndPostion.Y,
  292. StrokeThickness = 1,
  293. Stroke = new SolidColorBrush(Colors.AliceBlue)
  294. };
  295. MainCanvas.Children.Add(gridLine);
  296.  
  297. //画x轴字符
  298. var text = i.ToString(CultureInfo.InvariantCulture);
  299. var markText = new TextBlock
  300. {
  301. Text = text,
  302. Width = 130,
  303. Foreground = Brushes.Yellow,
  304. VerticalAlignment = VerticalAlignment.Top,
  305. HorizontalAlignment = HorizontalAlignment.Stretch,
  306. TextAlignment = TextAlignment.Left,
  307. FontSize = 15
  308. };
  309.  
  310. //Transform st = new SkewTransform(0, 0);
  311. //markText.RenderTransform = st;
  312. MainCanvas.Children.Add(markText);
  313. Canvas.SetTop(markText, EndPostion.Y + 5);
  314. Canvas.SetLeft(markText, x);
  315. }
  316.  
  317. }
  318.  
  319. /// <summary>
  320. /// X轴Y轴
  321. /// </summary>
  322. private void DrawAxis()
  323. {
  324. var xaxis = new Line
  325. {
  326. X1 = StartPostion.X,
  327. Y1 = EndPostion.Y,
  328. X2 = EndPostion.X,
  329. Y2 = EndPostion.Y,
  330. Stroke = new SolidColorBrush(Colors.Black)
  331. };
  332. MainCanvas.Children.Add(xaxis);
  333.  
  334. var yaxis = new Line
  335. {
  336. X1 = StartPostion.X,
  337. Y1 = StartPostion.Y,
  338. X2 = StartPostion.X,
  339. Y2 = EndPostion.Y,
  340. Stroke = new SolidColorBrush(Colors.Black)
  341. };
  342. MainCanvas.Children.Add(yaxis);
  343. }
  344.  
  345. /// <summary>
  346. /// 获取数据源
  347. /// </summary>
  348. /// <returns></returns>
  349. private PointCollection GetCollPoint()
  350. {
  351. PointCollection myPointCollection = new PointCollection()
  352. {
  353. new Point(1,12),
  354. new Point(2,20),
  355. new Point(3,50),
  356. new Point(4,21),
  357. new Point(6,10),
  358. new Point(21,90)
  359. };
  360.  
  361. return myPointCollection;
  362. }
  363.  
  364. private void Window_Loaded_1(object sender, RoutedEventArgs e)
  365. {
  366.  
  367. }
  368. }
  369. }

  

WPF 绘制曲线图的更多相关文章

  1. 封装:WPF绘制曲线视图

    原文:封装:WPF绘制曲线视图 一.目的:绘制简单轻量级的曲线视图 二.实现: 1.动画加载曲线 2.点击图例显示隐藏对应曲线 3.绘制标准基准线 4.绘制蒙板显示标准区域 曲线图示例: 心电图示例: ...

  2. 使用.net 的Chart控件绘制曲线图

    在进行软件开发过程中我们可能会碰到需要生成图表的情况,在.NET中以前经常用GDI去绘制,虽然效果也不错,自从.NET 4.0开始,专门为绘制图表而生的Chart控件出现了,有了它,就可以轻松的绘制你 ...

  3. WPF绘制党徽(立体效果,Cool)

    原文:WPF绘制党徽(立体效果,Cool) 前面用WPF方式绘制了党旗(WPF制作的党旗) ,去年3月份利用C# 及GDI+绘制过党徽,这次使用WPF来绘制党徽. ------------------ ...

  4. Highcharts绘制曲线图小结

    Higcharts绘制曲线图很好用! 虽然说Highcharts官网有API 刚接触这个领域,学有心得,理解不到位之处希望大家多多指教! 项目绘制的曲线是:平均水位随时间的变化而改变的水情走势图. 主 ...

  5. WPF绘制自定义窗口

    原文:WPF绘制自定义窗口 WPF是制作界面的一大利器,下面就用WPF模拟一下360的软件管理界面,360软件管理界面如下: 界面不难,主要有如下几个要素: 窗体的圆角 自定义标题栏及按钮 自定义状态 ...

  6. WPF绘制深度不同颜色的3D模型填充图和线框图

    原文:WPF绘制深度不同颜色的3D模型填充图和线框图 在机械测量过程中,测量的数据需要进行软件处理.通常测量一个零件之后,需要重建零件的3D模型,便于观察测量结果是否与所测工件一致. 重建的3D模型需 ...

  7. [原译]WPF绘制圆角多边形

    原文:[原译]WPF绘制圆角多边形 介绍 最近,我发现我需要个圆角多边形.而且是需要在运行时从用户界面来绘制.WPF有多边形.但是不支持圆角.我搜索了一下.也没找到可行的现成例子.于是就自己做吧.本文 ...

  8. WPF绘制折线

    WPF后台绘制折线,填充到一个GRID下 private void btnPreview_Click(object sender, RoutedEventArgs e) { GridImg.Child ...

  9. WPF绘制矢量图形模糊的问题

    WPF默认提供了抗锯齿功能,通过向外扩展的半透明边缘来实现模糊化.由于WPF采用了设备无关单位,当设备DPI大于系统DPI时,可能会产生像素自动扩展问题,这就导致线条自动向外扩展一个像素,并且与边缘相 ...

随机推荐

  1. Idea中类上有叉的解决方法

    idea中类的头上出现X解决办法 ctrl+alt+s 在弹出的菜单上选择Compiler下的Excludes 右边会有 移除掉,点击ok, 重启idea就可以了

  2. RestExpress response中addHeader 导致stackOverflow

    问题描述: 最近在项目使用中要在restExpress的header中增加一个键值对,同事在使用的时候没有对header的value进行非空判断,于是在测试环境测试的时候就出现了一个异常

  3. I2C的小结

    下面是 I 2 C 总线的一些特征 只要求两条总线线路 一条串行数据线 SDA 一条串行时钟线 SCL 每个连接到总线的器件都可以通过唯一的地址和一直存在的简单的主机 从机关系软件设定地 址 主机可以 ...

  4. Delphi RTTI的应用(一)

    1.获取DbgrdiEH 某一个选项的属性.加载到ComBox procedure TForm1.FormCreate(Sender: TObject); var PropInfo: PPropInf ...

  5. Mybatis-PageHelper分页插件

    PageHelper.startPage 静态方法调用 除了 PageHelper.startPage 方法外,还提供了类似用法的 PageHelper.offsetPage 方法. 在你需要进行分页 ...

  6. java中封装类(一)

    java中封装类共九个,分别是Boolean,Byte,Short,Integer,Long,Float,Double,Character,Void 其中Void对于使用者并无多大意义,也不可以构造任 ...

  7. 在delphi中XLSReadWriteII.组件的应用实例(2)

    第三方组件:XLSReadWriteII.v.5.20.67_XE3 实例源码如下:   unit Unit1; interface uses Winapi.Windows, Winapi.Messa ...

  8. [转]IIS应用程序池经典模式转集成模式解决方案

    经典模式和集成模式的区别: IIS7.0中的Web应用程序有两种配置形式:经典形式和集成形式. 经典形式是为了与之前的版本兼容,运用ISAPI扩展来调用ASP.NET运转库,原先运转于IIS6.0下的 ...

  9. easyui多图片上传+预览切换+支持IE8

    引入css和js: <link href="${pageContext.request.contextPath}/plugin/dialog/dialog.css" rel= ...

  10. 89. Gray Code返回位运算的所有生成值

    [抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...