score:1
Unfortunately it seems that Chart.js 2.8.0 supports reverse boolean parameter only for ticks object of yAxes.
This means, that without hacking or monkey patching it cannot be done within Chart.js API.
Also this dirty solution I came up with seems to be over-engineered, because simply changing dataset.data array item by referring to it by its index does not trigger Chart.js animations properly, therefore one needs to use splice.
Explanation:
Actually the idea is pretty simple:
In order to show bars from right to left for a priori known number of them
One needs to first create an array of length equal to number of bars and fill it with nulls.
In other words an array containing as many nulls as there are bars to be displayed.
Then each new value has to be inserted (using splice) from the right (end of the array) at first index under which array item is null or just simply at correct index, counting down from the array.length - 1 to 0.
"Visualisation":
let value = 1;
const graphData = Array(3).fill(null);
console.log(graphData)
for(let i = graphData.length - 1; i >= 0; --i){
graphData.splice(i, 1, value++);
console.log(graphData)
}
Example:
class GraphController {
size = 0;
data = [];
labels = [];
graphData = [];
ctx = null;
chart = null;
constructor(size, data, labels, canvasId) {
this.size = size;
this.data = data;
this.labels = labels;
this.ctx = document.getElementById(canvasId).getContext('2d');
this.graphData = Array(size).fill(null);
}
fetchAndDraw(index = 1) {
const item = this.data[index - 1];
if (item.TotalReward) {
this.graphData[this.size - index] = item.TotalReward;
}
if (index > 1 && index < this.size) {
this.chart.data.datasets[0].data.splice(this.size - index, 1, item.TotalReward);
this.chart.update();
} else {
if (this.chart) {
this.chart.destroy();
}
this.chart = new Chart(this.ctx, {
type: 'bar',
data: {
labels: this.labels,
datasets: [{
data: [...this.graphData],
backgroundColor: '#fff',
hoverBackgroundColor: '#d5d5d5',
borderWidth: 0
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false,
},
title: {
display: false,
},
tooltips: {
enabled: false,
},
scales: {
xAxes: [{
barPercentage: 1.0,
categoryPercentage: 0.55,
gridLines: {
display: false,
color: '#fff',
drawBorder: false,
zeroLineColor: '#fff'
},
ticks: {
fontFamily: '"Avenir Next", "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"',
fontSize: 16,
fontColor: '#fff',
padding: 15,
}
}],
yAxes: [{
categoryPercentage: 0.8,
barPercentage: 0.8,
gridLines: {
display: false,
color: '#fff',
drawBorder: false,
zeroLineColor: '#fff'
},
ticks: {
min: 0,
stepSize: 10,
fontFamily: '"Avenir Next", "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"',
fontSize: 16,
fontColor: '#fff',
padding: 10,
callback: function (value) {
return '$' + value;
}
}
}],
},
},
});
}
index++;
if (index <= this.size) {
setTimeout(() => {
this.fetchAndDraw(index);
}, 1000);
}
}
}
const data = [
{ "Month": "April ‘18", "TotalReward": 10.37 },
{ "Month": "May ‘18", "TotalReward": 18.11 },
{ "Month": "June ‘18", "TotalReward": 25.49 },
{ "Month": "January", "TotalReward": 35.55 },
{ "Month": "February", "TotalReward": 50.25 },
{ "Month": "March", "TotalReward": 59.15 },
]
const rewardLabels = ["April ‘18", "May ‘18", "June ‘18", "January", "February", "March"].reverse();
const graph = new GraphController(6, data, rewardLabels, 'rewardChart');
graph.fetchAndDraw();
.chart-wrap {
background: #333;
padding: 20px 10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="chart-wrap">
<canvas id="rewardChart" width="600" height="165"></canvas>
</div>
Source: stackoverflow.com
Related Query
- Chart.js show x-axis data from right to left
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Draw a horizontal bar chart from right to left
- How to show tooltip value of all data falling on the same axis in chart js?
- How to set 3 axis in google chart (V-Axis Left as Qty, V-Axis Right as Series Column and H-Axis as TimeOrder)?
- How to start the chart from specific time and offest hour and then show the data on chart from target datetime in chartjs
- Change Chartjs financial chart yaxis from left to right
- How to show label at right side of Y axis same as left side of Y Axis ChartJS
- How to start Y Axis Line at 0 from right for negative values in chart js 2?
- getting additional value fields from data source for dx.chartjs doughnut chart
- 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
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- Chartjs random colors for each part of pie chart with data dynamically from database
- Chart.js same Y axis on left and right
- show label in tooltip but not in x axis for chartjs line chart
- chart js tooltip how to control the data that show
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart JS show multiple data points for the same label
- Show data dynamically in line chart - ChartJS
- Chart Js Show the old data on mouse hover
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- Show all values in Chart js y axis
- Show "No Data" message for Pie chart with no data
- Show data on top of bar chart in Chart.js v 2.7.1
- How to show data values in top of bar chart and line chart in chart.js 3
- Using data from API with Chart JS
- Chart not displaying from JSON data
- Angular chart how to show the legend data value by default along with legend name
- How to create chartjs chart with data from database C#
More Query from same tag
- ChartJS, merge legends for multiple charts
- chart.js cuts off canvas - but padding makes doughnut very small
- Chart.js 2 - how to show all tooltips always and stylize it
- how to use require(chart.js) in codesandbox
- Bootstrap modal showing when clicking individual points in Linechart but not in individual bars in Barchart
- Chart.Js - Background Bar in Chart Bar
- How to plot Json data in Chart js
- Charts in wicket
- Customizing Chart.js troubleshoot
- How to integrate charts with js and jQuery?
- How do I fetch data from 2 tables and display into 1 chartjs
- Text inside Doughnut chart using Chart.js and Angular2, Ionic2
- beforeDraw in Chart JS 3.5.x
- Chart.js time scale not showing data
- Hide points in ChartJS LineGraph
- Chartjs not giving 'xLabel' properly on tooltip
- ChartJS 2 - Adding data to chart breaks after X number of items added
- Place Text in Chart-Canvas Area in ChartJS
- Chart JS - Grid colors and gradient fill not showing
- Chart in other module
- chart.js time series multi axis case
- Center point labels between ticks (Polar Area Chart JS)
- The chart is not getting shown when page is loaded
- Custom Tooltips On Line Chart Using Chart.js
- Realtime data from a service in ionic / angular
- How to remove transparency from bar charts?
- Chart.js not deploying properly on Heroku
- ChartJS multiple tooltip callbacks not working
- How to expand the slice of donut chart in chartjs?
- ChartJS unique datasets per label