How can I show all Timeline Highcharts data without any overlapping? - highcharts

I have data like this:
var data = [{
x: Date.UTC(1951, 5, 22),
name: 'First dogs in space',
label: 'fds',
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'</span><br/>{point.label}'
},
}, {
x: Date.UTC(1957, 9, 4),
name: 'First artificial satellite',
label: 'First artificial satellite',
}, {
x: Date.UTC(1959, 0, 4),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
}, {
x: Date.UTC(1961, 3, 12),
name: 'First human spaceflight',
label: 'First human spaceflight',
}, {
x: Date.UTC(1966, 1, 3),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
}];
Here is the fiddle link for your reference: Fiddle
Screenshot for reference:
I want to show all the label without any overlap right now if the date is near about then it's overlapped.
I tried :
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold; white-space:nowrap;" > ' +
'{point.x:%d-%b-%Y}</span><br/>{point.label}'
},
But it's only shows points instead of label when overlapped.

You can make more space for the labels by changing extremes:
Highcharts.chart('container', {
...
}, function() {
const min = Math.max(this.series[0].points.length - 3, 0);
const max = Math.max(this.series[0].points.length - 1, 0);
this.xAxis[0].setExtremes(
this.series[0].points[min].x,
this.series[0].points[max].x
);
});
Live demo: https://jsfiddle.net/BlackLabel/e82jtrsw/
or overwrite distributeDL timeline series method to make more use of the available space:
Example:
(function(H) {
H.seriesTypes.timeline.prototype.distributeDL = function() {
var series = this,
dataLabelsOptions = series.options.dataLabels,
options,
pointDLOptions,
newOptions = {},
visibilityIndex = 1,
j = 2,
distance;
series.points.forEach(function(point, i) {
distance = dataLabelsOptions.distance;
if (point.visible && !point.isNull) {
options = point.options;
pointDLOptions = point.options.dataLabels;
if (!series.hasRendered) {
point.userDLOptions = H.merge({}, pointDLOptions);
}
if (i === j || i === j + 1) {
distance = distance * 2.5
if (i === j + 1) {
j += 4
}
}
newOptions[series.chart.inverted ? 'x' : 'y'] =
dataLabelsOptions.alternate && visibilityIndex % 2 ?
-distance : distance;
options.dataLabels = H.merge(newOptions, point.userDLOptions);
visibilityIndex++;
}
});
}
}(Highcharts));
Live demo: https://jsfiddle.net/BlackLabel/fa2obzwq/
Docs: https://www.highcharts.com/docs/extending-highcharts

Related

How can I faded label in Highchart Timeline if it's not selected?

I have data like this:
var data = [{
x: Date.UTC(1951, 5, 22),
name: 'First dogs in space',
label: 'fds',
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'</span><br/>{point.label}'
},
}, {
x: Date.UTC(1957, 9, 4),
name: 'First artificial satellite',
label: 'First artificial satellite',
}, {
x: Date.UTC(1959, 0, 4),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
}, {
x: Date.UTC(1961, 3, 12),
name: 'First human spaceflight',
label: 'First human spaceflight',
}, {
x: Date.UTC(1966, 1, 3),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
}];
I want when user select on any label that label is highlighted and left all other gonna faded.
Also what I want in this when I move the cursor on the faded label it's changing label color so that user can click on that label.
I have added Fiddle link below:
enter link description here
For your reference.
Kindly help me out.
You can achieve it using Custom-Events plugin: https://www.highcharts.com/products/plugin-registry/single/15/Custom-Events.
Code:
series: [{
dataLabels: {
events: {
mouseover: function() {
if (!this.dataLabel.isSelected) {
this.dataLabel.attr({
fill: '#efefef'
});
}
},
mouseout: function() {
if (!this.dataLabel.isSelected) {
this.dataLabel.attr({
fill: '#fff'
});
}
},
click: function() {
var point = this.point,
series = point.series;
series.points.forEach(function(p) {
p.dataLabel.attr({
fill: '#fff'
});
p.dataLabel.isSelected = false;
});
this.dataLabel.attr({
fill: 'red'
});
this.dataLabel.isSelected = true;
}
}
},
marker: {
symbol: 'circle'
},
data: data,
}]
Demo:
https://jsfiddle.net/BlackLabel/u01hyk2x/

How we can redraw the chart keeping the occurred events on click in Highcharts?

