HighCharts set different xAxis labels - highcharts

I am using 2 arrays with some numbers and getting the percentage. What I would like to do is if both array indexes are 0 the I would like to set the Label to read "No Data".
I know I need to get the current column(point) index or something similar, I've tried using formatters to do this.
Anyways I imagine the code something like this.
xAxis: {
categories: trebleNotes,
labels: {
rotation: -45,
align: 'right',
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
},
formatter: function () {
if(array1[currentColumn] == 0 && array2[currentColumn] == 0){
return("NO DATA");
}else{
return(this.value);
}
},
}
},
I am not sure how to get the currentColumn index and use that to do the formatting. I hope this makes sense.

If I understand you correctly, wouldn't you just want the index of the category at this.value?
labels: {
formatter: function() {
var currentColumn = -1;
for (var idx = 0; idx < this.axis.categories.length; idx++){
if (this.axis.categories[idx] == this.value){
currentColumn = idx;
break;
}
}
console.log(currentColumn);
return this.value;
}
}

Related

Highlight weekend with a plot band or a zone

I would like highlight the weekend in a timeseries chart. Until now and reading the documentation I think it is two ways of achieving this: plot bands or zones. I just managed to highlight the saturdays and sunday on the x axis(see code below). I would like to plot a band instead.(see 1).
xAxis: {
type: 'datetime',
crosshair: {
width: 2,
color: '#F66',
dashStyle: 'shortdot'
},
labels: {
formatter: function () {
var day = Highcharts.dateFormat('%a', this.value);
if (day == "Sat" || day == "Sun") {
return `<b>${Highcharts.dateFormat('%a %e %b', this.value)}</b>`;
} else {
return Highcharts.dateFormat('%a %e %b', this.value);
}
}
}
}
}
Here is my idea how to add plotBands dynamically based on the used data. I think that everything is clear in the code - if not, feel free to ask.
Demo: https://jsfiddle.net/BlackLabel/6n4ab7jm/
events: {
load() {
let chart = this,
plotBandAr = [],
plotBand = {
color: '#FCFFC5',
};
chart.series[0].points.forEach(p => {
// start from the saturday
if (new Date(p.x).getDay() === 6) {
plotBand.from = p.x
}
// end on the sunday
if (new Date(p.x).getDay() === 0) {
plotBand.to = p.x
}
// add plotBand on monday and reset the plotBand object
if (new Date(p.x).getDay() === 1) {
plotBandAr.push(plotBand)
plotBand = {
color: '#FCFFC5',
};
}
});
chart.xAxis[0].update({
plotBands: plotBandAr
})
}
}
API: https://api.highcharts.com/highcharts/chart.events.load
API: https://api.highcharts.com/class-reference/Highcharts.Axis#update

Is there a way to update the legend to the value of a variable within the chart.events?

