有jqPlot条形图的问题

cho*_*obo 11 jquery jquery-plugins jqplot

我正在使用jqPlot创建一个条形图,但我遇到了一些问题.

问题1:图表上的第一个和最后一个条被切断.只有一半显示

问题2:我不希望我的数据点跨越整个x轴.是否没有数据跨越整个x轴?

在此输入图像描述 例如:这就是现在做的事情.

这是我传递给它的数据

var chartData = [["19-Jan-2012",2.61],["20-Jan-2012",5.00],["21-Jan-2012",6.00]]
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的jquery.

 // Plot chart
        function PlotChart(chartData, numberOfTicks) {
            $.jqplot.config.enablePlugins = true;
                 var plot2 = $.jqplot('chart1', [chartData], {
                    title: 'Mouse Cursor Tracking',
                     seriesDefaults:{
                         renderer: $.jqplot.BarRenderer,
                         rendererOptions: {
                            barPadding: 1,
                            barMargin: 15,
                            barDirection: 'vertical',
                            barWidth: 50
                        }, 
                        pointLabels: { show: true }
                    },
                    axes: {
                        xaxis: {
                            pad: 0,       // a factor multiplied by the data range on the axis to give the
                            numberTicks: numberOfTicks,
                            renderer:  $.jqplot.DateAxisRenderer,  // renderer to use to draw the axis,
                            tickOptions: {
                                formatString: '%b %#d'   // format string to use with the axis tick formatter
                            }
                        },
                        yaxis: {
                            tickOptions: {
                                formatString: '$%.2f'
                            }
                        }
                    },
                    highlighter: {
                        sizeAdjust: 7.5
                    },
                    cursor: {
                        show: true
                    }
                });
            }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 21

根据您希望绘图的外观,如果切换到使用CategoryAxisRenderer而不是DateAxisRenderer,您将省去很多麻烦.与真正的时间序列相比,CategoryAxisRenderer在显示谨慎的数据分组方面要好得多.

var axisDates = ["Jan 19", "Jan 20", "Jan 21"]
var chartData = [2.61,5.00,6.00]

        $.jqplot.config.enablePlugins = true;
             var plot2 = $.jqplot('chart2', [chartData], {
                title: 'Some Plot',
                 seriesDefaults:{
                     renderer: $.jqplot.BarRenderer,
                     rendererOptions: {
                        barPadding: 1,
                        barMargin: 15,
                        barDirection: 'vertical',
                        barWidth: 50
                    }, 
                    pointLabels: { show: true }
                },
                axes: {
                    xaxis: {                            
                            renderer:  $.jqplot.CategoryAxisRenderer,
                            ticks: axisDates
                    },
                    yaxis: {
                        tickOptions: {
                            formatString: '$%.2f'
                        }
                    }
                },
                highlighter: {
                    sizeAdjust: 7.5
                },
                cursor: {
                    show: true
                }
            });
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Pri*_*ark 10

DateAxisRenderer实际上是用于折线图,而不是条形图.将它与条形图组合使用时,它无法正常工作.DateAxisRenderer的想法/目标是在日期/时间内制作线性/准确的时间图.这样,如果您错过了日期条目,它仍将按时间缩放.在这里查看DateAxisRenderer上的示例:http://www.jqplot.com/tests/date-axes.php

您想要使用的是CategoryAxisRenderer.然后,您可以覆盖/创建自己的刻度标签渲染器并且很好.通常,您不希望将额外的空项附加到项目中,特别是如果它们是空的,但是,如果这样做,只需将它们附加到数据数组即可.

这是一个jsfiddle做你想要的:http://jsfiddle.net/fordlover49/JWhmQ/

请注意,您可能需要查看manage resources部分以验证需要引用的文件(除了jquery文件).

以下是jsfiddle运行时的javascript:

$.jqplot.config.enablePlugins = true;
var chartData = [["19-Jan-2012", 2.61], ["20-Jan-2012", 5.00], ["21-Jan-2012", 6.00]];

// add a custom tick formatter, so that you don't have to include the entire date renderer library.
$.jqplot.DateTickFormatter = function(format, val) {
    // for some reason, format isn't being passed through properly, so just going to hard code for purpose of this jsfiddle
    val = (new Date(val)).getTime();
    format = '%b&nbsp%#d'
    return $.jsDate.strftime(val, format);
};

function PlotChart(chartData, extraDays) {
    // if you want extra days, just append them to your chartData array.
    if (typeof extraDays === "number") {
        for (var i = 0; i < extraDays; i++) {
            var actualDate = new Date(chartData[chartData.length - 1]); // convert last entry to actual date
            var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() + 1); // create new increased date
            chartData.push([newDate, 0]);
        }
    }

    var plot2 = $.jqplot('chart1', [chartData], {
        title: 'Mouse Cursor Tracking',
        seriesDefaults: {
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barPadding: 1,
                barWidth: 50
            },
            pointLabels: {
                show: true
            }
        },
        axes: {
            xaxis: {
                pad: 1,
                // a factor multiplied by the data range on the axis to give the            
                renderer: $.jqplot.CategoryAxisRenderer,
                // renderer to use to draw the axis,     
                tickOptions: {
                    formatString: '%b %#d',
                    formatter: $.jqplot.DateTickFormatter
                }
            },
            yaxis: {
                tickOptions: {
                    formatString: '$%.2f'
                }
            }
        },
        highlighter: {
            sizeAdjust: 7.5
        },
        cursor: {
            show: true
        }
    });
}

PlotChart(chartData, 3);
Run Code Online (Sandbox Code Playgroud)