#javascript #html #css #highcharts
#javascript #HTML #css #highcharts
Вопрос:
Возможно ли отредактировать временную шкалу в Highcharts, чтобы она соответствовала изображению?
У меня есть рабочий пример временной шкалы, который я хочу, но я не могу получить цветовое кодирование / фильтрацию. Я пытаюсь фильтровать таким образом, чтобы при нажатии на фильтр уменьшалась непрозрачность других элементов.
(ДОПОЛНИТЕЛЬНЫЙ ВОПРОС: возможно ли создать детализацию на временной шкале? Не прошу помощи в кодировании, просто возможность)
Вот мой код временной шкалы:
<script>
(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 amp;amp; !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 amp;amp; visibilityIndex % 2 ?
-distance : distance;
options.dataLabels = H.merge(newOptions, point.userDLOptions);
visibilityIndex ;
}
});
}
}(Highcharts));
Highcharts.dateFormats = {
q: function(timestamp) {
var date = new Date(timestamp),
quarter = (Math.floor(date.getUTCMonth() / 3) 1);
console.log(quarter);
return quarter;
}
};
Highcharts.chart('container', {
chart: {
zoomType: 'x',
type: 'timeline'
},
//xAxis: {
//type: 'datetime',
//visible: true
//},
xAxis: {
type: 'datetime',
units: [ // responsive ticks
['month', [3, 6]],
['year', [1]]
],
labels: {
format: '{value:Q%q'%y}',
//rotation: -30
}
},
yAxis: {
gridLineWidth: 1,
title: null,
labels: {
enabled: false
}
},
legend: {
enabled: false
},
title: {
text: '<strong><font size=" 10">INITIATIVE TIMELINE</font></strong>'
},
subtitle: {
text: ''
},
tooltip: {
style: {
width: 300
}
},
credits: {
enabled: false
},
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}'
},
dataLabels: {
distance: 60,
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'
},
series: {
showInNavigator: true
},
data: [{
x: Date.UTC(1951, 5),
name: 'First dogs in space',
label: 'First dogs in space',
description: "Dezik and Tsygan were the first dogs to make a sub-orbital flight on 22 July 1951. Both dogs were recovered unharmed after travelling to a maximum altitude of 110 km."
}, {
x: Date.UTC(1957, 9),
name: 'First artificial satellite',
label: 'First artificial satellite',
description: "Sputnik 1 was the first artificial Earth satellite. The Soviet Union launched it into an elliptical low Earth orbit on 4 October 1957, orbiting for three weeks before its batteries died, then silently for two more months before falling back into the atmosphere."
}, {
x: Date.UTC(1959, 0),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
description: "Luna 1 was the first artificial satellite to reach the Moon vicinity and first artificial satellite in heliocentric orbit."
}, {
x: Date.UTC(1961, 3),
name: 'First human spaceflight',
label: 'First human spaceflight',
description: "Yuri Gagarin was a Soviet pilot and cosmonaut. He became the first human to journey into outer space when his Vostok spacecraft completed one orbit of the Earth on 12 April 1961."
}, {
x: Date.UTC(1966, 1),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
description: "Yuri Gagarin was a Soviet pilot and cosmonaut. He became the first human to journey into outer space when his Vostok spacecraft completed one orbit of the Earth on 12 April 1961."
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
description: "Apollo 11 was the spaceflight that landed the first two people on the Moon. Commander Neil Armstrong and lunar module pilot Buzz Aldrin, both American, landed the Apollo Lunar Module Eagle on July 20, 1969, at 20:17 UTC."
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
description: "Salyute 1 was the first space station of any kind, launched into low Earth orbit by the Soviet Union on April 19, 1971. The Salyut program followed this with five more successful launches out of seven more stations."
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
description: "Mars 3 was an unmanned space probe of the Soviet Mars program which spanned the years between 1960 and 1973. Mars 3 was launched May 28, 1971, nine days after its twin spacecraft Mars 2. The probes were identical robotic spacecraft launched by Proton-K rockets with a Blok D upper stage, each consisting of an orbiter and an attached lander."
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
description: "Helios-A and Helios-B (also known as Helios 1 and Helios 2) are a pair of probes launched into heliocentric orbit for the purpose of studying solar processes. A joint venture of West Germany's space agency DFVLR (70 percent share) and NASA (30 percent), the probes were launched from Cape Canaveral Air Force Station, Florida."
}, {
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
description: "The Pioneer Venus Orbiter entered orbit around Venus on December 4, 1978, and performed observations to characterize the atmosphere and surface of Venus. It continued to transmit data until October 1992."
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
description: "was a space station that operated in low Earth orbit from 1986 to 2001, operated by the Soviet Union and later by Russia. Mir was the first modular space station and was assembled in orbit from 1986 to 1996. It had a greater mass than any previous spacecraft."
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
description: "Hipparcos was a scientific satellite of the European Space Agency (ESA), launched in 1989 and operated until 1993. It was the first space experiment devoted to precision astrometry, the accurate measurement of the positions of celestial objects on the sky."
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
description: "The International Space Station (ISS) is a space station, or a habitable artificial satellite, in low Earth orbit. Its first component was launched into orbit in 1998, with the first long-term residents arriving in November 2000.[7] It has been inhabited continuously since that date."
}]
}]
});
</script>
Спасибо за любую помощь.
Ответ №1:
Это возможно, но требует нескольких изменений и пользовательских конфигураций, например, отмены установки legendType
внутри серии и colorByPoint
. Кроме того, потребуется распределить точки между отдельными сериями и установить для них разные расстояния между метками данных. Я отредактировал ваш пример, поэтому, пожалуйста, взгляните на него.
(function(H) {
H.seriesTypes.timeline.prototype.getXExtremes = function(xData) {
var series = this;
return {
min: H.arrayMin(xData),
max: H.arrayMax(xData)
};
}
}(Highcharts));
Highcharts.dateFormats = {
q: function(timestamp) {
var date = new Date(timestamp),
quarter = (Math.floor(date.getUTCMonth() / 3) 1);
return quarter;
}
};
Highcharts.chart('container', {
chart: {
zoomType: 'x',
type: 'timeline'
},
//xAxis: {
//type: 'datetime',
//visible: true
//},
xAxis: {
type: 'datetime',
units: [ // responsive ticks
['month', [3, 6]],
['year', [1]]
],
labels: {
format: '{value:Q%q'%y}',
//rotation: -30
}
},
yAxis: {
gridLineWidth: 1,
title: null,
labels: {
enabled: false
}
},
legend: {
enabled: true
},
title: {
text: '<strong><font size=" 10">INITIATIVE TIMELINE</font></strong>'
},
subtitle: {
text: ''
},
tooltip: {
style: {
width: 300
}
},
credits: {
enabled: false
},
plotOptions: {
timeline: {
lineWidth: 0,
legendType: '',
showInLegend: true,
colorByPoint: false,
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'
},
}
},
series: [{
dataLabels: {
distance: 40
},
data: [{
x: Date.UTC(1951, 5),
name: 'First dogs in space',
label: 'First dogs in space',
description: "Dezik and Tsygan were the first dogs to make a sub-orbital flight on 22 July 1951. Both dogs were recovered unharmed after travelling to a maximum altitude of 110 km."
}, {
x: Date.UTC(1971, 11, 2),
name: 'First soft Mars landing',
label: 'First soft Mars landing',
description: "Mars 3 was an unmanned space probe of the Soviet Mars program which spanned the years between 1960 and 1973. Mars 3 was launched May 28, 1971, nine days after its twin spacecraft Mars 2. The probes were identical robotic spacecraft launched by Proton-K rockets with a Blok D upper stage, each consisting of an orbiter and an attached lander."
}, {
x: Date.UTC(1976, 3, 17),
name: 'Closest flyby of the Sun',
label: 'Closest flyby of the Sun',
description: "Helios-A and Helios-B (also known as Helios 1 and Helios 2) are a pair of probes launched into heliocentric orbit for the purpose of studying solar processes. A joint venture of West Germany's space agency DFVLR (70 percent share) and NASA (30 percent), the probes were launched from Cape Canaveral Air Force Station, Florida."
}]
}, {
dataLabels: {
distance: 90
},
data: [{
x: Date.UTC(1959, 0),
name: 'First artificial satellite to reach the Moon',
label: 'First artificial satellite to reach the Moon',
description: "Luna 1 was the first artificial satellite to reach the Moon vicinity and first artificial satellite in heliocentric orbit."
}, {
x: Date.UTC(1966, 1),
name: 'First soft landing on the Moon',
label: 'First soft landing on the Moon',
description: "Yuri Gagarin was a Soviet pilot and cosmonaut. He became the first human to journey into outer space when his Vostok spacecraft completed one orbit of the Earth on 12 April 1961."
}, {
x: Date.UTC(1989, 7, 8),
name: 'First astrometric satellite',
label: 'First astrometric satellite',
description: "Hipparcos was a scientific satellite of the European Space Agency (ESA), launched in 1989 and operated until 1993. It was the first space experiment devoted to precision astrometry, the accurate measurement of the positions of celestial objects on the sky."
}]
}, {
dataLabels: {
distance: 140
},
data: [{
x: Date.UTC(1978, 11, 4),
name: 'First orbital exploration of Venus',
label: 'First orbital exploration of Venus',
description: "The Pioneer Venus Orbiter entered orbit around Venus on December 4, 1978, and performed observations to characterize the atmosphere and surface of Venus. It continued to transmit data until October 1992."
}, {
x: Date.UTC(1986, 1, 19),
name: 'First inhabited space station',
label: 'First inhabited space station',
description: "was a space station that operated in low Earth orbit from 1986 to 2001, operated by the Soviet Union and later by Russia. Mir was the first modular space station and was assembled in orbit from 1986 to 1996. It had a greater mass than any previous spacecraft."
}, {
x: Date.UTC(1998, 10, 20),
name: 'First multinational space station',
label: 'First multinational space station',
description: "The International Space Station (ISS) is a space station, or a habitable artificial satellite, in low Earth orbit. Its first component was launched into orbit in 1998, with the first long-term residents arriving in November 2000.[7] It has been inhabited continuously since that date."
}]
}, {
dataLabels: {
distance: 190
},
data: [{
x: Date.UTC(1957, 9),
name: 'First artificial satellite',
label: 'First artificial satellite',
description: "Sputnik 1 was the first artificial Earth satellite. The Soviet Union launched it into an elliptical low Earth orbit on 4 October 1957, orbiting for three weeks before its batteries died, then silently for two more months before falling back into the atmosphere."
}, {
x: Date.UTC(1961, 3),
name: 'First human spaceflight',
label: 'First human spaceflight',
description: "Yuri Gagarin was a Soviet pilot and cosmonaut. He became the first human to journey into outer space when his Vostok spacecraft completed one orbit of the Earth on 12 April 1961."
}, {
x: Date.UTC(1969, 6, 20),
name: 'First human on the Moon',
label: 'First human on the Moon',
description: "Apollo 11 was the spaceflight that landed the first two people on the Moon. Commander Neil Armstrong and lunar module pilot Buzz Aldrin, both American, landed the Apollo Lunar Module Eagle on July 20, 1969, at 20:17 UTC."
}, {
x: Date.UTC(1971, 3, 19),
name: 'First space station',
label: 'First space station',
description: "Salyute 1 was the first space station of any kind, launched into low Earth orbit by the Soviet Union on April 19, 1971. The Salyut program followed this with five more successful launches out of seven more stations."
}]
}]
});
#container {
min-width: 310px;
max-width: 800px;
height: 600px;
margin: 0 auto
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/timeline.src.js"></script>
<div id="container"></div>
Комментарии:
1. Идеальный ответ. Спасибо
daniel_s
!