2017-07-18 99 views
0

我有一個Flot條形圖代碼。浮圖。自定義顏色低於x軸值

$(function() { 
     var barOptions = { 
      series: { 
       bars: { 
        show: true, 
        barWidth: 0.4, 
        fill: true, 
        fillColor: { 
         colors: [{ 
          opacity: 0.8 
         }, { 
          opacity: 0.8 
         }] 
        } 
       } 
      }, 
      xaxis: { 
       mode: "categories", 
       tickLength: 0 
      },    
      yaxis: { 
       min:-200, max: 200, tickSize: 50, 
      }, 
      colors: ["#1ab394"], 
      grid: { 
       color: "#999999", 
       hoverable: true, 
       clickable: true, 
       tickColor: "#D4D4D4", 
       borderWidth:0 
      }, 
      legend: { 
       show: false 
      }, 
      tooltip: true, 
      tooltipOpts: { 
       content: "Cost: %y %" 
      } 
     }; 

     var barData = { 
      label: "bar", 
      data: [   
      ["First", -100],["Second", 66.67],["Third", -177.77],] 

     }; 

     $.plot($("#flot-bar-chart"), [barData], barOptions);    

    }); 

我希望每個低於0的條都是特定的(例如紅色)顏色。我試圖用閾值插件來做到這一點,但它增加了額外的值。有沒有解決方案?

回答

0

閾值插件不應該增加額外的價值,看到這個fiddle或下面的代碼片段:

$(function() { 
 
    var barOptions = { 
 
     series: { 
 
      bars: { 
 
       show: true, 
 
       barWidth: 0.4, 
 
       fill: true, 
 
       fillColor: { 
 
        colors: [{ 
 
         opacity: 0.8 
 
        }, { 
 
         opacity: 0.8 
 
        }] 
 
       }, 
 
       align: 'center' 
 
      } 
 
     }, 
 
     xaxis: { 
 
      mode: "categories", 
 
      tickLength: 0, 
 
      ticks: [ 
 
       [1, 'First'], 
 
       [2, 'Second'], 
 
       [3, 'Third'] 
 
      ] 
 
     }, 
 
     yaxis: { 
 
      min: -200, 
 
      max: 200, 
 
      tickSize: 50, 
 
     }, 
 
     colors: ["#1ab394"], 
 
     grid: { 
 
      color: "#999999", 
 
      hoverable: true, 
 
      clickable: true, 
 
      tickColor: "#D4D4D4", 
 
      borderWidth: 0 
 
     }, 
 
     legend: { 
 
      show: false 
 
     }, 
 
     tooltip: true, 
 
     tooltipOpts: { 
 
      content: "Cost: %y %" 
 
     } 
 
    }; 
 

 
    var barData = { 
 
     label: "bar", 
 
     data: [ 
 
      [1, -100], 
 
      [2, 66.67], 
 
      [3, -177.77], 
 
     ], 
 
     threshold: { 
 
      below: 0, 
 
      color: "rgb(200, 20, 30)" 
 
     }, 
 
    }; 
 

 
    $.plot($("#flot-bar-chart"), [barData], barOptions); 
 

 
});
#flot-bar-chart { 
 
    height: 300px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.threshold.min.js"></script> 
 
<div id="flot-bar-chart"></div>

+0

謝謝。這是因爲我的X軸沒有蜱蟲。也許你知道可以在X軸上顯示數字嗎?因爲現在我看不到一些數字,我也無法做到更小的步長。 http://i.imgur.com/bpMak0J.png –

+0

你的y軸最小值只有-100而不是-200。而對於非常小的酒吧,您需要在背景上可讀的酒吧標籤的顏色,例如黑色。 – Raidri