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.

[SUPPORT] How to draw between grid lines based on time scale?

See original GitHub issue

Chartjs 2.5

I want each weekend (Saturday, Sunday) to be visibly distinguishable from the rest of the week. Here a background with a specific color (Vertical bar) should be applied / displayed. The background should go from eg. 03.06.2017 00:00 to 05.06.2017 00:00 (Thus stretching over the whole weekend)

My current solution is based on looping the data labels, however this is not getting me where I want, as sometimes I don’t have enough data entries to find the beginning and end draw position of each weekend.

Here my current code:

var weekendPlugin = {
	getLinePosition: function (chart, pointIndex) {
		var meta = chart.getDatasetMeta(0); // first dataset is used to discover X coordinate of a point
		return meta.data[pointIndex]._model.x;
	},
	renderWeekendBackground: function (chartInstance, fromIndex, toIndex) {
		var y_scale = chartInstance.scales['y-axis-0'];
		var context = chartInstance.chart.ctx;
		var	x1 = chartInstance.chartArea.left;

		var outsideOfGraph = (this.getLinePosition(chartInstance, fromIndex) < x1);

		var xPosition = outsideOfGraph ? x1 : this.getLinePosition(chartInstance, fromIndex);
		var width = this.getLinePosition(chartInstance, toIndex) - this.getLinePosition(chartInstance, fromIndex);
		if (outsideOfGraph) {
			 width = width - (x1 - this.getLinePosition(chartInstance, fromIndex));

			 if (width < 0) {
				width = 0;
			 }
		}

		context.save();

		context.globalCompositeOperation = 'destination-over';
		context.beginPath();
		context.rect(xPosition, y_scale.top, width, y_scale.bottom - y_scale.top);
		context.clip();
		context.fillStyle = "#fed426";
		context.fill();

		context.restore();

	},
	afterDatasetsDraw: function (chartInstance, easing) {
		var self = this;

		var fromId, toId;
		
		for (var idx in chartInstance.config.data.labels) {
			var date = chartInstance.config.data.labels[idx];
			
			var dateObj = moment(date, "YYYY/MM/DD,HH:mm:ss");
			if (dateObj.isoWeekday() === 6 || dateObj.isoWeekday() === 7) {
				if (!fromId) {
					fromId = idx;
				} else {
					toId = idx;
				}
			} else {
				if (fromId)) {
					self.renderWeekendBackground(chartInstance, fromId, toId ? toId : idx);
					fromId = undefined;
					toId = undefined;
				}
			}
		}
	}
};

Chart.plugins.register(weekendPlugin);

How could I approach this? How can I find out at which pixel each weekend in the current chart view begins and ends independently of the dataset?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
etimbergcommented, Jun 6, 2017

@SesioN right, but wouldn’t you know the dates of the weekends in advance and could pass those as the value to draw the box to in the annotation plugin? Or do you not necessarily know the range of the data?

0reactions
etimbergcommented, Jun 8, 2017

Awesome, hope it works out. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Grid Method - Art is Fun
In a nutshell, the grid method involves drawing a grid over your reference photo, and then drawing a grid of equal ratio on...
Read more >
GRID SCALE TECHNIQUE - YouTube
The grid method involves drawing a grid over your reference photo, and then drawing a grid of equal ratio on your work surface....
Read more >
Line drawing on a grid - Red Blob Games
Set N to the diagonal distance between the start and end point. Pick N+1 interpolation points, evenly spaced. Round those points to the...
Read more >
Draw gridlines in the run-time - NI Community
Hi everybody, I build a real-time plot on the base of scattergraph where Axis X is in stripchart mode. Now I would like...
Read more >
Video: Change grid spacing size - Microsoft Support
In the Ruler & Grid box, select Fixed for Grid spacing Horizontal and Grid spacing Vertical. Type the spacing you want between gridlines...
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