HighCharts - Horizontal line with the zoomed average of the values - javascript

I need to display the average of the values of my 2 datasets, but with a specific characteristic: when the users zoom the chart, the average line is recalculated ONLY with the values of the zoomed area.
How is this done?
Here is my JS:
$(function () {
$('#container').highcharts({
chart: {
type: 'scatter',
zoomType: 'xy'
},
title: {
text: 'Test'
},
subtitle: {
text: ''
},
xAxis: {
type: 'datetime',
title: {
enabled: true,
text: 'Date'
},
startOnTick: true,
endOnTick: true,
showLastLabel: true
},
yAxis: {
title: {
text: ''
}
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
x: 100,
y: 70,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF',
borderWidth: 1
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
lineColor: 'rgb(100,100,100)'
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x} date, {point.y} dose'
}
}
},
series: [{
name: 'ag',
color: 'rgba(223, 83, 83, .5)',
data: [[1426719600000,0.9659],[1426719600000,0.8928],[1445205600000,1.6428],[1445205600000,1.4711],[1445205600000,1.4209],[1445205600000,1.9574],[1445205600000,1.1226],[1445205600000,0.8159],[1445205600000,1.0114],[1445205600000,0.9168],[1445464800000,1.175],[1445464800000,1.2219],[1445464800000,1.2641],[1445464800000,1.3006],[1445464800000,0.9375],[1445464800000,0.8966],[1445464800000,0.9374],[1445464800000,1.0811]]
}, {
name: 'cf',
color: 'rgba(119, 152, 191, .5)',
data: [[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.015],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.265],[1445378400000,2.015],[1445378400000,2.015],[1445378400000,2.015],[1445464800000,1.2254],[1445464800000,1.1065],[1445464800000,1.3482],[1445464800000,1.3015],[1445292000000,1.2297]]
}]
});
});
Thanks in advance!

This can be done several ways. I am going to show you the way to do it via chart.events.redraw. Whenever you do a zoom (or reset zoom) the chart is redrawn and this gives us a good hook into calculating and adding items to chart at that time. Here is the code block for the redraw event:
Here is a live demo.
I made it verbose so you can see each step. We are going to use getExtremes() to find out what are max and min xAxis and yAxis indexes are. From this we determine if the points in each series are within this "window". If they are not within the window we ignore. If they are then we add the y-value to a var and increment the point counter by 1. Once we are done we do a simple check to make sure we are not going to divide by zero and then calculate the average.
Once we have the average we then will draw a plotLine at the value of the average we calculated above. First we see if a plotline exists with our id. If it does we remove it. Then we create our new plotline.
There is some clean up to do as well (such as, do you include a 0-value plotline based on your business rules) but this should get you started.
Attempted to add code block in edit as original answer attempt would not let it through. Still unable to post code block.

Related

Highcharts Spiderweb chart xAxis labels disappear on long label name

I have a spiderweb chart with 12 labels on the x-axis. This chart is multilingual. In English, all labels show just fine. In German, due to the long names, some labels are hidden.
Highcharts.chart('container', {
chart: {
polar: true,
type: 'line',
animation: false,
},
title: {
text: "Does what ever a spider can",
},
pane: {
size: '80%'
},
xAxis: {
categories: [
"Strategisches Denken",
"Kreatives und innovatives Denken",
"Change Management",
"Mitarbeiterentwicklung",
"Kommunikation",
"Teamarbeit",
"Entscheidungsfindung",
"Kundenorientierung",
"Ergebnisorientierung",
"Selbstmanagement",
"Leistungsmotivation",
"Verantwortungsbewusstsein"
],
tickmarkPlacement: 'between',
lineWidth: 0,
},
yAxis: {
gridLineInterpolation: 'polygon',
lineWidth: 0,
min: 0,
max: 5,
tickInterval: 1,
},
tooltip: {
enabled: false,
},
legend: {
enabled: true,
verticalAlign: 'top'
},
plotOptions: {
series: {
animation: false
}
},
series: [{
name: "Stuff",
data: [1,2,3,4,5,4,3,2,1,2,3,4],
}],
});
Fiddle here.
This produces a Spiderweb chart where the xAxis labels "Kreatives und innovatives Denken", "Entscheidungsfindung" and "Verantwortungsbewusstsein" are hidden. If I replace these names with shorter names they do display.
I've tried making the font size smaller, but all labels are displayed when font size is 5 (default is 11). This is unforunately unacceptably small.
I can't find any fixed character length or even amount of line breaks after which a label disappears. It seems to be calculated dynamically based off of a labels position and neighboring labels. (Otherwise I would've been happy with truncating strings.)
Is there some setting to force all labels to show?
You can use internal allowOverlap property:
xAxis: {
labels: {
allowOverlap: true
},
...
}
Live demo: https://jsfiddle.net/BlackLabel/ok7Lrn5j/

