如何在jqplot堆叠水平条形图上更改标签

时间:2012-01-05 21:13:01

标签: jquery jqplot bar-chart

我正在使用jqplot使用此处显示的代码创建堆叠的水平条形图:

perc_data = [[[6, "1"]], [[92, "1"]], [[1, "1"]], [[1, "1"]]];
series_array = [ { label: "Mud", color: "#ccaa00"}, { label: "Sand", color: "#ffeecc"}, 
                 { label: "Gravel", color: "#dddddd"}, { label: "Rock", color: "#664400"} ];
var perc_chart = $.jqplot('perc_div', perc_data, {
    stackSeries: true,
    seriesDefaults: {
        renderer:$.jqplot.BarRenderer,
        shadowAngle: 135,
        rendererOptions: {  barWidth: 25,
                            barDirection: 'horizontal',
        }
    },
    series: series_array,
    axes: {
        yaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            rendererOptions: {  tickRenderer: $.jqplot.AxisTickRenderer, 
                                tickOptions: {  mark: null,
                                                fontSize: 12
                                             }
            }
        },
        xaxis: {
            min: 0,
            max: 100,
            numberTicks: 6
        }
    },
    grid: {
        drawGridlines: false,
        drawBorder: false,
        shadow: false
    }
});

生成的条形图如下所示:

enter image description here

接下来我要做的是将栏的标签从“1”更改为“我的标签”。 我原以为我可以简单地将perc_data从原始值更改为以下内容:

perc_data = [[[6, "My Label"]], [[92, "My Label"]], [[1, "My Label"]], [[1, "My Label"]]];

但这会产生一个空条形图:

enter image description here

有人可以告诉我我做错了什么以及如何调整这个标签。

感谢。

1 个答案:

答案 0 :(得分:6)

使用ticks option (2nd example on this page)

perc_data = [[[6, "1"]], [[92, "1"]], [[1, "1"]], [[1, "1"]]];
ticks = ["My Label"];

series_array = [ { label:'Mud', color:"#ccaa00"}, { label:"Sand", color:"#ffeecc"}, { label:"Gravel", color:"#dddddd"}, { label:"Rock", color:"#664400"} ];

var perc_chart = $.jqplot('chart1', perc_data, {
    stackSeries: true,
    seriesDefaults: {
        renderer:$.jqplot.BarRenderer,
        shadowAngle: 135,
        rendererOptions: {  barWidth: 25,
                            barDirection: 'horizontal',
        }
    },
    series: series_array,
    axes: {
        yaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            rendererOptions: {  tickRenderer: $.jqplot.AxisTickRenderer, 
                                tickOptions: {  mark: null,
                                                fontSize: 12
                                             }
            },
            ticks: ticks
        },
        xaxis: {
            min: 0,
            max: 100,
            numberTicks: 6
        }
    },
    grid: {
        drawGridlines: false,
        drawBorder: false,
        shadow: false
    }
});

enter image description here

BTW,{ label="Mud", color="#ccaa00"}无效javascript应为{ label:"Mud", color:"#ccaa00"}