score:6
After reading thru the core scale source, I was able to come up with a way to configure your chart to get pretty dang close to your desired behavior.
It requires that we manually set gridline colors as well as manipulate the internal scale instance ticks
array (which is used by the scale draw method to paint on the canvas).
First, in order to hide some gridlines but show others, I used the gridLines.color
property and passed in an array of colors where the first index color is the default gridline color and all others are white (the first index is used to color the "zero" gridline). Note, since we are later going to manipulate the internal scale ticks
array, you must add an extra index to the color array with a color of white. Here is an example of what I mean.
gridLines: {
// since we only want to show the "zero line" gridline (chart.js
// uses the color of the first index to paint this line), we
// set the first index to the default color and all others to white
// note, later we add a "dummy" tick (explained later) so the length
// of this array has to be 1 greater than the number of gridlines you
// want displayed
color: [
"rgba(0, 0, 0, 0.1)", // this is for the zero line
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",
"rgb(255, 255, 255)",],
}
}
Next, we use the afterTickToLabelConversion
scale callback configuration property to manipulate the internal scale ticks
array to force the gridlines to display like you want. Here is the implementation that works for your use case.
// we will use this callback to manipulate the ticks array
// before they are drawn on the canvas
afterTickToLabelConversion: function(scaleInstance) {
// the top gridline (which is at index 0) is always shown
// so to overcome this we add a "dummy" tick at index 0
scaleInstance.ticks.unshift(null);
// we have to do the same this to this tick array as well
// because it uses the values in this array to map to the data
scaleInstance.ticksAsNumbers.unshift(null);
// since we have added an extra tick, we need to move the
// zero line index to point to the new index of 0
scaleInstance.zeroLineIndex++
}
So putting it all together you end up with a chart that looks like this.
Check out this codepen for a working example (note, I left the other attempts to solve this...so see option 3 at the bottom).
Source: stackoverflow.com
Related Query
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- How to start Y Axis Line at 0 from right for negative values in chart js 2?
- How to make x-axis start from 0 in chart.js graphs?
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- How to Remove axis Lines from chart in chart js
- I have 10 points inn x-axis, ranging [-25,-20,,-15,-10,0,10,20,30,40,50]. But I want my line chart to start from -15 of x-axis. How we can achieve?
- chart.js: How do I make the radar chart axis labels bigger?
- How to stop axis label from being cut off in chart JS?
- Chart.js - how to make proportional intervals on X axis on line chart
- How to remove the extra Y axis from a bar chart in chart.js
- How to start the chart from specific time and offest hour and then show the data on chart from target datetime in chartjs
- How to make the x-axis start from zero in chartjs-plugin-streaming?
- How to make labels on both side from horizontal bar chart js
- How do I stop chart js from auto rescaling the y axis upon calling chart.update()?
- How to make pie chart from array values?
- Chart.js move start position of chart from axis
- How to make chart which has both positive and negative value, starts with 0 - x axis
- How to clear a chart from a canvas so that hover events cannot be triggered?
- How to remove square label from tooltip and make its information in one line?
- How can I make two of my lines in Chart JS thicker
- How to prevent first/last bars from being cut off in a chart with time scale
- Chart.js Bar Chart - how to chart bars from 0
- How can I make a stepline or stepped chart in chart.js or D3?
- Chart JS - set start of week for x axis time series
- How to add datas to chart js from javascript array itself?
- How do I change the 'months' language displayed on the date axis in Chart JS?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- how to make a chart.js bar chart scrollable
More Query from same tag
- Set backgroundColor of bar-char (chart.js)
- js error: while trying to display graph using chart.js: Uncaught TypeError: Cannot read property 'call' of undefined
- How to add variable-pie chart to react project?
- How to customize chart using chartJs?
- Chart.js, returning an "date" array to plot the line graph
- Regarding stacked chart in chart.js
- Adding a Label to Chart js Pie Graph
- Unable to change size of Chart.js
- The scale on the axis doesn't automatically narrow
- Chart.js bar chart labels do not valign
- Chart.js "display:none" Issue
- Dynamic Chart.js goes blank on resize
- chart.js - dynamically added chart makes previous ones disappear
- Control which lines have points?
- Is it posssible to create a Line-Chart with only 2 values? - Chart.js
- How to pass a nested function to an HTML button and Dropdown menu
- Angular 2 chart - change point radius
- Unable to make y-axis to begin at zero on Charts.js v2.1.4
- How to add datas to chart js from javascript array itself?
- Javascript array not initialized
- Changing default values for Chart JS
- Changing Fonts In Javascript's CanvasJS
- Chartjs Timestamp to Datetime labels
- VueJs + Chart.js - Avoid ref for reactive updates
- Chart.js - Fill Text only appearing when hovering over one part of the doughnut
- how to creat a dynamic datasets and new dynamic multi yAxes
- Chart js dataset label not showing
- Why is the child component updating but not re-render Reactjs
- Chart.js: Chaing Font Color and Size not working
- Update Chart.js plugin