Empty space above plot lines in Highcharts

I am using Highcharts (Highstock) to render a chart on my page.
I am specifying a max and min value for my axis, but despite this I end up with blank space above my graph:
This is what my options look like currently:
var myChart = new Highcharts.Chart('container', {
rangeSelector:{
enabled: false
},
credits: {
enabled: false
},
legend: {
enabled: true,
},
yAxis: [{
opposite: false,
title: {
text: 'Gravity'
},
min: 1,
max: items.options.gravity_max + 0.01
},
{
opposite: true,
title: {
text: 'Temperature (C)'
},
min: items.options.temperature_min - 1,
max: items.options.temperature_max + 1
}],
navigator: {
enabled: false,
},
scrollbar: {
enabled: false
},
series: [{
name: 'Temperature',
data: items.temperature,
dataGrouping: {
enabled: true
},
yAxis: 1,
}, {
name: 'Gravity',
data: items.specific_gravity,
dataGrouping: {
enabled: true
}
},
{
name: 'OG',
data: items.original_gravity,
dataGrouping: {
enabled: true
},
dashStyle: 'dot',
color: '#000'
},
{
name: 'FG',
data: items.final_gravity,
dataGrouping: {
enabled: true
},
dashStyle: 'dot',
color: '#000'
}]
});
Here is a link to a fiddle with a simplified version of the data; http://jsfiddle.net/3eLrn61w/
What is causing that blank space above, and how do I get rid of it?
This is because Highcharts by default will try to start and end an axis on a label. Your current max is 1.071+0.01 (1.081). This is more than the label checkpoint of 1.075, which causes another label to be made.
You can fix this by doing (JSFiddle example):
yAxis: [{
// ...
endOnTick: false
}]
A few other options you might want to evaluate is the matching axis.startOnTick: false, possibly axis.maxPadding: 0 to avoid creating labels that are marginally needed and chart.alignTicks: false to free up the connection between the axis.
An alternative approach will also be to alter the tick positions (axis.tickPositions or axis.tickPositioner) so that they line up better with your min and max values.
Or your third option is to alter your handcoded maximum of the axis (max: items.options.gravity_max + 0.01), so that it does not create such a label gap, f.x. by being max: 1.074.
However, I think your best bet may be with the startOnTick and endOnTick options.

Highcharts setting the combined line and bar graph maximum width fails

I would like to set my line graph to have a minimum of 0 and maximum of 100 and even when its a combined line graph the value of the line graph maximum should always remain at 100 even though the other graph y axis increase
So i have
title: {
text: 'Mombasa Plant Truck Compliance reports'
},
credits: {
enabled: false
},
chart: {
zoomType: 'xy'
},
subtitle: {
text: ''
},
xAxis: {
categories: xaxis,
crosshair: true
},
yAxis: [{ // Primary yAxis
title: {
text: 'Passed Inspection',
},
},
{ // Primary yAxis
title: {
text: 'Failed Inpsection',
},
},
{ // Tertiary yAxis
gridLineWidth: 0,
title: {
text: 'Percentage Compliance',
},
labels: {
format: '{value} %',
},
max: 100,
min: 0,
opposite: true
}
],
plotOptions: {
column: {
dataLabels: {
enabled: true,
crop: false,
overflow: 'none'
}
}
},
tooltip: {
shared: true
},
series: chartdata
}
So the above generates
As you can see the line graph y axis appear to be increasing beyond 100%.
I would like it to be at a maximum of 100
What do i need to adjust as i have already set max: 100, min: 0, but it doesnt work.
If you don't want to relay only on Highcharts algorithms for finding ticks, you can use tickPositions or tickPositioner:
https://api.highcharts.com/highcharts/yAxis.tickPositions
https://api.highcharts.com/highcharts/yAxis.tickPositioner
Demo for tick positions: http://jsfiddle.net/BlackLabel/cuutqgys/
Another approach that will work in this particular case is setting endOnTick to false: http://jsfiddle.net/BlackLabel/dxq4y822/

