leeoniya / uPlot

📈 A small, fast chart for time series, lines, areas, ohlc & bars
MIT License
8.48k stars 370 forks source link

Labels above all bar and lines #904

Closed YaminoYuki closed 4 months ago

YaminoYuki commented 4 months ago

I have a canvas with more than one chart type, I have line, bars, area, ....

The idea is the labels of lines have sometimes been drawn behind the label (I think this is because of the order of drawing , the bars were drawn after the lines which is why they are above the lines' labels)

Is there a way to make the labels above anything regardless of the order of play?

for example I am using the drawPoint function

` u.ctx.save();

u.ctx.font = font;
u.ctx.fillStyle = 'black';

uPlot.orient(
      // code goes here
);

u.ctx.restore();

`

`opts.series.forEach((s, i) => {
    if (i > 0 && !ignore.includes(i) && s?.paths) {
      uPlot.assign(s, {
        paths: barsBuilder,
        points: {
          show: disableDataLabels ? '' : drawPoints,
        },
      });
    } else {
      uPlot.assign(s, {
        points: {
          show: disableDataLabels ? true : drawPoints,
        },
      });
    }
  });`

can I control the labels to be over everything else ? something like zIndex or any thing

leeoniya commented 4 months ago

you can use the "draw" hook to draw on top of everything, but that's not a great solution. sometimes you need this to make sure point labels from different series don't collide.

i've wanted to add something like what you are asking, but it would not solve the label collision issue, and you're back to using "draw" hook.

so not sure if z index is enough. i think we'd have to collect text and coords from each series (without drawing), then call a single drawText at the end, or something?

also, drawing text on top for points that are behind (not visible) can become confusing.

YaminoYuki commented 4 months ago

@leeoniya

Thanx for the fast response

I think yes we might need to do so .. but I was trying to keep the original implementation which was taken from you and one of your demos .

thank you very much .. if I came up with something I will paste it here