Please if you have successfully worked on months or other time like years days in Django chartjs, please kindly share a link where I can see how it is utilized. As I can't figure why mine isn't working, the moment I put the x-axis it doesn't show any data.
But if I remove the x-axis part, it works perfectly. my main aim is to make the months display so I can get some data into it. Any help would be really appreciated.
Below is the updated script snippet
<script>
$(document).ready(function(){
var ctx2 = document.getElementById("myChart2");
var myChart2 = new Chart(ctx2, {
type: "bar",
data: {
datasets: [
{
label: "US Dates",
data: [
{
x: "04/01/2014",
y: 175,
},
{
x: "10/01/2014",
y: 175,
},
{
x: "04/01/2015",
y: 178,
},
{
x: "10/01/2015",
y: 178,
},
],
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 2,
},
{
label: "UK Dates",
data: [
{
x: "04/01/2014",
y: 143,
},
{
x: "10/1/2014",
y: 175,
},
{
x: "04/01/2015",
y: 165,
},
{
x: "10/1/2015",
y: 178,
},
],
backgroundColor: "rgba(99, 255, 132, 0.2)",
borderColor: "rgba(99, 255, 132, 1)",
borderWidth: 2,
},
],
},
options: {
responsive: true,
title: {
display: true,
text: "Chart.js Time Scale",
},
scales: {
xAxes: [
{
type: "time",
position: "bottom",
time: {
displayFormats: { day: "MM/YY" },
tooltipFormat: "DD/MM/YY",
unit: "month",
},
},
],
yAxes: [
{
scaleLabel: {
display: true,
labelString: "value",
},
},
],
},
},
});
})
</script>
There is not problem with option for x-axis.
I bet there is a problem with dataset.
Dataset of x and y should be specified.
Following snippet will help you.
var ctx2 = document.getElementById("myChart2");
var myChart = new Chart(ctx2, {
type: "bar",
data: {
datasets: [
{
label: "US Dates",
data: [
{
x: "04/01/2014",
y: 175,
},
{
x: "10/01/2014",
y: 175,
},
{
x: "04/01/2015",
y: 178,
},
{
x: "10/01/2015",
y: 178,
},
],
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 2,
},
{
label: "UK Dates",
data: [
{
x: "04/01/2014",
y: 143,
},
{
x: "10/1/2014",
y: 175,
},
{
x: "04/01/2015",
y: 165,
},
{
x: "10/1/2015",
y: 178,
},
],
backgroundColor: "rgba(99, 255, 132, 0.2)",
borderColor: "rgba(99, 255, 132, 1)",
borderWidth: 2,
},
],
},
options: {
responsive: true,
title: {
display: true,
text: "Chart.js Time Scale",
},
scales: {
xAxes: [
{
type: "time",
position: "bottom",
time: {
displayFormats: { day: "MM/YY" },
tooltipFormat: "DD/MM/YY",
unit: "month",
},
},
],
yAxes: [
{
scaleLabel: {
display: true,
labelString: "value",
},
},
],
},
},
});
Related
hi i have a question on adding title to axes for line chart. i am current using react-chartjs-2 and chart.js v4.2 . i have a line chart with 3 different datasets and i want two y axes. this is my current config for line component
const optionsMulti = {
responsive: true,
interaction: {
mode: "index" as const,
intersect: false,
},
stacked: false,
plugins: {
title: {
display: true,
text: "Test Chart",
},
},
scales: {
y: {
type: "linear" as const,
display: true,
position: "left" as const,
title: { display: true, title: "title1" },
},
y1: {
type: "linear" as const,
display: true,
position: "right" as const,
grid: {
drawOnChartArea: false,
},
title: { display: true, text: "title2" },
},
},
};
const data = {
labels: "label",
datasets: [
{
label: "one",
data: data1,
borderColor: "rgb(255, 99, 132)",
backgroundColor: "rgba(255, 99, 132, 0.5)",
yAxisID: "y",
},
{
label: "two",
data: data2,
borderColor: "rgb(53, 162, 235)",
backgroundColor: "rgba(53, 162, 235, 0.5)",
yAxisID: "y",
},
{
label: "three",
data: data3,
borderColor: "rgba(75, 192, 192)",
backgroundColor: "rgba(75, 192, 192, 0.5)",
yAxisID: "y1",
},
],
}
<Line options={optionsMulti} data={data} />
title for yAxisID: "y1", is showing but yAxisID: "y" title is not showing.
screenshot of current chart
how can i set the config for both y axes to show titles?
I would like to create a horizontal BarChart.
The data range is from 0 to 20 or from 0 to -20.
The labels of the data should always be on the opposite side of the bar.
I also need additional text on the left and right.
The picture should make it clear:
I would like to do it with Chart.js.
edit:
This is my approach with Chart.js:
var ctx = document.getElementById("myChart");
var chartStatus = Chart.getChart("myChart");
if (chartStatus != undefined) {
chartStatus.destroy();
}
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["Cats", "Dogs", "Dugs", "Mice", "Hamster", "Tiger"],
datasets: [
{
label: false,
data: [-9, 1, 9, -9, 1, 9],
// data: [12, 19, -3],
backgroundColor: [
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)",
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)"
],
borderColor: [
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)",
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)"
],
borderWidth: 6
}
]
},
options: {
responsive: true,
indexAxis: "y",
plugins: {
gridLines: {
display: false
},
title: {
display: true,
text: "Pets",
font: {
size: 22
}
},
legend: {
display: false
},
tooltip: {
enabled: false
}
},
scales: {
x: {
min: -20,
max: 20,
gridLines: {
display: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
y: {
labels: ["Text1", "Text2", "Text3", "Text4", "Text5", "Text6"],
gridLines: {
display: false
},
barPercentage: 1,
gridLines: {
display: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
gridLines: {
display: false
},
secondY: {
labels: ["Text7", "Text8", "Text9", "Text10", "Text11", "Text12"],
position: "right",
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<canvas id="myChart"></canvas>
<img style="display:none" id="url" />
I could solve the different text for the left and the right side with the labels on the respective axes.
But how do I get the labels (cat, dog etc) into the graph so that they are always on the opposite side of the bars?
Solution:
var ctx = document.getElementById("myChart");
var chartStatus = Chart.getChart("myChart");
if (chartStatus != undefined) {
chartStatus.destroy();
}
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["Cats", "Dogs", "Dugs", "Mice", "Hamster", "Tiger"],
datasets: [
{
label: false,
data: [-9, 1, 9, -9, 1, 9],
// data: [12, 19, -3],
backgroundColor: [
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)",
"rgba(39, 152, 228, 0.7)",
"rgba(233, 85, 59, 0.7)",
"rgba(152, 140, 202, 0.7)"
],
borderColor: [
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)",
"rgba(39, 152, 228,1)",
"rgba(233, 85, 59, 1)",
"rgba(152, 140, 202, 1)"
],
borderWidth: 6
}
]
},
options: {
animation: {
duration: 1,
// https://github.com/jtblin/angular-chart.js/issues/605
onComplete: function () {
var chart = this;
var ctx = chart.ctx;
ctx.font = Chart.helpers.fontString(
Chart.defaults.font.size,
Chart.defaults.font.style,
Chart.defaults.font.family
);
ctx.textAlign = "left";
ctx.textBaseline = "censter";
var labels = this.data.labels;
this.data.datasets.forEach(function (dataset, i) {
var meta = chart.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
var data = dataset.data[index];
let posx;
if (data < 0) {
posx = bar.x + bar.$context.element.width + 10;
ctx.textAlign = "left";
} else {
posx = bar.x - bar.$context.element.width - 10;
ctx.textAlign = "right";
}
// posx = bar.x;
ctx.fillText(`${labels[index]} (${data})`, posx, bar.y + 3);
});
});
}
},
responsive: true,
indexAxis: "y",
plugins: {
title: {
display: true,
text: "Pets",
font: {
size: 22
}
},
legend: {
display: false
},
tooltip: {
enabled: false
}
},
scales: {
x: {
grid: {
display: false,
drawBorder: false,
drawOnChartArea: false,
drawTicks: false
},
labels: {
display: false
},
min: -20,
max: 20,
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
y: {
grid: {
display: false,
drawBorder: false,
drawOnChartArea: false,
drawTicks: false
},
labels: ["Text1", "Text2", "Text3", "Text4", "Text5", "Text6"],
gridLines: {
display: false
},
barPercentage: 1,
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
},
secondY: {
labels: ["Text7", "Text8", "Text9", "Text10", "Text11", "Text12"],
position: "right",
grid: {
display: false,
drawBorder: false,
drawOnChartArea: false,
drawTicks: false
},
ticks: {
color: "#d6d6d9",
font: {
size: 22
}
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<canvas id="myChart"></canvas>
I'm trying to remove a dataset from label:
But the label callback is called twice, so i don't know how to do it.
I was expecting an array in the callback and that you can simply remove the one not needed.
here's what i have so far and tried:
var labels = [];
for (let index = 0; index < 12; index++) {
labels.push(index);
}
window.onload = function () {
var canvas = document.getElementById('elm-chart'),
ctx = canvas.getContext('2d');
var myLineChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: '-15',
data: [
{
x: 0,
y: 10,
},
{
x: 1,
y: 20,
},
],
borderColor: 'red',
},
{
label: '15',
data: [
{
x: 1,
y: 20,
},
{
x: 2,
y: 30,
},
],
borderColor: 'blue',
},
{
label: '25',
data: [
{
x: 2,
y: 30,
},
{
x: 3,
y: 35,
},
],
borderColor: 'yellow',
},
{
label: '-15',
data: [
{
x: 6,
y: -10,
},
{
x: 7,
y: -20,
},
],
borderColor: 'red',
},
{
label: '15',
data: [
{
x: 7,
y: -20,
},
{
x: 8,
y: -30,
},
],
borderColor: 'blue',
},
{
label: '25',
data: [
{
x: 8,
y: -30,
},
{
x: 9,
y: -35,
},
],
borderColor: 'yellow',
},
],
},
options: {
responsive: true,
plugins: {
legend: {
onClick: (evt, legendItem, legend) => {
let newVal = !legendItem.hidden;
legend.chart.data.datasets.forEach((dataset) => {
if (dataset.label === legendItem.text) {
dataset.hidden = newVal;
}
});
legend.chart.update();
},
labels: {
filter: (legendItem, chartData) => {
let entries = chartData.datasets.map((e) => e.label);
return entries.indexOf(legendItem.text) === legendItem.datasetIndex;
},
},
},
},
scales: {
x: {
type: 'linear',
ticks: {
stepSize: 30,
},
},
},
plugins: {
tooltip: {
callbacks: {
title: function (context) {
return [`test`, 'test2'];
},
label: function (context) {
console.log(context.dataset.label);
console.log(context.formattedValue);
console.log(context);
return [
`${context.dataset.label}:${context.formattedValue}`,
'test',
];
},
},
},
},
},
});
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.1/chart.min.js"></script>
<canvas id="elm-chart" width="640" height="480"></canvas>
So to be clear i don't want to remove the datapoint in the dataset. I just want to remove 1 datapoint from the label, the top one always
What you can do is instead of using the default tooltip instead use a html tooltip.
In the JS that makes the tooltip instead of looping over each active item you only take the first one and display that.
Official sample of html tooltip: https://www.chartjs.org/docs/master/samples/tooltip/html.html
I'm working on a project that could display sensing data on local web pages in real time with Django and I made this stream live data chart. But I don't need to this realtime x-axis ticks. I want to make the x-axis starting at zero and increasing 1 step by one second.
I think "xAxes type realtime" is the key, but I'm new to this and it's hard for me to solve alone, so I need your help.
enter image description here
<script>
var chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)'
};
function onRefresh(chart) {
var now = Date.now();
chart.data.datasets.forEach(function(dataset) {
dataset.data.push({
x: now,
y: GetData()
});
});
}
var color = Chart.helpers.color;
var config = {
type: 'line',
data: {
datasets: [{
backgroundColor: [
'rgba(100, 200, 200, 0)'
],
borderColor: chartColors.blue,
fill: false,
lineTension: 0,
borderDash: [8, 0],
data: [],
borderWidth:3,
pointRadius:0,
}]
},
options: {
legend:{
display:false
},
title: {
display: true,
text: 'Some Chart',
fontSize:20,
fontFamily:"Tahoma"
},
scales: {
xAxes: [{
ticks:{
beginAtZero: true
},
gridLines:{
display:true
},
type: 'realtime',
display:true,
position:'bottom',
realtime: {
duration: 20000,
refresh: 1000,
delay: 0,
onRefresh: onRefresh
}
}],
yAxes: [{
gridLines:{
display:true
},
scaleLabel: {
display: true,
labelString: 'value'
}
}]
},
}
};
window.onload = function() {
var ctx = document.getElementById('myChart').getContext('2d');
window.myChart = new Chart(ctx, config);
};
</script>
Is there any other way to make streaming live graph with Django, please let me know.
I solved this myself. The key was about 'xAxes ticks callback'. thank you.
Using Chart.js, I have made a scatter plot. The points on the chart will always be on an axis. Currently, I am using the following options and data:
public scatterChartLabels:string[] = ['Experiencing', 'Ideation', 'Thinking', 'Evaluation'];
public scatterChartData = [
{
x: 0,
y: localStorage.getItem('col1')
}, {
x: localStorage.getItem('col2'),
y: 0
}, {
x: 0,
y: - localStorage.getItem('col3')
},
{
x: - localStorage.getItem('col4'),
y: 0
},
{
x: 0,
y: localStorage.getItem('col1')
}
]
public scatterChartOptions = {
showLines: true,
yAxes: {
gridLines: {
zeroLineWidth: 100
}
},
scales: {
xAxes: [{
gridLines: {
zeroLineColor: '#c1272d'
},
}],
yAxes: [{
gridLines: {
zeroLineColor: '#c1272d'
}
}],
}
}
public scatterChartType: string = 'scatter';
public scatterChartCustomColors = [{
backgroundColor: 'rgba(255, 147, 29, 0.5)',
borderColor: 'rgba(255, 147, 29)',
pointBackgroundColor: 'rgba(255, 147, 29, 0.5)',
pointBorderColor: 'rgba(255, 255, 255)'
}]
This produces the following chart:
How can I make the chart use the same scale on both axes? For example, If the highest y-axis value is 130, how can I make the lowest y-axis tick 130, and the x-axis ticks -130 and 130?
I was able to do this by updating the tick settings for each axis:
public scatterChartOptions = {
...
scales: {
xAxes: [{
ticks: {
min : - 130,
max : 130
}
}],
yAxes: [{
ticks: {
min : - 130,
max : 130
}
}]
}
}