Highcharts scatter external data

I created document with highcharts scatter graph
Highcharts.chart('container', {
chart: {
type: 'scatter',
zoomType: 'xy'
},
title: {
text: ''
},
subtitle: {
text: ''
},
xAxis: {
title: {
enabled: true,
text: 'Date of entry'
},
startOnTick: true,
endOnTick: true,
showLastLabel: true
},
yAxis: {
title: {
text: 'Values'
}
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
x: 100,
y: 70,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF',
borderWidth: 1
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
lineColor: 'rgb(100,100,100)'
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: 'Dana {point.x} = {point.y}'
}
}
},
series: [{
name: 'Values',
color: 'rgba(223, 83, 83, .5)',
data: [[167.6, 64.5], [167.6, 72.3], [167.6, 61.4]]
}]
});
and this works. This code is from documentation page. I get normal scatter data like on this page Highcharts scatter jsfiddle
Now, I created another PHP file that produces me with data I actually need. it is in document highcharts.php and result is this
[07.03.2017,21000],[07.03.2017,25000],[07.03.2017,33000],[07.03.2017,27000],[07.03.2017,30000],[01.01.2017,700],[11.05.2017,0],[11.05.2017,0],[11.05.2017,0],[11.05.2017,0],
how to connect data to this highcharts.php file? I found some examples but I cannot get it to work. So for start I need this, ONE line of data to show on scatter diagram. I lost few days and just do not get it what I am doing wrong.
If your PHP file returns the array presented above, you need to parse it a little bit in order to use it as data array in a scatter series. First of all, dates should be strings. Secondly, you need to use new Date() to create Date instance and use getTime() to return timestamps. Also, change xAxis type to datetime.
API Reference:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
Example:
http://jsfiddle.net/0025rsmt/

Justify stack labels

I am having problems with my stack labels. I have them rotated -90 degrees and I have set align to left and vertical align to bottom. But for some reason the label is aligned to the bottom of each column but not as I expected it to do. The center of the text label is align to the bottom, not the first letter of the label. Is there a way to accomplish what I want without manually calculating the y offset for each individual stack label? And if not, how would I best do that tiresome calculation?
See attached fiddle:
$(function () {
$('#container').highcharts({
chart: { type: 'column' },
title: { text: '' },
xAxis: { categories: [""] },
yAxis: [{
title: { text: '', style: { color: '#4572A7'} },
labels: {
formatter: function () { return Highcharts.numberFormat(this.value, 0) + " USD" },
style: { color: '#4572A7' }
},
stackLabels: {
enabled: true,
style: { fontSize: '1.5em', fontWeight: 'bold', color: 'black' },
formatter: function () { return this.stack; },
rotation: -90, x: -5,
verticalAlign: 'bottom',
align: 'left'
}}],
legend: { borderWidth: 1, shadow: false },
plotOptions: {
column: { stacking: 'normal' },
series: { shadow: false}
},
series: [{ data: [34, 34, 54, 12, 23], stack: 'Stack 1' }]
});
});
http://jsfiddle.net/Zprjn/6/
Cheers!
EDIT
This is a simplified version of what I actually have. In my real chart I have dynamic content so there are different texts in the stack labels and the columns have various heights. None of this I know beforehand and thus I need to, somehow, dynamically adjust the stack labels to the bottom (and to the left of) of the columns regardless of the length of the text in the actual label.
Thanks again!
You can change:
verticalAlign: 'middle'
Example: http://jsfiddle.net/Zprjn/7/
or if you want at bottom, add:
y: -40
Example: http://jsfiddle.net/Zprjn/8/
I just figured it out! I had to specify
textAlign: 'left'
as well and that fixed it!
See the updated jsFiddle:
http://jsfiddle.net/Zprjn/9/

Categories