score:4
Accepted answer
You can implement a custom scale to change the base of the the log scale like so:
class Log2Axis extends Chart.Scale {
constructor(cfg) {
super(cfg);
this._startValue = undefined;
this._valueRange = 0;
}
parse(raw, index) {
const value = Chart.LinearScale.prototype.parse.apply(this, [raw, index]);
return isFinite(value) && value > 0 ? value : null;
}
determineDataLimits() {
const {
min,
max
} = this.getMinMax(true);
this.min = isFinite(min) ? Math.max(0, min) : null;
this.max = isFinite(max) ? Math.max(0, max) : null;
}
buildTicks() {
const ticks = [];
let power = Math.floor(Math.log2(this.min || 1));
let maxPower = Math.ceil(Math.log2(this.max || 2));
while (power <= maxPower) {
ticks.push({
value: Math.pow(2, power)
});
power += 1;
}
this.min = ticks[0].value;
this.max = ticks[ticks.length - 1].value;
return ticks;
}
/**
* @protected
*/
configure() {
const start = this.min;
super.configure();
this._startValue = Math.log2(start);
this._valueRange = Math.log2(this.max) - Math.log2(start);
}
getPixelForValue(value) {
if (value === undefined || value === 0) {
value = this.min;
}
return this.getPixelForDecimal(value === this.min ? 0 :
(Math.log2(value) - this._startValue) / this._valueRange);
}
getValueForPixel(pixel) {
const decimal = this.getDecimalForPixel(pixel);
return Math.pow(2, this._startValue + decimal * this._valueRange);
}
}
Log2Axis.id = 'log2';
Log2Axis.defaults = {};
Chart.register(Log2Axis);
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange", "Black", "White"],
datasets: [{
label: '# of Votes',
data: [8, 3, 60, 20, 130, 80, 1030, 250],
backgroundColor: 'pink',
borderColor: 'pink'
}]
},
options: {
scales: {
x: {
display: true
},
y: {
display: true,
type: 'log2',
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.0/chart.js"></script>
</body>
Fiddle link: https://jsfiddle.net/Leelenaleee/oyLf0wed/6/
Credit to chart.js team who made a sample for this: https://www.chartjs.org/docs/master/samples/advanced/derived-axis-type.html
Source: stackoverflow.com
Related Query
- Changing the base of the logarithmic scale in Chart.js
- Changing the label of chart
- Chart.js reduce canvas size without changing the size of the chart
- How do i have a single Chart.vue component instead of different chart components and then display charts from it by changing id of the API
- Is there a more optimized way to implement the change function of chart js upon changing the dropdowns?
- How do remove the bottom scale on a Chart.js bar chart
- data in charts of charts.js is changing when I click in the line chart
- How to fix a stacked logarithmic bar chart values to fit the grid
- 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
- Changing the opacity of the colour on the Polar Area chart
- How to make Chart JS ignore the scale between DatasSets
- Getting the HTML code of a chart created by chart.js
- How to add text inside the doughnut chart using Chart.js?
- Remove the vertical line in the chart js line chart
- Moving vertical line when hovering over the chart using chart.js
- How do I prevent the scale labels from being cut off in chartjs?
- How to prevent first/last bars from being cut off in a chart with time scale
- How do you hide the title of a chart tooltip?
- Changing fontFamily on ChartJS bar chart
- Chart.js: Dynamic Changing of Chart Type (Line to Bar as Example)
- chartjs : how to set custom scale in bar chart
- Make the X-Axis labels in chart.js increment by a certain scale
- Dynamically update the options of a chart in chartjs using Javascript
- Chart.js line chart is cut off at the top?
- How to assign a logarithmic scale to y axis in chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- Making the labels responsive in chart js
- Chart Js Negative Scale
- chart js tooltip how to control the data that show
- Changing cursor to pointer on Chart.js bar chart when hover (mousemove) event is disabled?
More Query from same tag
- The real time chart sometimes does not display when I switch the target
- Can you do regions in line charts with Chart.js?
- Chart js scatter graph labels
- Charts.js display two combined line charts for last 7 days
- vue-chart.js doesn't show data
- Ng2 ChartData filled with observable
- Execute chartjs after an ajax call with PHP
- Cannot zoom in Chart.js
- How to get Data from API to display chart using chartjs in Vuejs
- Force two Chart.js doughnut charts with legends to same size in Bootstrap columns
- How to create dynamic charts with Django and Chart.js?
- chart.js custom legend colour based
- Displaying JSON data into a pie chart using chart.js
- Chart.js line chart tooltip shows wrong label when line doesn't start at first label
- why isnt vue-chartjs receiving data from api?
- Vertical stacked bar chart using SharePoint List
- Why aren't my data points getting placed in the corresponding locations in my chart?
- charts.js update global variables
- custom tooltips with react-chartjs-2 library
- How to visualize data with chart.js properly?
- Chartjs v2.8 showAllTooltips does not exist
- Chart JS: Ignoring x values and putting point data on first available labels
- Placing JavaScript content in Bootstrap Popover
- Looping variables or array
- Why scatter chart does not show axes?
- Hide points in ChartJS LineGraph
- Getting clicked object from each section of a single stack bar as my stack bar has multiple sections Chart.js
- How to hide other section of chartjs on click of lengend
- How to reduce the gap between bars on bar chart
- Chart JS dynamic chart