I have data like this:
var data = [{
x: Date.UTC(1951, 5, 22),
name: 'First dogs in space',
label: 'fds',
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'</span><br/>{point.label}'
},
}, {
x: Date.UTC(1957, 9, 4),
name: 'First artificial satellite',
label: 'First artificial satellite',
}, {
x: Date.UTC(1959, 0, 4),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
}, {
x: Date.UTC(1961, 3, 12),
name: 'First human spaceflight',
label: 'First human spaceflight',
}, {
x: Date.UTC(1966, 1, 3),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
}];
Highcharts.chart('container', {
chart: {
// zoomType: 'x',
type: 'timeline'
},
xAxis: {
type: 'datetime',
max: 5,
visible: false,
},
scrollbar: {
enabled: data.length < 10 ? false : true
},
yAxis: {
gridLineWidth: 1,
title: null,
labels: {
enabled: false
}
},
plotOptions: {
series: {
cursor: 'pointer',
}
},
legend: {
enabled: false
},
title: {
text: ''
},
subtitle: {
text: ''
},
// colors: [fr_color,nfr_color],
colors: [
'#FFC160',
'#ff9993',
],
tooltip: {
style: {
width: 300
}
},
series: [{
point: {
events: {
click: function(data) {
var points = this.series.points;
for (var i = points.length; i >= 0; i--) {
if (i !== this.index) {
this.dataLabel.attr({
fill: ''
});
// points[i].remove(false);
} else {
i = -1;
}
}
this.series.chart.redraw();
this.dataLabel.attr({
fill: '#5daf34'
});
THIS.GetinfoToCorrespondingTimeline(THIS.rejections_array,data.point.colorIndex,data.point.label);
}
}
},
dataLabels: {
allowOverlap: true,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'{point.x:%d %b %Y}</span><br/>{point.label}'
},
marker: {
symbol: 'circle'
},
data: data,
}]
});
And what I want in this is:
When I click on any label it's color changes to green and when I scroll and doing any event that color removed. and what I want it to maintain it's color throughout the movement.
At every event click, I want to redraw the chart keeping the selected event.
Why I want this?
After event click my div width gonna change ex: when I click on any event my width is changed 100 to 70 or 60 % and after that, some part of the chart is hidden because of the sudden change in width. That's why I want to redraw the chart so that it can manage width size again.
Initially, it looks like this:
After clicking on any event:
What I tried:
redraw: function(event) {
this.chart.redraw();
this.series.redraw();
}
And also this code in click function: this.series.redraw();
Fiddle for reference.
You can store some flag as a point property and set the color again in redraw event function. I have refactored your code a bit:
chart: {
type: 'timeline',
events: {
redraw: function() {
var series = this.series[0];
series.points.forEach(function(p) {
if (p.customDataLabel && p.dataLabel.fill !== '#5daf34') {
p.dataLabel.attr({
fill: '#5daf34'
});
}
});
}
}
},
...,
series: [{
point: {
events: {
click: function(data) {
var points = this.series.points;
for (var i = points.length; i >= 0; i--) {
if (i !== this.index) {
if (points[i]) {
points[i].dataLabel.attr({
fill: '#fff'
});
points[i].customDataLabel = false;
}
}
}
this.customDataLabel = true;
this.dataLabel.attr({
fill: '#5daf34'
});
}
}
},
...
}]
Live demo: https://jsfiddle.net/BlackLabel/ek23uhop/
Image for reference:

How can I make some events are clickable or others not in Timeline Highchart?

I have data like this:
var data = [{
x: Date.UTC(1951, 5, 22),
name: 'First dogs in space',
label: 'fds',
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'</span><br/>{point.label}'
},
}, {
x: Date.UTC(1957, 9, 4),
name: 'First artificial satellite',
label: 'First artificial satellite',
}, {
x: Date.UTC(1959, 0, 4),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
}, {
x: Date.UTC(1961, 3, 12),
name: 'First human spaceflight',
label: 'First human spaceflight',
}, {
x: Date.UTC(1966, 1, 3),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
}];
Here is the fiddle link for your reference: Fiddle
In which I want some clickable event or others not according to my condition.
Ex: If the label name is "First artificial satellite to reach the Moon" and "First soft landing on the Moon" It won't be clickable and others are clickable.
Screenshot for reference:
You could check if it is "not clickable" within the click processing (JSFiddle). First define those labels not clickable:
var unclickable = ["First artificial satellite to reach the Moon", "First soft landing on the Moon"];
Then implement the check in click to prevent processing, as well as showing the default cursor when hovering the points in mouseOver:
series: [{
point: {
events: {
mouseOver: function() {
if(unclickable.includes(this.label)) {
this.dataLabel.element.style.setProperty('cursor', 'default');
this.dataLabel.text.element.style.setProperty('cursor', 'default');
}
},
click: function(data) {
if(!unclickable.includes(this.label)) {
// ...
}
}
}
}
}]
Be aware that your example JSFiddle had some characters in the labels, which meant that regular didn't get a match when checking for includes. Make sure you have only regular spaces, or you have to be very exact when matching the label against another string.

