I am creating a full % stacked area chart using Chart.js. However, I cannot find in the documentation how to reverse the order in which the values are shown in the tooltip.
Here is what I get:
As you can see, the top area (in blue) is the bottom one in the tooltip.
My code:
var ctx = document.getElementById("EvolutionBreackdownsChart");
var EvolutionBreackdownsChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March", "April"],
datasets: [{
label:'O',
data: [20, 21, 19, 17, 22, 25, 23, 18, 15, 16, 17, 19, 20],
fill: 'origin',
backgroundColor: 'rgba(255, 148, 31, 0.2)',
borderWidth:0.1,
borderColor: 'rgba(255, 148, 31, 0.55)',
pointRadius:3,
pointBackgroundColor: 'rgba(255, 148, 31, 0.2)',
pointBorderColor: 'rgba(255, 148, 31, 0.55)',
pointHoverRadius:5,
pointHoverBorderWidth:1,
lineTension:0,
}, {
label:'A',
data: [30, 28, 29, 32, 32, 31, 25, 29, 29, 30, 31, 28, 29],
fill: '-1',
backgroundColor:'rgb(255, 148, 31, 0.35)',
borderWidth:0.1,
borderColor: 'rgb(255, 148, 31, 0.75)',
pointRadius:3,
pointBackgroundColor: 'rgb(255, 148, 31, 0.35)',
pointBorderColor: 'rgb(255, 148, 31, 0.75)',
pointHoverRadius:5,
pointHoverBorderWidth:1,
lineTension:0,
}, {
label:'M',
data: [10, 12, 13, 10, 11, 12, 11, 10, 12, 11, 11, 10, 9],
fill: '-1',
backgroundColor:'rgb(255, 148, 31, 0.5)',
borderWidth:0.1,
borderColor: 'rgb(255, 148, 31, 0.9)',
pointRadius:3,
pointBackgroundColor: 'rgb(255, 148, 31, 0.5)',
pointBorderColor: 'rgb(255, 148, 31, 0.9)',
pointHoverRadius:5,
pointHoverBorderWidth:1,
lineTension:0,
}, {
label:'R',
data: [28, 29, 32, 31, 27, 25, 31, 33, 28, 33, 31, 30, 32],
fill: '-1',
backgroundColor:'rgba(255, 148, 31, 0.60)',
borderWidth:0.1,
borderColor: 'rgba(255, 148, 31, 1)',
pointRadius:3,
pointBackgroundColor: 'rgba(255, 148, 31, 0.60)',
pointBorderColor: 'rgba(255, 148, 31, 1)',
pointHoverRadius:5,
pointHoverBorderWidth:1,
lineTension:0,
}, {
label:'B',
data: [12, 10, 7, 10, 8, 7, 10, 10, 16, 10, 10, 13, 10],
fill: '-1',
backgroundColor:'rgba(3, 169, 244, 0.25)',
borderWidth:0.1,
borderColor: 'rgba(3, 169, 244, 1)',
pointRadius:3,
pointBackgroundColor: 'rgba(3, 169, 244, 0.25)',
pointBorderColor: 'rgba(3, 169, 244, 1)',
pointHoverRadius:5,
pointHoverBorderWidth:1,
lineTension:0,
},],
},
options: {
scales: {
yAxes: [{
stacked:true,
gridLines: {
display:false,
},
ticks: {
beginAtZero:true,
fontColor:'#999999',
fontFamily:"'Source Sans Pro', sans-serif",
fontSize:10,
},
}],
xAxes: [{
ticks:{
fontColor:'#999999',
fontFamily:"'Source Sans Pro', sans-serif",
fontSize:10,
maxTicksLimit: 15,
callback: function(tick) {
var characterLimit = 4;
if (tick.length >= characterLimit) {
return tick.slice(0, tick.length).substring(0, characterLimit - 1).trim();;
}
return tick;
},
},
}]
},
legend: {
display: false,
},
tooltips: {
backgroundColor:'rgba(3, 169, 244, 0.75)',
bodyFontFamily:"'Source Sans Pro', sans-serif",
titleFontStyle:"normal",
bodyFontFamily:"'Source Sans Pro', sans-serif",
displayColors: true,
cornerRadius:0,
intersect:false,
mode: 'x-axis',
callbacks: {
title: function(tooltipItem){
return this._data.labels[tooltipItem[0].index];
}
},
},
}
});
Can you help?
You can reverse the order of tooltip values, using itemSort function, like so ...
tooltips: {
itemSort: function(a, b) {
return b.datasetIndex - a.datasetIndex;
},
...
}
Related
Is there a way to add an animation to an annotation?
I have the following
point1: {
type: 'point',
drawTime: 'afterDraw',
xValue: 1,
yValue: 119,
radius: 18,
backgroundColor: 'rgba(255, 99, 132, 0.25)',
borderColor: 'rgba(255, 99, 132, 0.25)'
}
And I'd like to change the radius size.
please help, thank you.
i am now writing datapart of chartjs
var configcount = {type: "line",data: {
labels: ["Apr20", "May20", "Jun20", "Jul20", "Aug20", "Sep20", "Oct20"],
datasets: [{label: "Balance1",
data: [10, 20, 30, 40, 50, 60, 70],
fill: false,pointBorderWidth: ...
,},},],},},};
want to change multiple line that Balance1: 10, 20, 30, 40, 50, 60, 70 & Balance2: 15, 30, 15, 30, 15, 30, 15
You need to define two distinct datasets as follows.
data: {
labels: ["Apr20", "May20", "Jun20", "Jul20", "Aug20", "Sep20", "Oct20"],
datasets: [{
label: "Balance1",
data: [10, 20, 30, 40, 50, 60, 70],
...
}, {
label: "Balance2",
data: [15, 30, 15, 30, 15, 30, 15],
fill: false,
...
}]
}
Please take a loo at below code and see how it works.
new Chart(document.getElementById("chart"), {
type: 'line',
data: {
labels: ["Apr20", "May20", "Jun20", "Jul20", "Aug20", "Sep20", "Oct20"],
datasets: [{
label: "Balance1",
data: [10, 20, 30, 40, 50, 60, 70],
fill: false,
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgb(255, 99, 132)",
pointBorderWidth: 10
}, {
label: "Balance2",
data: [15, 30, 15, 30, 15, 30, 15],
fill: false,
backgroundColor: "rgba(54, 162, 235, 0.2)",
borderColor: "rgb(54, 162, 235)",
pointBorderWidth: 4,
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="chart" height="80"></canvas>
I am working on creating a Line graph that plots points at different times during one day for different days and in order to do that, I need to use the type: time on the xAxes. I have the moment.js downloaded, but the type:time is giving me an error. Without the type:time, the X axis label is too long and makes the graph look bad. Any ideas what I would need to do to fix this?
I have tried to put the data points this way:
data: [ {x: newDate(2019, 06, 24, 08, 00), y: 24}]
but it still didn't work with the type:time
<canvas id="timechart" width="800" height="400" role="img"></canvas>
<script>
var ctx = document.getElementById('timechart');
var timeFormat = 'MM/DD/YY HH:mm';
function newDate(days) {
return moment().add(days).toDate();
}
function newDateString(days) {
return moment().add(days).format();
}
var timechart = new Chart(ctx, {
type: "line",
data: {
labels: [newDate(0), newDate(1), newDate(2), newDate(3),
newDate(4), newDate(5), newDate(6), newDate(7), newDate(8), newDate(9),
newDate(10), newDate(11),
newDate(12), newDate(13), newDate(14), newDate(15),
newDate(16), newDate(17)],
datasets: [
{
data: [12, 21, 32, 25, 16, 14, 7, 25, 18, 20, 22, 15,
17, 11, 19, 28, 30, 10],
label: 'D Speed',
fill: false,
backgroundColor: 'rgba(102, 147, 188, 1)',
borderColor: 'rgba(102, 147, 188, 1)',
radius: 4,
hoverRadius: 5,
}, {
data: [8, 16, 24, 30, 20, 10, 12, 21, 32, 15, 17, 11,
22, 18, 29, 17, 8],
label: 'U Speed',
fill: false,
backgroundColor: 'rgba(255, 147, 44, 1)',
borderColor: 'rgba(255, 147, 44, 1)',
radius: 4,
hoverRadius: 5,
}
],
},
options: {
title: {
display: true,
},
scales: {
xAxes: [{
display: true,
type: 'time',
}],
yAxis: [{
display: true,
}]
},
}
});
</script>
I expect the x axis labels to show just the MM/DD/YY HH:MM but it is actually not showing the chart at all.
The code you shared worked for me. Make sure that you've included Moment and the non-bundled version of Chart.js in your project. E.g.:
<script src="https://cdn.jsdelivr.net/npm/moment#2.24.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.8.0"></script>
Also, check your browser's developer console to see if there are any JavaScript errors that can help give more clues as to the problem. You can access in Chrome by hitting F12.
i want to create multi line chart using ChartJS. multi line chart is working fine. but tool-tip only i am getting problem. please check below code and image.
var ctx = document.getElementById("myChart").getContext("2d");
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 80, 81, 56, 55, 40],
spanGaps: false,
},
{
label: "My Second dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(255, 99, 132, 1)",
borderColor: "rgba(255, 99, 132, 1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(255, 99, 132, 1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(255, 99, 132, 1)",
pointHoverBorderColor: "rgba(255, 99, 132, 1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [40, 59, 80, 100, 56, 80, 70],
spanGaps: false,
}
]
};
var myLineChart = new Chart(ctx, {
type: 'line',
data: data
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
January month i have two data ["My first dataset":65, "My Second Dataset": 40] so tool-tip showing correctly but in march month i have two data ["My first dataset":80, "My Second Dataset": 80] but tool-tip showing only "My First dataset" value only so i m not able to find "My Second Dataset" value.
I tried like this. now its working fine
options: {
tooltips: {
mode: 'label'
}
}
I'm trying to figure out, how I can reload my ChartJS components whenever the screensize is changed as it's messing up the responsiveness at the moment. My goal is to either reload them after resize, or remove them while resizing and loading again after resize.
This is my code so far for the graph component, for some reason it's ignoring the timer, but maybe the ready() isn't just called when the component is loaded?
export default {
props: {
type: {
type: String,
required: true
}
},
data() {
return {
data: {
labels: ['2014', '2015', '2016', '2016'],
datasets: [
{
label: "Administration",
backgroundColor: "rgba(255, 219, 77, 0.5)",
borderColor: "rgba(255, 219, 77, 1)",
borderWidth: 0,
hoverBackgroundColor: "rgba(255, 219, 77, 0.8)",
hoverBorderColor: "rgba(255, 219, 77, 1)",
data: [65, 59, 80, 81],
},
{
label: "Teaching",
backgroundColor: "rgba(255, 166, 77, 0.5)",
borderColor: "rgba(255, 166, 77, 1)",
borderWidth: 0,
hoverBackgroundColor: "rgba(255, 166, 77, 0.8)",
hoverBorderColor: "rgba(255, 166, 77, 1)",
data: [65, 59, 80, 81],
},
{
label: "Exam & Supervision",
backgroundColor: "rgba(102, 153, 204, 0.5)",
borderColor: "rgba(102, 153, 204, 1)",
borderWidth: 0,
hoverBackgroundColor: "rgba(102, 153, 204, 0.8)",
hoverBorderColor: "rgba(102, 153, 204, 1)",
data: [65, 59, 80, 81],
}
]
},
timoutHandle: null
}
},
ready() {
window.addEventListener('resize', this.reload);
this.loadChart();
},
methods: {
loadChart() {
console.log('loading..');
new Chart(
this.$el.getContext('2d'),
{
type: this.type,
data: this.data,
options: {
legend: {
position: 'bottom'
}
}
}
);
},
reload() {
window.clearTimeout(this.timeoutHandle);
this.timeoutHandle = window.setTimeout(
this.loadChart(),
3);
}
}
}
Have you consider using vue-charts? It's google-chart wrapper for vue and it exports draw method and resizing on window size change is quite easy:
<template>
<vue-chart
:chart-type="chartType"
:columns="columns"
:chart-events="chartEvents"
:rows="rowData"
:options="options"
v-ref:thisherechart
></vue-chart>
</template>
ready: function() {
var self = this;
jQuery(window).resize(function () {
self.$refs.thisherechart.drawChart();
})
},
Same jQuery may help You with your problem.
I fixed this part of the problem, look here for reference: https://github.com/chartjs/Chart.js/issues/2777#issuecomment-226219784