It is important to understand that all the editable information about a chart is stored in the variable that holds the chart data (myChart
customarily, or my2Bar
as shown in this fiddle).
If you wish to make global changes to the graph, you will need to modify attributes within myChart.config.options
.
For specific chart modifications, adjust attributes located in myChart.config.data
.
In this instance, you are required to alter a specific chart, namely the horizontal bar.
Upon examining your graph logs and delving deep into the
config
, you will discover that the bars in your chart are created using elements saved in
myChart.config.data.datasets[0]._meta[0].data[n]._model
(
n representing the order of rectangles drawn from top to bottom).
Some attributes found here include:
base
: The starting X position where the rectangle is drawn (e.g., 0 on your x-axis).
x
: The final X position where the rectangle ends.
height
: The height of the rectangle.
- and more ...
To adjust these values, iterate through the various rectangles (represented by n
in the path above).
However, manual adjustments directly in your variable's config
won't suffice due to chart responsiveness (redrawing with previous settings upon resizing).
The solution lies in utilizing
Chart.js plugins.
Plugins enable you to manage events occurring during graph creation, updating, and rendering.
Within the beforeRender
event (initiated after setup but before drawing), iterate over the rectangles to edit values affecting their appearance:
beforeRender: function(chart) {
for (var i = 0; i < chart.config.data.datasets[0]._meta[0].data.length; i++) {
// Adjust both `3` values to change point height & width
chart.config.data.datasets[0]._meta[0].data[i]._model["base"] = chart.config.data.datasets[0]._meta[0].data[i]._model["x"] + 3;
chart.config.data.datasets[0]._meta[0].data[i]._model["height"] = 3;
}
}
Here is the jsFiddle link showcasing the end result.
Note: I was unable to achieve rounded dots instead of squares.
https://i.stack.imgur.com/mZ4YT.png
Update :
I have also created another jsFiddle demonstrating all dots linked together, resembling a horizontal line chart (subject to enhancement, yet a solid beginning).