Jquery Select2:- How To Add Custom Image / Attribute in Select Dropdowns - jquery-select2

Need to insert custom image(s) in the drop down menu options. Don't know how to do that. Can someone describe a method to do so?
Thanks in advance!

Please check the code snippet, hope this helps.
(function($) {
$(function() {
var isoCountries = [
{ id: 'AF', text: 'Afghanistan'},
{ id: 'AX', text: 'Aland Islands'},
{ id: 'AL', text: 'Albania'},
{ id: 'DZ', text: 'Algeria'},
{ id: 'AS', text: 'American Samoa'},
{ id: 'AD', text: 'Andorra'},
{ id: 'AO', text: 'Angola'},
{ id: 'AI', text: 'Anguilla'},
{ id: 'AQ', text: 'Antarctica'},
{ id: 'AG', text: 'Antigua And Barbuda'},
{ id: 'AR', text: 'Argentina'},
{ id: 'AM', text: 'Armenia'},
{ id: 'AW', text: 'Aruba'},
{ id: 'AU', text: 'Australia'},
{ id: 'AT', text: 'Austria'},
{ id: 'AZ', text: 'Azerbaijan'},
{ id: 'BS', text: 'Bahamas'},
{ id: 'BH', text: 'Bahrain'},
{ id: 'BD', text: 'Bangladesh'},
{ id: 'BB', text: 'Barbados'},
{ id: 'BY', text: 'Belarus'},
{ id: 'BE', text: 'Belgium'},
{ id: 'BZ', text: 'Belize'},
{ id: 'BJ', text: 'Benin'},
{ id: 'BM', text: 'Bermuda'},
{ id: 'BT', text: 'Bhutan'},
{ id: 'BO', text: 'Bolivia'},
{ id: 'BA', text: 'Bosnia And Herzegovina'},
{ id: 'BW', text: 'Botswana'},
{ id: 'BV', text: 'Bouvet Island'},
{ id: 'BR', text: 'Brazil'},
{ id: 'IO', text: 'British Indian Ocean Territory'},
{ id: 'BN', text: 'Brunei Darussalam'},
{ id: 'BG', text: 'Bulgaria'},
{ id: 'BF', text: 'Burkina Faso'},
{ id: 'BI', text: 'Burundi'},
{ id: 'KH', text: 'Cambodia'},
{ id: 'CM', text: 'Cameroon'},
{ id: 'CA', text: 'Canada'},
{ id: 'CV', text: 'Cape Verde'},
{ id: 'KY', text: 'Cayman Islands'},
{ id: 'CF', text: 'Central African Republic'},
{ id: 'TD', text: 'Chad'},
{ id: 'CL', text: 'Chile'},
{ id: 'CN', text: 'China'},
{ id: 'CX', text: 'Christmas Island'},
{ id: 'CC', text: 'Cocos (Keeling) Islands'},
{ id: 'CO', text: 'Colombia'},
{ id: 'KM', text: 'Comoros'},
{ id: 'CG', text: 'Congo'},
{ id: 'CD', text: 'Congo}, Democratic Republic'},
{ id: 'CK', text: 'Cook Islands'},
{ id: 'CR', text: 'Costa Rica'},
{ id: 'CI', text: 'Cote D\'Ivoire'},
{ id: 'HR', text: 'Croatia'},
{ id: 'CU', text: 'Cuba'},
{ id: 'CY', text: 'Cyprus'},
{ id: 'CZ', text: 'Czech Republic'},
{ id: 'DK', text: 'Denmark'},
{ id: 'DJ', text: 'Djibouti'},
{ id: 'DM', text: 'Dominica'},
{ id: 'DO', text: 'Dominican Republic'},
{ id: 'EC', text: 'Ecuador'},
{ id: 'EG', text: 'Egypt'},
{ id: 'SV', text: 'El Salvador'},
{ id: 'GQ', text: 'Equatorial Guinea'},
{ id: 'ER', text: 'Eritrea'},
{ id: 'EE', text: 'Estonia'},
{ id: 'ET', text: 'Ethiopia'},
{ id: 'FK', text: 'Falkland Islands (Malvinas)'},
{ id: 'FO', text: 'Faroe Islands'},
{ id: 'FJ', text: 'Fiji'},
{ id: 'FI', text: 'Finland'},
{ id: 'FR', text: 'France'},
{ id: 'GF', text: 'French Guiana'},
{ id: 'PF', text: 'French Polynesia'},
{ id: 'TF', text: 'French Southern Territories'},
{ id: 'GA', text: 'Gabon'},
{ id: 'GM', text: 'Gambia'},
{ id: 'GE', text: 'Georgia'},
{ id: 'DE', text: 'Germany'},
{ id: 'GH', text: 'Ghana'},
{ id: 'GI', text: 'Gibraltar'},
{ id: 'GR', text: 'Greece'},
{ id: 'GL', text: 'Greenland'},
{ id: 'GD', text: 'Grenada'},
{ id: 'GP', text: 'Guadeloupe'},
{ id: 'GU', text: 'Guam'},
{ id: 'GT', text: 'Guatemala'},
{ id: 'GG', text: 'Guernsey'},
{ id: 'GN', text: 'Guinea'},
{ id: 'GW', text: 'Guinea-Bissau'},
{ id: 'GY', text: 'Guyana'},
{ id: 'HT', text: 'Haiti'},
{ id: 'HM', text: 'Heard Island & Mcdonald Islands'},
{ id: 'VA', text: 'Holy See (Vatican City State)'},
{ id: 'HN', text: 'Honduras'},
{ id: 'HK', text: 'Hong Kong'},
{ id: 'HU', text: 'Hungary'},
{ id: 'IS', text: 'Iceland'},
{ id: 'IN', text: 'India'},
{ id: 'ID', text: 'Indonesia'},
{ id: 'IR', text: 'Iran}, Islamic Republic Of'},
{ id: 'IQ', text: 'Iraq'},
{ id: 'IE', text: 'Ireland'},
{ id: 'IM', text: 'Isle Of Man'},
{ id: 'IL', text: 'Israel'},
{ id: 'IT', text: 'Italy'},
{ id: 'JM', text: 'Jamaica'},
{ id: 'JP', text: 'Japan'},
{ id: 'JE', text: 'Jersey'},
{ id: 'JO', text: 'Jordan'},
{ id: 'KZ', text: 'Kazakhstan'},
{ id: 'KE', text: 'Kenya'},
{ id: 'KI', text: 'Kiribati'},
{ id: 'KR', text: 'Korea'},
{ id: 'KW', text: 'Kuwait'},
{ id: 'KG', text: 'Kyrgyzstan'},
{ id: 'LA', text: 'Lao People\'s Democratic Republic'},
{ id: 'LV', text: 'Latvia'},
{ id: 'LB', text: 'Lebanon'},
{ id: 'LS', text: 'Lesotho'},
{ id: 'LR', text: 'Liberia'},
{ id: 'LY', text: 'Libyan Arab Jamahiriya'},
{ id: 'LI', text: 'Liechtenstein'},
{ id: 'LT', text: 'Lithuania'},
{ id: 'LU', text: 'Luxembourg'},
{ id: 'MO', text: 'Macao'},
{ id: 'MK', text: 'Macedonia'},
{ id: 'MG', text: 'Madagascar'},
{ id: 'MW', text: 'Malawi'},
{ id: 'MY', text: 'Malaysia'},
{ id: 'MV', text: 'Maldives'},
{ id: 'ML', text: 'Mali'},
{ id: 'MT', text: 'Malta'},
{ id: 'MH', text: 'Marshall Islands'},
{ id: 'MQ', text: 'Martinique'},
{ id: 'MR', text: 'Mauritania'},
{ id: 'MU', text: 'Mauritius'},
{ id: 'YT', text: 'Mayotte'},
{ id: 'MX', text: 'Mexico'},
{ id: 'FM', text: 'Micronesia}, Federated States Of'},
{ id: 'MD', text: 'Moldova'},
{ id: 'MC', text: 'Monaco'},
{ id: 'MN', text: 'Mongolia'},
{ id: 'ME', text: 'Montenegro'},
{ id: 'MS', text: 'Montserrat'},
{ id: 'MA', text: 'Morocco'},
{ id: 'MZ', text: 'Mozambique'},
{ id: 'MM', text: 'Myanmar'},
{ id: 'NA', text: 'Namibia'},
{ id: 'NR', text: 'Nauru'},
{ id: 'NP', text: 'Nepal'},
{ id: 'NL', text: 'Netherlands'},
{ id: 'AN', text: 'Netherlands Antilles'},
{ id: 'NC', text: 'New Caledonia'},
{ id: 'NZ', text: 'New Zealand'},
{ id: 'NI', text: 'Nicaragua'},
{ id: 'NE', text: 'Niger'},
{ id: 'NG', text: 'Nigeria'},
{ id: 'NU', text: 'Niue'},
{ id: 'NF', text: 'Norfolk Island'},
{ id: 'MP', text: 'Northern Mariana Islands'},
{ id: 'NO', text: 'Norway'},
{ id: 'OM', text: 'Oman'},
{ id: 'PK', text: 'Pakistan'},
{ id: 'PW', text: 'Palau'},
{ id: 'PS', text: 'Palestinian Territory}, Occupied'},
{ id: 'PA', text: 'Panama'},
{ id: 'PG', text: 'Papua New Guinea'},
{ id: 'PY', text: 'Paraguay'},
{ id: 'PE', text: 'Peru'},
{ id: 'PH', text: 'Philippines'},
{ id: 'PN', text: 'Pitcairn'},
{ id: 'PL', text: 'Poland'},
{ id: 'PT', text: 'Portugal'},
{ id: 'PR', text: 'Puerto Rico'},
{ id: 'QA', text: 'Qatar'},
{ id: 'RE', text: 'Reunion'},
{ id: 'RO', text: 'Romania'},
{ id: 'RU', text: 'Russian Federation'},
{ id: 'RW', text: 'Rwanda'},
{ id: 'BL', text: 'Saint Barthelemy'},
{ id: 'SH', text: 'Saint Helena'},
{ id: 'KN', text: 'Saint Kitts And Nevis'},
{ id: 'LC', text: 'Saint Lucia'},
{ id: 'MF', text: 'Saint Martin'},
{ id: 'PM', text: 'Saint Pierre And Miquelon'},
{ id: 'VC', text: 'Saint Vincent And Grenadines'},
{ id: 'WS', text: 'Samoa'},
{ id: 'SM', text: 'San Marino'},
{ id: 'ST', text: 'Sao Tome And Principe'},
{ id: 'SA', text: 'Saudi Arabia'},
{ id: 'SN', text: 'Senegal'},
{ id: 'RS', text: 'Serbia'},
{ id: 'SC', text: 'Seychelles'},
{ id: 'SL', text: 'Sierra Leone'},
{ id: 'SG', text: 'Singapore'},
{ id: 'SK', text: 'Slovakia'},
{ id: 'SI', text: 'Slovenia'},
{ id: 'SB', text: 'Solomon Islands'},
{ id: 'SO', text: 'Somalia'},
{ id: 'ZA', text: 'South Africa'},
{ id: 'GS', text: 'South Georgia And Sandwich Isl.'},
{ id: 'ES', text: 'Spain'},
{ id: 'LK', text: 'Sri Lanka'},
{ id: 'SD', text: 'Sudan'},
{ id: 'SR', text: 'Suriname'},
{ id: 'SJ', text: 'Svalbard And Jan Mayen'},
{ id: 'SZ', text: 'Swaziland'},
{ id: 'SE', text: 'Sweden'},
{ id: 'CH', text: 'Switzerland'},
{ id: 'SY', text: 'Syrian Arab Republic'},
{ id: 'TW', text: 'Taiwan'},
{ id: 'TJ', text: 'Tajikistan'},
{ id: 'TZ', text: 'Tanzania'},
{ id: 'TH', text: 'Thailand'},
{ id: 'TL', text: 'Timor-Leste'},
{ id: 'TG', text: 'Togo'},
{ id: 'TK', text: 'Tokelau'},
{ id: 'TO', text: 'Tonga'},
{ id: 'TT', text: 'Trinidad And Tobago'},
{ id: 'TN', text: 'Tunisia'},
{ id: 'TR', text: 'Turkey'},
{ id: 'TM', text: 'Turkmenistan'},
{ id: 'TC', text: 'Turks And Caicos Islands'},
{ id: 'TV', text: 'Tuvalu'},
{ id: 'UG', text: 'Uganda'},
{ id: 'UA', text: 'Ukraine'},
{ id: 'AE', text: 'United Arab Emirates'},
{ id: 'GB', text: 'United Kingdom'},
{ id: 'US', text: 'United States'},
{ id: 'UM', text: 'United States Outlying Islands'},
{ id: 'UY', text: 'Uruguay'},
{ id: 'UZ', text: 'Uzbekistan'},
{ id: 'VU', text: 'Vanuatu'},
{ id: 'VE', text: 'Venezuela'},
{ id: 'VN', text: 'Viet Nam'},
{ id: 'VG', text: 'Virgin Islands}, British'},
{ id: 'VI', text: 'Virgin Islands}, U.S.'},
{ id: 'WF', text: 'Wallis And Futuna'},
{ id: 'EH', text: 'Western Sahara'},
{ id: 'YE', text: 'Yemen'},
{ id: 'ZM', text: 'Zambia'},
{ id: 'ZW', text: 'Zimbabwe'}
];
function formatCountry (country) {
if (!country.id) { return country.text; }
var $country = $(
'<span class="flag-icon flag-icon-'+ country.id.toLowerCase() +' flag-icon-squared"></span>' +
'<span class="flag-text">'+ country.text+"</span>"
);//you can replace the first span with your images
return $country;
};
//Assuming you have a select element with name country
// e.g. <select name="name"></select>
$("[name='country']").select2({
placeholder: "Select a country",
templateResult: formatCountry,
data: isoCountries
});
});
})(jQuery);
.flag-text { margin-left: 10px; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/0.8.2/css/flag-icon.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<select name="country"></select>
or check the fiddle here.

Related

Filling the Total Column with jqxgrid Multiplication

Filling the Total Column with the jqxgrid Multiplier.
Hi there;
I want to multiply ItemQty and SinglePrice columns and write to TotalPrice column.
How can I do that.
I tried to use the code in this link but it didn't work.
The other columns show the data in the database.
var initFBListGrid=function(){
source =
{
dataType: 'json',
datafields: [
{ name: 'Id' },
{ name: 'OrderNumber' },
{ name: 'LineNumber' },
{ name: 'ItemNumber' },
{ name: 'ItemDescription' },
{ name: 'ItemQty' },
{ name: 'SinglePrice' },
{ name: 'Ixitem' },
{ name: 'IsUrgentHTML' },
{ name: 'StatusHTML' },
{ name: 'IsApproved' },
{ name: 'IsRejected' },
{ name: 'LatestPurchasePrice' },
{ name: 'PriceLevel' },
{ name: 'DeliveryDate' },
{ name: 'SupplierID' },
{ name: 'SupplierName' },
{ name: 'ProjectNumber' },
{ name: 'ProjectName' },
{ name: 'BuyerName' },
{ name: 'PendingApproverName' },
{ name: 'Company' },
{ name: 'Currency' },
{ name: 'HasAttachmentHTML' },
{ name: 'Glco' },
{ name: 'RequesterName' },
{ name: 'ApproverName' },
{
name: 'TotalPrice',
formatter: function (cellvalue, options, rowObject) {
var ItemQty = parseInt(rowObject.ItemQty, 10),
SinglePrice = parseInt(rowObject.SinglePrice,10);
return $.fmatter.util.NumberFormat(ItemQty * SinglePrice, $.jgrid.formatter.currency);
}
}
],
id: 'Id'
};

Unable to change default rangeSelector Highstock

I am trying to make 5M as my default rangeSelector but it seems that it keeps on insisting the 'all' on the selector.
rangeSelector: {
enabled: true,
buttons: [{
count: 2,
type: 'minute',
text: '2M'
}, {
count: 5,
type: 'minute',
text: '5M'
}, {
count: 15,
type: 'minute',
text: '15M'
}, {
count: 30,
type: 'minute',
text: '30M'
}, {
count: 3,
type: 'hour',
text: '3H'
}, {
count: 1,
type: 'day',
text: '1D'
}, {
count: 30,
type: 'day',
text: '30D'
}, {
type: 'all',
text: 'All'
}],
selected: 5,
},
Can someone tell me where my code goes wrong ?
Appreciate your help.
I have fixed my problem by adding allButtonsEnabled: true and changing selected: 1. I just got a bit confused a while ago
rangeSelector: {
enabled: true,
allButtonsEnabled: true,
buttons: [{
count: 2,
type: 'minute',
text: '2M'
}, {
count: 5,
type: 'minute',
text: '5M'
}, {
count: 15,
type: 'minute',
text: '15M'
}, {
count: 30,
type: 'minute',
text: '30M'
}, {
count: 3,
type: 'hour',
text: '3H'
}, {
count: 1,
type: 'day',
text: '1D'
}, {
count: 30,
type: 'day',
text: '30D'
}, {
type: 'all',
text: 'All'
}],
selected: 1,
},

How do I not show GMT and Timezone labels for Highcharts

As shown below:
Instead of that I want the x-axis labels to look something like this:
Without the GMT and time-zone. Sorry I am new to Highcharts !
EDIT: added code
Highcharts.theme = {
colors: ["#2b908f", "#90ee7e", "#f45b5b", "#7798BF", "#aaeeee", "#ff0066", "#eeaaee",
"#55BF3B", "#DF5353", "#7798BF", "#aaeeee"],
chart: {
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 1, y2: 1 },
stops: [
[0, '#2a2a2b'],
[1, '#3e3e40']
]
},
style: {
fontFamily: "'Unica One', sans-serif"
},
plotBorderColor: '#606063'
},
title: {
style: {
color: '#E0E0E3',
textTransform: 'uppercase',
fontSize: '20px'
}
},
subtitle: {
style: {
color: '#E0E0E3',
textTransform: 'uppercase'
}
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
month: '%e. %b'
},
gridLineColor: '#707073',
labels: {
style: {
color: '#E0E0E3'
}
},
lineColor: '#707073',
minorGridLineColor: '#505053',
tickColor: '#707073',
title: {
style: {
color: '#A0A0A3'
}
}
},
yAxis: {
gridLineColor: '#707073',
labels: {
style: {
color: '#E0E0E3'
}
},
lineColor: '#707073',
minorGridLineColor: '#505053',
tickColor: '#707073',
tickWidth: 1,
title: {
style: {
color: '#A0A0A3'
}
}
},
tooltip: {
backgroundColor: 'rgba(0, 0, 0, 0.85)',
style: {
color: '#F0F0F0'
}
},
plotOptions: {
series: {
dataLabels: {
color: '#B0B0B3'
},
marker: {
lineColor: '#333'
}
},
boxplot: {
fillColor: '#505053'
},
candlestick: {
lineColor: 'white'
},
errorbar: {
color: 'white'
}
},
credits: {
style: {
color: '#666'
}
},
labels: {
style: {
color: '#707073'
}
},
drilldown: {
activeAxisLabelStyle: {
color: '#F0F0F3'
},
activeDataLabelStyle: {
color: '#F0F0F3'
}
},
navigation: {
buttonOptions: {
symbolStroke: '#DDDDDD',
theme: {
fill: '#505053'
}
}
},
navigator: {
handles: {
backgroundColor: '#666',
borderColor: '#AAA'
},
outlineColor: '#CCC',
maskFill: 'rgba(255,255,255,0.1)',
series: {
color: '#7798BF',
lineColor: '#A6C7ED'
},
xAxis: {
gridLineColor: '#505053'
}
},
scrollbar: {
barBackgroundColor: '#808083',
barBorderColor: '#808083',
buttonArrowColor: '#CCC',
buttonBackgroundColor: '#606063',
buttonBorderColor: '#606063',
rifleColor: '#FFF',
trackBackgroundColor: '#404043',
trackBorderColor: '#404043'
},
global: {
useUTC: false
},
// special colors for some of the
legendBackgroundColor: 'rgba(0, 0, 0, 0.5)',
background2: '#505053',
dataLabelsColor: '#B0B0B3',
textColor: '#C0C0C0',
contrastTextColor: '#F0F0F3',
maskColor: 'rgba(255,255,255,0.3)'
};
Highcharts.setOptions(Highcharts.theme);
////////////////////
$('#high').highcharts({
chart: {
borderColor: '#EBBA95',
borderRadius: 20,
borderWidth: 2,
zoomType: 'x',
spacingTop: 40
},
title: {
text: 'Humidity/Temperature Graph',
x: -20, //center
y: 5
},
subtitle: {
text: 'Elemental Electronics | Click and drag to zoom',
x: -20,
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
month: '%e. %b'
},
categories: yearSeries,
},
yAxis: {
title: {
text: 'All points on one graph'
},
plotBands: [
{
color: 'orange', // Color value
from: 0, // Start of the plot band
to: -25 // End of the plot band
,label: {
text: 'Lower Range: 1 ppm',
color: '#ffffff',
align: 'left'
}
},
{
label: {
text: 'Upper Range: 5ppm',
color: '#ffffff',
align: 'left'
},
color: 'orange', // Color value
from: 75, // Start of the plot band
to: 100// End of the plot band
}
],
},
tooltip: {
valueSuffix: '°C'
},
series: [
{
name: 'Temperature',
textColor: '#ffffff',
data: mySeriestemp
}
],
});
});
mySeriestemp and yearSeries are both from arrays that I got using:
for (var i = 0; i < year.length; i++) {
yearSeries.push(new Date(year[i]));
mySeriestemp.push(temp[i]);
}
update
As I seen your code you need to add following
labels: {
formatter: function() {
return Highcharts.dateFormat('%e. %b', this.value);
}
See the working fiddle with your code
Use this
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
month: '%e. %b'
}
}
formatter function needs to added here to show the labels in desired format
labels: {
formatter: function() {
return Highcharts.dateFormat('%e. %b', this.value);
}

Set file type dynamically when click on exporting menu in highcharts using chart.exportChart function

I want to set filetype according to click on export menu on the right in the given url. For example if click on pdf then filetype should be set as application/pdf.
var defaultTitle = "Kalar";
var drilldownTitle = "";
var ydrillupTitle = "# of Kalar";
var ydrilldownTitle = "";
var chart = new Highcharts.Chart({
chart: {
type: 'column',
renderTo: 'upendra',
events: {
drilldown: function(e) {
chart.setTitle({ text: drilldownTitle + e.point.title });
chart.yAxis[0].axisTitle.attr({
text: ydrilldownTitle
});
},
drillup: function(e) {
chart.setTitle({ text: defaultTitle });
chart.yAxis[0].axisTitle.attr({
text: ydrillupTitle
});
},
click: function(e) {
chart.exportChart({
filename: chart.options.title.text
});
}
}
},
title: {
text: defaultTitle
},
subtitle: {
text: ''
},
credits: {
enabled: false
},
xAxis: {
categories: ['one', 'two']
},
yAxis: {
allowDecimals: false,
min: 0,
title: {
text: ydrillupTitle
}
},
plotOptions: {
column: {
stacking: 'normal',
showInLegend: true
}
},
series:[
{
name:'A',
color:'#E9ECEE',
tooltip: {
headerFormat: '',
pointFormat: '{point.name}:<br/><b>{point.y}</b> Bookings (<b>{point.percentage:, .1f} %</b>)<br/>'
},
data:[
{
name: "A",
title: "A by Company",
x:0, y:2
}, {
name: "",
x:1, y:0
}
]
},
{
name:'Arrived',
color:'#B3E5C4',
tooltip: {
headerFormat: '',
pointFormat: '{point.name}:<br/><b>{point.y}</b> Bookings (<b>{point.percentage:, .1f} %</b>)<br/>'
},
data:[
{
name: "Arrived",
title: "Planned Bookings by Goods Type",
x:0, y:10
}, {
name: "",
x:1, y:0
}
]
},
{
name:'Unscheduled',
color:'#A9ABAD',
tooltip: {
headerFormat: '',
pointFormat: '{point.name}:<br/><b>{point.y}</b> Deliveries (<b>{point.percentage:, .1f} %</b>)<br/>'
},
data:[
{
name: "",
x:0, y:0
},
{
name: "Unscheduled",
title: "Unscheduled Deliveries by Company",
x:1, y:8
}
]
},
{
name:'Rejected',
color:'#FF838F',
tooltip: {
headerFormat: '',
pointFormat: '{point.name}:<br/><b>{point.y}</b> Deliveries (<b>{point.percentage:, .1f} %</b>)<br/>'
},
data:[
{
name: "",
x:0, y:0
},
{
name: "Rejected",
title: "Rejected Deliveries Reasons",
x:1, y:7
}
]
},
{
name:'Complete',
color:'#B3E5C4',
tooltip: {
headerFormat: '',
pointFormat: '{point.name}:<br/><b>{point.y}</b> Deliveries (<b>{point.percentage:, .1f} %</b>)<br/>'
},
data:[
{
name: "",
x:0, y:0
},
{
name: "Complete",
title: "Actual Deliveries by Goods Type",
x:1, y:6
}
]
}
]
});
});
You can edit menuItems in exporting options and then set filename, extracting that information from title.
exporting: {
buttons: {
contextButton: {
menuItems: [{
textKey: 'printChart',
onclick: function () {
this.print();
}
}, {
separator: true
}, {
textKey: 'downloadPNG',
onclick: function () {
var title = this.title.textStr;
this.exportChart({
type: 'image/png',
filename: title
});
}
}, {
textKey: 'downloadJPEG',
onclick: function () {
var title = this.title.textStr;
this.exportChart({
type: 'image/jpeg',
filename: title
});
}
}, {
textKey: 'downloadPDF',
onclick: function () {
var title = this.title.textStr;
this.exportChart({
type: 'application/pdf',
filename: title
});
}
}, {
textKey: 'downloadSVG',
onclick: function () {
var title = this.title.textStr;
this.exportChart({
type: 'image/svg+xml',
filename: title
});
}
}]
}
}
},
Example: http://jsfiddle.net/bmp6Leh5/4/

Highcharts - Can a column chart drill down to a bar chart?

I have a project where the data in the main chart is best represented as a column chart and the data in a drilldown should be a bar chart.
However when I try to drilldown, the drilldown is still a column chart even with the type set to bar.
Is this possible to do with the provided Highcharts drilldown API? I would like to have the "back" button and support multiple drilldown levels.
Here is a basic example that demonstrates the problem:
$(function () {
// Create the chart
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Basic drilldown'
},
xAxis: {
type: 'category'
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true,
}
}
},
series: [{
name: 'Things',
colorByPoint: true,
data: [{
name: 'Animals',
y: 5,
drilldown: 'animals'
}, {
name: 'Fruits',
y: 2,
drilldown: 'fruits'
}, {
name: 'Cars',
y: 4,
drilldown: 'cars'
}]
}],
drilldown: {
series: [{
id: 'animals',
type: 'bar',
data: [
['Cats', 4],
['Dogs', 2],
['Cows', 1],
['Sheep', 2],
['Pigs', 1]
]
}, {
id: 'fruits',
type: 'bar',
data: [
['Apples', 4],
['Oranges', 2]
]
}, {
id: 'cars',
type: 'bar',
data: [
['Toyota', 4],
['Opel', 2],
['Volkswagen', 2]
]
}]
}
})
});
Here is a JSFIDDLE
http://jsfiddle.net/N2g98/
EDIT: This seems like it could be considered a bug in Highcharts as it is more of a problem than just drilling down from a column to a bar chart. If you start with a bar graph and drill down to a line graph, the axes are messed up in the drilldown.
EDIT2: Here is the solution I ended up with
$(function () {
(function (H) {
H.wrap(H.Point.prototype, 'init', function (proceed, series, options, x) {
var point = proceed.call(this, series, options, x),
chart = series.chart,
tick = series.xAxis && series.xAxis.ticks[x],
tickLabel = tick && tick.label;
if (point.drilldown) {
if (tickLabel) {
if (!tickLabel._basicStyle) {
tickLabel._basicStyle = tickLabel.element.getAttribute('style');
}
tickLabel.addClass('highcharts-drilldown-axis-label').css({
'text-decoration': 'none',
'font-weight': 'normal',
'cursor': 'auto'
}).on('click', function () {
if (point.doDrilldown) {
return false;
}
});
}
}
return point;
});
H.wrap(H.Pointer.prototype, 'setDOMEvents', function (proceed) {
proceed.call(this);
var pointer = this,
container = pointer.chart.container;
container.onclick = function (e) {
if (typeof pointer.onContainerClick === 'function') {
pointer.onContainerClick(e);
}
};
});
H.wrap(H.Chart.prototype, 'drillUp', function (proceed) {
if (!this.customDrilldown) {
proceed.call(this);
} else {
var userOptions = this.userOptions;
var drilldownLevels = this.drilldownLevels;
var level = drilldownLevels.pop();
var newChartConfig = level.userOptions;
this.destroy();
$(userOptions.chart.renderTo).highcharts(newChartConfig);
var newChart = $(userOptions.chart.renderTo).highcharts();
newChart.drilldownLevels = drilldownLevels;
if (drilldownLevels.length !== 0) {
newChart.showDrillUpButton();
}
newChart.customDrilldown = true;
HighchartsAdapter.fireEvent(newChart, 'drillup', {
seriesOptions: newChartConfig
});
}
});
H.wrap(H.Point.prototype, 'doDrilldown', function (proceed, _holdRedraw) {
if (!$.isPlainObject(this.drilldown)) {
proceed.call(this, _holdRedraw);
} else {
var newChartConfig = this.drilldown;
var oldChart = this.series.chart;
var userOptions = oldChart.userOptions;
var drilldownLevels = oldChart.drilldownLevels;
if (!drilldownLevels) {
drilldownLevels = [];
}
var oldSeries = this.series;
var level = {
userOptions: userOptions,
seriesOptions: oldSeries.userOptions
};
drilldownLevels.push(level);
oldChart.destroy();
$(userOptions.chart.renderTo).highcharts(newChartConfig);
var newChart = $(userOptions.chart.renderTo).highcharts();
newChart.drilldownLevels = drilldownLevels;
newChart.showDrillUpButton();
newChart.customDrilldown = true;
HighchartsAdapter.fireEvent(newChart, 'drilldown', {
point: this,
seriesOptions: newChartConfig
});
}
});
}(Highcharts));
var pieDD1 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 5
}, {
name: 'Blue',
y: 25
}]
}]
};
var pieDD2 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 15
}, {
name: 'Blue',
y: 25
}]
}]
};
var pieDD3 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 3
}, {
name: 'Blue',
y: 5
}]
}]
};
var pieDD4 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 50
}, {
name: 'Blue',
y: 3
}]
}]
};
var pieDD5 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 13
}, {
name: 'Blue',
y: 11
}]
}]
};
var pieDD6 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 50
}, {
name: 'Blue',
y: 25
}]
}]
};
var pieDD7 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 5
}, {
name: 'Blue',
y: 5
}]
}]
};
var pieDD8 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 15
}, {
name: 'Blue',
y: 5
}]
}]
};
var pieDD9 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 4
}, {
name: 'Blue',
y: 8
}]
}]
};
var pieDD10 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 33
}, {
name: 'Blue',
y: 66
}]
}]
};
var pieDD11 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 9
}, {
name: 'Blue',
y: 1
}]
}]
};
var pieDD12 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 8
}, {
name: 'Blue',
y: 13
}]
}]
};
var pieDD13 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 70
}, {
name: 'Blue',
y: 25
}]
}]
};
var pieDD14 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 50
}, {
name: 'Blue',
y: 10
}]
}]
};
var pieDD15 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 8
}, {
name: 'Blue',
y: 5
}]
}]
};
var pieDD16 = {
chart: {
type: 'pie'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'colors'
},
series: [{
name: 'Color',
data: [{
name: 'Red',
y: 5
}, {
name: 'Blue',
y: 8
}]
}]
};
var drilldown = {
chart: {
type: 'column'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'subtitle'
},
xAxis: {
type: 'category',
title: {
text: 'X Axis'
}
},
yAxis: {
title: {
text: 'Y Axis'
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'Widget A',
data: [{
name: 'Qtr 1',
y: 5,
drilldown: pieDD1
}, {
name: 'Qtr 2',
y: 25,
drilldown: pieDD2
}, {
name: 'Qtr 3',
y: 25,
drilldown: pieDD3
}, {
name: 'Qtr 4',
y: 20,
drilldown: pieDD4
}]
}, {
name: 'Widget B',
data: [{
name: 'Qtr 1',
y: 25,
drilldown: pieDD5
}, {
name: 'Qtr 2',
y: 5,
drilldown: pieDD6
}, {
name: 'Qtr 3',
y: 5,
drilldown: pieDD7
}, {
name: 'Qtr 4',
y: 15,
drilldown: pieDD8
}]
}]
};
var drilldown2 = {
chart: {
type: 'column'
},
title: {
text: 'Basic drilldown'
},
subtitle: {
text: 'subtitle2'
},
xAxis: {
type: 'category',
title: {
text: 'X Axis'
}
},
yAxis: {
title: {
text: 'Y Axis'
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'Widget A',
data: [{
name: 'Qtr 1',
y: 15,
drilldown: pieDD9
}, {
name: 'Qtr 2',
y: 15,
drilldown: pieDD10
}, {
name: 'Qtr 3',
y: 30,
drilldown: pieDD11
}, {
name: 'Qtr 4',
y: 5,
drilldown: pieDD12
}]
}, {
name: 'Widget B',
data: [{
name: 'Qtr 1',
y: 5,
drilldown: pieDD13
}, {
name: 'Qtr 2',
y: 25,
drilldown: pieDD14
}, {
name: 'Qtr 3',
y: 10,
drilldown: pieDD15
}, {
name: 'Qtr 4',
y: 20,
drilldown: pieDD16
}]
}]
};
// Create the chart
$('#container').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Basic drilldown'
},
xAxis: {
type: 'category'
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'Yearly Orders',
data: [{
name: 'Location 1',
y: 100,
drilldown: drilldown
}, {
name: 'Location 2',
y: 150,
drilldown: drilldown2
}]
}]
});
});
http://jsfiddle.net/skTHx/22/

Resources