I am plotting a 3D line chart over time. Each time the count changes at the end of the loop, a new point is plotted for each series. Is there a way to set the value of the legend as the value of count in my example code? The count represents hours since the start of an experiment, so being able to display this is necessary. Thanks!
events: {
load: function() {
var thischart = this;
for (i = 0; i < allpoints.length; i++) {
thischart.addSeries({
enableMouseTracking: false,
lineWidth: 1,
marker: {
enabled: false
},
data: [0, 0, 0]
}, false)
thischart.redraw(false);
}
setInterval(function() {
if (count >= max_data_length) {
if (!pause_at_end) {
for (i = 0; i < allpoints.length; i++) {
thischart.series[i + marker_series_length].setData([0, 0, 0], false);
}
thischart.redraw(false);
count = 1;
} else {
is_paused = true;
document.getElementById('pauseit').value = "Unpause";
}
}
if (!is_paused) {
for (i = 0; i < allpoints.length; i++) {
if (allpoints[i].length > count) {
thischart.series[i + marker_series_length].addPoint([
allpoints[i][count][0], allpoints[i][count][2], allpoints[i][count][1]
], false);
}
}
thischart.redraw(false);
count = count + 4;
}
}, 10)
}
}
You can use series.update method to set new data and change the series name in legend:
chart: {
...,
events: {
load: function() {
var counter = 0,
series = this.series[0];
setInterval(function() {
counter++;
series.update({
data: getRandomData(),
name: 'Name ' + counter
});
}, 1000);
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/7bq21tak/
API Reference: https://api.highcharts.com/class-reference/Highcharts.Series#update
Thanks ppotaczek! This pointed me in the right direction. I used chart.update instead to update the subtitle. Here is the code I used:
chart.update({
chart: {
inverted: false,
polar: false
},
subtitle: {
style: {
fontSize: '20px'
},
text:'Day:'+ countdays
}
});

JSFiddle becomes unresponsive after continuous zooming in the chart

When we continuously zoom in the chart around after 8 or 10 times, the JSFiddle becomes unresponsive.
JSFiddle
HTML
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<div id="container"></div>
CSS
#container {
max-width: 800px;
height: 400px;
margin: 1em auto;
}
JavaScript + jQuery 1.7.2
$(function () {
/**
* Experimental Highcharts plugin to implement chart.alignThreshold option. This primary axis
* will be computed first, then all following axes will be aligned to the threshold.
* Author: Torstein Hønsi
* Last revision: 2015-05-18
*/
(function (H) {
var Axis = H.Axis,
inArray = H.inArray,
wrap = H.wrap;
wrap(Axis.prototype, 'adjustTickAmount', function (proceed) {
var chart = this.chart,
primaryAxis,
primaryThreshold,
primaryIndex,
index,
newTickPosLow,
threshold,
newTickPositionsHigh,
max,
nextStep;
// Find the index and return boolean result
function isAligned(axis) {
index = inArray(threshold, axis.tickPositions); // used in while-loop
return axis.tickPositions.length === axis.tickAmount && index === primaryIndex;
}
for(var i = chart[this.coll].length - 1; i >= 0; i--){
chart[this.coll][i].removePlotLine('zero-point-plotline');
if(chart[this.coll][i].hasVisibleSeries){
primaryAxis = chart[this.coll][i];
}
}
primaryThreshold = (primaryAxis.series[0] && primaryAxis.series[0].options.threshold) || 0;
threshold = 0;
for(var i = 0; i < this.series.length; i++){
if(this.series[i].visible){
threshold = this.series[i].options.threshold;
break;
}
}
primaryIndex = primaryAxis.tickPositions && inArray(primaryThreshold, primaryAxis.tickPositions);
if(primaryIndex > 0 && primaryIndex < primaryAxis.tickPositions.length - 1){
primaryAxis.addPlotLine({
color: 'black',
value: 0,
width: 0,
id: 'zero-point-plotline',
zIndex: 3
})
}
if (chart.options.chart.alignThresholds && primaryAxis && this !== primaryAxis && this.hasVisibleSeries) {
if (this.tickPositions && primaryIndex >= 0 && primaryIndex <= primaryAxis.tickPositions.length - 1) {
if (this.tickAmount) {
while (!isAligned(this)) {
if (index < primaryIndex) {
// fill up the tick positions, so that we can calculate the new interval-steps
while (this.tickPositions.length < primaryAxis.tickPositions.length) {
var nextStep = Math.abs(Math.abs(this.tickPositions[this.tickPositions.length - 1]) - Math.abs(this.tickPositions[this.tickPositions.length - 2]));
this.tickPositions.push(this.tickPositions[this.tickPositions.length - 1] + nextStep);
}
newTickPositionsHigh = [];
for(var i = 0; i < primaryAxis.tickPositions.length; i++){
max = this.tickPositions[this.tickPositions.length - 1];
nextStep = max / (primaryAxis.tickPositions.length - 1 - primaryIndex);
newTickPositionsHigh.push(max - i * nextStep);
}
this.tickPositions = newTickPositionsHigh.reverse();
this.max = this.tickPositions[this.tickPositions.length - 1];
this.min = this.tickPositions[0];
} else {
newTickPosLow = this.tickPositions[this.tickPositions.length - 1] + this.tickInterval;
this.tickPositions.push(newTickPosLow);
this.max = newTickPosLow;
}
proceed.call(this);
}
}
}
} else {
proceed.call(this);
}
});
}(Highcharts));
$('#container').highcharts({
chart: {
alignThresholds: true,
type: 'area',
zoomType:'xy'
},
title: {
text: 'The <em>alignThreshold</em> option is true'
},
xAxis: {
categories: ['Flags(1)','Flags(2)','Flags(3)','Shopping (4)','Shopping(5)','Shopping(6)','Shopping(7)','Town(8)','Town(9)','Town(10)','Town(11)','Town(12)','Town(14)','Town(15)','Town(16)','Town(17)','Small(18)','Small(19)','Small(20)','Town(13)'],
scrollbar:
{
enabled:true
}
},
yAxis: [{
title: {
text: 'Traffic',
},
}, {
title: {
text: 'Conversion Rate'
},
opposite: true
},
{
title: {
text: 'ATV'
},
opposite: true
},
{
title: {
text: 'TotalTransaction'
},
opposite: true
},
{
title: {
text: 'Sta'
},
opposite: true
}],
series: [{
data: [8.16,7.6,2.86,2.02,-1.98,-2.83,4.53,-63.35,0],
type: 'spline',
yAxis: 0
}, {
data: [0.9,0.4,-0.4,-2.2,-0.3,0.0,-0.5,1.1,-0.4,3.4,0.8,1.4,2.4,-1.2,3.2,1.7,-0.5,-0.3,2.3,2.8],
type: 'column',
yAxis: 1,
visible: false
}, {
data: [-6.44,6.56,-9.94,35.70,58.07,0.00,103.36,91.03,-84.11,-41.35,25.15,-6.32,4.66,2.93,59.47,-6.39,22.44,17.30,46.46,-14.18],
type: 'column',
yAxis: 2,
visible: false
}]
});
});

Highcharts pie graph with two rings filtering

I have a pie chart here that I'm working on that has two "rings" in it. The inner ring is just a summation of the outer ring for that given category.
Here is the fiddle: http://jsfiddle.net/jeffvan576/a50859s7/1/
(Apologies for the code - it's a bit of a mess right now)
I've been messing around with the showInLegend functionality but that will (as it's intended) only pull out the given piece of the pie chart. So, for instance, if you click google, it pulls out that piece of the pie chart but leaves the outer ring. To completely eliminate google you need to click "google", "match", "funds added" and "organic" for google.
My question is, is there a way to remove the entire slice (google and all it's children) from the chart at once?
The issue is that in order to get the functionality / layout on the chart that I need, this pie chart is actually built out of two series.
ShowInLegend code:
pie: {
shadow: false,
center: ['50%', '50%'],
showInLegend: true
}
I started building a custom visibility function at the bottom of the fiddle but dialed it back until I understood showInLegend a little better.
Thanks in advance!
you can achieve this by getting name of series on which clicked by using http://api.highcharts.com/highcharts#plotOptions.pie.events.click of plotoptions -> pie.
after that calling visibility function to hide Channel series along with its children to hide/show.
Event:
plotOptions: {
pie: {
shadow: false,
center: ['50%', '50%'],
showInLegend: true,
point: {
events: {
}
}
}
}
also static line put into visibility function to hide/show need to remove.
// chart.series[0].data[0].visible = false;
http://jsfiddle.net/a50859s7/27/
Full code:
$(function () {
var dataObject = {
facebook: {
'organic': 10.85,
'match': 7.35,
'fundsadded': 33.06,
'total': 0,
'status': 'disabled'
},
google: {
'organic': 10.85,
'match': 7.35,
'fundsadded': 33.06,
'total': 0,
'status': 'disabled'
},
email: {
'organic': 10.85,
'match': 7.35,
'fundsadded': 33.06,
'total': 0,
'status': 'enabled'
},
colorSelections: {
'facebook': '#3b5998',
'google': '#dd4b39',
'disabled': '#c6c6c6'
}
}
var sumObjects = function () {
for (var channel in dataObject) {
if (channel === 'colorSelections') continue;
var sum = 0;
for (var key in dataObject[channel]) {
if (key === 'status') continue;
sum += dataObject[channel][key];
}
dataObject[channel].total = sum;
}
}
sumObjects();
var colors = Highcharts.getOptions().colors,
categories = ['Facebook', 'Google', 'Email'],
data = [{
y: dataObject.facebook.total + 1,
//color: dataObject.facebook.status === 'disabled' ? dataObject.colorSelections.disabled : dataObject.colorSelections.facebook,
color: 'rgba(59, 89, 152, 0.3)',
drilldown: {
name: 'Facebook',
categories: ['organic', 'match', 'funds added'],
data: [
dataObject.facebook.organic,
dataObject.facebook.match,
dataObject.facebook.fundsadded],
color: 'rgba(59, 89, 152, 0.3)'
},
}, {
y: dataObject.google.total + 1,
color: '#dd4b39',
drilldown: {
name: 'Google',
categories: ['organic', 'match', 'funds added'],
data: [
dataObject.google.organic,
dataObject.google.match,
dataObject.google.fundsadded],
color: '#e46f61'
}
}, {
y: dataObject.email.total + 1,
color: colors[2],
drilldown: {
name: 'Email',
categories: ['organic', 'match', 'funds added'],
data: [
dataObject.email.organic,
dataObject.email.match,
dataObject.email.fundsadded],
color: colors[2]
}
}],
browserData = [],
versionsData = [],
i,
j,
dataLen = data.length,
drillDataLen,
brightness;
// Build the data arrays
for (i = 0; i < dataLen; i += 1) {
// add browser data
browserData.push({
name: categories[i],
y: data[i].y,
color: data[i].color
});
// add version data
drillDataLen = data[i].drilldown.data.length;
for (j = 0; j < drillDataLen; j += 1) {
brightness = 0.2 - (j / drillDataLen) / 5;
versionsData.push({
name: data[i].drilldown.categories[j],
y: ((data[i].drilldown.data[j] / browserData[0].y) * 100),
color: Highcharts.Color(data[i].color).brighten(brightness).get()
});
}
}
// Create the chart
$('#container').highcharts({
chart: {
type: 'pie'
},
title: {
text: 'Browser market share, April, 2011'
},
yAxis: {
title: {
text: 'Total percent market share'
}
},
plotOptions: {
pie: {
shadow: false,
center: ['50%', '50%'],
showInLegend: true,
point: {
events: {
click: function (event) {
var seriesIndex;
var secondarySeriesIndex;
if (this.name == 'Facebook') {
seriesIndex = 0;
secondarySeriesIndex = 0;
} else if (this.name == 'Google') {
seriesIndex = 1;
secondarySeriesIndex = 3;
} else if (this.name == 'Email') {
seriesIndex = 2;
secondarySeriesIndex = 6;
}
var chart = $('#container').highcharts();
visibility(chart.series[0].data[seriesIndex]);
visibility(chart.series[1].data[secondarySeriesIndex]);
visibility(chart.series[1].data[secondarySeriesIndex + 1]);
visibility(chart.series[1].data[secondarySeriesIndex + 2]);
}
}
}
}
},
tooltip: {
valueSuffix: '%'
},
series: [{
name: 'Channel',
type: 'pie',
data: browserData,
size: '120%',
dataLabels: {
formatter: function () {
return this.y > 5 ? this.point.name : null;
},
color: 'white',
distance: -30
}
}, {
name: 'Added',
type: 'pie',
data: versionsData,
size: '120%',
innerSize: '80%',
dataLabels: {
formatter: function () {
// display only if larger than 1
return this.y > 1 ? '<b>' + this.point.name + ':</b> ' + this.y + '%' : null;
}
}
}]
});
var visibility = function (series) {
series.visible ? series.graphic.hide() : series.graphic.show();
// chart.series[0].data[0].visible = false;
}
var chart = $('#container').highcharts();
$('.update').click(function () {
visibility(chart.series[0].data[0]);
visibility(chart.series[1].data[0]);
visibility(chart.series[1].data[1]);
visibility(chart.series[1].data[2]);
chart.redraw();
});
function synchronizePieSeries(event, slice) {
debugger;
$(chart.series[1].data).each(function (i, e) {
if (slice.name === e.name) {
slice.visible ? e.graphic.hide() : e.graphic.show();
}
});
}
//$('.update').click(function (event) {
// synchronizePieSeries(event, this);
//});
});

Changing color of spline lines based on values in HighCharts

I can change the marker color based on if it's above (red) or below (green) or between (yellow), but cannot change the color of the line in between the markers. Is this possible? Right now, it just displays as a blue line.
function makeRun(divId, last, current, goal, title, yAxisTitle) {
var width = 1000; var SizeOfFont = '14px';
var min = checkMinMaxArray(current, last, goal, 'min') * 995 / 1000; //sets min closely below the real min. **consider changing to 4/5 digits**
var max = checkMinMaxArray(current, last, goal, 'max') * 1005 / 1000; //sets max closely above the real max
//if there are projections, color code the columns here
preprocessData = function (data, last, goal) {
var nData = [];
var colorGood = 'green'; var colorBad = '#E42217'; var colorUse;
for (var i = 0; i < data.length; i++) {
if (data[i] <= goal[i]) { colorUse = colorGood; }
else if (data[i] > last[i]) { colorUse = colorBad; }
else { colorUse = '#FFE303'; }
nData.push({
y: data[i],
x: i,
fillColor: colorUse,
color: colorUse
});
}
return nData;
};
var chart = new Highcharts.Chart({
chart: {
renderTo: divId,
height: 275, //little bigger than alotted height to make more readable
width: width //dependent on #gauges
},
title: {
text: title, //should all be the same, can make a parameter if need to be different
style: { //size of text above
fontSize: SizeOfFont
}
},
xAxis: {
//categories: xAxisTitles,
labels: { //size of the Text above^^
style: {
fontSize: SizeOfFont
}
}
},
yAxis: {
min: min,
max: max,
title: {
text: yAxisTitle, //parameter since some are days, percents, etc
style: {//size of y axis title
fontSize: SizeOfFont
}
},
labels: {
style: {//size of the y axis tick labels
fontSize: SizeOfFont
}
}
},
credits: {//gets rid of the highcharts logo in bottom right
enabled: false
},
legend: {//the legend at the bottom
style: {
fontSize: '12px'
}
},
series: [{
type: 'spline',
name: '2012',
data: last,
color: 'orange',
marker: {
symbol: 'diamond'
}
}, {
type: 'spline',
name: '2013',
data: preprocessData(current, last, goal),
marker: {
symbol: 'diamond'
}
}, {
type: 'spline',
name: 'Goal',
data: goal,
color: 'blue',
marker: {
symbol: 'diamond'
}
}]
});
}
It is not possible in one serie, you should prepare two separate serie with different colors.

Resources