score:4
Updated accepted answer to also work with V3 since scale config has been changed:
var ctx = $("#c");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["January;2015", "February;2015", "March;2015", "January;2016", "February;2016", "March;2016"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3]
}]
},
options: {
scales: {
x: {
ticks: {
callback: function(label) {
let realLabel = this.getLabelForValue(label)
var month = realLabel.split(";")[0];
var year = realLabel.split(";")[1];
return month;
}
}
},
xAxis2: {
type: "category",
grid: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
ticks: {
callback: function(label) {
let realLabel = this.getLabelForValue(label)
var month = realLabel.split(";")[0];
var year = realLabel.split(";")[1];
if (month === "February") {
return year;
} else {
return "";
}
}
}
},
y: {
beginAtZero: true
}
}
}
});
<body>
<canvas id="c" width="400" height="300"></canvas>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>
score:0
okay maby a bit late ;)
how can we show the last tick in the second x-axis row?
with the code from above, we return a empty string. i want to see the label of the last point.
ticks:{
callback:function(label){
var month = label.split(";")[0];
var year = label.split(";")[1];
if(month === "February"){
return year;
}else{
return ""; **<==== ???**
}
thx for the help.
EDIT
i change it a bit but not complete like i will i don't want the label at 30 and 31 just the last day w.a.w 31 label , 30 not a label
month ended at 30 => label
return month;
}else if
(Nbrday === "31"){
return month;
}else if
(Nbrday === "30"){
return month;
}
else{
// return month;
return "";
score:3
var myChart = new Chart(ctx, {
type: "line",
data: {
datasets: [{
data: [20, 50, 100, 75, 25, 0],
label: "Left dataset",
// This binds the dataset to the left y axis
yAxisID: "left-y-axis",
}, {
data: [0.1, 0.5, 1.0, 2.0, 1.5, 0],
label: "Right dataset",
// This binds the dataset to the right y axis
yAxisID: "right-y-axis",
}],
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
},
options: {
scales: {
yAxes: [{
id: "left-y-axis",
type: "linear",
position: "left",
}, {
id: "right-y-axis",
type: "linear",
position: "right",
}],
},
},
});
score:33
For v2 only (v3 see @LeeLenalee's answer)
This question has already been answered on github here
Here is a working JSFiddle
var ctx = $("#c");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["January;2015", "February;2015", "March;2015", "January;2016", "February;2016", "March;2016"],
datasets: [{
label: '# of Votes',
xAxisID:'xAxis1',
data: [12, 19, 3, 5, 2, 3]
}]
},
options:{
scales:{
xAxes:[
{
id:'xAxis1',
type:"category",
ticks:{
callback:function(label){
var month = label.split(";")[0];
var year = label.split(";")[1];
return month;
}
}
},
{
id:'xAxis2',
type:"category",
gridLines: {
drawOnChartArea: false, // only want the grid lines for one axis to show up
},
ticks:{
callback:function(label){
var month = label.split(";")[0];
var year = label.split(";")[1];
if(month === "February"){
return year;
}else{
return "";
}
}
}
}],
yAxes:[{
ticks:{
beginAtZero:true
}
}]
}
}
});
<body>
<canvas id="c" width="400" height="300"></canvas>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
</body>
Source: stackoverflow.com
Related Query
- How to create two x-axes label using chart.js
- How to create stacked bar chart using react-chartjs-2?
- How to create single value Doughnut or Pie chart using Chart.js?
- How to create a gantt chart using Chart.js and populate dates?
- How to create two pie charts using Chart.js API?
- How to change default label of each bubble in bubble chart using chartjs-plugin-datalabels
- How to create Waterfall model chart using QuickChart?
- How to create a Doughnut chart in django admin interface using foreign key field data?
- How do you create a Radar chart using chart.js with a y-axis of 0 to 100?
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- How to set Solid label in bar chart using Chart JS
- How to create a chart.js scatter chart with data from two lists
- How to create a line on a chart using chartjs-plugin-annotation
- How to custom index label on each bar chart using chartjs?
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Chart.js how to create chart wirhout y-axis for two data sets
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- how to create line chart using chart.js in angular 2+
- How to run Chart.js samples using source code
- How to create an animate-on-update Stacked Bar chart using Chart.js(or Chartist)?
- How to add label square to Bar Chart using Chart.js
- how to create bar chart with group and sam color for each group using chart.js?
- How to add text inside the doughnut chart using Chart.js?
- How to use two Y axes in Chart.js v2?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- create a multi line chart using Chart.js
- How can I make two of my lines in Chart JS thicker
- How to add an on click event to my Line chart using Chart.js
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to Draw Gantt chart using chart js or other libraries
More Query from same tag
- Display data at ends of floating bar graph (created using chart js)
- How to retain spacing between bars for two different bar charts in Chart.js 2
- Angularjs line chart get data from sql
- How to update with animation a Pie Chart?
- How to replay animation chart.js
- Chart.js animate chart after clicking a button
- How do I incorporate Luxon timezone options with chart.js?
- How show data label in the graph on Chart.js?
- Determining which chart the user has clicked on
- Pie chart using chart.js and asp.net web Service (asmx)
- How to mutate VueJS prop?
- Protractor - Get array from canvas attribute in Angular2 using ng2-charts/Chart.js
- Chart.js Change color of the values *inside* the bars of a Bar chart
- Chart js Backgroundimage Scaling
- ChartJS Draw grid line X-Axis and Y-Axis
- Chart.JS - show values on top of points
- ChartJS padding from lines left and right
- Chartjs: Is it possible to hide the data labels on the axis but show up on the graph on hover?
- How to extract ChartJs data and config properties
- How can I get A radar chart like this by chart.js
- ChartJS Tooltips with time axis and multiple datasets
- Line Chart with null values: How to keep Lines connected?
- Adding additional properties to a Chart JS dataset
- Render Javascript in DOMPDF is not working as expected
- ChartJS Custom text on certain xAxis and yAxis linesS
- Fixed time frame in Chart.JS
- Custom gridLines and Axes Chartjs
- Chart.js addData is undefined when using SignalR
- Chart Js Change Label orientation on x-Axis for Line Charts
- Pie Chart using chart.js not showing up but bar charts are?