Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

javascript - How to assign custom color in google column chart based on value (Django)

I have values :

m=72
n=34
z=59

I got the following google column chart :

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
               <script type="text/javascript">
                 google.charts.load("current", {packages:['corechart']});
                 google.charts.setOnLoadCallback(drawChart);
                 function drawChart() {
                   var data = google.visualization.arrayToDataTable([
                     ["Apple", {{m}}, "green"],
                     ["Orange", {{n}}, "red"],
                     ["Grape", {{z}}, "orange"],);

                   var view = new google.visualization.DataView(data);
                   view.setColumns([0, 1,
                                    { calc: "stringify",
                                      sourceColumn: 1,
                                      type: "string",
                                      role: "annotation" },
                                    2]);

                   var options = {title: "Fruits",width: 1000, height: 400, bar: {groupWidth: "95%"},};

                   var chart = new google.visualization.ColumnChart(document.getElementById

("columnchart_values"));chart.draw(view, options);}
               </script>
              <div id="columnchart_values" style="width: 900px; height: 300px;"></div>

i want to color values m,n or z in the following way:

if value (m,n or z) more than 70 color green
if value (m,n or z) more than 50 but less than 70 color orange
if value (m,n or z) less than 50 color red

I am not good with js just learned to copy pasted and modify ready code however in this case I do not how to customise colors of columns based on conditions.

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Could you use django template logic?

{% if m > 70 %}green{% elif m > 50 %}orange{% else %}red{%endif %}

function drawChart() {
   var data = google.visualization.arrayToDataTable([
     ["Apple", {{m}}, "{% if m > 70 %}green{% elif m > 50 %}orange{% else %}red{%endif %}"],
     ["Orange", {{n}}, "{% if n > 70 %}green{% elif n > 50 %}orange{% else %}red{%endif %}"],
     ["Grape", {{z}}, "{% if z > 70 %}green{% elif z > 50 %}orange{% else %}red{%endif %}"],)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...