I am trying ChartJS line chart with a set of data.I have multiple lines in it. I want to show the tooltip for the data. but I am not able to.
My code is
new Chart(canvas).Line(data,{
responsive : true,
animation: true,
barValueSpacing : 5,
barDatasetSpacing : 1,
tooltipFillColor: "rgba(0,0,0,0.8)",
multiTooltipTemplate: "<%= datasetLabel %> - <%= value %>",
showTooltips:true
});
currently my view of line chart is
What I want is like enter image description here
You can use the custom tooltips option. The below code is adapted from the example at https://github.com/nnnick/Chart.js/blob/master/samples/line-customTooltips.html
Preview
CSS
#canvas-holder {
width: 500px;
height: 300px;
}
#chartjs-tooltip {
opacity: 0;
position: absolute;
background: rgba(0, 0, 0, .7);
font-size: 12px;
color: white;
padding: 5px;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
}
.chartjs-tooltip-section{
padding: 1px;
}
HTML
<div id="canvas-holder">
<canvas id="chart" />
</div>
<div id="chartjs-tooltip"></div>
Script
function CustomLabel(short, long) {
this.short = short;
this.long = long;
}
CustomLabel.prototype.toString = function () {
return this.short;
}
var data = {
labels: [
new CustomLabel("January", "January 11"),
new CustomLabel("February", "February 12"),
new CustomLabel("March", "March 13"),
new CustomLabel("April", "April 14"),
new CustomLabel("May", "May 15"),
new CustomLabel("June", "June 16"),
new CustomLabel("July", "July 17")],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var ctx = document.getElementById("chart").getContext("2d");
new Chart(ctx).Line(data, {
responsive: true,
customTooltips: function (tooltip) {
var tooltipEl = $('#chartjs-tooltip');
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
tooltipEl.removeClass('above below');
tooltipEl.addClass(tooltip.yAlign);
var innerHtml = ['<div class="chartjs-tooltip-section">',
' <span>' + tooltip.title.long + '</span>',
'</div>'
].join('');
for (var i = tooltip.labels.length - 1; i >= 0; i--) {
innerHtml += [
'<div class="chartjs-tooltip-section">',
' <span style="color:' + tooltip.legendColors[i].fill + '">' + data.datasets[i].label + ': ' + tooltip.labels[i] + '</span>',
'</div>'
].join('');
}
tooltipEl.html(innerHtml);
tooltipEl.css({
opacity: 1,
left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px',
fontFamily: tooltip.fontFamily,
fontStyle: tooltip.fontStyle,
});
}
});
Fiddle - http://jsfiddle.net/fLm8c5ee/
Related
I would like to make my hAxis scrollable and show all my hAxis label values. On the hAxis, the IDs of certain stores are displayed. I have written the following code for the graph:
let array = [[
'Store', 'Amount',
]];
for (let item of response){
array.push([item.store_id.toString(), item.count]);
}
console.log(array);
let data = google.visualization.arrayToDataTable(array);
let options = {
chartArea: {
top: 20,
height: '65%',
width: '75%'
},
hAxis: {
title: 'Store ID',
slantedText: true,
slantedTextAngle: 90,
},
bar: {
groupWidth: 1
},
legend: {
position: 'none'
}
};
let chart = new google.visualization.ColumnChart(document.getElementById('chart-canvas-3'));
chart.draw(data, options);
And the .scss code:
.card-image {
display: block;
box-sizing: border-box;
position: relative;
overflow-x: scroll;
margin-bottom: sz(10pt);
padding-left: 10px;
padding-top: 10px;
padding-right: 10px;
.card-image-inner {
width: 100%;
}
}
So my question is, how can I display all 53 store ID labels on the hAxis (with scroll functionality)?
Thanks!
Current graph
Following this Mapbox example (Show and hide layers: https://docs.mapbox.com/mapbox-gl-js/example/toggle-layers/), I have been able to toggle between several layers on a map. But what I would like to achieve is when Layer 1 is visible, the visibility of Layer 2 and Layer 3 is none, when Layer 2 is visible, the visibility of Layer 1 and Layer 3 is none, etc.
Here's a fiddle example with the code I've written so far:
https://jsfiddle.net/reL53uo1/
Here's the logic for the toggle part:
// enumerate ids of the layers
var toggleableLayerIds = ['contours', 'museums', 'contours2'];
// set up the corresponding toggle button for each layer
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
var link = document.createElement('a');
link.href = '#';
link.className = 'active';
link.textContent = id;
link.onclick = function(e) {
var clickedLayer = this.textContent;
e.preventDefault();
e.stopPropagation();
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
// toggle layer visibility by changing the layout object's visibility property
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
} else {
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};
var layers = document.getElementById('menu');
layers.appendChild(link);
What would it be the most efficient approach to achieve this? Thanks.
You could change your onClick function so that instead of toggling individual layers off-and-on it activates the one you want and hides the others. You could do this with a for loop:
link.onclick = function(e) {
var clickedLayer = this.textContent;
e.preventDefault();
e.stopPropagation();
for (var j = 0; j < toggleableLayerIds.length; j++) {
if (clickedLayer === toggleableLayerIds[j]) {
layers.children[j].className = 'active';
map.setLayoutProperty(toggleableLayerIds[j], 'visibility', 'visible');
}
else {
layers.children[j].className = '';
map.setLayoutProperty(toggleableLayerIds[j], 'visibility', 'none');
}
}
};
Additionally, you should set the 'visibility' property of each layer to 'none' in your addLayer() function so that they are hidden until selected.
'layout': {
// make layer invisible by default
'visibility': 'none',
'line-join': 'round',
'line-cap': 'round'
},
I made these changes to your JSFiddle code and pasted below:
mapboxgl.accessToken = 'pk.eyJ1Ijoid2FzaGluZ3RvbnBvc3QiLCJhIjoibWJkTGx1SSJ9.6cMdwgs-AYrRtQsEkXlHqg';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
zoom: 15,
center: [-71.97722138410576, -13.517379300798098]
});
map.on('load', function() {
// add source and layer for museums
map.addSource('museums', {
type: 'vector',
url: 'mapbox://mapbox.2opop9hr'
});
map.addLayer({
'id': 'Layer 1',
'type': 'circle',
'source': 'museums',
'layout': {
// make layer invisible by default
'visibility': 'none'
},
'paint': {
'circle-radius': 8,
'circle-color': 'rgba(55,148,179,1)'
},
'source-layer': 'museum-cusco'
});
// add source and layer for contours
map.addSource('contours', {
type: 'vector',
url: 'mapbox://mapbox.mapbox-terrain-v2'
});
map.addLayer({
'id': 'Layer 2',
'type': 'line',
'source': 'contours',
'source-layer': 'contour',
'layout': {
// make layer invisible by default
'visibility': 'none',
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#877b59',
'line-width': 5
}
});
map.addLayer({
'id': 'Layer 3',
'type': 'line',
'source': 'contours',
'source-layer': 'contour',
'layout': {
// make layer invisible by default
'visibility': 'none',
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': 'yellow',
'line-width': 2
}
});
});
// enumerate ids of the layers
var toggleableLayerIds = ['Layer 1', 'Layer 2', 'Layer 3'];
// set up the corresponding toggle button for each layer
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
var link = document.createElement('a');
link.href = '#';
link.className = '';
link.textContent = id;
link.onclick = function(e) {
var clickedLayer = this.textContent;
e.preventDefault();
e.stopPropagation();
for (var j = 0; j < toggleableLayerIds.length; j++) {
if (clickedLayer === toggleableLayerIds[j]) {
layers.children[j].className = 'active';
map.setLayoutProperty(toggleableLayerIds[j], 'visibility', 'visible');
}
else {
layers.children[j].className = '';
map.setLayoutProperty(toggleableLayerIds[j], 'visibility', 'none');
}
}
};
var layers = document.getElementById('menu');
layers.appendChild(link);
}
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#menu {
background: #fff;
position: absolute;
z-index: 1;
top: 10px;
right: 10px;
border-radius: 3px;
width: 120px;
border: 1px solid rgba(0, 0, 0, 0.4);
font-family: 'Open Sans', sans-serif;
}
#menu a {
font-size: 13px;
color: #404040;
display: block;
margin: 0;
padding: 0;
padding: 10px;
text-decoration: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.25);
text-align: center;
}
#menu a:last-child {
border: none;
}
#menu a:hover {
background-color: #f8f8f8;
color: #404040;
}
#menu a.active {
background-color: #3887be;
color: #ffffff;
}
#menu a.active:hover {
background: #3074a4;
}
<link href="https://api.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.css" rel="stylesheet"/>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.js"></script>
<nav id="menu"></nav>
<div id="map"></div>
I am using echarts library for bar charts. I want scrollbar to display outside the canvas. Is there any way I can accomplish this?
Unfortunately, Echarts don't support scrollbar option.
However you can simply achieve this use a wrap DOM.
check this demo:
let echartsObj = echarts.init(document.querySelector('#canvas1'));
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar'
}]
};
echartsObj.setOption(option)
echartsObj = echarts.init(document.querySelector('#canvas2'));
option = {
xAxis: {},
yAxis: {},
series: [{
symbolSize: 20,
data: [
[10.0, 8.04],
[8.0, 6.95],
[13.0, 7.58],
[9.0, 8.81],
[11.0, 8.33],
[14.0, 9.96],
[6.0, 7.24],
[4.0, 4.26],
[12.0, 10.84],
[7.0, 4.82],
[5.0, 5.68]
],
type: 'scatter'
}]
};
echartsObj.setOption(option)
.echart-wrap {
display: inline-block;
width: 300px;
height: 300px;
overflow: scroll;
border: 1px solid black;
}
<html>
<header>
<script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts-en.min.js"></script>
</header>
<body>
<div class="echart-wrap">
<div id="canvas1" style="width: 500px; height: 500px">
</div>
</div>
<div class="echart-wrap">
<div id="canvas2" style="width: 500px; height: 500px">
</div>
</div>
</body>
</html>
I'm new to google chart, so my apology if this question is too noobs of me,
i have a material column chart with 2 bars each column, and i want the 2 bars at the last column to have a different color, and i dont want these color to be seen on the legend.i know i should use series for this, but i dont know how to construct it.
currenty what i have is:
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawchart);
function drawchart(){
var data = google.visualization.arrayToDataTable([
['Year', 'bar1', { role: 'annotation'}, 'bar2', { role: 'annotation'}],
['2015', 90, '90%', 69, '69%'],
['2016', 86, '86%', 96, '96%'],
['2017', 83, '83%', 87, '87%'],
['2018', 84, '84%', 78, '78%'],
['AVERAGE', 86, '86%', 82, '82%'],
]);
var options = {
height:500,
vAxis: {
gridlines: {count: 12},
viewWindow: { max: 120, min: 0 } ,
textStyle: { color: 'white' }
},
seriesType: 'bars' ,
series: {4:{colors: ['green', 'yellow']}},
legend: {position: 'bottom'},
annotations: {
textStyle: { fontSize: 18, bold: true }
}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart-div'));
var formatter = new google.visualization.NumberFormat(
{suffix:'%', fractionDigits: 0});
formatter.format(datasecond, 1); // Apply formatter to second column
formatter.format(datasecond, 3);
chart.draw(datasecond, google.charts.Bar.convertOptions(options));
}
click here to see the image of my goal output
thanks in advance!
you can use a 'style' column role to change the color of individual bars.
add the style to the data table, similar to annotations,
use null for the values that should be the default style
var data = google.visualization.arrayToDataTable([
['Year', 'bar1', {role: 'annotation'}, {role: 'style'}, 'bar2', {role: 'annotation'}, {role: 'style'}],
['2015', 90, '90%', null, 69, '69%', null],
['2016', 86, '86%', null, 96, '96%', null],
['2017', 83, '83%', null, 87, '87%', null],
['2018', 84, '84%', null, 78, '78%', null],
['AVERAGE', 86, '86%', '#109618', 82, '82%', '#ff9900'],
]);
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Year', 'bar1', {role: 'annotation'}, {role: 'style'}, 'bar2', {role: 'annotation'}, {role: 'style'}],
['2015', 90, '90%', null, 69, '69%', null],
['2016', 86, '86%', null, 96, '96%', null],
['2017', 83, '83%', null, 87, '87%', null],
['2018', 84, '84%', null, 78, '78%', null],
['AVERAGE', 86, '86%', '#109618', 82, '82%', '#ff9900'],
]);
var options = {
chartArea: {
height: '100%',
width: '100%',
top: 64,
left: 64,
right: 32,
bottom: 48
},
height: '100%',
width: '100%',
vAxis: {
gridlines: {count: 12},
viewWindow: {max: 120, min: 0} ,
textStyle: {color: 'white'}
},
seriesType: 'bars',
legend: {position: 'bottom'},
annotations: {
textStyle: {fontSize: 18, bold: true}
}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart-div'));
var formatter = new google.visualization.NumberFormat({
suffix:'%',
fractionDigits: 0
});
formatter.format(data, 1);
formatter.format(data, 3);
chart.draw(data, options);
});
html, body {
height: 100%;
margin: 0px 0px 0px 0px;
overflow: hidden;
padding: 0px 0px 0px 0px;
}
.chart {
height: 100%;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div class="chart" id="chart-div"></div>
note: you're actually using a classic chart, not material
google.visualization.ComboChart
material charts use namespace --> google.charts
classic --> google.visualization
probably shouldn't use google.charts.Bar.convertOptions with a classic chart...
i would like to ask is it possible to display data values on a half donuts chart? Want to show the data value as text at the bottom of the chart by taking its value from the array
here is the link: https://jsfiddle.net/z638ttv0/16/
Thanks for the help in advance
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["Red"],
datasets: [{
label: '# of Votes',
data: [50,100-50],
backgroundColor: [
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
rotation: 1 * Math.PI,
circumference: 1 * Math.PI
}
});
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["Red"],
datasets: [{
label: '# of Votes',
data: [50,100-50],
text: "ff",
backgroundColor: [
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
rotation: 1 * Math.PI,
circumference: 1 * Math.PI
}
});
.test
{
position:absolute;
bottom:-20px;
left:calc(50% - 10px);
display:block;
}
<body>
<span class="test">test</span>
<canvas id="myChart" width="400" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
</body>
Customised value