1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
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 WinFormsChart
{
public partial class FormIntegraleSinus : Form
{
Series ptSeries1; //type Line
Series ptFilter; //type Area
ChartArea area;
public FormIntegraleSinus()
{
InitializeComponent();
this.chart1.MouseClick += new MouseEventHandler(chart1_MouseClick);
}
int oldBorderWidth ;
Color oldBorderColor ;
List<int> l = new List<int>();
void chart1_MouseClick(object sender, MouseEventArgs e)
{
if (l.Count == 2) //Reset selection. =>clear liste .
{
ptSeries1.Points[l[0]].BorderWidth = oldBorderWidth;
ptSeries1.Points[l[0]].BorderColor = oldBorderColor;
l.Clear();
ptFilter.Points.Clear();
}
//HitTest selection
HitTestResult result = chart1.HitTest(e.X, e.Y);
if ( result.ChartElementType == ChartElementType.DataPoint
&& result.ChartArea == area
&& result.Series == ptSeries1 )
{
// ajoute index du point selectionne
l.Add(result.PointIndex);
//memorise son style actuel pour restore
oldBorderWidth = ptSeries1.Points[result.PointIndex].BorderWidth;
oldBorderColor = ptSeries1.Points[result.PointIndex].BorderColor;
//feedback user :applique un style au point selectionne..
ptSeries1.Points[result.PointIndex].BorderWidth = 4;
ptSeries1.Points[result.PointIndex].BorderColor = Color.Tomato;
}
//s'il y a 2 point. =>dessine integrale ptFilter
if (l.Count == 2)
{
int startIndex =l[0];
int endIndex =l[1]+1;
for(int i= startIndex;i< endIndex ;i++)
{
DataPoint p = new DataPoint();
p.XValue = ptSeries1.Points[i].XValue;
p.YValues = ptSeries1.Points[i].YValues;
ptFilter.Points.Add(p);
}
}
}
private void FormIntegraleSinus_Load(object sender, EventArgs e)
{
area = chart1.ChartAreas[0];
ptSeries1 =this.chart1.Series[0];
ptFilter =this.chart1.Series[1];
GetCourbeSinus();
}
private void GetCourbeSinus()
{
Double dx = 0.5;
Double x=0.0 ;
Double y=0.0 ;
for (double i= -50.0; i< 51.0;i++)
{
x += dx;
y = 10.0 * Math.Sin(x);
ptSeries1.Points.AddXY(x, y);
}
}
}
} |