1
I am using the column chart of the Google Chart API.
I would like to insert a vertical line in a given position dynamically.
I have an array that indicates value => quantity. In a fictional example, it would be something like (5 (note) => 500 (number of people who got it right 5)).
Ex.:
$arr = array('<=4' => 500, 5 => 500, 6 => 300);
There is a variable x, which will receive a value from 0 to 10, and which must be represented in the graph through the vertical line already mentioned, indicating the position among the others.
In the image example, I used $x = 9
and allocated in their respective position.
Then, at last, doubts remain:
How to insert the vertical bar?
How to make it position according to the X axis proportional?
Expected result:
JS + HTML code
google.charts.load("current", {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
["Element", "Density", {role: 'annotation'}],
["<=4", 672, "672"],
["5", 648, "648"],
["6", 709, "709"],
["7", 569, "569"],
["8", 459, "459"],
["9", 223, "223"],
["10", 17, "17"],
]);
var view = new google.visualization.DataView(data);
var options = {
title: "Descrição",
width: 1159,
height: 400,
bar: {groupWidth: "95%"},
legend: {position: "none"},
};
var chart = new google.visualization.ColumnChart(document.getElementById("grafico"));
chart.draw(view, options);
}
<div id="grafico" style="width: 900px; height: 300px;"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
What would be the reason to create this vertical line? Wouldn’t disturb the graph view?
– MarceloBoni
The line would serve to indicate, in parallel with the graph, the position of the person in a range of 0 to 10.
– Cobra