Display data with Google Chart - Stacked

Asked

Viewed 1,319 times

12

I’m using the Google Chart - Stacked to create a chart on my system. Even this part is working correctly, as can be seen in the example below:

<script src="https://www.google.com/jsapi?autoload={%27modules%27:[{%27name%27:%27visualization%27,%27version%27:%271.1%27,%27packages%27:[%27bar%27]}]}.js"></script>

<div id="chart_div"></div>

<script>

  google.setOnLoadCallback(drawChart);
    function drawChart() {
             var tdata = google.visualization.arrayToDataTable([
                   ['Estados', 'Concluídos', 'Total'],
                   ['MS', 1, 262],
                   ['RJ', 70, 205],
                   ['SP', 57, 176],
                   ['MG', 0, 82]
                ]);
                var options = {
                    chart: {
                        title: 'Gráfico Clientes',
                        subtitle: 'Total Clientes',
                    },
                    bars: 'horizontal', // Required for Material Bar Charts.
                    hAxis: { format: 'decimal' },
                    height: 400,
                    colors: ['#1b9e77', '#d95f02'],
                    isStacked: true
                };

                var chart = new google.charts.Bar(document.getElementById('chart_div'));

                chart.draw(tdata, google.charts.Bar.convertOptions(options));
        };
</script>

Example in Jsfiddle.

My problem is showing the chart (completed - Total).

Example: In the state of MS I have a total value of 262 and completed of 1. In the state of RJ I have a total of 205 and completed 70. In the graph it is adding up the two dates, as can be seen in the "full bar size". My question is: How to make the bar follow the total value only, disregarding the value of completed? I need to show the two values, but the chart bar needs to be in order of the Total column, not the samotory of the two.

Editing

The intention is only to show the difference within a value. For example:

In the state of RJ I have 205 clients. Of these 205, 70 are completed. As it stands, it is adding (70 + 205) and forming the chart bar with the size of this sum, in this case, 275.

To try to explain further, I will add these images as an example.

This is the way the graph is found, as can be seen in the fiddle above: Como está

And this is the way I seek to arrive: Como deveria

Observing, one can see that the expected result does not carry out the sum of the two bars, only "tag" a corresponding value of the total.

  • You will achieve the same effect if you put concluído + não concluído = total. Then just erase the label of those not completed. and label total.

1 answer

5


Operation of the tool is correct,

To do what you want, you must subtract the TOTAL - COMPLETED

total values, as follows:.

<script src="https://www.google.com/jsapi?autoload={%27modules%27:[{%27name%27:%27visualization%27,%27version%27:%271.1%27,%27packages%27:[%27bar%27]}]}.js"></script>

<div id="chart_div"></div>

<script>

  google.setOnLoadCallback(drawChart);
    function drawChart() {
             var tdata = google.visualization.arrayToDataTable([
                   ['Estados', 'Concluídos', 'Total'],
                   ['MS', 1, 262 -1],
                   ['RJ', 70, 205 -70],
                   ['SP', 57, 176 -57],
                   ['MG', 0, 82 -0]
                ]);
                var options = {
                    chart: {
                        title: 'Gráfico Clientes',
                        subtitle: 'Total Clientes',
                    },
                    bars: 'horizontal', // Required for Material Bar Charts.
                    hAxis: { format: 'decimal' },
                    height: 400,
                    colors: ['#1b9e77', '#d95f02'],
                    isStacked: true
                };

                var chart = new google.charts.Bar(document.getElementById('chart_div'));

                chart.draw(tdata, google.charts.Bar.convertOptions(options));
        };
</script>

If you want to do it in hand, just use percentage

Gosh, the times I had to do it, I did it on hand with CSS ... Think you have a value that is 100% let’s say 300 is the Total, and of that 300 you fill 100px,... ie 33,33%..

Take a width X DIV, take 33.33% of that X widht and put another div in with the color you want to fill DARK GREEN IN THE CASE and put widht of it equal to that 33.33%

<div id="holder" style="height:30px; widht:300px; color:light green;">

<div id="progresso" style="height:30px; widht:100px; color:green;">
</div>

</div>
  • Yes, I tried to do that. But the total amount should show exactly the total. Example: In its code the total of RJ is 135, but should show 205. The graph should indicate that of 205 customers, 70 are completed. I don’t know if I was clear, sorry if I’m confused.

  • If show 205 as you want, your chart will be half wrong right... Total is sum of 2 things... what are you adding to what? Finished + XPTO = Total.

  • Not exactly. The logic in this case is not to know how many are completed and how many are not, but to know how many are completed. From 205, 70 are completed.

  • What if you put 2 Legends, 1 Finished 2 Not Finished? The way you want with this plugin will not scroll...

  • In this case it would imply another type of graph, which would not be interesting to me.

  • why for example, You take the completed and the unfinished, sum and has the Total... which even appears in the range x of the chart...

  • I tried to use annotation to make the legend, but it is a lot of information beyond that, and gets polluted the graphic.

  • Yeah.. I test that too... what if you put the Total down and the Concave up? 2 horizontal bars per state? I tested and gave, I just don’t know if the look you want

  • I really tried too and it didn’t get very nice. If you know any plugin that does this, could add also that I have the freedom to change.

  • Gosh, the times I had to do it, I did it on hand with CSS ... Think you have a value that is 100% let’s say 300 is the Total, and of that 300 you fill 100,... that is 33,33%.. takes one DIV with width X, takes 33.33% of that widht X and puts another div in with the color you want to fill in DARK GREEN IN THE CASE and puts widht of it equal to that 33.33%

  • Yeah, I guess I’ll just have to. I’ll just see if anyone else has any other answers, if not, add that part to your question and I’ll accept it later.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.