none
请问mschart如何能画出来一条竖线? RRS feed

  • 问题

  • 我试了,只给两个节点的坐标,(1000,0)和(1000,500),出来是一条斜线?另外,如何取到line上每个节点的坐标值?谢谢!
    2014年6月11日 6:26

答案

  • 你好,

    经过搜索,我找到了这段示例代码,你可以尝试一下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    using System.Windows.Forms.DataVisualization.Charting;
    
    namespace EE_Q_28234146
    {
        public partial class Form1 : Form
        {
            public Form1() {
                InitializeComponent();
            }
    
            double currentPriceOfUnderlying = 5.4;
    
            double[] xlines = { 2, 3.4, 5, 6.7, 8, 9 };
    
            private void Form1_Load(object sender, EventArgs e) {
                double totalAtThisStrike;
                for (double i = currentPriceOfUnderlying * .5; i < currentPriceOfUnderlying * 1.5; i = i + .1) {
                    totalAtThisStrike = somefunction(i);
                    chart1.Series["Series1"].Points.Add(new DataPoint(i, totalAtThisStrike));
                }
                chart1.ChartAreas["ChartArea1"].AxisX.Interval = .1;
    
                chart1.Series["Series1"].ChartType = SeriesChartType.Line;
                chart1.Series["Series1"].BorderWidth = 3;
                chart1.Series["Series1"].MarkerStyle = MarkerStyle.Diamond;
                chart1.Series["Series1"].MarkerSize = 7;
                chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(chart1_PostPaint);
            }
    
            void chart1_PostPaint(object sender, ChartPaintEventArgs e) {
                foreach (double xval in xlines) {
                    PaintVerticalChartLine(e.Chart, e.ChartGraphics, xval);
                }
            }
    
            private void PaintVerticalChartLine(Chart chart, ChartGraphics chartGraphics, double xval) {
    
                // should be globals
                double xvalMin = currentPriceOfUnderlying * .5;
                double xvalMax = currentPriceOfUnderlying * 1.5;
    
                if (xvalMin <= xval && xvalMax >= xval) {
                    double x = chart.ChartAreas["ChartArea1"].AxisX.ValueToPixelPosition(xval);
                    double y0 = chart.ChartAreas["ChartArea1"].AxisY.ValueToPixelPosition(chart.ChartAreas["ChartArea1"].AxisY.Minimum);
                    double y1 = chart.ChartAreas["ChartArea1"].AxisY.ValueToPixelPosition(chart.ChartAreas["ChartArea1"].AxisY.Maximum);
    
                    int pointMax = chart.Series["Series1"].Points.Count - 1;
                    int pointNum = (int)Math.Round((double)pointMax * (xval - xvalMin) / (xvalMax - xvalMin));
                    double y2 = (int)chart.ChartAreas["ChartArea1"].AxisY.ValueToPixelPosition(chart.Series["Series1"].Points[pointNum].YValues[0]);
    
                    chartGraphics.Graphics.DrawLine(new Pen(Color.Silver, 5), (float)x, (float)(y0 - 1), (float)x, (float)(y1 + 1));
                    chartGraphics.Graphics.DrawLine(new Pen(Color.Red, 3), (float)x, (float)(y0 - 2), (float)x, (float)y2);
                }
            }
    
            private double somefunction(double i) {
                return 25 * Math.Sin(2 * Math.PI * i / currentPriceOfUnderlying);
            }
        }
    }
            private void PaintVerticalChartLine(Chart chart, ChartGraphics chartGraphics, double xval) {
                double x = chart.ChartAreas["ChartArea1"].AxisX.ValueToPixelPosition(xval);
                double y0 = chart.ChartAreas["ChartArea1"].AxisY.ValueToPixelPosition(chart.ChartAreas["ChartArea1"].AxisY.Minimum);
                double y1 = chart.ChartAreas["ChartArea1"].AxisY.ValueToPixelPosition(chart.ChartAreas["ChartArea1"].AxisY.Maximum);
                chartGraphics.Graphics.DrawLine(new Pen(Color.Silver, 5), (float)x, (float)(y0 - 1), (float)x, (float)(y1 + 1));
            }



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月12日 6:36
    版主