score:13
Accepted answer
I figured it out, here's what I did in case any one else needs it:
var packetsElement = $("#packetsGraph");
pckBarChart = new Chart(packetsElement, {
type: 'bar',
data: {
labels: ["Received", "Errors", "Lost"],
datasets: [{
label: '% of Packets (Logarithmic)',
data: packetsArr,
backgroundColor: [
'rgba(55,102,245,0.3)',
'rgba(55,102,245,0.2)',
'rgba(55,102,245,0.1)'
],
borderColor: [
'#3766F5',
'#3766F5',
'#3766F5'],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Packets',
fontSize: 20
},
scales: {
yAxes: [{
type: 'logarithmic',
ticks: {
min: 0,
max: 100,
callback: function(value, index, values) {//needed to change the scientific notation results from using logarithmic scale
return Number(value.toString());//pass tick values as a string into Number function
}
},
afterBuildTicks: function(pckBarChart) {
pckBarChart.ticks = [];
pckBarChart.ticks.push(0);
pckBarChart.ticks.push(25);
pckBarChart.ticks.push(50);
pckBarChart.ticks.push(75);
pckBarChart.ticks.push(100);
}
}]
},
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + ' %';
}
}
},
}
});
score:5
In my case, the y-axis values were not known ahead of time, so I used this in my options object:
(using underscore/lodash)
{
...
ticks: {
beginAtZero: true,
min: 0,
callback: function(tick, index, ticks){
return tick.toLocaleString();
},
},
afterBuildTicks: function(chart){
var maxTicks = 20;
var maxLog = Math.log(chart.ticks[0]);
var minLogDensity = maxLog / maxTicks;
var ticks = [];
var currLog = -Infinity;
_.each(chart.ticks.reverse(), function(tick){
var log = Math.max(0, Math.log(tick));
if (log - currLog > minLogDensity){
ticks.push(tick);
currLog = log;
}
});
chart.ticks = ticks;
}
...
}
maxTicks
can be adjusted to make the ticks more/less dense.
Source: stackoverflow.com
Related Query
- chart.js not allowing y axis steps with logarithmic scale
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Chart.js bar chart with time on X axis and category on Y axis is not rendered
- 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 does not display stacked data with linear axis and fixed steps
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How to prevent first/last bars from being cut off in a chart with time scale
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- show label in tooltip but not in x axis for chartjs line chart
- How to assign a logarithmic scale to y axis in chart.js?
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js Dynamically Updating Chart with X Axis Time
- Obtain max value of y axis of line chart rendered with Chart.js
- Line chart with large number of labels on X axis
- Series Details Not Showing in Angular Chart with Charts.js
- Maintain Y axis scale in graph with scroll in the X axis when the dataset grows bigger. Aspect-ratio problem
- Chart JS not showing On hover with small data
- Add buffer to Y axis with chart js
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Chartjs not working with d3 from csv source
- Doughnut Chart not displaying data with Chart Js and Backbone js
- Chart JS not working with date
- how to label axis within radar chart with chart.js
- I can not properly re scale <canvas> with width and height attributes
- Chartjs with Vue, Bar Chart Border Radius not working
- Chart.js Polar Area Chart is not scale right
- Vertical line using Chart.js annotations plugin with linear scale on x axis
- Papaparse with chart.js not displaying csv value on the x axis
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Chart not rendering with pug/jade and nodejs
More Query from same tag
- Can I modify or extend ChartJS?
- Possible to create quadrant chart with ChartJS, with the "origin" centered at a single point?
- Unable to make y-axis to begin at zero on Charts.js v2.1.4
- Chart.js Bar chart : display labels
- Code doesn't work after updating chart.js versioning
- Gradient line chart with ChartJS
- How to wait for data in useEffect hook before displaying it?
- How to allow copying of text within Chart.js tooltips?
- Chart JS: Old chart data not clearing
- Obtain max value of y axis of line chart rendered with Chart.js
- Revert tooltip order
- Display two datasets from an array using chart.js in node.js
- ChartJS horizontal label are 1-2px clipped
- Chart.js - Scale of secondary Y axis
- Vue chartjs is not rendering from API
- Text in center of chartJS lead to infiniteloop
- Chart JS not working with Dynamic Data
- How to set and adjust plots with equal axis aspect ratios - CHART.js
- Laravel query builder for Charts.js
- Chart.js: change title/legend positon to right
- impressions in google analytics is zero while sessions and users are more
- Better looking function
- Looping through json array properties
- Line chart Change background color of shaded region on hover (Chartjs)
- How to get clicked bar chart tooltip data?
- Chart.js doughnut chart isn't showing tooltip when you hover over the left/right of the doughnut
- Adding data to charts.js dynamically from tables
- angular-chart.js doughnut chart : how to show data in the center of a chart
- Unable to Draw Chart using ChartJS
- Chart.js HTML custom legend issues with doughnut chart