Discussion of article "Visualize this! MQL5 graphics library similar to 'plot' of R language"

New article Visualize this! MQL5 graphics library similar to 'plot' of R language has been published:

When studying trading logic, visual representation in the form of graphs is of great importance. A number of programming languages popular among the scientific community (such as R and Python) feature the special 'plot' function used for visualization. It allows drawing lines, point distributions and histograms to visualize patterns. In MQL5, you can do the same using the CGraphics class.

A zero divide error may occur when using the functions. There are two ways to handle this issue:

  • disable the check for zero divide in metaeditor.ini
    [Experts]
    FpNoZeroCheckOnDivision=1
  • or analyze an equation used in a function and return a valid value for such instances. An example of such handling using a macro can be found in the attached 3Functions.mq5 and bat.mq5 files.



Author: MetaQuotes Software Corp.

Good work done! Thanks. I was already going to fix the CircleAA algorithm
By the way, the CircleWu function can be simplified and accelerated:

void CCanvas::CircleWu(const int x,const int y,const double r,const uint clr,const uint style=UINT_MAX)
  {
   if(r<=0)
      return;
//--- preliminary calculations
   double r2=r*r;
   double quarter=round(r*M_SQRT1_2);
//--- set the line style
   uint prev_style=m_style;
   if(style!=UINT_MAX)
      LineStyleSet(style);
   uint mask=1<<m_style_idx;
//--- draw
   for(int dx=0; dx<=quarter; dx++)
     {
      double dy=sqrt(r2-dx*dx);
      double alpha1=dy-floor(dy);
      double alpha2=1-alpha1;
      if((m_style&mask)==mask)
        {
         PixelTransform4(x,y,dx,(int)(dy)+1,clr,alpha1);
         PixelTransform4(x,y,dx,(int)(dy),clr,alpha2);
         PixelTransform4(x,y,(int)dy+1,dx,clr,alpha1);
         PixelTransform4(x,y,(int)dy,dx,clr,alpha2);
        }
      mask<<=1;
      if(mask==0x1000000)
         mask=1;
     }
  
//--- set the previous line style
   if(style!=UINT_MAX)
      m_style=prev_style;
  }
Nikolai Semko:
Good work done! Thanks. I was already going to fix the CircleAA algorithm
By the way, the CircleWu function can be simplified and accelerated:
Thanks for the idea, edits will be made!

You can change the properties of the graph itself and each of its functions at any time. For example, let's add captions to the graph axes, change the curve name and enable spline approximation mode for it:

#include <Graphics\Graphic.mqh>
//+------------------------------------------------------------------+
//| Script programme start function|
//+------------------------------------------------------------------+
void OnStart()
  {
   CGraphic graphic;
   graphic.Create(0,"Graphic",0,30,30,780,380);
   double x[]={-10,-4,-1,2,3,4,5,6,7,8};
   double y[]={-5,4,-10,23,17,18,-9,13,17,4};
   CCurve *curve=graphic.CurveAdd(x,y,CURVE_LINES);
   curve.Name("Example");                 
   curve.LinesIsSmooth(true);             
   graphic.XAxis().Name("X - axis");      
   graphic.XAxis().NameSize(12);          
   graphic.YAxis().Name("Y - axis");      
   graphic.YAxis().NameSize(12);
   graphic.YAxis().ValuesWidth(15);
   graphic.CurvePlotAll();
   graphic.Update();
   DebugBreak();
  }

 У меня метод называется LinesSmooth LineIsSmooth нету. Продолжаю изучать дальше :)

 

Orangetree:

You can change the properties of the graph itself and each of its functions at any time. For example, let's add captions to the graph axes, change the curve name and enable spline approximation mode for it:

  {
   curve.LinesIsSmooth(true);             

 У меня метод называется LinesSmooth LineIsSmooth нету. Продолжаю изучать дальше :)

 

The method was renamed later. Thanks, corrected in the article

How can I make it so that when a curve is redrawn on the chart, the scale is also updated?

curve5.Update(resultPrices);
    
graphicfirst.Redraw(); и Update()


the curve itself is updated, but the scale with the old prices remains and the chart moves outside the canvas boundaries

I found it, I need to do CalculateMaxMinValues() :)

Maxim Dmitrievsky:
Could you tell me how to correctly redraw an already created chart, for example, when changing values in arrays? Redraw() and Update() have no effect

Good afternoon, how exactly did you try to redraw the chart?

If you just need to change the data for a particular curve, here is a working example:

#include <Graphics\Graphic.mqh>
//+------------------------------------------------------------------+
//| Script programme start function|
//+------------------------------------------------------------------+
void OnStart()
  {
//--- data 1
   double x1[]={-10,-4,-1,2,3,4,5,6,7,8};
   double y1[]={-5,4,-10,23,17,18,-9,13,17,4};
//--- data 2
   double x2[]={-10,-10,10,10};
   double y2[]={-10,10,-10,10};
//--- graphic
   CGraphic graph;
   graph.HistoryNameWidth(80);
   graph.Create(0,"Graph",0,30,30,830,430);
//--- Points
   CCurve *curve=graph.CurveAdd(x1,y1,CURVE_LINES);
   graph.CurvePlot(0);
   graph.Update();
   Sleep(1000);
   curve.Update(x2,y2);
   graph.Redraw(true);
   graph.Update();
   Sleep(1000);
  }
P.S. There is a thread on the forum regarding the Graphics library, where this issue has already been raised.
Roman Konopelko:

Good afternoon, how exactly were you trying to redraw the graph?

If you just need to change the data for a particular curve, here is a working example:

P.S. There is a thread on the forum regarding the Graphics library, in it this question has already been raised.
thanks, I've already figured it out, I was doing exactly the same thing, only on one array... you just need to recalculate graph.CalculateMaxMinValues() for the graph as well, then the scale is updated.
Maxim Dmitrievsky:
Thanks, I've already figured it out, I did exactly the same thing, only on one array. you just need to recalculate graph.CalculateMaxMinValues() for the graph as well, then the scale is updated
If you call CGraphic::Redraw(const bool rescale=false) method with the parameter true, the scales will be recalculated automatically (no need to call CalculateMaxMinValues separately).
Roman Konopelko:
If you call the CGraphic::Redraw(const bool rescale=false) method with the parameter true, the scales will be recalculated automatically (no need to call CalculateMaxMinValues separately).

Right, got it. thanks )

Is it possible to make two or more scales on the graph - similar to a ruler, where the divisions are given as centimetres and inches, with values only given if the point was used to plot the graph?

To add comments, please log in or register