amcharts / amcharts5

The newest, fastest, and most advanced amCharts charting library for JavaScript and TypeScript apps.
Other
353 stars 95 forks source link

How to fire the drawing tool on the button active. #1673

Closed workingbuddy10 closed 2 months ago

workingbuddy10 commented 3 months ago

Currently on the button click I want to initiate the 'Horizontal Line' .

I tried looking in the codebase there is a protected function _setTool which does the job : https://github.com/amcharts/amcharts5/blob/1e2243d7153ef537807abeced990b66dbc01fce3/src/.internal/charts/stock/toolbar/DrawingControl.ts#L838

I tried doing this in my codebase but it is taking 2 steps and it didn't worked :

                series.enableDrawing();
                this._currentEnabledSeries.push(series);

To be more precise my code snippet looks like this :

      document.getElementById("anchor")?.classList.remove("active");
      this.selectedDrawingControl?._maybeInitToolSeries(tool);
      const seriesList = this.selectedDrawingControl?._drawingSeries[tool];
      if (seriesList && seriesList.length > 0) {
        seriesList[0].enableDrawing();
        this.selectedDrawingControl._currentEnabledSeries.push(seriesList[0]);
      }

I want if I click on my button (BT) the horizontal line draws. codepen : https://codepen.io/Ansh-m-the-reactor/pen/JjQyrqB

martynasma commented 3 months ago

You can just set "tool" setting on the drawing tool instance:

selectedDrawingControl.set("tool", "Horizontal Line");
workingbuddy10 commented 3 months ago

Did same in the codepen @martynasma It didn't wokred : https://codepen.io/Ansh-m-the-reactor/pen/JjQyrqB

martynasma commented 3 months ago

It does work. It switches the tool, but it does not enable drawing mode. If you need it enabled, you need to do enable by setting active: true on drawing control:

drawingControl.setAll({
  active: true,
  tool: "Horizontal Line"
});

Related docs: https://www.amcharts.com/docs/v5/charts/stock/toolbar/drawing-control/#Enabling_draw_mode

workingbuddy10 commented 3 months ago

@martynasma As you can see in the below video :

https://github.com/user-attachments/assets/b72a01f7-eb60-4be3-8247-a96f0afd8cce

https://github.com/user-attachments/assets/c3985da2-b868-4bc2-a2ac-7a7f0f630731

workingbuddy10 commented 2 months ago

@martynasma

And the second issue :

codepen link : https://codepen.io/Ansh-m-the-reactor/pen/JjQyrqB?editors=0010

workingbuddy10 commented 2 months ago

Hey @martynasma ,Sorry for tagging! Any lead on this would be very helpful!!

workingbuddy10 commented 2 months ago

So will be able to draw the line via clicking on the button? As explained here https://github.com/amcharts/amcharts5/issues/1673#issuecomment-2293182147

If yes, then how it will look like?

martynasma commented 2 months ago

I'll let you know the details, when the new version is out later this week.

martynasma commented 2 months ago

Implemented in 5.10.2.

[5.10.2] - 2024-08-23

Added

Fixed

Full change log.

Download options.

Make sure you clear your browser cache after upgrading. And feel free to contact us again if you are still experiencing this issue.

martynasma commented 2 months ago

Refer to this tutorial for info and example: https://www.amcharts.com/docs/v5/charts/stock/toolbar/drawing-control/#Adding_line_drawings

workingbuddy10 commented 2 months ago

@martynasma a minor enhancement : as you know we can create a horizontal line by clicking on the plus button.

In Trading Technology charts, one can draw the same line by double clicking on the valueAxis. So it will be drawn at that price point. Can we have this, any function?

martynasma commented 2 months ago

You can add a dblclick event on an axis, and use addLine() in the event handler.

workingbuddy10 commented 2 months ago

Sorry, I should have framed the question in a better way. Yes, I can draw the line on ddclick on valueAxis. I want to show the label at which price the line is marked. Like this : image

And if I click on the eraser button of drawings, both the line and its respective label should get delete. If I click on the delete all, the all the drawings with respect to its label should get removed.

martynasma commented 2 months ago

In that case you should probably add an axis range rather than horizontal line drawing.