c# - Get coordinates of a drawing point from Chart Values in TextBoxes? -


in designer have 2 textboxes. , chart control. want when type in first textbox number 120 , in second 1 type number 1 draw point on chart in 120,1 mean 120 , 1 axis x , axis y values.

chart

the red filled circle not @ 120 , 1. mean red circle should drawn on left axis on 120. , if put instead 120 116 , instead 1 25 circle should drawn @ left axis 116 , on bottom axis on 25.

but circle drawn out of chart.

this code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.windows.forms.datavisualization.charting; using system.drawing.drawing2d; using system.collections;  namespace test {     public partial class form1 : form     {         private point startpoint = new point();         private point endpoint = new point();         private int x = 0;         private int y = 0;         private list<point> points = new list<point>();         private point lastpoint = point.empty;         private arraylist mypts = new arraylist();          public form1()         {             initializecomponent();         }          private void button1_click(object sender, eventargs e)         {             random rdn = new random();             (int = 120; > 0; i--)             {                 chart1.series["series1"].points.addxy                     (rdn.next(0, 10), rdn.next(0, 10));             }              chart1.series["series1"].charttype = seriescharttype.fastline;             chart1.series["series1"].color = color.red;              chartarea area = chart1.chartareas[0];              area.axisx.minimum = 1;             area.axisx.maximum = 30;             area.axisy.minimum = 1;             area.axisy.maximum = 120;              lineannotation line = new lineannotation();             point p1 = new point(1, 120);             chart1.annotations.add(line);             line.axisx = area.axisx;             line.axisy = area.axisy;             line.issizealwaysrelative = false;             line.x = 1; line.y = 120;             line.right = 30; line.bottom = 1;             line.linecolor = color.blue;             line.linewidth = 3;         }          solidbrush mybrush = new solidbrush(color.red);         private void chart1_paint(object sender, painteventargs e)         {                         graphics g = e.graphics;             foreach (point p in mypts)             g.fillellipse(mybrush, p.x, p.y, 10, 10);                     }          private void chart1_mouseclick(object sender, mouseeventargs e)         {             mypts.add(new point(x,y));             chart1.invalidate();         }          private void txtt_textchanged(object sender, eventargs e)         {             x = int.parse(txtweight.text);         }          private void txtdays_textchanged(object sender, eventargs e)         {             y = int.parse(txtdays.text);         }     } } 

what did after enter both textboxes values when click anywhere on chart control area mouse should draw circule on coordinates textboxes.

but circle not drawing on right place.

the textbox name txtt left 1 axis on left values. textbox txtdays should axis on bottom values.

the task of translating drawing coordinates datapoints , not intuitive.

it possible need know rules , pay price.

i have outlined way in post , worth looking into..

but problem coming repeatedly, here more general solution.

here how call it:

private void button11_click(object sender, eventargs e) {     valuepoints.add(new pointf(640, 1));     valuepoints.add(new pointf(670, 10));     painttocalaculate = true;     chart1.invalidate(); } 

and here result: 2 red points drawn @ values 640, 1 , 670, 10: enter image description here

the points placed correctly event though have zoomed , scrolled in chart , resized it..

to make work need 3 class level variables: flag , 2 point lists. 1 list input values in chart points are, other output, receiving current pixel coordinates.

bool painttocalaculate = false; list<point> drawpoints = new list<point>(); list<pointf> valuepoints = new list<pointf>(); 

i use flag avoid recalculating points when system causes redraws. , after setting trigger paint event invalidating chart. during paint event reset flag.

please note these values volatile: change:

  • whenever zoom or scroll
  • whenever resize chart
  • whenever layout changes, maybe because new points need room or trigger change in label format..

therefore drawing coordinates have updated on each such event!

here 1 example, takes care of zoom , scrolling:

private void chart1_axisviewchanged(object sender, vieweventargs e) {     painttocalaculate = true;     chart1.invalidate(); } 

you need add these 2 lines, or function wrap them, few other spots in program, depending things allow happen in chart.. resize obvious candidate..

now actual caculation. using valuetopixelposition, work. unfortunately works inside of of 3 paint events of chart (prepaint,paint , postpaint) . here use normal paint event.

private void chart1_paint(object sender, painteventargs e) {     if (painttocalaculate)     {         series s = chart1.series.findbyname("dummy");         if (s == null) s = chart1.series.add("dummy");         drawpoints.clear();         s.points.clear();         foreach (pointf p in valuepoints)         {             s.points.addxy(p.x, p.y);             datapoint pt = s.points[0];             double x = chart1.chartareas[0].axisx.valuetopixelposition(pt.xvalue);             double y = chart1.chartareas[0].axisy.valuetopixelposition(pt.yvalues[0]);             drawpoints.add(new point((int)x, (int)y));             s.points.clear();         }         painttocalaculate = false;         chart1.series.remove(s);     }     //..     // can draw our points @ current positions:     foreach (point p in drawpoints)         e.graphics.fillellipse(brushes.red, p.x - 2, p.y - 2, 4, 4); } 

note add , remove dummy series , add , clear 1 point each data point, calculate pixel coordinates. yes, bit involved, results worth it..

i assume can change button_click code read in values textboxes instead of using hard-coded numbers..


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -