Javascript 使用阈值更改条形图的颜色

Javascript 使用阈值更改条形图的颜色,javascript,jquery,jqplot,Javascript,Jquery,Jqplot,在Jqplot中,我只有一个数据系列,但阈值为200。我需要改变颜色如下所示。这怎么可能? 一种方法是定义一个根据数据值定义的颜色数组 为此,可以在绘制图形之前进行预处理。您需要迭代数据,并根据阈值在颜色数组中添加红色或蓝色: var data = [['Nissan', 4],['Porche', 6],['Acura', 2],['Aston Martin', 5],['Rolls Royce', 6]]; var colors = []; var threshold = 5; $.each

在Jqplot中,我只有一个数据系列,但阈值为200。我需要改变颜色如下所示。这怎么可能?

一种方法是定义一个根据数据值定义的颜色数组

为此,可以在绘制图形之前进行预处理。您需要迭代数据,并根据阈值在颜色数组中添加红色或蓝色:

var data = [['Nissan', 4],['Porche', 6],['Acura', 2],['Aston Martin', 5],['Rolls Royce', 6]];
var colors = [];
var threshold = 5;
$.each(data, function(index, value){
    if(value[1] > threshold)
      colors.push("#FF0000");
    else
      colors.push("#0000FF");
});
请参阅一个工作示例

$(文档).ready(函数(){

var数据=[['Nissan',460],'Porche',-260],'Acura',690],'Aston Martin',820],'Rolls-Royce',850];
var颜色=[];
风险值阈值=-200;
$.each(数据、函数(索引、值){
如果(值[1]<阈值)
颜色。推送(“FF0000”);
其他的
颜色。按(“#0000FF”);
});
plot=$.jqplot('chart1',[data]{
标题:“不同颜色的条形图”,
系列颜色:颜色,
系列默认值:{
渲染器:$.jqplot.blunderer,
渲染器选项:{
是的,
瓦里巴科洛尔:是的,
动画:{速度:2500},
useNegativeColors:错误
}
},
轴线:{
xaxis:{
渲染器:$.jqplot.CategoryAxisRenderer
}
}
});
}))

var data = [['Nissan', 460],['Porche', -260],['Acura', 690],['Aston Martin', 820],['Rolls Royce', 850]];
var colors = [];
var threshold = -200;
$.each(data, function(index, value){
    if(value[1] < threshold)
      colors.push("#FF0000");
    else
      colors.push("#0000FF");
});
plot = $.jqplot('chart1', [data], {
    title:'Bar Chart with Varying Colors',
    seriesColors: colors,
    seriesDefaults:{
        renderer:$.jqplot.BarRenderer,
        rendererOptions: {
            fillToZero: true,
            varyBarColor: true,
            animation: {speed: 2500},
            useNegativeColors: false
        }
    },
    axes:{
        xaxis:{
            renderer: $.jqplot.CategoryAxisRenderer
        }
    }
});