Resize Custom Charts created with CGraphic

 

I want to create a small Chart showing the graph of a funktion. If I click on the chart, the chart should enlarge, to be able to see all details.

Creating the small chart works quite well:

Small Chart after initial drawing


Now I want to click on the chart to enlarge the chart.

Problem 1:

I can't find any posibility to resize the chart after I created it . Is there a possibilty to resize a CGraphic Chart?

Problem 2:

Then I tried to Destroy the small chart and to create a new larger chart with a different CGraphic object. If I do that. The small chart disapears and the large chart is displayed.

Again, if I click on the large Chart, the large chart disapears and the small chart will be displayed. But now without the Axis and I see two (same) graphs (I now it because, two lines are displayed):

Small Chart

My impression is, that if I use CGraphic.Destroy() the Axis aren't destroyed!?


Global Definition:

CGraphic          GoalGraph;
CGraphic          GoalGraphLarge;


Function to draw om chart:

//+------------------------------------------------------------------+
//| Formel                                                                  |
//+------------------------------------------------------------------+
double ZinzFormel(double x)
  {
   double KapEnd;
   long KapStart = 600;

   KapEnd = 600*pow(1.0+(1.0/100),x);

   return (KapEnd);
  }


Code to Draw Chart:
One function is for the small chart (as shown below) and nearly the same function (PlotGoalLarge) just with different size parameters and the second CGraphic Object GoalGraphLarge .
(The "bSize" part is obsolete).



//+------------------------------------------------------------------+
//| Plot trading Goal                                                |
//+------------------------------------------------------------------+
void PlotGoal(bool bSize, string objName)
  {

   double from=1;
   double to=200;
   double step=0.21;

   string FName;
   int FSize;
   uint FFlags;
   uint FWinkel;

   int  X1;
   int  X2;
   int  Y1;
   int  Y2;

   long ChartHeight = ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
   long ChartWidth = ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);


   if(bSize)
     {
      //Plotbereich ist groß
      X1 = 300;
      Y1 = (int)ChartHeight-330;
      X2 = 900;
      Y2 = (int)ChartHeight-30;
     }
   else
     {
      //Plotbereich ist klein
      X1 = 10;
      Y1 = (int)ChartHeight-130;
      X2 = 200;
      Y2 = (int)ChartHeight-30;
     }

   GoalGraph.Create(0,objName,0,X1,Y1,X2,Y2);


   GoalGraph.GapSize(1);
   GoalGraph.FontGet(FName, FSize, FFlags,  FWinkel);
   bool reusut = GoalGraph.FontSet(FName, 100, 0,0);
   int einzug = GoalGraph.IndentRight();
   GoalGraph.IndentRight(5);
   GoalGraph.IndentLeft(-15);
   GoalGraph.HistoryNameWidth(0);
   GoalGraph.HistorySymbolSize(0);

   GoalGraph.XAxis().ValuesSize(8);
   GoalGraph.XAxis().Min(0);
   GoalGraph.XAxis().Max(200);

   GoalGraph.YAxis().ValuesSize(8);

   if(bSize)
     {
      GoalGraph.XAxis().AutoScale(true);
      GoalGraph.YAxis().AutoScale(true);

     }
   else
     {
      GoalGraph.XAxis().AutoScale(false);
     }


//--- Farben
   CColorGenerator generator;
   uint blue= generator.Next();
   uint red = generator.Next();
   uint orange=generator.Next();

//--- Zeichnen aller Kurven
   GoalGraph.CurveAdd(ZinzFormel,from,to,step,orange,CURVE_LINES);

   GoalGraph.CurvePlotAll();
   GoalGraph.Update();

  }



And the Code within the ChartEvent function  to handle MouseClick Events:

 if(id==CHARTEVENT_OBJECT_CLICK)
     {
      Print("Clicking a mouse button on an object named '"+sparam+"'");
      if(sparam == PlotName)
        {
         GoalGraph.Destroy();
         bPlotsizeLarge = true;
         PlotGoalLarge(bPlotsizeLarge,PlotName+"large");
        }

      if(sparam == PlotName+"large")
        {
         GoalGraphLarge.Destroy();
         bPlotsizeLarge = false;
         PlotGoal(bPlotsizeLarge,PlotName);
        }
     }


Plese give me a hint how to resize a Chart Object (or how to properly Destroy a CGraphic Object).

 

I found the Problem.

To bring up the missing Axis you I had to add the Command CalculateMaxMinValues bevor plotting and updating the Graph:

GoalGraph.CalculateMaxMinValues();
GoalGraph.CurvePlotAll();
GoalGraph.Update();


Second issue in my code was, that I added the curves every time I called the PlotGoal function. This has to be done just once.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...