score:4
In case your chart (canvas) is of fixed size, you can easily solve this problem by adding an additional dataset
that specifies nothing but the icons to be shown in the chart.
{
data: data2.map((v, i) => imageIndexes.includes(i) ? v + 1.2 : null),
fill: false,
pointStyle: icon,
pointRadius: 22,
pointHoverRadius: 22
}
Given the data array data2
and the array imageIndexes
, the data
of the icons dataset
can be built using Array.map
. Note that the values - where any - are derived from corresponding values in data2
but slightly increased to make the images appear on top of them.
data2.map((v, i) => imageIndexes.includes(i) ? v + 1.2 : null)
Further you'll need to define a tooltips
object inside the chart options
in order to style the popup and to make sure, tooltips are only displayed when the mouse hovers over the icons.
tooltips: {
filter: tooltipItem => tooltipItem.datasetIndex == 2,
titleFontSize: 16,
titleAlign: 'center',
callbacks: {
title: (tooltipItem) => tooltipItem.length == 0 ? null : tooltipText,
label: () => null
}
},
Please have a look at the runnable code snipped below.
const labels = ['A', 'B', 'C', 'D', 'E', 'F'];
const alerts = ['B', 'D'];
const data1 = [0, 2, 1, 3, 2, 1];
const data2 = [1, 3, 3, 4, 3, 2];
const imageIndexes = [1, 3];
const tooltipText = 'Efficiency of Standard Curve\nnot opimal';
var icon = new Image();
icon.src = 'https://i.stack.imgur.com/YvlWY.png';
const chart = new Chart(document.getElementById("myChart"), {
type: "line",
data: {
labels: labels,
datasets: [{
data: data1,
fill: false,
backgroundColor: 'blue',
borderColor: 'blue',
lineTension: 0,
pointRadius: 5,
pointHoverRadius: 5,
pointBorderWidth: 3,
pointHoverBorderWidth: 3,
pointBorderColor: 'white',
pointHoverBorderColor: 'white'
},
{
data: data2,
fill: false,
showLine: false,
backgroundColor: 'orange',
pointRadius: 4,
pointHoverRadius: 4
},
{
data: data2.map((v, i) => imageIndexes.includes(i) ? v + 1.2 : null),
fill: false,
pointStyle: icon,
pointRadius: 22,
pointHoverRadius: 22
}
]
},
options: {
responsive: false,
title: {
display: false
},
legend: {
display: false
},
tooltips: {
filter: tooltipItem => tooltipItem.datasetIndex == 2,
titleFontSize: 16,
titleAlign: 'center',
callbacks: {
title: (tooltipItem) => tooltipItem.length == 0 ? null : tooltipText,
label: () => null
}
},
scales: {
yAxes: [{
ticks: {
min: 0,
max: 6,
stepSize: 1
}
}],
xAxes: [{
gridLines: {
display: false
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" style="width: 500px; height: 200px"></canvas>
Source: stackoverflow.com
Related Query
- ChartJs line chart - display permanent icon above some data points with text on hover
- Display line chart with connected dots using chartJS
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Display two line graphs with data starting at different points
- How to display chart using Chartjs with JSON data in Laravel
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- Real-time line chart with ChartJS using Ajax data
- Chart.js combined line and bar chart with differing data points
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- Can chart.js display text associated with each point on a line chart that is permanently visible?
- Position of the x-axis labels is not in sync with the line chart data points
- Chartjs - line chart display with addData() is bugged?
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS - Draw chart with label by month, data by day
- line chart with {x, y} point data displays only 2 values
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to display Line Chart dataset point labels with Chart.js?
- ChartJS - Line Chart with different size datasets
- How to display data labels outside in pie chart with lines in ionic
- Show data dynamically in line chart - ChartJS
- Chartjs - data format for bar chart with multi-level x-axes
- ChartJS - Line chart issue with only 1 point
- ChartJS - handling of overlapping points in line chart
- How to create chartjs chart with data from database C#
- Gradient line chart with ChartJS
- Missing Tooltip for some data points using chartjs
- Adding line over stacked line chart with ChartJS
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
- react-chartjs-2 line chart with time on X-axes with multiple data sets plotted wrong
More Query from same tag
- Display chart data based on API call
- Can i use crosshairs with OHLC/candlestick charts using ChartJS
- chart.js hide gridline inside chartarea but keep y-axis border
- chart.js how do we extract clicked var?
- Javascript Chart: How to hide the data
- ChartJS vertical line not removed on touchend
- How to convert Json to Array In javascript for chart.js
- How to make a 30days chart responsive on mobile using chart.js?
- how to show REALTIME chartjs of specific ID in mysql when click on corresponding marker on googlemap javascript
- Why is my line graph going backwards in chartjs?
- How can I create a time series line graph in chart.js?
- How can I add dataset toggle to Chart.js?
- Can't generate multi-axis combo chart (bar/line) using chart.js 2.7
- drawing bar chart with chart.js jQuery
- Django 1.11 - child template not inheriting from parent
- How to pass a variable to the dataset for a graph in chart.js?
- Chart js shows old chart data when hover
- Chart Js , loading data on selection but bar graph displaying old values as well on hovering
- Chart.js multiple columns of data for the same label
- Hide gridlines in chartjs without the drawTicks
- Fully destroy charts on ngDestroy method from Angular 4
- ChartJS date formating
- How I create a pie chart based on COUNT(php + mysql)?
- Bar chart from Vue-chartjs doesn't render
- Manipulate chart.js based plots in R (radarchart)
- Chart.js how to use scriptable options
- ChartJS 3 get height of stacked vertical bar
- ChartJS V3 borderRadius only works on top
- Aurelia - Unhandled rejection - Chart.js and Moment.js
- Chartjs stacked bar separate tooltip for all stacked