Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stockchart label issue #1608

Open
prackode opened this issue Jul 10, 2024 · 13 comments
Open

stockchart label issue #1608

prackode opened this issue Jul 10, 2024 · 13 comments

Comments

@prackode
Copy link

I have created a label to show the price in the stockchart, but sometimes the background values seems to override .

Even though I gave the opaque color, then too.
something like this :

image

@martynasma
Copy link
Collaborator

Would you be able to post your whole chart and data on CodePen/jsFiddle so that we can test?

@prackode
Copy link
Author

prackode commented Jul 11, 2024

@martynasma
Below is the code for creating label.

_createUpdateLabel(lastData) {
    if (!this.currentValueDataItem) {
      let currentValueDataItem =  valueAxis.createAxisRange(
         valueAxis.makeDataItem({ value: 0 })
      );
      this.currentValueDataItem = currentValueDataItem;
    }

    let currentLabel = this.currentValueDataItem.get("label");

    if (currentLabel) {
      currentLabel.setAll({
        fill: am5.color(0xffffff),
      })
    }

    let closeValue = lastData?.Close;
    let openValue = lastData?.Open;

    this.currentValueDataItem.animate({
      key: "value",
      to: closeValue,
      duration: 500,
      easing: am5.ease.out(am5.ease.cubic),
    });

    // Ensure the existence or update of the label
    if (!currentLabel) {
      // If the label doesn't exist, create it
      currentLabel = this.currentValueDataItem.set("label", am5.Label.new( root, {}));
    }

    currentLabel.set("text",  .stockChart.getNumberFormatter().format(closeValue));
    currentLabel.set("opacity", 1);
    var bg = currentLabel.get("background") || currentLabel.set("background", am5.Rectangle.new( root, {}));

    if(bg){
      if(closeValue < openValue){
        bg.set("fill", am5.color("rgba(235, 91, 60, 1)"));
      }else{
        bg.set("fill", am5.color("rgba(10, 187, 146, 1)"));
      }
    }
  }

I don't want this :

image (1)

  • It should be opaque at any case.

@martynasma
Copy link
Collaborator

Have you tried setting fillOpacity: 0 label's background?

bg.set("fillOpacity", 1);

@prackode
Copy link
Author

Did that @martynasma
image (2)

Now my full code looks like

_createUpdateLabel(lastData) {
    if (!this.currentValueDataItem) {
      let currentValueDataItem =  valueAxis.createAxisRange(
         valueAxis.makeDataItem({ value: 0 })
      );
      this.currentValueDataItem = currentValueDataItem;
    }

    let currentLabel = this.currentValueDataItem.get("label");

    if (currentLabel) {
      currentLabel.setAll({
        fill: am5.color(0xffffff),
      })
    }

    let closeValue = lastData?.Close;
    let openValue = lastData?.Open;

    this.currentValueDataItem.animate({
      key: "value",
      to: closeValue,
      duration: 500,
      easing: am5.ease.out(am5.ease.cubic),
    });

    // Ensure the existence or update of the label
    if (!currentLabel) {
      // If the label doesn't exist, create it
      currentLabel = this.currentValueDataItem.set("label", am5.Label.new( root, {}));
    }
    currentLabel.set("text",  this.chartInstance.stockChart.getNumberFormatter().format(closeValue));
    currentLabel.set("opacity", 1);
    currentLabel.set("fillOpacity", 1);
    var bg = currentLabel.get("background") || currentLabel.set("background", am5.Rectangle.new( this.chartInstance.instance, {}));
    // bg.setAll({
    //   fill: am5.color(closeValue < openValue ? "rgba(235, 91, 60, 1)" : "rgba(10, 187, 146, 1)"),
    // });
    bg.set("fillOpacity", 1);
    if(bg){
      if(closeValue < openValue){
        bg.set("fill", am5.color("rgba(235, 91, 60, 1)"));
      }else{
        bg.set("fill", am5.color("rgba(10, 187, 146, 1)"));
      }
    }

@martynasma
Copy link
Collaborator

Are you seeing the same issue here?
https://www.amcharts.com/demos/live-stock-data/

@prackode
Copy link
Author

I got the issue, actually my valueAxis opacity was : 0.7 that's why it was giving the transparent look. Sorry @martynasma my bad.

But still I am able to see 2 ticks like this :
image (3)

code pen to see : https://codepen.io/Ansh-m-the-reactor/pen/bGPNQBL?editors=1010

@prackode
Copy link
Author

I don't want to see this two ticks in the label how can I achieve this ?
@martynasma

@martynasma
Copy link
Collaborator

Just disable it:

currentValueDataItem.get("tick").set("forceHidden", true);

@prackode
Copy link
Author

@martynasma How can I fix the width of the label, currently its dynamic according to the the number.

@martynasma
Copy link
Collaborator

Have you tried setting width on it?

@prackode
Copy link
Author

@martynasma I want it to sync it with the valueAxis width, so whatever the width of the valueAixs I want the same to be of lablel.
I tried getting the width of valueAxis but it was undefined, I guess it is dynamically set.

  • Either I can handle it dynamically and sync label width with the valueAxis.

OR

  • Sometime I get the value in decimals then it shows bigger label and sometimes it I get in whole number then it gets smaller.

So If I can make it constant? Like if we get the decimal value then show it else show ZERO after decimal number by this way the label size will be fixed.

@martynasma
Copy link
Collaborator

Maybe you can use an adapter:

currentLabel.adapters.add("width", function(width, target) {
  return yAxis.width();
});

Copy link

This issue is stale because it has been open 30 days with no activity. It will be closed in 5 days unless a new comment is added.

@github-actions github-actions bot added the stale label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants