Highcharts:Add image to legend - highcharts

I have this chart. I want to add some check icons in the rectangle from the legend, when it is selected.
How can I do this?
var chart = new Highcharts.Chart({
chart: {
renderTo: this,
type: 'pie'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
series: [{
data: jsonChart
}],
legend: {
symbolWidth: 40,
align: 'right',
layout: 'vertical',
verticalAlign: 'top',
x: 40,
y:0,
width: 150,
},
});
});The chart

You can use Renderer to render an image in the chart.
function drawTicks() {
const {
translateX: tx,
translateY: ty
} = this.legend.group;
this.series[0].points.forEach(p => {
let img = p.img;
if (!img) {
p.img = img = this.renderer.image('http://icons.iconarchive.com/icons/visualpharm/must-have/256/Check-icon.png', -9999, 0, 15, 15).attr({
zIndex: 10
}).add();
}
const bbox = p.legendSymbol.getBBox();
const x = p.legendGroup.translateX + tx;
const y = p.legendGroup.translateY + ty;
img.attr({
x: x,
y: y
});
});
}
on load/redraw draw ticks
chart: {
events: {
load: drawTicks,
redraw: drawTicks
}
},
on legendItemClick change the img's visibility
series: [{
type: 'pie',
name: 'Browser share',
point: {
events: {
legendItemClick: function() {
this.img.attr({
visibility: !this.visible ? 'visibile' : 'hidden'
});
}
}
},
example: http://jsfiddle.net/3348yjrq/

Related

How to detect when dataLabels are overlapping and adjust them programmatically

I have a stacked column/scatter chart with some dataLabels off to one side. The issue I am facing is that when my two markers begin to get close to one another, their dataLabels overlap
I need to always show both labels, so is there a way to detect when labels are overlapping and move the bottom one down by adjusting its y value based on how much overlap there is?
sample fiddle of the issue
Highcharts.chart('container', {
chart: {
type: 'column',
width: 500
},
title: {
text: 'Stacked column chart'
},
xAxis: {
visible: false,
},
yAxis: {
min: 0,
visible: false,
title: {
},
},
legend: {
layout:"vertical",
align: "right",
verticalAlign: "bottom",
itemMarginTop: 15,
y: -10,
x: -50
},
tooltip: {
enabled: false,
},
plotOptions: {
scatter: {
marker: {
symbol: "triangle",
},
dataLabels: {
enabled: true,
x: -80,
y: 50,
allowOverlap: true,
useHTML: true,
}
},
column: {
pointWidth: 70,
stacking: 'normal',
dataLabels: {
enabled: false
}
}
},
series: [{
name: '',
data: [100],
color: "#ededed"
}, {
name: '',
data: [500]
}, {
name: '',
data: [400]
},
{
type: "scatter",
data: [1000],
color: "#000",
dataLabels: {
formatter: function(){
return "<div class='label-text'>Your goal of <br/>$"+ this.y +"<br/>text</div>"
},
}
},
{
type: "scatter",
data: [900],
color: "#000",
dataLabels: {
formatter: function(){
return "<div class='label-text'>You are here <br/>$"+ this.y +"<br/>text</div>"
},
}
}]
});
You can correct data-labels positions by using the attr method on their SVG elements.
For example:
chart: {
events: {
render: function() {
const series = this.series;
const dl1 = series[3].points[0].dataLabel;
const dl2 = series[4].points[0].dataLabel;
if (dl1.y + dl1.height > dl2.y) {
dl2.attr({
y: dl1.y + dl1.height
});
}
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/5Lmh4owb/
API Reference:
https://api.highcharts.com/class-reference/Highcharts.SVGElement.html#attr
https://api.highcharts.com/highcharts/chart.events.render

How to return series/point data from mouseover event?

I've created a donut chart using vue-highcharts/highcharts, and am trying to append text on mouseover to the center of the donut. I've got it appending static text, however I'm trying to return the data from the point I hover over, to display that in the center, however I can't seem to find any relevant data from within the event object. Does anyone know how I might be able to access that data?
chartOptions: {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
legend: {
align: 'right',
enabled: true,
layout: 'vertical',
verticalAlign: 'middle'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
center: ['50%', '50%'],
showInLegend: true,
events: {
mouseOver: (e) => {
const { chart } = this.$refs.highchartsRef;
const height = chart.chartHeight / 2;
const width = e.target.center[3];
console.log({ chart });
console.log({ e });
chart.renderer
.text('Top Test</br>Bottom Test', width, height, true)
.css({
color: '#4a4a4a',
fontSize: '16px',
fontWeight: 700
})
.addClass('highcharts-center-label text-center')
.add();
},
mouseOut: () => {
document.querySelector('.highcharts-center-label').remove();
}
}
}
},
series: [{
name: 'Categories',
colorByPoint: true,
size: '100%',
innerSize: '60%',
data: this.categories.map(category => {
return {
name: category.name,
y: calculateCategoryWeight(category)
};
})
}],
title: null,
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
}
}
Use point.events and a basic function instead of the arrow one.
plotOptions: {
pie: {
point: {
events: {
mouseOver: function() {
var point = this,
series = point.series,
chart = series.chart;
console.log(point.y);
}
}
}
}
}
Live demo: http://jsfiddle.net/BlackLabel/7oub1xzj/
API Reference: https://api.highcharts.com/highcharts/plotOptions.pie.point.events

highcharts heatmap with getjson

I need help with this heatmap chart
$(document).ready(function(){
var grf_heatmap = {
chart: {
type: 'heatmap',
renderTo:'map',
height: 600,
marginTop: 40,
marginBottom: 40
},
title: {
text: 'heatmap'
},
xAxis: {
categories: []
},
yAxis: {
categories: []
},
colorAxis: {
min: 0,
minColor: '#FFFFFF',
maxColor: '#990041'
},
legend: {
align: 'right',
layout: 'vertical',
margin: 0,
verticalAlign: 'top',
y: 25,
symbolHeight: 320
},
series: [{
turboThreshold:0,
borderWidth: 1,
point: {
events: {
click: function(e) {
//this.slice();
//console.log(e);
location.href = e.point.options.url;
e.preventDefault();
}
}
},
data:[],
dataLabels: {
enabled: true,
color: 'black',
style: {
textShadow: 'none',
HcTextStroke: null
}
}
}]}
$.getJSON("data.php", function(json) {
grf_heatmap.yAxis.categories = json[0]['data'];
grf_heatmap.xAxis.categories = json[2]['data'];
grf_heatmap.series[0].data = json[1]['data'];
chart = new Highcharts.Chart(grf_heatmap);
});
})
data.php retuns a json like this
[{"name":"EjeY",
"data":["NameA"]},
{"name":"datos",
"data":["{x:0,y:0,value:1,url:'test.php'}",
"{x:1,y:0,value:3,url:'test.php'}"]},
{"name":"EjeX",
"data":["1","2"]}
]
but the result is a blank chart.
what am i doing wrong?,
if i use only the json for xAxis and yAxis categories and complete the chart with this data
data[{x:0,y:0,value:1,url:'test.php'},{x:1,y:0,value:3,url:'test.php'}],
it works
Tanks,

Highcharts pie chart legend items showing gradient colors

I have created high-chart's pie chart with gradient colors,The problem is legend item of pie chart is displaying in gradient colors. I want legend item's color as without gradient. While doing this the gradient for pie-chart should remain same any suggestions please.
Here is my code:
/* Donut Chart */
$(function () {
colorsArray =['#AAE1FE', '#57C1FD', '#018BFD', '#0258E0','#002E77'];
$('#pieChart').highcharts({
credits: {
enabled: false
},
chart: {
type: 'pie',
options: {
enabled: true,
alpha: 45
},
plotShadow: false
},
colors:$.map(colorsArray,function (color) {
return {
radialGradient: { cx: 0.5, cy: 0.5, r: 0.5 },
stops: [
[0, Highcharts.Color(color).brighten(-0.5).get('rgb')],
[0.5, Highcharts.Color(color).brighten(-0.3).get('rgb')],
[0.75, Highcharts.Color(color).brighten(-0.1).get('rgb')],
[1,Highcharts.Color(color).brighten(-0.5).get('rgb')]// darken
]
};
}),
tooltip: {
useHTML: true,
formatter: function() {
return this.point.name +'<br>' +'<b>'+ 'Share: ' + this.y +'%';
}
},
plotOptions: {
pie: {
allowPointSelect: false,
innerSize: '60%',
depth: 20,
size: '100%',
showInLegend: true ,
dataLabels: {
enabled: false
},
point: {
events: {
legendItemClick: function () {
return false;
}
}
},
borderColor:'white',
borderWidth: '2px',
animation:false,
startAngle: 90,
}
},
series: [{
data: datapie,
states: {
hover: {
enabled: false
}
}
}],
legend: {
layout: 'vertical',
backgroundColor: 'white',
align: 'right',
verticalAlign: 'middle',
x:-100,
borderWidth: 1,
borderRadius: 10,
borderColor: '#eee',
zIndex: 20,
itemMarginTop: 5,
itemMarginBottom: 5,
labelFormatter: function () {
return this.name + ': ' + this.y + '%';
}
}
});
});
You can wrap drawLegendSymbol and then manipulate color in renderer.
var colorsArray = ['#AAE1FE', '#57C1FD', '#018BFD', '#0258E0', '#002E77'],
i = 0;
(function (HC) {
HC.wrap(Highcharts.seriesTypes.pie.prototype, 'drawLegendSymbol', function (fn, legend, item) {
var symbolHeight = legend.options.symbolHeight || legend.fontMetrics.f;
this.chart.renderer.rect(
0,
legend.baseline - symbolHeight + 1, // #3988
legend.symbolWidth,
symbolHeight,
legend.options.symbolRadius || 0).attr({
zIndex: 3,
fill: colorsArray[i]
}).add(item.legendGroup);
i++;
});
})(Highcharts)
Example: http://jsfiddle.net/od69fb7y/1/

highcharts graph no show yaxis values

The graph does not show the y-axis values​​. Could anyone help me with this? thanks.
If I put the value of prueba in the series data, everything worsk correctly
the prueba array and the dinero array are displayed correctly.
Thanks,
$.getJSON('http://localhost/jquery/grafica4.php', function(data) {
var dinero = [];
var categories = [];
var prueba = [79, 71, 66,44,42,38,32,10];
$.each(data, function(index, value) {
categories.push(data[index].producto_id);
dinero.push(data[index].cantidad);
alert("hola");
});
alert(dinero);
alert(prueba);
chart4 = new Highcharts.Chart({
chart: {
renderTo: 'container3',
type: 'column'
},
title: {
text: 'Productos más vendidos'
},
xAxis: {
categories: categories
},
yAxis: {
min: 0,
title:
text: 'Cantidad'
},
labels: {
formatter: function() {
return this.value; // clean, unformatted
}
}
},
legend: {
layout: 'vertical',
backgroundColor: '#FFFFFF',
align: 'left',
verticalAlign: 'top',
x: 100,
y: 70,
floating: true,
shadow: true
},
tooltip: {
formatter: function() {
return ''+
'<b>'+'Producto:'+this.x +'</b>'+'<br/>'+ this.y+ ' unidades';
}
},
plotOptions: {
column: {
pointPadding: 0.3,
borderWidth: 0
}
},
series: [{
name: 'Cantidad',
data: dinero
}],
exporting: {
filename: 'Facturacion-de-empleados'
}
});
});

Resources