question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Box annotation starts from center of category axes

See original GitHub issue

I need box annotation the same as the purple box on the example but it doesn’t work as expected on my chart. I need it to be started and finished on axes lines, not the center!

Here is the config:

chart.options.annotation.annotations.push({
   drawTime: "beforeDatasetsDraw",
   type: "box",
   xScaleID: "x-axis-0",
   yScaleID: "y-axis-0",
   xMin: "2018 December",
   xMax: "2019 February",
   yMin: 0,
   yMax: 200000,
   backgroundColor: "rgba(101, 33, 171, 0.5)"
});
chart.update();

and scales config:

"scales": {
	"yAxes": [{stacked: true}],
	"xAxes": [{stacked: true}]
}

Also Adding absolute min/max or offset parameters will be nice. Any help will be appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AmirHosseincommented, Jan 12, 2021

@danielissac Unfortunately, it didn’t work for me. I created the plugin below for my case:

Chart.pluginService.register({
    id: "highlightBarColumns",
    beforeDatasetsDraw: function (chart, foo, options) {
        options = options || {};
        options.color = options.color || 'rgba(0,0,0,0.1)';
        options.indexes = options.indexes || [];
        var axeX = chart.scales[chart.config.options.scales.xAxes[0].id];
        var axeY = chart.scales[chart.config.options.scales.yAxes[0].id];
        var ctx = chart.chart.ctx;
        options.indexes.forEach(function (index) {
            if (axeX.ticks.length > index) {
                var tickPosition = axeX.getPixelForTick(index);
                var width = index > 0 ? tickPosition - axeX.getPixelForTick(index - 1) : axeX.ticks.length > 1 ? axeX.getPixelForTick(index + 1) - tickPosition : axeX.width;
                var x = tickPosition - width / 2;
                var y = axeY.getPixelForValue(axeY.max);
                var height = axeY.getPixelForValue(axeY.min) - y;
                ctx.fillStyle = options.color;
                ctx.fillRect(x, y, width, height);
                ctx.fill();
            }
        });
    }
});
...
var chart = new Chart(ctx, {
    ...
    options: {
        plugins: {
            highlightBarColumns: {
                color: 'rgba(0,0,0,0.1)',
                indexes: [1, 2, 5]
            }
        },
        ...
    }
});
1reaction
kurklecommented, Jan 12, 2021

gridlines are offset, you can change that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

9.1. Annotations — VisIt User Manual 3.1 documentation
These annotations convey a good deal of information about what is being visualized, what values are in the plots, and who created the...
Read more >
How to add annotations and decorations to charts - think-cell
Learn how to add value axes, CAGRs, difference arrows and legends to think-cell charts.
Read more >
matplotlib.axes.Axes.annotate — Matplotlib 3.6.2 documentation
The exact starting point position of the arrow is defined by relpos. It's a tuple of relative coordinates of the text box, where...
Read more >
Managing Axes in OVERLAY Layouts - SAS Help Center
If PRIMARY=TRUE for a plot within an overlay-type layout, that plot's data columns and type are used to determine the default axis features, ......
Read more >
Axes appearance and behavior - MATLAB - MathWorks
Axes properties control the appearance and behavior of an Axes object. By changing property values, you can modify certain aspects of the axes....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found