How can I show last 10 indexes in Highcharts Timeline and left using scroll?

I have data like this:
var data = [{
x: Date.UTC(1951, 5, 22),
name: 'First dogs in space',
label: 'fds',
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'</span><br/>{point.label}'
},
}, {
x: Date.UTC(1957, 9, 4),
name: 'First artificial satellite',
label: 'First artificial satellite',
}, {
x: Date.UTC(1959, 0, 4),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
}, {
x: Date.UTC(1961, 3, 12),
name: 'First human spaceflight',
label: 'First human spaceflight',
}, {
x: Date.UTC(1966, 1, 3),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
}];
Fiddle For refrence:
Fiddle
In this, I want to show the last 10 indexes Timeline and left indexes I can access through scrolling back.
And also I want when I click on any label div size is changed to random size So I want it's redraw() the chart and adjust according to div size.
If you have static values you can make sure the 10 last indexes are shown by setting them as min and max for the x-axis. You could calculate this before setting up the chart options if you know the data. In your case (JSFiddle):
xAxis: {
type: 'datetime',
min: Date.UTC(1961, 3, 12),
max: Date.UTC(1998, 10, 20),
visible: false,
},
If it is dynamic you can do it in postprocessing of the chart by finding the min and max for the axis by looking at the points of your series. For example (JSFiddle):
Highcharts.chart('container', {
// ...
}, function() {
const min = Math.max(this.series[0].points.length - 10, 0);
const max = Math.max(this.series[0].points.length - 1, 0);
this.xAxis[0].setExtremes(this.series[0].points[min].x, this.series[0].points[max].x);
});

How can I highlight selected label in HIghcharts Timeline?

I have data like this:
Js fiddle
Highcharts.chart('container', {
chart: {
zoomType: 'x',
type: 'timeline'
},
xAxis: {
type: 'datetime',
visible: false
},
yAxis: {
gridLineWidth: 1,
title: null,
labels: {
enabled: false
}
},
legend: {
enabled: false
},
title: {
text: 'Timeline of Space Exploration'
},
subtitle: {
text: 'Info source: www.wikipedia.org'
},
tooltip: {
style: {
width: 300
}
},
series: [{
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'{point.x:%d %b %Y}</span><br/>{point.label}'
},
marker: {
symbol: 'circle'
},
data: [{
x: Date.UTC(1951, 5, 22),
name: 'First dogs in space',
label: '<div class="row">Granted chances = 38%<div class="col-sm-12">Interview = 24</div><div class="col-sm-12">Amendment = 24</div><div class="col-sm-12">Appeal = 24</div></div>',
dataLabels: {
allowOverlap: false,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'</span><br/>{point.label}'
},
}, {
x: Date.UTC(1957, 9, 4),
name: 'First artificial satellite',
label: 'First artificial satellite',
}, {
x: Date.UTC(1959, 0, 4),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
}, {
x: Date.UTC(1961, 3, 12),
name: 'First human spaceflight',
label: 'First human spaceflight',
}, {
x: Date.UTC(1966, 1, 3),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
}]
}]
});
Now I want to highlight the selected label Ex:
When I click on the first index which is 3 Feb 1996. The color of label gonna change and after the first index all gonna faded.
Like this:
And all other indexes after 3 Feb gonna fade or invisible Can you help me out from this?
I have tried below code in dataLabels as well as plotOption but it does not work for me.
allowPointSelect: true,
states: {
select: {
color: null,
borderWidth:5,
label:'Blue'
}
},
You can use point.events.click function to change a dataLabel color and remove points with a bigger index than the clicked one:
series: [{
point: {
events: {
click: function() {
var points = this.series.points;
for (var i = points.length - 1; i >= 0; i--) {
if (i !== this.index) {
points[i].remove(false);
} else {
i = -1;
}
}
this.series.chart.redraw();
this.dataLabel.attr({
fill: 'blue'
});
}
}
},
...
}]
Live demo: https://jsfiddle.net/BlackLabel/6ctuga0L/
API Reference:
https://api.highcharts.com/highcharts/series.timeline.point.events.click
https://api.highcharts.com/class-reference/Highcharts.Point#remove

Resources