jQuerty UI range slider bug after change min value - jquery-ui

I have one slider in my page, with this code
<div id="sale-price-range" class="price-range" data-min="200000" data-max="2000000" data-unit="R$" data-min-field="precoMinVenda" data-max-field="precoMaxVenda" data-increment="100000"></div>
My js code is:
// Price Range
$(".price-range").each(function() {
var dataMin = $(this).attr('data-min');
var dataMax = $(this).attr('data-max');
var dataUnit = $(this).attr('data-unit');
var minField = $(this).attr('data-min-field');
var maxField = $(this).attr('data-max-field');
var increment = $(this).attr('data-increment');
$(this).append( "<input type='text' class='first-slider-value' name='"+minField+"' val=''/><input type='text' class='second-slider-value' name='"+maxField+"'/>" );
$(this).slider({
range: true,
min: 0,
max: dataMax,
step: increment,
values: [ dataMin, dataMax ],
slide: function( event, ui ) {
event = event;
if(ui.value < dataMin) {
return false;
}
$(this).find( ".first-slider-value" ).val( dataUnit + " " + ui.values[ 0 ].toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.") );
$(this).find( ".second-slider-value" ).val( dataUnit + " " + ui.values[ 1 ].toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.") );
},
stop: function(event,ui) {
if(ui.value >= dataMin) {
postAjaxSearch();
}
}
});
$(this).find( ".first-slider-value" ).val( dataUnit + " " + $( this ).slider( "values", 0 ).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.") );
$(this).find( ".second-slider-value" ).val( dataUnit + " " + $( this ).slider( "values", 1 ).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.") );
});
It works very well, but if I try to change
min: 0,
To
min: dataMin,
My slider stop to work (I can't drag it). It continues to call postAjaxSearch();, but it doesn't change max/min value neither move the slider-handle.
Why do I need to change min to dataMin?
Because my slider start from 0, but the slider-handle move from dataMin from dataMax. The range betweem dataMin and 0 is on page, but can't move there. I don't want this, the slider should start from dataMin.
Work, but with this space on left (min: 0)
No space, but doesn't work (min: dataMin)

I had the same exact problem, I solved it by using parseInt() on the min value
$("#price-slider").slider({
range: true,
min: parseInt(price_min),
max: price_max,
values: [price_min, price_max],
slide: function (event, ui) {
$("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
}
});
I have no idea why this works, it would seem logical that both extremes would work the same but apparently they don't.

Related

Display additional informatin in the tooltip

I have the following highcharts graph
https://jsfiddle.net/deemgfay/
and I am trying to display the "Consum Test" values in the tooltip but without adding them to the series. I just want to add Consum (l/100km)
Total Consum (l) to the series. Is that possible with hightcharts? Please see the screenshot below.
You can set the extra series to be hidden and ignored in legend:
visible: false,
showInLegend: false
Then use tooltip formatter function (useHTML must be enabled) to display points from all series in the shared tooltip regardless of their visibility:
formatter: function() {
var html,
originalPoint = this.points[0];
// header
html = "<span style='font-size: 10px'>" + originalPoint.x + "</span><br/>";
// points
originalPoint.series.chart.series.forEach(function(series) {
var point = series.points.find((p) => p.x === originalPoint.point.x);
html += "<span style='color: " + series.color + "'>\u25CF</span> " + series.name + ": <b>" + point.y + "</b><br/>"
});
return html;
}
Live demo: https://jsfiddle.net/kkulig/1oggzsx0/
API references:
http://api.highcharts.com/highcharts/tooltip.formatter
http://api.highcharts.com/highcharts/tooltip.useHTML
This can be done by using tooltip.formatter. Here I append to tooltip info based on index of current series from index of required extra array.
formatter: function() {
var s = '<b>' + this.x + '</b>';
var reqpoint = 0;
$.each(this.points, function() {
var reqpoint = this.point.index
s += '<br/>' + this.series.name + ': ' +
this.y.toFixed(2) + 'm';
if (this.series.index == 1) {
s += '<br/>Test Consum (l): ' + extraData[reqpoint] + 'm';
}
});
return s;
},
Fiddle demo

Can We Calculate The DOM Length of Each Steps in jQuery UI Slider

Using jquery UI Slider I just wonder if there is a way to calculate the CSS length(width) of between steps in UI Slider? For example at following code I need to calculate the actual width between step1 (min) and step2 and so on!
$( function() {
$('#slider').slider({
min: 1,
max: 5,
range: "min",
animate:"slow",
value: 0
});
} );
Not sure why you'd need this detail. It can be calculated.
http://api.jqueryui.com/slider/#option-step
step
Type: Number
Default: 1
Determines the size or amount of each interval or step the slider takes between the min and max. The full specified value range of the slider (max - min) should be evenly divisible by the step.
JavaScript
$(function() {
$('#slider').slider({
min: 1,
max: 5,
range: "min",
animate:"slow",
value: 0
});
// Collect full width
var slideWidth = $("#slider").width();
// Collect number of steps in slider
var sliderSteps = $("#slider").slider("option", "step");
var stepWidth = slideWidth / sliderSteps;
console.log("The Slider Width: " + slideWidth + "px, with " + sliderSteps + " steps, and each steps is " + stepWidth + "px wide.");
});

How to get series marker symbol when displaying shared tooltips?

Link to JSFiddle with question content marked by comments: https://jsfiddle.net/z1q7aqo3/
Specifically, the part which needs filling in is
'tooltip': {
'formatter': function(){
var output = '<strong>' + this.y + '</strong>';
var sorted = this.points.sort(function(a, b){
if (a.y == b.y){
return 0;
}
return a.y < b.y ? 1 : -1;
});
sorted.forEach(function(point, index){
var marker = '';
/*
TODO: How do I determine what symbol is used for the marker?
*/
output += '<br /><span style="color: ' + point.series.color + '">' + marker + '</span> ' + point.series.name + ': ' + point.y;
});
return output;
},
'shared': true
}
I have a line chart with a number of series. On mouseover, the tooltip displays all the values of all of them, in sorted order. This part is done and can be seen in the JSFiddle. My question is, I want each the text for each series in the tooltip to include the marker symbol used for that series, styled in the color of the series. The color styling is also complete and can be seen in the JSFiddle, but how do I get the marker symbol?
Modifying form earlier Answer it is for individual series tooltip. Here modified to shared tooltip
Fiddle demo
Highcharts.chart('container', {
'title': {
'text': 'Random Chart'
},
'tooltip': {
'formatter': function(){
var output = '<strong>' + this.y + '</strong>';
var sorted = this.points.sort(function(a, b){
if (a.y == b.y){
return 0;
}
return a.y < b.y ? 1 : -1;
});
sorted.forEach(function(point, index){
var marker = '';
if ( point.point.graphic && point.point.graphic.symbolName ) {
switch ( point.point.graphic.symbolName ) {
case 'circle':
marker = '●';
break;
case 'diamond':
marker = '♦';
break;
case 'square':
marker = '■';
break;
case 'triangle':
marker = '▲';
break;
case 'triangle-down':
marker = '▼';
break;
}
}
/*
TODO: How do I determine what symbol is used for the marker?
*/
output += '<br /><span style="color: ' + point.series.color + '">' + marker + '</span> ' + point.series.name + ': ' + point.y;
});
return output;
},
'shared': true
},
'series': [{
'name': 'Series 1',
'data': [1, 3, 7, 19, 11, 27, 8, 15]
}, {
'name': 'Series 2',
'data': [2, 1, 8, 12, 14, 20, 9, 10]
}]
});

Highcharts: Returning N/A value instead of 0% in data label

I'd like to have the y-axis data labels return 'N/A' for null values. Currently nothing is displayed. I tried using the y-formatter function to say if the y value equaled null, return N/A but haven't had luck for some reason? I'm somewhat new to Highcharts, so please forgive the basic nature of the question.
formatter: function() {
if (this.y !== null)
return 'test';
var chart = this.series.chart;
chart.renderer.text('n/a', this.point.plotX - 10, chart.plotHeight - 10).add(this.series.group)
},
Here's the link to the JSFiddle: http://jsfiddle.net/v5vJR/
Remove format from your dataLabels options. Then you need to properly calculate where put that label. plotY will be undefined, since there is no value, right?
formatter: function () {
var str;
if (this.y !== null) {
return this.y + '%';
} else {
var chart = this.series.chart,
categoryWidth = chart.plotHeight / chart.xAxis[0].categories.length,
offset = (this.point.x + 1) * categoryWidth - this.point.pointWidth + 3; //
chart.renderer.text('n/a', chart.plotLeft, chart.plotTop + offset).add();
}
return false;
},
Demo: http://jsfiddle.net/v5vJR/3/
Solution
Documentation: http://api.highcharts.com/highcharts#Renderer
Fiddle: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/renderer-text/
chart.renderer.text('<span style="color: #a2a5a1">N/A</span>', chart.plotLeft, chart.plotTop + offset)
.css({
color: '#a2a5a1',
fontSize: '11px'
})
.add();

Stock Chart - Formatted Tooltip And Prefixes or Suffixes Not Showing

I am using a stock chart to show trend data. On the backend I am getting what the valueSuffix should be (or valuePrefix as the case may be). I am also formatting the date display in the tooltip. Here is the important part of the series declaration:
...
name: 'Wages',
tooltip: {
valuePrefix: '$',
valueDecimals: 0
},
...
Here is the tooltip formatter:
...
tooltip: {
formatter: function () {
var s = '<b>';
if (Highcharts.dateFormat('%b', this.x) == 'Jan') {
s = s + 'Q1';
}
if (Highcharts.dateFormat('%b', this.x) == 'Apr') {
s = s + 'Q2';
}
if (Highcharts.dateFormat('%b', this.x) == 'Jul') {
s = s + 'Q3';
}
if (Highcharts.dateFormat('%b', this.x) == 'Oct') {
s = s + 'Q4';
}
s = s + ' ' + Highcharts.dateFormat('%Y', this.x) + '</b>';
$.each(this.points, function (i, point) {
s += '<br/><span style="color: ' + point.series.color + '">' + point.series.name + ':</span>' + point.y;
});
return s;
}
}
...
Example jsFiddle.
If you notice the prefix for the dollar sign is not showing on the Wage series. I am not really sure what I am missing here.
The fix is to break up the label formatting and the value formatting into distinct sections. See example jsFiddle.
Set the chart.tooltip like:
...
tooltip: {
headerFormat: '<b>{point.key}</b><br>',
xDateFormat: '%Q'
},
...
On the xAxis I replaced the label formatter with:
...
format: '{value: %Q}'
...
Inside the series I kept my suffix/prefix/decimals the same:
...
tooltip: {
valuePrefix: '$',
valueDecimals: 0
},
...
The big change came when I found that you can set your own date format label. I created one for Quarters (which is what I did the original cumbersome code for):
Highcharts.dateFormats = {
Q: function (timestamp) {
var date = new Date(timestamp);
var y = date.getFullYear();
var m = date.getMonth() + 1;
if (m <= 3) str = "Q1 " + y;
else if (m <= 6) str = "Q2 " + y;
else if (m <= 9) str = "Q3 " + y;
else str = "Q4 " + y;
return str;
}
};
I now get the tooltip to show the correct labels.
Unless something has changed in a recent version, you can't set your tooltip options within the series object. You can set anything that you would set in the plotOptions on the series level, but not the tooltip.
You can set a check within your main tooltip formatter to check for the series name, and apply the prefix accordingly.
{{edit::
This appears to be an issue of the formatter negating the valuePrefix setting.
Old question... but I spent hours looking for an acceptable way to do this.
/!\ : Before OP's answer...
If someone is looking for a way to use the formatter provided by highcharts (pointFormat attribute) (Doc here: LABELS AND STRING FORMATTING), you will find a (bad) example here :
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/tooltip/pointformat/
Why "bad" ? because if you change :
pointFormat: '{series.name}: <b>{point.y}</b><br/>',
with :
pointFormat: '{series.name}: <b>{point.y:,.1f}</b><br/>',
you add thousand separators (useless here) and force number format to 1 decimal ... and loose the suffix.
The answer is : series.options.tooltip.valueSuffix (or series.options.tooltip.valuePrefix)
Example :
pointFormat: '<span style="color:{series.color}">\u25CF</span> {series.name}: <b>{point.y:,.0f}{series.options.tooltip.valueSuffix}</b><br/>',
Hope this will save you some time.
Now OP's Answer :
Based on what I said, you can change :
$.each(this.points, function (i, point) {
s += '<br/><span style="color: ' + point.series.color + '">' + point.series.name + ':</span>' + point.y;
});
For this :
$.each(this.points, function (i, point) {
s += '<br/><span style="color: ' + point.series.color + '">'
+ point.series.name + ':</span>'
+ (typeof point.series.options.tooltip.valuePrefix !== "undefined" ?
point.series.options.tooltip.valuePrefix : '')
+ point.y
+ (typeof point.series.options.tooltip.valueSuffix !== "undefined" ?
point.series.options.tooltip.valueSuffix : '');
});
This will add prefix and/or suffix if they exists

Resources