Set jQuery flot bar colors based on its score -
i new jquery flot plugin , want set each bar's color based on score.
for score of "myself" bar:
- 0 - 20: red
- 20 - 70: yellow
- 70 - 100: green
for score of "you" bar:
- 0 - 20: blue
- 20 - 70: black
- 70 - 100: orange
code snippet:
$.plot( $("#placeholder"),[{ data: [[1, 81], [3.5, 33]], label: 'myself', bars: { show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: { colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true }, align: "center" } },{ data: [[2, 18], [4.5, 75]], label: 'you', color: 'red', bars: { show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: { colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true }, align: "center" } }],{ xaxis: { ticks: [[1.5, '1st half'], [4, '2nd half']], min: 0, max: 5.5, ticklength: 0 }, yaxis: {ticklength: 0}, grid: { backgroundcolor: "#ffffff", borderwidth: 0, markings: [{xaxis: {from: 0, to: 0}, color: "#909090"}, {yaxis: {from: 0, to: 0}, color: "#909090"}] }, legend: {backgroundopacity: 0.6, position: 'ne', margin: [-10, 0]}, valuelabels: {show: true, truncatevalue: 2} });
a jsfiddle of above code here. appreciate help.
you could implement custom draw hook this. when i've needed i've taken different approach when there not many ranges.
if have control on source data formatting create separate series each of value ranges , assign appropriate colours way. you'd have following series: myself0-20, myself20-70, myself70-100, you0-20, you20-70, you70-100.
the modification jsfiddle code addition of new series (fiddle):
$.plot($("#placeholder"), [ {data: [[1, 0], [3.5, 0]], label: 'myself0-20', color: 'red', bars: {show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: {colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true}, align: "center"}}, {data: [[1, 0], [3.5, 33]], label: 'myself20-70', color: 'yellow', bars: {show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: {colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true}, align: "center"}}, {data: [[1, 81], [3.5, 0]], label: 'myself70-100', color: 'green', bars: {show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: {colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true}, align: "center"}}, {data: [[2, 18], [4.5, 0]], label: 'you0-20', color: 'blue', bars: {show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: {colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true}, align: "center"}}, {data: [[2, 0], [4.5, 0]], label: 'you20-70', color: 'black', bars: {show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: {colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true}, align: "center"}}, {data: [[2, 0], [4.5, 75]], label: 'you70-100', color: 'orange', bars: {show: true, barwidth: 1, linewidth: 0, fill: true, fillcolor: {colors: [{brightness: 1}, {brightness: 0.5}], horizontal: true}, align: "center"}} ],{ xaxis: {ticks: [[1.5, '1st half'], [4, '2nd half']], min: 0, max: 5.5, ticklength: 0}, yaxis: {ticklength: 0}, grid: {backgroundcolor: "#ffffff", borderwidth: 0, markings: [{xaxis: {from: 0, to: 0}, color: "#909090"}, {yaxis: {from: 0, to: 0}, color: "#909090"}]}, legend: {backgroundopacity: 0.6, position: 'ne', margin: [-10, 0]}, valuelabels: {show: true, truncatevalue: 2} });
Comments
Post a Comment