Use the. net Chart control to draw a graph. netchart

Source: Internet
Author: User

Use the. net Chart control to draw a graph. netchart

During software development, we may encounter situations where charts need to be generated. NET is often used to draw with GDI, although the effect is good, since. NET 4.0, the Chart control generated specifically for drawing the Chart appeared. With it, you can easily draw the graph and column Chart you need. If you are not talking about it, directly attach the code to draw a curve, scale the view, comment a lot, and you will understand it at a glance.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. windows. forms; 6 using System. windows. forms. dataVisualization. charting; 7 using System. drawing; 8 9 namespace DrawGraph 10 {11 /// <summary> 12 // plot the curve class 13 /// </summary> 14 public static class DrawClass 15 {16 # region plot the curve function 17 // <summary> 18 // plot the curve function 19 /// </summary> 20 // <param name = "listX"> X value set </ param> 21 // <param name = "listY"> Y value set </param> 22 // <param name = "chart"> Chart Control </param> 23 public static void DrawSpline (List <int> listX, list <double> listY, Chart chart) 24 {25 try 26 {27 // X, Y value member 28 chart. series [0]. points. dataBindXY (listX, listY); 29 chart. series [0]. points. dataBindY (listY); 30 31 // point color 32 chart. series [0]. markerColor = Color. green; 33 // set the chart type to a spline curve 34 chart. series [0]. chartType = SeriesChartType. spline; 35 // set the point size to 36 chart. series [0]. markerSize = 5; 37 // set the curve color 38 chart. series [0]. color = Color. orange; 39 // set the curve width to 40 chart. series [0]. borderWidth = 2; 41 // chart. series [0]. customProperties = "PointWidth = 4"; 42 // you can specify whether to display the coordinate 43 chart. series [0]. isValueShownAsLabel = false; 44 45 // set the cursor 46 chart. chartAreas [0]. cursorX. isUserEnabled = true; 47 chart. chartAreas [0]. cursorX. autoScroll = true; 48 chart. chartAreas [0]. cursorX. isUserSelectionEnabled = true; 49 // sets whether the X axis can be scaled to 50 chart. chartAreas [0]. axisX. scaleView. zoomable = true; 51 // place the scroll bar outside the chart 52 chart. chartAreas [0]. axisX. scrollBar. isPositionedInside = false; 53 // set the size of the scroll bar 54 chart. chartAreas [0]. axisX. scrollBar. size = 15; 55 // set the style of the scroll bar button. The code below shows all the buttons on the scroll bar 56 chart. chartAreas [0]. axisX. scrollBar. buttonStyle = ScrollBarButtonStyles. all; 57 chart. chartAreas [0]. axisX. scrollBar. buttonColor = Color. skyBlue; 58 // you can specify a minimum of 59 charts to automatically zoom in and out. chartAreas [0]. axisX. scaleView. smallScrollSize = double. naN; 60 chart. chartAreas [0]. axisX. scaleView. smallScrollMinSize = 1; 61 // sets the scale interval 62 chart. chartAreas [0]. axisX. interval = 10; 63 // remove the 64 chart from the grid on the X axis. chartAreas [0]. axisX. majorGrid. enabled = false; 65 // the X axis and Y axis title 66 chart. chartAreas [0]. axisX. title = "ring"; 67 chart. chartAreas [0]. axisY. title = "diameter"; 68 // you can set the Y axis range to re-Modify 69 double max = listY [0] according to the actual situation; 70 double min = listY [0]; 71 foreach (var yValue in listY) 72 {73 if (max <yValue) 74 {75 max = yValue; 76} 77 if (min> yValue) 78 {79 min = yValue; 80} 81} 82 chart. chartAreas [0]. axisY. maximum = max; 83 chart. chartAreas [0]. axisY. minimum = min; 84 chart. chartAreas [0]. axisY. interval = (max-min)/10; 85 // bind the data source 86 chart. dataBind (); 87} 88 catch (Exception exc) 89 {90 MessageBox. show (exc. toString (); 91} 92} 93 # endregion 94 95 # region click the mouse and display the cursor through the ring number, and zoom to the response position 96 /// <summary> 97 /// click the mouse to display the cursor through the ring number, and scaled to the response position function 98 // </summary> 99 // <param name = "ringNum"> ring number </param> 100 // <param name = "chart"> Chart Control </param> 101 public static void ShowCurByClick (int ringNum, chart chart) 102 {103 // set the cursor position 104 chart. chartAreas [0]. cursorX. position = ringNum; 105 // you can specify a 106 chart for scaling a view. chartAreas [0]. axisX. scaleView. zoom (ringNum-1, ringNum + 2); 107 // change the curve width by 108 chart. series [0]. borderWidth = 3; 109 // change the X axis scale interval by 110 chart. chartAreas [0]. axisX. interval = 1; 111} 112 # endregion113 114} 115}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.